开发者

Why Can't I Make My Pygame Sprite Move on OSX?

开发者 https://www.devze.com 2023-04-02 09:01 出处:网络
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

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()      


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....

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号