为什么我不能让我的 Pygame Sprite 在 OSX 上移动?
我正在玩 Pygame(从 Andy Harris 2007 年出版的《游戏编程》一书中自学)。
他给出了几个移动精灵的例子。我看不出他的示例和我下面的代码之间的区别,但我的精灵(我制作的鱼 jpeg)没有移动。 “岛”也是我制作的一张jpeg。基本上,鱼应该每帧水平移动 5 个像素。但相反,它只是坐在那里。也许我只是忽略了一些明显的事情,或者我的系统有问题(我安装了多个版本的Python,并且似乎正在运行游戏的版本显示导入错误,没有名为 scrap 的模块
)。
[编辑:我删除了所有疯狂的 OSX 东西,因为它非常分散注意力,但这不是问题]。
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()
I'm playing with Pygame (teaching myself from the 2007 book "Game Programming" by Andy Harris.)
He gives several examples of moving sprites. I don't see the difference between his examples and my code below, but my sprite (a fish jpeg that I made) doesn't move. The "island" is also a jpeg that I made. Basically, the fish is supposed to move horizontally 5 pixels every frame. But instead, it just sits there. Maybe I'm just overlooking something obvious, or maybe there's something wrong with my system (I have several versions of Python installed and the one that appears to be running the game says import error, no module named scrap
).
[edit: I removed all my crazy OSX stuff b/c it's pretty distracting and it wasn't the problem].
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我是安迪(您正在使用的书的作者)
我直接回复了你的电子邮件,但如果其他人正在阅读,为了 SO 社区的利益,我会在这里发帖。
对于 OSX 来说这根本不是问题。
所有 Mac 的东西都是转移注意力的东西(抱歉,这是一个钓鱼程序……我无法抗拒。)
问题出在第 42 行:
allsprites.update
它应该读取
allsprites.update()
这是微妙但关键的区别:
在Python中,方法或函数也可以被读取为变量。这是一项非常强大的功能,但您尚未使用。如果省略括号,则将其作为变量读取(并且不执行任何操作)。如果包含括号,则执行该函数。
你忘记了括号(事实上,有些编辑器有时似乎会删除它们),因此 Python 很高兴地接受你的代码作为合法指令。然而,它只是承认实际上有一个名为 allsprites.update 的方法。 update 方法从未被调用,因此精灵从未更新,因此它的位置也从未改变。
我的版本实际上调用了精灵组的更新方法,该方法又调用了所有成员精灵的更新方法,程序运行得很好。可以肯定的是,我已经在所有三个主要操作系统(Ubuntu、Mac 和 Win 7)上对其进行了测试,并且运行良好。
如果您对代码是否在您的操作系统上运行有疑问,请从我的网站 (http://www.aharrisbooks.net) 下载我的原始测试代码,看看它是否运行。
如果您在其他地方遇到困难,请告诉我。
顺便说一句,我目前正在编写一本 HTML5 游戏编程书籍。请访问我的网站以获取更多信息......
I'm Andy (author of the book you're using)
I answered your email directly, but if anybody else is reading, I'll post here for the good of the SO community.
It's not a problem with OSX at all.
All the Mac stuff is a red herring (sorry, it was a fish program... I couldn't resist.)
The problem is line 42:
allsprites.update
It should read
allsprites.update()
Here's the subtle but crucial difference:
In Python, a method or function also can be read as a variable. That's a really powerful feature, but not one you're using yet. If the parentheses are left off, it's read as a variable (and it doesn't do anything.) If the parentheses are included, the function is performed.
You forgot the parentheses (in fact, some editors seem to remove them sometimes) and so Python happily accepted your code as legal instructions. However, it just acknowledged that there is in fact a method called allsprites.update. The update method was never called, so the sprite never updated, thus its position never changed.
My version actually calls the update method of the sprite group, which in turn calls the update methods of all member sprites, and the program works just fine. Just to be sure, I have tested it on all three major operating systems (Ubuntu, Mac, and Win 7) and it is working fine.
If you ever have doubts about whether code is working on your OS, please download my original tested code from my web site (http://www.aharrisbooks.net) and see if that's working.
Let me know if you get stuck anywhere else.
BTW, I'm currently working on a game programming book for HTML5. Stop by my site for more info....