Coursera_An Introduction to Interactive Programming in Python_Mini-project # 5 Memory

http://www.codeskulptor.org/#user38_6vsNKw1hBx_9.py


# implementation of card game - Memory

import simplegui
import random

card_list = []
click_index=[] # record the index of first two mouse click
state = 0
counter = 0
# To indicate the draw handler to either draw a blank green rectangle or the card's value
# True means card is face up
# False means card is face down
exposed = [False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False] 

# helper function to initialize globals
def new_game():
    global state, card_list, counter, exposed,click_index
    state = 0
    counter = 0
    click_index = []
    label.set_text("Turns = 0")
    # create the deck of cards
    card_list = range(8)
    card_list.extend(range(8))
    random.shuffle(card_list)
    exposed = [False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False] 
# define event handlers
def mouseclick(pos):
    global state,click_index,counter
    # find the mouse click which card
    for i in range(0,16):
        if i*50 < pos[0] and pos[0] < (i+1)*50:
            if not exposed[i]:
               if state == 0:
                   state = 1
                   exposed[i] = True
                   click_index.append(i)
               elif state ==1:
                   state = 2
                   exposed[i] = True
                   click_index.append(i)
                   counter += 1
                   label.set_text("Turns = "+str(counter))
               else:
                    state = 1
                    exposed[i] = True
                    if card_list[click_index[0]] == card_list[click_index[1]]:
                        exposed[click_index[0]] = True
                        exposed[click_index[1]] = True
                        click_index = []
                        click_index.append(i)
                    else:
                        exposed[click_index[0]] = False
                        exposed[click_index[1]] = False
                        click_index = []
                        click_index.append(i)
                     
# cards are logically 50x100 pixels in size    
def draw(canvas):
   
    for i in range(0,16):
        if not exposed[i]:
            canvas.draw_polygon([(i*50, 0), (i*50, 100), ((i+1)*50, 100),((i+1)*50, 0)], 3, 'Grey', 'Green')
        else:
            canvas.draw_text(str(card_list[i]), ((2*i+1)*50/2-10, 50+15),44, 'White')


# create frame and add a button and labels
frame = simplegui.create_frame("Memory", 800, 100)
frame.add_button("Reset", new_game)
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()



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值