pygame.sprite.Group.draw 不绘制图像
pygame.sprite.Group.draw doesn't draw image
我正在使用 pygame 开发游戏,我正在使用 pygame 类 处理游戏角色的精灵:pygame.sprite.Sprite() 和pygame.sprite.Group()。我得到了 Sprite Class,我想用 pygame.sprite.Group.draw 绘制 Sprites。但是图像不显示。如果我正常 blit 它,它可以工作,但如果我想用 Group.draw 绘制它,它就不会。
这是我的主要内容:
def main():
pygame.init()
WIN = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption("Ultra Super Street Pyther")
clock = pygame.time.Clock()
run = True
index = 0
# set up Sprite Group
p1_sprites = NormalGuy("Idle")
p1 = pygame.sprite.Group(p1_sprites)
while run:
# FPS of gameloop
clock.tick(FPS)
# check events during game loop
for event in pygame.event.get():
# check for quit pygame
if event.type == pygame.QUIT:
run = False
# draw sprites
p1.update()
p1.draw(WIN)
# draw window
WIN.fill(WHITE)
pygame.display.update()
pygame.quit()
这是我的精灵Class:
class NormalGuy(pygame.sprite.Sprite):
def __init__(self,action="Air", lo=(0,0)):
super(NormalGuy, self).__init__()
# parameters for getting sprites from spritesheet
self.spriteConfigs = get_config("Super_Strong_Normal_Guy.json")
self.spriteConfigs = self.spriteConfigs["NormalGuy"][action]
self.path = self.spriteConfigs["path"]
self.width = self.spriteConfigs["width"]
self.height = self.spriteConfigs["height"]
self.sheet_specs = self.spriteConfigs["sheet_specs"]
self.scale = self.spriteConfigs["scale"]
# getting sprites from spritesheet
self.spriteSheet = Spritesheet(self.path)
self.images = self.spriteSheet.get_sprites(self.width, self.height, self.sheet_specs, self.scale)
# essentials for Sprite class
self.image = self.images[0]
self.rect = self.image.get_rect()
self.rect.topleft = lo
self.index = 0
def update(self):
if self.index >= len(self.images):
self.index = 0
self.image = self.images[self.index]
self.index += 1
你必须改变指令的顺序。绘制图像前清除背景:
def main():
# [...]
while run:
# FPS of gameloop
clock.tick(FPS)
# check events during game loop
for event in pygame.event.get():
# check for quit pygame
if event.type == pygame.QUIT:
run = False
p1.update()
WIN.fill(WHITE) <-- INSERT
p1.draw(WIN)
# WIN.fill(WHITE) <-- DELETE
pygame.display.update()
我正在使用 pygame 开发游戏,我正在使用 pygame 类 处理游戏角色的精灵:pygame.sprite.Sprite() 和pygame.sprite.Group()。我得到了 Sprite Class,我想用 pygame.sprite.Group.draw 绘制 Sprites。但是图像不显示。如果我正常 blit 它,它可以工作,但如果我想用 Group.draw 绘制它,它就不会。
这是我的主要内容:
def main():
pygame.init()
WIN = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption("Ultra Super Street Pyther")
clock = pygame.time.Clock()
run = True
index = 0
# set up Sprite Group
p1_sprites = NormalGuy("Idle")
p1 = pygame.sprite.Group(p1_sprites)
while run:
# FPS of gameloop
clock.tick(FPS)
# check events during game loop
for event in pygame.event.get():
# check for quit pygame
if event.type == pygame.QUIT:
run = False
# draw sprites
p1.update()
p1.draw(WIN)
# draw window
WIN.fill(WHITE)
pygame.display.update()
pygame.quit()
这是我的精灵Class:
class NormalGuy(pygame.sprite.Sprite):
def __init__(self,action="Air", lo=(0,0)):
super(NormalGuy, self).__init__()
# parameters for getting sprites from spritesheet
self.spriteConfigs = get_config("Super_Strong_Normal_Guy.json")
self.spriteConfigs = self.spriteConfigs["NormalGuy"][action]
self.path = self.spriteConfigs["path"]
self.width = self.spriteConfigs["width"]
self.height = self.spriteConfigs["height"]
self.sheet_specs = self.spriteConfigs["sheet_specs"]
self.scale = self.spriteConfigs["scale"]
# getting sprites from spritesheet
self.spriteSheet = Spritesheet(self.path)
self.images = self.spriteSheet.get_sprites(self.width, self.height, self.sheet_specs, self.scale)
# essentials for Sprite class
self.image = self.images[0]
self.rect = self.image.get_rect()
self.rect.topleft = lo
self.index = 0
def update(self):
if self.index >= len(self.images):
self.index = 0
self.image = self.images[self.index]
self.index += 1
你必须改变指令的顺序。绘制图像前清除背景:
def main():
# [...]
while run:
# FPS of gameloop
clock.tick(FPS)
# check events during game loop
for event in pygame.event.get():
# check for quit pygame
if event.type == pygame.QUIT:
run = False
p1.update()
WIN.fill(WHITE) <-- INSERT
p1.draw(WIN)
# WIN.fill(WHITE) <-- DELETE
pygame.display.update()