Python 精灵上的背景更新
我的游戏背景很奇怪。它不工作,精灵出现,然后它工作,但在精灵之上Python 精灵上的背景更新,python,background,pygame,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.dis
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
导入pygame
随机输入
#定义一些颜色
黑色=(0,0,0)
白色=(255,255,255)
寿命=10
#调用此函数,以便Pygame库可以自行初始化
pygame.init()
screen=pygame.display.set_模式([700600])
clock=pygame.time.clock()
#设置图形的位置
背景位置=[0,0]
#使鼠标隐形
pygame.mouse.set_可见(False)
pygame.font.init()
font=pygame.font.font(无,50)
#这个类代表球
#它源于Pygame中的“精灵”类
类背景(pygame.sprite.sprite):
#构造器。输入块的颜色,
#以及它的x和y位置
定义初始值(自身、宽度、高度):
#调用父类(Sprite)构造函数
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
#创建块的图像。
#这也可能是从磁盘加载的映像。
self.image=pygame.image.load(“Grass2.fw.png”).convert()
#获取具有图像尺寸的矩形对象
#形象。
#通过设置值更新此对象的位置
#关于矩形x和矩形y
self.rect=self.image.get_rect()
#重置屏幕右侧x位置的位置。
#如果发生冲突,则由update()或主程序循环调用。
def复位位置(自身):
self.rect.y=(0)
self.rect.x=(0)
#调用每个帧
def更新(自我):
#将块向左移动一些像素
自校正x-=1
#如果块太左,则重置到屏幕右侧。
如果自校正x<-700:
self.reset_pos()
类块(pygame.sprite.sprite):
#构造器。输入块的颜色,
#以及它的x和y位置
定义初始值(自身、颜色、宽度、高度):
#调用父类(Sprite)构造函数
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
#创建块的图像,并用颜色填充。
#这也可能是从磁盘加载的映像。
self.image=pygame.image.load(“敌方small.fw.png”).convert()
self.image.set_颜色键(白色)
#获取具有图像尺寸的矩形对象
#形象。
#通过设置值更新此对象的位置
#关于矩形x和矩形y
self.rect=self.image.get_rect()
#在随机x位置将位置重置到屏幕顶部。
#如果发生冲突,则由update()或主程序循环调用。
def复位位置(自身):
self.rect.y=random.randrange(0600)
self.rect.x=random.randrange(700800)
#调用每个帧
def更新(自我):
#将块向右移动一个像素
自校正x-=1
#如果块太左,则重置到屏幕顶部。
如果自校正x<-100:
self.reset_pos()
职业球员(拦网):
定义初始值(自身、宽度、高度):
#调用父类(Sprite)构造函数
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
#创建块的图像。
#这也可能是从磁盘加载的映像。
self.image=pygame.image.load(“ship.fw.png”).convert()
self.image.set_颜色键(白色)
#获取具有图像尺寸的矩形对象
#形象。
#通过设置值更新此对象的位置
#关于矩形x和矩形y
self.rect=self.image.get_rect()
def更新(自我):
#获取当前鼠标位置。这将返回位置
#作为两个数字的列表。
pos=pygame.mouse.get_pos()
#从列表中取出x和y,
#就像我们从字符串中提取字母一样。
#将播放器对象设置为鼠标位置
self.rect.x=pos[0]
自校正y=位置[1]
#初始化Pygame
pygame.init()
#设置屏幕的高度和宽度
屏幕宽度=700
屏幕高度=600
screen=pygame.display.set_模式([屏幕宽度,屏幕高度])
pygame.display.set_标题('Razazone')
时钟滴答(70)
#这是一个“精灵”列表。程序中的每个块都是
#已添加到此列表中。该列表由一个名为“组”的类管理
block_list=pygame.sprite.Group()
#这是每个精灵的列表。所有区块和球员区块以及。
所有精灵列表=pygame.sprite.Group()
background\u list=pygame.sprite.Group()
#这是一个背景
背景=背景(20,15)
#设置块的位置
background.rect.x=(屏幕宽度)
background.rect.y=(屏幕高度)
背景列表。添加(背景)
#将块添加到对象列表中
所有精灵列表。添加(背景)
对于范围(1)中的i:
#这表示一个块
块=块(黑色,20,15)
#设置块的随机位置
block.rect.x=random.randrange(屏幕宽度)
block.rect.y=random.randrange(屏幕高度)
#将块添加到对象列表中
块列表。添加(块)
所有精灵列表。添加(块)
#创建一个红色玩家块
玩家=玩家(350300)
所有精灵列表。添加(玩家)
完成=错误
我知道我的更新顺序有问题,但我不知道我需要做什么来解决这个问题
# -------- 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()
#------主程序循环-----------
完成时==False:
对于pygame.event.get()中的事件:#用户执行了某些操作
if event.type==pygame.QUIT:#如果用户单击关闭
done=True#标记我们已完成,因此我们退出此循环#
#对列表中的每个精灵调用update()方法
所有精灵列表。更新()
#看看玩家方块是否与任何东西碰撞。
blocks\u hit\u list=pygame.sprite.spritecollide(玩家,block\u list,False)
#检查碰撞列表。
对于块中的块,单击列表:
寿命-=1
印刷(生活)
#将块重设到屏幕顶部以再次下降。
block.reset_pos()
#将块重置到右侧