python - Cookie Clicker 游戏错误“无法获取属性“gm”'
问题描述
我正在尝试制作一个 cookie clicker 游戏,一切正常,但现在出现错误
new_dict=pickle.load(infile)
AttributeError: Can't get attribute 'gm' on <module '__main__' (built-in)>
import tkinter as tk
from tkinter import *
import pickle
#save data transfer
filename='cookie_clicker'
infile=open(filename,'rb')
new_dict=pickle.load(infile)
infile.close()
print(new_dict)
cookies = new_dict.get('cookies')
click_amount=new_dict.get('click_amount')
gm= new_dict.get('grandmas')
gm_cost=new_dict.get('gm_cost')
root = Tk()
root.title("Cookie Clicker")
#button commands
def click():
global cookies,click_amount
cookies += click_amount
cookies_Label.config(text='cookies you have: '+str(cookies))
cookies_dict={'cookies':cookies,
'click_amount':1,
'grandmas':gm,
'gm_cost': 50}
outfile=open(filename,'wb')
pickle.dump(cookies_dict,outfile)
outfile.close()
def buy_grandma():
global cookies,gm
if cookies>= gm_cost:
gm +=1
cookies -= gm_cost
cookies_Label.config(text='cookies you have: '+str(cookies))
cookies_dict={'cookies':cookies,
'click_amount':1,
'grandmas':gm,
'gm_cost': 50}
outfile=open(filename,'wb')
pickle.dump(cookies_dict,outfile)
outfile.close()
def quit():
root.destroy()
#click section
frame=Frame(master=root,bg='#F0FFFC',width =400, height=400)
frame.pack(side=TOP,fill=tk.BOTH)
cookies_Label =Label(frame,text = 'cookies you have: ' + str(cookies),relief='groove',borderwidth= 3)
cookies_Label.grid(row = 0, column=1,columnspan=3, padx=110, pady=10)
cookie_button= Button(frame,padx = 110,pady = 100,command = click)
cookie_button.grid(row=1, column=0,columnspan=4)
exit_button = Button(frame,text = 'X',relief='groove',borderwidth=3, command=quit)
exit_button.grid(row=0,column=0)
#upgrade section
up_frame=Frame(root,bg= '#FFF1E4',relief='groove',borderwidth=3,width=400, height=400)
up_frame.pack(side=BOTTOM,fill=tk.BOTH)
clicker_up=Frame(up_frame,text='''clicker upgrade
+1 cookie per click
costs : '''+str(clicker_up_cost)+'cookies',relief='groove',borderwidth=3)
clicker_up.grid(row=0, column=0)
gm_label = Label(up_frame,text='''grandma
+1 cookies per second''',relief='groove', borderwidth=3)
gm_label.grid(row=1, column=0)
gm_button = Button(up_frame,text= '''upgrade grandma :
'''+ str(gm_cost)+' cookies',relief='groove',borderwidth=3,command=buy_grandma,state=NORMAL)
gm_button.grid(row=1,column=1)
#autoclicker
root.mainloop()
我不知道要发送代码的哪一部分,所以我全部发送了。
请帮助它将不胜感激。
解决方案
推荐阅读
- ios - 我想像在 JIOtv App 中一样创建 collectionView。这是新东西,网上没有答案
- git - 如何在git中使HEAD分离
- jquery - 如何每 X 秒刷新一次 DIV
- java - 发送 id 的常规方式是 @PathParam 还是 @HeaderParam 还是 @QueryParam?
- elasticsearch - Elasticsearch:简单查询不匹配字段
- java - 使用 Winiumdriver 的 WebDriver 异常
- ios - 如何使用核心数据使用 id 数组获取对象数组?
- python - Python:使用Requests包在HTTPS页面上执行get请求时,如何确保登录凭据被加密
- angular - 使用 ngFor 循环数据,通过索引 # 匹配两个对应项,并将其他数据放入 ngFor 循环中
- ios - 如何检查通用视图类是否实际实现了 init(frame:)?