精灵的背景更新

时间:2013-11-13 13:05:59

标签: python background pygame

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

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()

1 个答案:

答案 0 :(得分:1)

您将background添加到background_listall_sprite_list,然后绘制background_listall_sprite_list,以便background被抽出两次。

删除all_sprites_list.add(background)