开发者

Quick debugging question [Python, pygame]

It's still an incomplete program, but for some reason the value of the textbox doesn't increase when it should... Why is this?? When the Pizza sprite overlaps with the Pan sprite, the score in the textbox is supposed to increase in value by 10. Why does this not occur?

Thanks!

'''
Created on Jul 1, 2011

@author: ******* Louis
'''
#Watch me do.
from livewires import games, color
import random

games.init (screen_width = 640, screen_height = 480, fps = 50)

#Pizza Class
class Pizza (games.Sprite):
    pizzaimage = games.load_image ("pizza.bmp", transparent = True)
    def __init__(self, x = random.randrange(640), y = 90, dy = 4):
        super (Pizza, self).__init__(x = x, 
                                     y = y,
                                     image = Pizza.pizzaimage, 
                                     dy = dy)

    def handle_caught (self):
        self.destroy()


class Pan (games.Sprite):
    panimage = games.load_image ("pan.bmp", transparent = True)
    def __init__ (self, x = games.mouse.x, y = games.mouse.y):
        super (Pan, self).__init__(x = x, 
                                   y = y, 
                                   image = Pan.panimage)
      开发者_如何学Python  self.score = 0
        self.textbox = games.Text (value = str(self.score),
                                    size = 20,
                                    color = color.black,
                                    x = 550,
                                    y = 50)
        games.screen.add(self.textbox)


    def update (self): #WWWWOW There is actually an *update* method
        self.x = games.mouse.x
        self.y = games.mouse.y

        if self.left < 0:
            self.left = 0
        if self.right >640:
            self.right = 640
        if self.top < 0:
            self.top = 0
        if self.bottom > 480:
            self.bottom = 480 
        self.check_collision()

    def check_collision (self):
        for Pizza in self.overlapping_sprites:
            self.score = self.score + 10
            Pizza.handle_caught()


#main
def main():
    wallbackground = games.load_image ("wall.jpg", transparent = False)
    games.screen.background = wallbackground

    games.screen.add(Pizza())

    games.screen.add(Pan())
    games.mouse.is_visible = False
    games.screen.event_grab = True

    games.screen.mainloop()
main()


The textbox takes a value that is a string. When you create the textbox, you create a string from the current value of score, and set the text to that string. No lasting connection between score and textbox is made.

The textbox probably has a method available to update its text; call that method with the value str(self.score) after you increment the score.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜