-
Notifications
You must be signed in to change notification settings - Fork 0
/
slide_puzzle.py
272 lines (228 loc) · 8.98 KB
/
slide_puzzle.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
__version__ = "0.3"
from random import choice
import pygame
class Game:
"""The game class."""
def __init__(self, width, height, game_rect=(0, 0, 328, 328)):
# Init the display
self.width, self.height = width, height
self.game_rect = game_rect
self.screen = pygame.display.set_mode((self.width, self.height))
pygame.display.set_caption('Pygame Slide Puzzle')
# Creates the level selector
self.level_selector = LevelSelector(on_select=self.start)
# Marks the game as running, but level was not selected yet
self.running = True
self.started = False
def start(self, image_path):
"""Starts the game, loads and shuffles the image."""
self.puzzle = make_puzzle(image_path, self.game_rect)
self.puzzle.shuffle(moves=150)
self.started = True
def update(self):
"""Processes input events and updates the game."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type != pygame.KEYDOWN:
continue
# Pass the control to the level selector if game is not started
if not self.started:
self.level_selector.update(event)
else:
self.puzzle.update(event)
def draw(self):
"""Draws either the level selector or the game puzzle."""
surface = self.screen
if not self.started:
self.level_selector.draw(surface)
else:
self.puzzle.draw(surface)
def game_loop(self):
"""Performs the game loop: process input, update screen etc."""
clock = pygame.time.Clock()
while self.running:
elapsed = clock.tick(30)
self.screen.fill((0, 0, 0))
self.update()
self.draw()
pygame.display.update()
pygame.quit()
class LevelSelector:
"""The level selector scene."""
IMAGES = [
"puzzles/deepelf.png",
"puzzles/demilich.png",
"puzzles/dragonwhelp.png",
"puzzles/fireelemental.png",
"puzzles/goblinrunts.png",
"puzzles/goblin.png",
]
def __init__(self, on_select):
self.on_select = on_select # Callback to start the game
self._level = 0
self._images = []
for image in self.IMAGES:
self._images.append(load_puzzle_image(image, image_size=(220, 220)))
self._select = pygame.image.load("select.png").convert_alpha()
def prev(self):
"""Slide to the previous image."""
if self._level > 0:
self._level -= 1
def next(self):
"""Slide to the next image."""
if self._level < len(self._images) - 1:
self._level += 1
def _current_puzzle(self):
"""Returns the image for the current level."""
return self._images[self._level]
def _prev_puzzle(self):
"""Returns the image for the previous level (if exists)."""
if self._level > 0:
return self._images[self._level - 1]
def _next_puzzle(self):
"""Returns the image for the next level (if exists)."""
if self._level < len(self._images) - 1:
return self._images[self._level + 1]
def update(self, event):
"""Processes user's input."""
if event.type != pygame.KEYDOWN:
return
elif event.key == pygame.K_LEFT:
self.prev()
elif event.key == pygame.K_RIGHT:
self.next()
elif event.key == pygame.K_RETURN:
self.on_select(self.IMAGES[self._level])
def draw(self, surface):
"""Draws current state."""
pos = ((-180, 90), (90, 104), (360, 90))
levels = (self._prev_puzzle(),
self._current_puzzle(),
self._next_puzzle())
# Draws the images at the predefined positions
for level, p in zip(levels, pos):
if level is not None:
surface.blit(level, p)
# Draws a white border around the current image and the `select puzzle` image
pygame.draw.rect(surface, (255, 255, 255), (80, 94, 240, 240), 3)
surface.blit(self._select, (127, 40))
class Puzzle:
"""The puzzle object."""
def __init__(self, x, y, image_pieces):
self.x, self.y, self.image_pieces = x, y, image_pieces
@property
def rect(self):
"""Returns a rect representing the board."""
rect = pygame.Rect(self.x, self.y, 0, 0)
for s in self.image_pieces:
rect.union_ip(s.rect)
return rect
def update(self, event):
"""Processes user's input."""
if event.type != pygame.KEYDOWN:
return
elif event.key == pygame.K_UP:
self.move('up')
elif event.key == pygame.K_RIGHT:
self.move('right')
elif event.key == pygame.K_DOWN:
self.move('down')
elif event.key == pygame.K_LEFT:
self.move('left')
def move(self, direction):
"""Move an image piece in the given direction. Possible directions
are 'up', 'right', 'down' or 'left'."""
board_rect = self.rect
x_spacing, y_spacing = (board_rect.width//4,
board_rect.height//4)
x, y = {
'up': (0, -y_spacing),
'right': (x_spacing, 0),
'down': (0, y_spacing),
'left': (-x_spacing, 0)
}[direction]
# Helper function to check if a rect is valid (inside the puzzle)
is_valid = board_rect.colliderect
# The current state of the puzzle
current_pos = set((s.x, s.y) for s in self.image_pieces)
# Searchs sequentially for the only peice that can be moved
# to the given direction
for piece in self.image_pieces:
new_x, new_y = piece.x + x, piece.y + y
# Checks if the position is empty and is valid (inside the puzzle).
if ((new_x, new_y) not in current_pos and is_valid(
new_x, new_y, piece.width, piece.height)):
# If everything is ok, then moves the image piece
piece.x, piece.y = new_x, new_y
def shuffle(self, moves=100):
"""Shuffles the board applying random moves."""
for _ in range(moves):
m = choice(('up', 'right', 'down', 'left'))
self.move(m)
# Makes sure the blank space is at the botton-right corner
self.move('left'), self.move('left'), self.move('left')
self.move('up'), self.move('up'), self.move('up')
def draw(self, surface):
"""Draw the image pieces on the surface."""
for subsurf in self.image_pieces:
surface.blit(subsurf.image, (subsurf.x, subsurf.y))
# Draws a white border around the image
brect = self.rect
inflated_brect = brect.inflate(int(brect.width * 0.05),
int(brect.height * 0.05))
pygame.draw.rect(surface, (255, 255, 255), inflated_brect, 3)
class ImagePiece(pygame.sprite.Sprite):
"""A piece (subsuface) of the puzzle image."""
def __init__(self, x, y, subsurface):
super(ImagePiece, self).__init__()
# Saves the initial position of this piece. Used later
# to check if the puzzle is solved or not.
self.start_pos = (x, y)
self.x, self.y = x, y
self.image = subsurface
@property
def width(self):
return self.image.get_width()
@property
def height(self):
return self.image.get_height()
@property
def rect(self):
return pygame.Rect(
self.x,
self.y,
self.width,
self.height
)
def make_puzzle(image_path, board_rect):
"""Creates the game puzzle"""
x, y, width, height = board_rect
puzzle_image = load_puzzle_image(image_path,
image_size=(width, height))
image_pieces = list(make_subsurfaces(puzzle_image,
offset=(x, y)))
# Create the puzzle, leaving out the last piece of the image.
return Puzzle(x, y, image_pieces[:-1])
def load_puzzle_image(path, image_size):
"""Loads the puzzle image. The image is scaled to the `image_size` param."""
surface = pygame.image.load(path).convert_alpha()
return pygame.transform.scale(surface, image_size)
def make_subsurfaces(surface, offset=(0, 0)):
"""Cuts the image in small pieces of the same size."""
width, height = surface.get_size()
assert width % 4 + height % 4 == 0, (
"image's dimention is not div by 4: {}".format(surface.get_size()))
offx, offy = offset
for y in range(0, height, height//4):
for x in range(0, width, width//4):
subsurface = surface.subsurface(x, y, width//4, height//4)
yield ImagePiece(offx + x, offy + y, subsurface)
if __name__ == '__main__':
game = Game(
width=400,
height=428,
game_rect=(36, 50, 328, 328)
)
game.game_loop()