-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
188 lines (157 loc) · 7.33 KB
/
main.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
import pygame
import random
import time
from sprite import *
from settings import *
class Game(object):
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption(title)
self.clock = pygame.time.Clock()
self.shuffle_time = 0
self.start_shuffle = False
self.previous_choice = ""
self.start_game = False
self.start_timer = 0
self.elapsed_time = 0
self.high_score = float(self.get_high_score()[0])
def get_high_score(self):
with open("high_score.txt","r") as file:
scores = file.read().splitlines()
return scores
def save_score(self):
with open("high_score.txt","w") as file:
file.write(str("%.3f\n" % self.high_score))
def create_game(self):
grid = []
grid = [[x + y * GAME_SIZE for x in range(1,GAME_SIZE + 1)] for y in range(GAME_SIZE)]
grid[-1][-1] = 0
return grid
def shuffle(self):
possible_moves = []
for row ,tiles in enumerate(self.tiles):
for col ,tile in enumerate(tiles):
if tile.text == "empty":
if tile.right():
possible_moves.append("right")
if tile.left():
possible_moves.append("left")
if tile.up():
possible_moves.append("up")
if tile.down():
possible_moves.append("down")
break
if len(possible_moves) > 0:
break
if self.previous_choice == "right":
possible_moves.remove("left") if "left" in possible_moves else possible_moves
elif self.previous_choice == "left":
possible_moves.remove("right") if "right" in possible_moves else possible_moves
elif self.previous_choice == "up":
possible_moves.remove("down") if "down" in possible_moves else possible_moves
elif self.previous_choice == "down":
possible_moves.remove("up") if "up" in possible_moves else possible_moves
choice = random.choice(possible_moves)
self.previous_choice = choice
if choice == "right":
self.tiles_grid[row][col],self.tiles_grid[row][col + 1] = self.tiles_grid[row][col + 1],self.tiles_grid[row][col]
elif choice == "left":
self.tiles_grid[row][col],self.tiles_grid[row][col - 1] = self.tiles_grid[row][col - 1],self.tiles_grid[row][col]
elif choice == "up":
self.tiles_grid[row][col],self.tiles_grid[row - 1][col] = self.tiles_grid[row - 1][col],self.tiles_grid[row][col]
elif choice == "down":
self.tiles_grid[row][col],self.tiles_grid[row + 1][col] = self.tiles_grid[row + 1][col],self.tiles_grid[row][col]
def draw_tiles(self):
self.tiles = []
for row ,x in enumerate(self.tiles_grid):
self.tiles.append([])
for col,tile in enumerate(x):
if tile != 0:
self.tiles[row].append(Tile(self,col,row,str(tile)))
else:
self.tiles[row].append(Tile(self, col, row, "empty"))
def new(self):
self.all_sprites = pygame.sprite.Group()
self.tiles_grid = self.create_game()
self.tiles_grid_completed = self.create_game()
self.elapsed_time = 0
self.start_timer = False
self.start_game = False
self.button_list = []
self.button_list.append(Button(775, 100, 200, 50,"Shuffle",WHITE,BLACK))
self.button_list.append(Button(775, 170, 200, 50,"Reset",WHITE,BLACK))
self.draw_tiles()
def run(self):
self.playing = True
while self.playing:
self.clock.tick(FPS)
self.events()
self.update()
self.draw()
def update(self):
if self.start_game:
if self.tiles_grid == self.tiles_grid_completed:
self.start_game = False
if self.high_score > 0:
self.high_score = self.elapsed_time if self.elapsed_time < self.high_score else self.high_score
else:
self.high_score = self.elapsed_time
self.save_score()
if self.start_timer:
self.timer = time.time()
self.start_timer = False
self.elapsed_time = time.time() - self.timer
if self.start_shuffle:
self.shuffle()
self.draw_tiles()
self.shuffle_time += 1
if self.shuffle_time > 120:
self.start_shuffle = False
self.start_game = True
self.start_timer = True
self.all_sprites.update()
def draw_gird(self):
for row in range(-1,GAME_SIZE * TILESIZE,TILESIZE):
pygame.draw.line(self.screen, LIGHTGREY, (row,0),(row,GAME_SIZE * TILESIZE))
for col in range(-1,GAME_SIZE * TILESIZE,TILESIZE):
pygame.draw.line(self.screen,LIGHTGREY, (0,col),(GAME_SIZE * TILESIZE, col))
def draw(self):
self.screen.fill(BGCOLOUR)
self.all_sprites.draw(self.screen)
self.draw_gird()
for button in self.button_list:
button.draw(self.screen)
UIelement(825,35, "%.3f" % self.elapsed_time).draw(self.screen)
UIelement(710,380, "High Score - %.3f " % (self.high_score if self.high_score > 0 else 0)).draw(self.screen)
pygame.display.flip()
def events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit(0)
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_x ,mouse_y = pygame.mouse.get_pos()
for row ,tiles in enumerate(self.tiles):
for col ,tile in enumerate(tiles):
if tile.click(mouse_x,mouse_y):
if tile.right() and self.tiles_grid[row][col + 1] == 0:
self.tiles_grid[row][col],self.tiles_grid[row][col + 1] = self.tiles_grid[row][col + 1],self.tiles_grid[row][col]
if tile.left() and self.tiles_grid[row][col - 1] == 0:
self.tiles_grid[row][col],self.tiles_grid[row][col - 1] = self.tiles_grid[row][col - 1],self.tiles_grid[row][col]
if tile.up() and self.tiles_grid[row -1][col] == 0:
self.tiles_grid[row][col],self.tiles_grid[row - 1][col] = self.tiles_grid[row - 1][col],self.tiles_grid[row][col]
if tile.down() and self.tiles_grid[row + 1][col] == 0:
self.tiles_grid[row][col],self.tiles_grid[row + 1][col] = self.tiles_grid[row + 1][col],self.tiles_grid[row][col]
self.draw_tiles()
for button in self.button_list:
if button.click(mouse_x,mouse_y):
if button.text == "Shuffle":
self.shuffle_time = 0
self.start_shuffle = True
if button.text == "Reset":
self.new()
game = Game()
while True:
game.new()
game.run()