你在draw_sprite()中有事件循环,所以它一直运行到游戏结束,window.set_background永远不会执行。在
您的代码构造不正确。在
我试着更正它,稍后再发送代码。在
编辑:
如何组织代码的简单示例。在
现在它在一个文件中。在Pygame中总是一个窗口,所以不需要使Panel+Widnow。在run()中有一个事件循环。所有代码都在窗口内(创建精灵,更改背景,绘图等)。在
我将精灵添加到(python)列表并从列表中绘制所有精灵-player is exception-所以我只能从列表中移除最后一个精灵:/If您需要更好的东西,请参见pygame.sprite.Sprite()和pygame.sprite.Group()。在
使用Arrows移动红球,Space暂停游戏,ESC退出。在
最后我附上了我的位图。在import pygame
#from pygame.locals import *
#
# class for single sprite
#
class MySprite():
def __init__(self, image, x, y):
self.image = pygame.image.load(image)
image_rect = self.image.get_rect()
# Rect class to use "Sprite collision detect" - in the future
# In rect you have sprite position and size
# You can use self.rect.x, self.rect.y, self.rect.width, self.rect.height
# and self.rect.center, self.rect.centerx, self.rect.top, self.rect.bottomright etc.
self.rect = pygame.rect.Rect(x, y, image_rect.width, image_rect.height)
def draw(self, screen):
screen.blit(self.image, self.rect)
#
# class for player
#
class MyPlayer(MySprite):