-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory game.py
66 lines (56 loc) · 2.1 KB
/
memory game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# implementation of card game - Memory
# Author : Amanadeep
import simplegui
import random
# helper function to initialize globals
def new_game():
global list_nums, turn, card_list, turns, exposed
exposed = [] #stores the exposed set of cards in last two moves
turn = 0
turns = 0 #stores total number of turns that are to be displayed
label.set_text("Turns = "+str(turns))
#generate random list of elements
list_nums = [[i%8+1, False] for i in range(16)]
#radomly shuffle list of numbers
random.shuffle(list_nums)
# define event handlers
def mouseclick(pos):
global turns, turn, exposed
for card in range(16):
if pos[0] > (card*50) and pos[0] < (card*50+48) and not (list_nums[card][1]):
list_nums[card][1] = True
# Keep track of the current state of game
if turn == 0:
turn = 1
exposed.append(card)
elif turn == 1:
exposed.append(card)
turn = 2
turns += 1
label.set_text("Turns = "+str(turns))
else:
if not (list_nums[exposed[0]] == list_nums[exposed[1]]):
list_nums[exposed[0]][1] = False
list_nums[exposed[1]][1] = False
exposed = []
exposed.append(card)
turn = 1
# cards are logically 50x100 pixels in size
def draw(canvas):
for card in range(16):
#Draw cards that are visible
if list_nums[card][1]:
canvas.draw_text(str(list_nums[card][0]),[card + 50*card + 10, 70], 45, "white")
#Draw cards that are not visible
else:
canvas.draw_polygon([[card*50+25, 0], [card*50+25, 100]], 48, "green")
# create frame and add a button and labels
frame = simplegui.create_frame("Memory", 800, 100)
frame.add_button("Restart", 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()