我这里有一个棘手的问题。基本上我想做的是有一个类似tamaguchi的程序,人们可以选择tamaguchi做什么,它或者增加大小或减少。这对于只有一个tamaguchi非常有用!但是应该可以同时创建和管理几个tamaguchis。我尝试这个时遇到了大问题。基本上这就是我到目前为止的想法:
class Tamaguchi(object):
def __init__(self,name,size=1):
self.name=name
self.size=size
def increasesize(self):
self.size+=1
GoodBadChange.config(text="Good job. You increased in size!")
def decreasesize(self):
self.size-=1
GoodBadChange.config(text="Bad job. You decreased in size!")
def main():
print(name)
root = tkinter.Tk()
global root
root.title("Tamaguchi-spelet")
root.geometry("900x900")
getLists()
getPhotos()
Picture = tkinter.Label(root, image=normal)
Picture.pack()
global Picture
ScoreBoard = tkinter.Label(root, text="Score " + str(tamaguchin.size), font=('Helvetica', 15))
ScoreBoard.pack()
global ScoreBoard
GoodBadChange = tkinter.Label(root, text="", font=('Helvetica', 12))
GoodBadChange.pack()
global GoodBadChange
LatestAction = tkinter.Label(root, text="", font=('Helvetica', 12))
LatestAction.pack()
global LatestAction
app = Application(root)
app.pack()
updateDisplay()
LatestAction.config(text="Hello and welcome to the tamaguchi game!\nThe tamaguchi starts the day with the size "+str(tamaguchin.size)+"!\nThe tamaguchi starts the day off with the last three actions of "+lista[0]+"-"+lista[1]+"-"+lista[2]+"\n")
root.mainloop()
tamaguchi_list=[]
amount=int(input("Number of tamaguchis in your farm: "))
for i in range(amount):
name = input("What do you want to name your tamaguchis?: ")
tamaguchi_name = Tamaguchi(name)
tamaguchi_list.append(tamaguchi_name)
for tamaguchis in tamaguchi_list:
main()
for tamaguchis in tamaguchi_list:
print(name,"reached a size of ",name.size,"!")
对不起它有点长,我还是缩短了不相关的部分。但我在想,我们创建一个包含所有tamaguchis的列表,然后我们只为tamaguchi列表中的每个tamaguchi运行main函数。这样,例如“erik”获得一个分数,并且“mary”获得另一个分数,然后应该最终写入。但是,这似乎不起作用,因为你可以看到我在main函数的开头写了“print(name)”只是为了看到它实际上遍历列表中的所有名称,但实际上它只是打印出来同名一遍又一遍。我不知道为什么它没有通过所有名称。
另一个问题是,当我想要显示tamaguchi的大小时,我在主要功能中写了这样的事实,如str(tamaguchin.size)
,但这是因为当我只有一个tamaguchi时,我刚开始创建它我可以在程序的其余部分中引用它(tamaguchin = Tamaguchi('SomeName')就是我以前拥有的!)这可以解决吗?
非常感谢你的帮助,我真的很难受。
编辑:也许不清楚,因为我没有显示所有的代码。我觉得它可能太长了,但也许最好明白我的意思! I uploaded it here!
答案 0 :(得分:0)
本章描述了一种游戏,其中玩家会因为游戏中的事件而导致生物“增大”:http://inventwithpython.com/pygame/chapter6.html
通读游戏设计,类似于增加尺寸,可以减少生物的大小。
为了管理多个生物,请阅读本章:http://inventwithpython.com/pygame/chapter8.html
答案 1 :(得分:-1)
我需要您更具体地说明您的错误发生在哪些代码行中才能正确回答这个问题。尽管如此,我建议使用更有效的结构来管理你的tamaguchins。
我所理解你描述的是'tamaguchi做某事'然后'tamaguchi增长或缩小'。
使用字典尝试这样的事情:
tamaguchins = {"ralph":10, "george":5, "chester":2}
def size_modify(tamaguchin, plus_minus, tamaguchins):
tamaguchins[tamaguchin] += plus_minus
return tamaguchins
这样称呼:
tamaguchins = size_modify("ralph", -1, tamaguchins)
或者像这样:
tamaguchins = size_modify("george", 1, tamaguchins)
或者你喜欢,但请记住,在这种情况下,你必须始终在调用函数之前使用'tamaguchins =',并且你的第三个参数必须始终是tamaguchins。
干杯