欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

An Introduction to Interactive Programming in Python 第六周作业

程序员文章站 2022-07-14 10:05:07
...

描述:颠倒相同的扑克牌就会常亮。

An Introduction to Interactive Programming in Python 第六周作业

上代码,逻辑废点脑子

import simplegui
import random

numbers = [0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
STATE   = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
f=s=t=n=0
turn = click = 0
label = 0

def new_game():
    global numbers,state,turns,click
    random.shuffle(numbers)
    state = 0
    for m in range(16):   #restart
        STATE[m] = 0 
    turns = click = 0
    #print numbers
    #print STATE
    label.set_text("Turns = " + str(turns)) 
def mouseclick(pos):
    global numbers,state,n,f,s,small,middle,large,turns,click
    n = pos[0] // 50
    w = 1
    e = 2
    click += 1 
    if STATE[n] == 100:
        return
    
    if state ==0:
        state = 1
        STATE[n] = 100
        small=n
        f = numbers[n]
    elif state == 1:
        state = 2
        STATE[n] = 100
        middle = n
        s = numbers[n]
        #print "second step"
        if f == s: 
            state = 0
            #print "same",f,s
    else:
        #print "there"
        state = 1
        STATE[n] = 100
        large = n
        STATE[small] = 0   #第一张纸牌变暗
        t = numbers[n]
        if t == s:
            
            state = 0  
        else:
            #print "!="
            STATE[middle] = 0  #第二张纸牌变暗
            small = large
            f = numbers[n]
        #print "tired step"
    #print STATE
    turns = click // 2
    label.set_text("Turns = " + str(turns))

    
        
    

     
    
def draw(canvas):
    global numbers
    #draw 底色以及线
    for i in range(16):
        
        canvas.draw_polygon([[i * 50, 0], [i * 50, 100], [(i+1)*50, 100], [(i+1)*50, 0]], 2, 'Black', 'Green')
    #draw 数字示例 
    for j in range(16):
        if STATE[j] == 100:
                canvas.draw_polygon([[j * 50, 0], [j * 50, 100], [(j+1)*50, 100], [(j+1)*50, 0]], 2, 'Black','Black')
                canvas.draw_text(str(numbers[j]),[j*50+12,65],50,"white")
                





frame = simplegui.create_frame("Memory", 800, 100)
frame.add_button("Restart",new_game)
label = frame.add_label("Turns = 0 ") # label添加



frame.set_draw_handler(draw)
frame.set_mouseclick_handler(mouseclick)
frame.start()
new_game()