Mini-project # 5 - Memory

=======================第一版=============================


# implementation of card game - Memory

import simplegui
import random

num = [] 
state=0  
fst_index=0  
sec_index=0  
turn=0
mark = 0

# helper function to initialize globals
def new_game():
    global num, exposed, turn, mark
    num = range(0,8)
    num.extend(range(0,8))
    random.shuffle(num)
    exposed = [False] *16
    turn = 0
    mark = 0
    
 
    
# define event handlers
def mouseclick(pos):
    global exposed, state, fst_index, sec_index, turn, mark
    index = pos[0]/50
    if not exposed[index]:
        exposed[index] = True
        turn += 1
        if turn%2 != 0:
            mark += 1
        if state == 0:
            state = 1
            fst_index = index
        elif state == 1:
            sec_index = index
            state = 2
        elif state == 2:
            if num[fst_index] != num[sec_index]:
                exposed[fst_index] = False
                exposed[sec_index] = False
            fst_index = index
            state = 1
            
        
        
                        
# cards are logically 50x100 pixels in size    
def draw(canvas):
    global num
    position = 0
    index = 0
    
    label.set_text("Turns = " + str(mark))
    for n in num:
        if exposed[index] == True:
            canvas.draw_text(str(n), (position, 90), 100, "White") 
        else:
            canvas.draw_polygon([(position, 0), (position, 100),  
                                 (position+50, 100),(position+50, 0)],  
                                1, "Gray", "Silver")  
        position += 50
        index += 1


# create frame and add a button and labels
frame = simplegui.create_frame("Memory", 800, 100)
frame.add_button("Reset", new_game, 100)
label = frame.add_label("Turns = 0")

# register event handlers
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)

# get things rolling
new_game()
frame.start()


# Always remember to review the grading rubric


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值