我在玩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()