【问题标题】:Quick debugging question [Python, pygame]快速调试题【Python、pygame】
【发布时间】:2011-09-27 01:53:41
【问题描述】:

它仍然是一个不完整的程序,但由于某种原因,文本框的值在它应该增加的时候没有增加......这是为什么? 当 Pizza sprite 与 Pan sprite 重叠时,文本框中的分数应该增加 10。为什么不会发生这种情况?

谢谢!

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

【问题讨论】:

    标签: python debugging pygame livewires


    【解决方案1】:

    文本框接受一个字符串值。当您创建文本框时,您从分数的当前值创建一个字符串,并将文本设置为该字符串。乐谱和文本框之间没有持久的联系。

    文本框可能有一个可用于更新其文本的方法;增加分数后,使用值str(self.score) 调用该方法。

    【讨论】:

    • 嗯...我知道另一种方法可以将分数更改为属性 self.textbox.value = _____ 但我更关心事情的逻辑。为什么分数没有变化?该变量与递增的变量相同,并且所有代码末尾的 mainloop() 无休止地循环代码,这是所有此类游戏所必需的……因此,如果您能解释逻辑,我将不胜感激.
    • 文本框在 Pan 的 __init__ 函数中创建 - 即,为每个创建的 Pan 对象创建一次。这是设置文本框值的唯一位置。重要的是要意识到str(self.score) 是一个不可变的字符串,它基于 Pan 创建时self.score 的当前数值构建。这与self.score 命名的变量完全不同。 python中的字符串也是不可变的,所以即使你创建了一个Pan的字符串成员来保存分数的字符串表示,并将文本框指向它,它仍然不会自动更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 2014-08-24
    • 2017-06-09
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多