-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpuzzle_classes.py
More file actions
187 lines (145 loc) · 5.23 KB
/
puzzle_classes.py
File metadata and controls
187 lines (145 loc) · 5.23 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'''
Mariah Maynard
CS5001 Final Project
Sliding Puzzle Game
'''
import turtle
import random
import time
screen = turtle.Screen()
class Tiles:
"""
Makes each tile in the game a turtle object
Performs different actions
"""
def __init__(self, image, size, name, square, num) -> None:
self.turtle = turtle.Turtle(visible=False)
self.turtle.penup()
self.turtle.image = image
self.turtle.name = name
self.size = size
self.square = square
self.num = num
screen.addshape(image)
self.turtle.shape(image) # each turtle will take the image of a tile
def seen(self):
"""Tile shows itself"""
return self.turtle.showturtle()
def position(self):
"""Retrieve tile's position in x,y coordinates"""
return self.turtle.position()
def xcoord(self):
"""Retrieve the tile's x coordinated"""
return self.turtle.xcor()
def ycoord(self):
"""Retrieve tile's y coordinate"""
return self.turtle.ycor()
def go_here(self, x, y):
"""Sends tile to a specific position"""
return self.turtle.goto(x,y)
def swap(self, other):
"""Swaps tile's position with another tile's position"""
s = self.position()
o = other.position()
self.turtle.goto(o)
other.turtle.goto(s)
def place_tile(self) -> list:
""""
Purpose: Places the tiles into their positions
Parameter: None
Returns: A list of the correct tile positions
"""
tile_pos = [] # creating list of tile positions
for row in range(4, 0, -1):
for column in range(1, 5, 1):
tile_pos.append([column * 110 - 380, row * 110 - 180])
pos = int(self.num - 1) # take position of tile from list
if self.square == 4:
self.turtle.penup()
self.turtle.speed('fastest')
self.turtle.goto(tile_pos[pos][0], tile_pos[pos][1])
# self.turtle.showturtle()
return tile_pos
if self.square == 3:
mod_pos = tile_pos[:3]+tile_pos[4:7]+tile_pos[8:11]
self.turtle.penup()
self.turtle.speed('fastest')
self.turtle.goto(mod_pos[pos][0], mod_pos[pos][1])
return mod_pos
if self.square == 2:
mod_pos = tile_pos[:2] + tile_pos[4:6]
self.turtle.penup()
self.turtle.speed('fastest')
self.turtle.goto(mod_pos[pos][0], mod_pos[pos][1])
return mod_pos
def shuffle_tile(self, shuffled_tile_pos: list) -> list:
""""
Purpose: Places the tiles into their shuffled positions
Parameter: List of true positions, self
Returns: A list of the shuffled tile positions
"""
pos = int(self.num - 1) # take position of tile from list
self.turtle.penup()
self.turtle.speed('fastest')
self.turtle.goto(shuffled_tile_pos[pos][0], shuffled_tile_pos[pos][1])
self.turtle.showturtle()
return shuffled_tile_pos
def reset(self) -> list:
""""
Purpose: Places the tiles into their true positions
Parameter: Self
Returns: None
"""
tile_pos = [] # creating list of tile positions
for row in range(4, 0, -1):
for column in range(1, 5, 1):
tile_pos.append([column * 110 - 380, row * 110 - 180])
pos = int(self.num - 1) # take position of tile from list
if self.square == 4:
self.turtle.penup()
self.turtle.speed('fastest')
self.turtle.goto(tile_pos[pos][0], tile_pos[pos][1])
# self.turtle.showturtle()
#return tile_pos
if self.square == 3:
mod_pos = tile_pos[:3]+tile_pos[4:7]+tile_pos[8:11]
self.turtle.penup()
self.turtle.speed('fastest')
self.turtle.goto(mod_pos[pos][0], mod_pos[pos][1])
#return mod_pos
if self.square == 2:
mod_pos = tile_pos[:2] + tile_pos[4:6]
self.turtle.penup()
self.turtle.speed('fastest')
self.turtle.goto(mod_pos[pos][0], mod_pos[pos][1])
#return mod_pos
self.turtle.showturtle()
class Buttons:
"""Places buttons for user options"""
def __init__(self, image):
self.turtle = turtle.Turtle(visible=False)
self.image = image
self.turtle.image = image
screen.addshape(image)
self.turtle.shape(image)
self.turtle.speed('fastest')
def position(self):
"""Retrieves button's position"""
return self.turtle.position()
def xcoord(self):
"""Retrieves button's x coordinate"""
return self.turtle.xcor()
def ycoord(self):
"""Retrieves button's y coordinate"""
return self.turtle.ycor()
def seen(self):
"""Shows button"""
return self.turtle.showturtle()
def place(self, x, y):
"""Places button at specific location"""
self.turtle.penup()
self.turtle.goto(x, y)
self.seen()
def button_click(self, on_clicks):
"""Checks if button is clicked on"""
return self.turtle.onclick(on_clicks)