我在玩Pygame(自己从安迪哈里斯2007年的书“Game Programming”中学到了自己。)为什么我不能让我的Pygame Sprite在OSX上移动?
他给出了几个移动精灵的例子。我没有看到他的例子和我的代码之间的区别,但我的精灵(我制作的鱼jpeg)不移动。这个“岛”也是我制作的jpeg。基本上,鱼应该每帧水平移动5个像素。但是,它只是坐在那里。也许我只是忽视了一些显而易见的东西,或者说我的系统出了问题(我安装了几个版本的Python,看起来运行游戏的版本是import error, no module named scrap
)。
[编辑:我删除了所有我的疯狂OSX的东西B/C它是相当分散注意力,这是没有问题的。
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
background = pygame.image.load('island_background.jpg')
background = background.convert()
class Fish(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('fish.jpg')
self.image = self.image.convert()
self.rect = self.image.get_rect()
self.rect.centerx = (600)
self.rect.centery = (500)
self.dx = 5
def update(self):
self.rect.centerx += self.dx
def main():
keepgoing = True
clock = pygame.time.Clock()
fish = Fish()
allsprites = pygame.sprite.Group(fish)
while keepgoing:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepgoing = False
screen.blit(background, (0, 0))
allsprites.clear(screen, background)
#I thought the line above might be a problem,
#but commented it out and nothing changed (well, maybe I got a
#black background, but I don't recall. The fish just sat there in
#the same spot still
allsprites.update
allsprites.draw(screen)
pygame.display.flip()
if __name__ == '__main__':
main()