莱斯大学Python课程Mini-project#5 - Memory
来源:互联网 发布:sql怎么导入数据 编辑:程序博客网 时间:2024/05/22 20:12
要求在这里。 我编的这一坨虽然执行起来像模像样的,但真是乱七八糟。
# Memoryimport simpleguiimport randomimport math# 初始化n=8 # 牌的张数是2*ncounter=0 # 显示翻牌次数 color='Teal' # 翻牌前后的颜色变化new_pos=0 # 鼠标目前点击的哪张牌Mlist=list(range(0,n)*2)# 创建Listrandom.shuffle(Mlist)#随机调整顺序exposed=[] #初始化exposed数组for i in range(0,2*n): exposed.append(False) state_counter=0 # state 状态计数器state=[] #初始化state数组for i in range(0,2*n): state.append(0) # helper function: 判断牌的现有状态def judge_state(): for i in range (0,2*n): if exposed[i]==False:# 牌扣住 state[i]=0 # 状态为0不理睬 elif exposed[i]==True: # 如果牌翻开 for j in range(0,2*n): if exposed[j]==True and Mlist[i]==Mlist[j] and i!=j: state[i]=2 # 已经翻开的成对牌状态为2不理睬 state[j]=2 # 已经翻开的成对牌状态为2不理睬 elif state[i]!=2: state[i]=1 down_two_single() #调用另一个helper function # helper function 把两个单张牌扣上 def down_two_single(): global state_counter state_counter=0 for i in range (0,2*n): if state[i]==1 : state_counter +=1 if state_counter>2: for i in range (0,2*n) : if state[i]==1 and i!=new_pos: # 不能把鼠标目前所点的那张牌消掉 exposed[i]=False state[i]=0 # 画出canvas def draw(canvas): global color for i in range(0,2*n): if exposed[i]==True: show=str(Mlist[i]) color='Black' elif exposed[i]==False: show=" " color='Teal' canvas.draw_polygon([[i*60, 0], [(i+1)*60,0], [(i+1)*60, 100], [i*60, 100]], 1, "Orange",color) canvas.draw_text(show,[20+i*60,60],40,'White') # 鼠标点击的event handlerdef click(pos): global new_pos for i in range(0,2*n): if pos[0]>i*60 and pos[0]<(i+1)*60: if exposed[i]==False: global counter exposed[i]= True counter +=1 new_pos=i label.set_text("counter: "+str(counter)) judge_state() # 调用一个helper #下面这些print 都是用来检查的 print "Mlist:" ,Mlist print "exposed:" ,exposed print "state:" ,state print "state_counter:",state_counter print" " # 新开一局 按钮event handlerdef newgame(): global counter random.shuffle(Mlist)#随机调整顺序 counter=0 for i in range(0,2*n): exposed[i]=False state[i]=0 # Create a frame and assign callbacks to event handlersframe = simplegui.create_frame("Memory", 2*n*60, 100)frame.set_draw_handler(draw)frame.add_button("newgame",newgame )frame.set_mouseclick_handler(click)label=frame.add_label(" counter: ",200) # 显示翻牌次数 # start frameframe.start()
0 0
- 莱斯大学Python课程Mini-project#5 - Memory
- 莱斯大学Python课程Mini-project#4 - "Pong"
- 莱斯大学Python课程Mini-project # 2 - "Guess the number" game
- 莱斯大学Python课程Mini-project # 1- Rock-paper-scissors-lizard-Spock
- 莱斯大学Python课程Mini-project# 3 - "Stopwatch: The Game"
- Interactive Python:Mini-project # 5 - Memory
- [Interactive Python]Memory游戏 Mini-project #5
- Mini-project # 5 - Memory
- Mini-project #5 - Memory
- 【莱斯大学 Python交互编程入门】 作业Week5 Memory
- 莱斯大学Python课程第四周演示程序
- 莱斯大学Python课程第五周演示程序
- 莱斯大学Python课程第六周演示程序
- Interactive Python:Mini-project #4 - "Pong"
- Interactive Python:Mini-project # 6 - Blackjack
- Interactive Python:Mini-project # 7 - Spaceship
- Interactive Python:Mini-project #8 - RiceRocks (Asteroids)
- Interactive Programming in Python Mini Project 1
- 符号释意字典
- 【NoisyHeart的炼成】Part5:Perlin噪声的HLSL实现
- hdu4763(KMP的应用)
- POJ3278 Catch That Cow(BFS入门)
- textblock 实现滚动条和自动换行
- 莱斯大学Python课程Mini-project#5 - Memory
- 黑马程序员_Java技术博客书写规范
- 大家来写2048 。。。控制台版
- cocos2d-x3.0 RenderTexture(二)
- windows下tomcat的安装与配置及eclipse的tomcat服务器搭建
- 【LeetCode】Linked list Cycle
- Android调用 Webservice报org.ksoap2.serializa 异常
- Struct2设置全局共享的action
- qsort和sort函数中的 cmp函数