-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_solution_other_memory_game.py
89 lines (68 loc) · 2.56 KB
/
class_solution_other_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Initialize a game of Memory using the Tile class
#################################################
# Student adds code where appropriate
import simplegui
import random
# define globals
TILE_WIDTH = 50
TILE_HEIGHT = 100
DISTINCT_TILES = 8
# helper function to initialize globals
def new_game():
global my_tiles, state, turns
tile_numbers = range(DISTINCT_TILES) * 2
random.shuffle(tile_numbers)
my_tiles = [Tile(tile_numbers[i], False, [TILE_WIDTH * i, TILE_HEIGHT]) for i in range(2 * DISTINCT_TILES)]
state = 0
turns = 0
label.set_text("Turns = "+str(turns))
# definition of a Tile class
class Tile:
# definition of intializer
def __init__(self, num, exp, loc):
self.number = num
self.exposed = exp
self.location = loc
# definition of getter for number
def get_number(self):
return self.number
# check whether tile is exposed
def is_exposed(self):
return self.exposed
# expose the tile
def expose_tile(self):
self.exposed = True
# hide the tile
def hide_tile(self):
self.exposed = False
# string method for tiles
def __str__(self):
return "Number is " + str(self.number) + ", exposed is " + str(self.exposed)
# draw method for tiles
def draw_tile(self, canvas):
loc = self.location
if self.exposed:
text_location = [loc[0] + 0.2 * TILE_WIDTH, loc[1] - 0.3 * TILE_HEIGHT]
canvas.draw_text(str(self.number), text_location, TILE_WIDTH, "White")
else:
tile_corners = (loc, [loc[0] + TILE_WIDTH, loc[1]], [loc[0] + TILE_WIDTH, loc[1] - TILE_HEIGHT], [loc[0], loc[1] - TILE_HEIGHT])
canvas.draw_polygon(tile_corners, 1, "Red", "Green")
# selection method for tiles
def is_selected(self, pos):
inside_hor = self.location[0] <= pos[0] <= self.location[0] + TILE_WIDTH
inside_vert = self.location[1] - TILE_HEIGHT <= pos[1] <= self.location[1]
return inside_hor and inside_vert
# draw handler
def draw(canvas):
for tile in my_tiles:
tile.draw_tile(canvas)
# create frame and add a button and labels
frame = simplegui.create_frame("Memory", 2 * DISTINCT_TILES * TILE_WIDTH, TILE_HEIGHT)
frame.add_button("Restart", new_game)
label = frame.add_label("Turns = 0")
frame.set_draw_handler(draw)
# get things rolling
new_game()
frame.start()
###################################################
# Create a horizontal row of 16 Memory tile with numbers hidden