forked from anthonymonori/python-minigames
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Memory-week5.py
116 lines (96 loc) · 3.21 KB
/
Memory-week5.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# implementation of card game - Memory
import simpleguitk as simplegui
import simpleplot
import random
HEIGHT = 100
WIDTH = 800
CARD_HEIGHT = 100
CARD_WIDTH = 50
number_list = [] # list with the numbers
deck = []
exposed = []
cool = []
card_color = "#006400"
text_color = "#006400"
TEXT_SIZE = 28 #px
CARD_LINE_COLOR = "#FF0000"
INIT_STATE = "unflipped"
moves = 0
# helper function to initialize globals
def init():
global deck, number_list, card_color, text_color, exposed, cool, moves
number_list = [range(1,9) for i in range(2)]
number_list = number_list[0] + number_list[1]
random.shuffle(number_list) # Everyday I'm shuffeling
INIT_STATE = "unflipped"
i = 0
cool = []
moves = 0
while i < len(number_list):
text_position = [((i * CARD_WIDTH) + (CARD_WIDTH / 2) -10), CARD_HEIGHT - 40]
card = [([i*CARD_WIDTH, 0], [(i+1)*CARD_WIDTH, 0], [(i+1)*CARD_WIDTH, CARD_HEIGHT], [i*CARD_WIDTH, CARD_HEIGHT]), 1, CARD_LINE_COLOR, card_color, number_list[i], text_position, text_color, INIT_STATE]
deck.append(card)
i += 1
#------------------------------------------------------#
# define event handlers
def mouseclick(pos):
global exposed, cool
for card in deck:
if ((card[0][0][0] <= pos[0]) and (card[0][1][0] >= pos[0]) and (card[7] == "unflipped") and len(exposed) < 2):
card[3] = "#000000"
card[6] = "#FFFFFF"
card[7] = "flipped"
exposed.append(card[4])
break
if len(exposed) >= 2:
exposed = []
clean()
break
#-----------------------------------------------------#
def checkcard():
global deck, cool
for card in deck:
for c in cool:
if c == card[4] and card[7] == "flipped":
card[7] = "exposed"
def clean():
global moves
for card in deck:
if card[7] == "flipped":
card[7] = "unflipped"
moves += 1
#-----------------------------------------------------#
# cards are logically 50x100 pixels in size
def draw(canvas):
global deck, exposed, cool, moves
i = 0
for card in deck:
canvas.draw_polygon(deck[i][0], deck[i][1], deck[i][2], deck[i][3])
if len(exposed) == 0:
card[3] = "#006400"
card[6] = "#006400"
if card[7] == "exposed":
card[3] = "#000000"
card[6] = "#FFFFFF"
canvas.draw_text(str(card[4]), card[5], TEXT_SIZE, card[6])
i += 1
if len(exposed) == 2 and exposed[0] == exposed[1]:
cool.append(exposed[0])
cool.append(exposed[1])
exposed.pop()
exposed.pop()
moves += 1
checkcard()
l.set_text("Moves = " + str(moves))
# create frame and add a button and labels
frame = simplegui.create_frame("Memory", WIDTH, HEIGHT)
frame.add_button("Restart", init)
l=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