This repository has been archived by the owner on Oct 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
memory.py
143 lines (119 loc) · 4.19 KB
/
memory.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'''
The MIT License (MIT)
Copyright (c) 2016 Antal Janos Monori.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
###########
# Imports #
###########
try:
import simplegui
except ImportError:
import SimpleGUICS2Pygame.simpleguics2pygame as simplegui
import simpleplot
import random
#############
# Variables #
#############
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 functions #
####################
# 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
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
############
# Handlers #
############
# 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
# 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))
######
# UI #
######
# 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")
########
# Main #
########
# initialize global variables
init()
# register event handlers
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)
# get things rolling
frame.start()