Memory is a card game in which the player deals out a set of cards face down. In Memory, a turn (or a move) consists of the player flipping over two cards. If they match, the player leaves them face up. If they don't match, the player flips the cards back face down. The goal of Memory is to end up with all of the cards flipped face up in the minimum number of turns. For this project, we will keep our model for Memory fairly simple. A Memory deck consists of eight pairs of matching cards.
for
loop and uses draw_text
to draw the number associated with each card on the canvas. The result should be a horizontal sequence of evenly-spaced numbers drawn on the canvas.random.shuffle()
. Remember to debug your canvas drawing code before shuffling to make debugging easier.exposed
. In the exposed
list, the ith entry should be True
if the ith card is face up and its value is visible or False
if the ith card is face down and it's value is hidden. We suggest that you initialize exposed
to some known values while testing your drawing code with this modification.i
th card, you can change the value of exposed[i]
from False
to True
. If the card is already exposed, you should ignore the mouseclick. At this point, the basic infrastructure for Memory is done.set_text
to update this counter as a label in the control panel. (BTW, Joe's record is 12 turns.) This counter should be incremented after either the first or second card is flipped during a turn.init()
function (if you have not already) so that the "Reset" button reshuffles the cards, resets the turn counter and restarts the game. All cards should start the game hidden.draw_text
for each card by a draw_image
that uses one of eight different images.While this project may seem daunting at first glance, our full implementation took well under 100 lines with comments and spaces. If you feel a little bit intimidated, focus on developing your project to step six. Our experience is that, at this point, you will begin to see your game come together and the going will get much easier.
代码如下:
# implementation of card game - Memory import simplegui import random num=[] state=0 preindex=0 aftindex=0 turn=0 # helper function to initialize globals def init(): global num,exposed,turn num=range(0,8) num.extend(range(0,8)) random.shuffle(num) exposed=[False,False,False,False,False,False,False,False, False,False,False,False,False,False,False,False] turn=0 #print num # define event handlers def mouseclick(pos): # add game state logic here global exposed,state,preindex,aftindex,turn index=pos[0]//50 if not exposed[index]: exposed[index]=True #state=(state+1)%3 if state==0: state=1 preindex=index elif state==1: aftindex=index state=2 elif state==2: if num[preindex]!=num[aftindex]: exposed[aftindex]=False exposed[preindex]=False preindex=index state=1 turn+=1 # cards are logically 50x100 pixels in size def draw(canvas): global num horzoffset=0 index=0 label.set_text("Moves = "+str(turn)) for tmp in num: if exposed[index]: canvas.draw_text(str(tmp), (horzoffset, 100), 100, "White") else: canvas.draw_polygon([(horzoffset, 0), (horzoffset, 100), (horzoffset+50, 100),(horzoffset+50, 0)], 1, "Green", "Green") horzoffset+=50 index+=1 # create frame and add a button and labels frame = simplegui.create_frame("Memory", 800, 100) frame.add_button("Restart", init) label = frame.add_label("Moves = 0") # initialize global variables init() # register event handlers frame.set_mouseclick_handler(mouseclick) frame.set_draw_handler(draw) # get things rolling frame.start() # Always remember to review the grading rubric