0

我的游戏背景很奇怪。它不起作用并且精灵出现了,然后它起作用了,但是在精灵之上!

import pygame
import random

# Define some colors
black    = (   0,   0,   0)
white    = ( 255, 255, 255)
lives = 10

# Call this function so the Pygame library can initialize itself
pygame.init()

screen = pygame.display.set_mode([700, 600])

clock = pygame.time.Clock()

# Set positions of graphics
background_position = [0,0]

# Make mouse invisible
pygame.mouse.set_visible(False)


pygame.font.init()
font= pygame.font.Font(None, 50)

# This class represents the ball        
# It derives from the "Sprite" class in Pygame
class Background(pygame.sprite.Sprite):

    # Constructor. Pass in the color of the block, 
    # and its x and y position
    def __init__(self, width, height):
        # Call the parent class (Sprite) constructor
        pygame.sprite.Sprite.__init__(self) 

        # Create an image of the block.
        # This could also be an image loaded from the disk.
        self.image = pygame.image.load("Grass2.fw.png").convert()

        # Fetch the rectangle object that has the dimensions of the image
        # image.
        # Update the position of this object by setting the values 
        # of rect.x and rect.y
        self.rect = self.image.get_rect()

    # Reset position to the right of the screen, at an x location.
    # Called by update() or the main program loop if there is a collision.
    def reset_pos(self):
        self.rect.y =(0)
        self.rect.x =(0)        

    # Called each frame
    def update(self):
        # Move block left some pixels
        self.rect.x -= 1

        # If block is too far left, reset to right of screen.
        if self.rect.x < -700:
            self.reset_pos()

class Block(pygame.sprite.Sprite):

# Constructor. Pass in the color of the block, 
# and its x and y position
def __init__(self, color, width, height):
    # Call the parent class (Sprite) constructor
    pygame.sprite.Sprite.__init__(self) 

    # Create an image of the block, and fill it with a color.
    # This could also be an image loaded from the disk.
    self.image = pygame.image.load("Enemy small.fw.png").convert()
    self.image.set_colorkey(white)

    # Fetch the rectangle object that has the dimensions of the image
    # image.
    # Update the position of this object by setting the values 
    # of rect.x and rect.y
    self.rect = self.image.get_rect()

# Reset position to the top of the screen, at a random x location.
# Called by update() or the main program loop if there is a collision.
def reset_pos(self):
    self.rect.y = random.randrange(0, 600)
    self.rect.x = random.randrange(700, 800)        

# Called each frame
def update(self):
    # Move block right one pixel
    self.rect.x -= 1

    # If block is too far left, reset to top of screen.
    if self.rect.x < -100:
        self.reset_pos()

class Player(Block):
    def __init__(self, width, height):
        # Call the parent class (Sprite) constructor
        pygame.sprite.Sprite.__init__(self)

        # Create an image of the block.
        # This could also be an image loaded from the disk.
        self.image = pygame.image.load("ship.fw.png").convert()
        self.image.set_colorkey(white)

        # Fetch the rectangle object that has the dimensions of the image
        # image.
        # Update the position of this object by setting the values 
        # of rect.x and rect.y
        self.rect = self.image.get_rect()
    def update(self):
        # Get the current mouse position. This returns the position
        # as a list of two numbers.
        pos = pygame.mouse.get_pos()

        # Fetch the x and y out of the list, 
        # just like we'd fetch letters out of a string.
        # Set the player object to the mouse location
        self.rect.x=pos[0]
        self.rect.y=pos[1]

# Initialize Pygame
pygame.init()

# Set the height and width of the screen
screen_width=700
screen_height=600
screen=pygame.display.set_mode([screen_width,screen_height])
pygame.display.set_caption('Razazone')

clock.tick(70)

#This is a list of 'sprites.' Each block in the program is
# added to this list. The list is managed by a class called 'Group.'
block_list = pygame.sprite.Group()

# This is a list of every sprite. All blocks and the player block as well.
all_sprites_list = pygame.sprite.Group()

background_list = pygame.sprite.Group()

# This represents a background
background = Background(20,15)

# Set a location for the block
background.rect.x = (screen_width)
background.rect.y = (screen_height)

background_list.add(background)
    # Add the block to the list of objects
all_sprites_list.add(background)

for i in range(1):
    # This represents a block
    block = Block(black, 20, 15)

    # Set a random location for the block
    block.rect.x = random.randrange(screen_width)
    block.rect.y = random.randrange(screen_height)

    # Add the block to the list of objects
    block_list.add(block)
    all_sprites_list.add(block)

    # Create a red player block
    player = Player(350, 300)
    all_sprites_list.add(player)


done=False

我知道我的更新顺序有问题,但我不知道我需要做什么来解决这个问题。

# -------- Main Program Loop -----------
while done==False:
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done=True # Flag that we are done so we exit this loop# 

    # Calls update() method on every sprite in the list
    all_sprites_list.update()

    # See if the player block has collided with anything.
    blocks_hit_list = pygame.sprite.spritecollide(player, block_list, False)  

    # Check the list of collisions.
    for block in blocks_hit_list:
        lives -=1
        print (lives)
        # Reset block to the top of the screen to fall again.
        block.reset_pos()


    # Reset block to the right of the screen to fall again.
    #background.reset_pos()

    if lives<=1:
        lives=1

    screen.fill(black)
    background.update()

    background_list.draw(screen)

    # Draw all the spites
    all_sprites_list.draw(screen)

    lives_text=font.render("%d lives" % lives, True, white)
    screen.blit(lives_text, [100,100])


    #background.update()

    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

pygame.quit()
4

1 回答 1

1

你添加backgroundbackground_list然后all_sprite_list你画background_listall_sprite_list所以你background被画了两次。

消除all_sprites_list.add(background)

于 2013-11-13T14:44:35.047 回答