Skip to content

Commit 339fd90

Browse files
committed
Add Tiles puzzle game
1 parent f5310d8 commit 339fd90

File tree

7 files changed

+306
-0
lines changed

7 files changed

+306
-0
lines changed

Tiles Puzzle/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
**Minesweeper**
2+
3+
**GOAL**
4+
5+
The goal of the Tile puzzle game is to reorder the pieces in the correct order by moving tiles into the blank space one by one
6+
7+
**DESCRIPTION**
8+
9+
The player is first presented with a grid containing 8 numbered tiles and a blank space
10+
To play the game, the player has to click on the shuffle button in the grid that will shuffle and start the puzzle timer
11+
12+
The player can swap the position of the blank tile with any adjacent numbered tile by a single mouse click on the desired numbered tile
13+
14+
To complete the puzzle the player needs to put every tile in the correct position in the grid
15+
16+
The high score stores the least time that the player has taken the puzzle
17+
18+
19+
**LIBRARIES NEEDED**
20+
21+
1) Pygame
22+
2) Random
23+
24+
**DEMONSTRATION**
25+
26+
![image](https://user-images.githubusercontent.com/95132851/206364984-20c8a513-ee95-43cb-b66e-de7af2d71529.png)
27+
28+
**YOUR NAME**
29+
30+
Kshitij Maurya
414 Bytes
Binary file not shown.
3.32 KB
Binary file not shown.

Tiles Puzzle/high_score.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
99999

Tiles Puzzle/main.py

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
import pygame
2+
import random
3+
import time
4+
from sprite import *
5+
from settings import *
6+
7+
8+
class Game:
9+
def __init__(self):
10+
pygame.init()
11+
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
12+
pygame.display.set_caption(title)
13+
self.clock = pygame.time.Clock()
14+
self.shuffle_time = 0
15+
self.start_shuffle = False
16+
self.previous_choice = ""
17+
self.start_game = False
18+
self.start_timer = False
19+
self.elapsed_time = 0
20+
self.high_score = float(self.get_high_scores()[0])
21+
22+
def get_high_scores(self):
23+
with open("high_score.txt", "r") as file:
24+
scores = file.read().splitlines()
25+
return scores
26+
27+
def save_score(self):
28+
with open("high_score.txt", "w") as file:
29+
file.write(str("%.3f\n" % self.high_score))
30+
31+
def create_game(self):
32+
grid = [[x + y * GAME_SIZE for x in range(1, GAME_SIZE + 1)] for y in range(GAME_SIZE)]
33+
grid[-1][-1] = 0
34+
return grid
35+
36+
def shuffle(self):
37+
possible_moves = []
38+
for row, tiles in enumerate(self.tiles):
39+
for col, tile in enumerate(tiles):
40+
if tile.text == "empty":
41+
if tile.right():
42+
possible_moves.append("right")
43+
if tile.left():
44+
possible_moves.append("left")
45+
if tile.up():
46+
possible_moves.append("up")
47+
if tile.down():
48+
possible_moves.append("down")
49+
break
50+
if len(possible_moves) > 0:
51+
break
52+
53+
if self.previous_choice == "right":
54+
possible_moves.remove("left") if "left" in possible_moves else possible_moves
55+
elif self.previous_choice == "left":
56+
possible_moves.remove("right") if "right" in possible_moves else possible_moves
57+
elif self.previous_choice == "up":
58+
possible_moves.remove("down") if "down" in possible_moves else possible_moves
59+
elif self.previous_choice == "down":
60+
possible_moves.remove("up") if "up" in possible_moves else possible_moves
61+
62+
choice = random.choice(possible_moves)
63+
self.previous_choice = choice
64+
if choice == "right":
65+
self.tiles_grid[row][col], self.tiles_grid[row][col + 1] = self.tiles_grid[row][col + 1], \
66+
self.tiles_grid[row][col]
67+
elif choice == "left":
68+
self.tiles_grid[row][col], self.tiles_grid[row][col - 1] = self.tiles_grid[row][col - 1], \
69+
self.tiles_grid[row][col]
70+
elif choice == "up":
71+
self.tiles_grid[row][col], self.tiles_grid[row - 1][col] = self.tiles_grid[row - 1][col], \
72+
self.tiles_grid[row][col]
73+
elif choice == "down":
74+
self.tiles_grid[row][col], self.tiles_grid[row + 1][col] = self.tiles_grid[row + 1][col], \
75+
self.tiles_grid[row][col]
76+
77+
def draw_tiles(self):
78+
self.tiles = []
79+
for row, x in enumerate(self.tiles_grid):
80+
self.tiles.append([])
81+
for col, tile in enumerate(x):
82+
if tile != 0:
83+
self.tiles[row].append(Tile(self, col, row, str(tile)))
84+
else:
85+
self.tiles[row].append(Tile(self, col, row, "empty"))
86+
87+
def new(self):
88+
self.all_sprites = pygame.sprite.Group()
89+
self.tiles_grid = self.create_game()
90+
self.tiles_grid_completed = self.create_game()
91+
self.elapsed_time = 0
92+
self.start_timer = False
93+
self.start_game = False
94+
self.buttons_list = []
95+
self.buttons_list.append(Button(500, 100, 200, 50, "Shuffle", WHITE, BLACK))
96+
self.buttons_list.append(Button(500, 170, 200, 50, "Reset", WHITE, BLACK))
97+
self.draw_tiles()
98+
99+
def run(self):
100+
self.playing = True
101+
while self.playing:
102+
self.clock.tick(FPS)
103+
self.events()
104+
self.update()
105+
self.draw()
106+
107+
def update(self):
108+
if self.start_game:
109+
if self.tiles_grid == self.tiles_grid_completed:
110+
self.start_game = False
111+
if self.high_score > 0:
112+
self.high_score = self.elapsed_time if self.elapsed_time < self.high_score else self.high_score
113+
else:
114+
self.high_score = self.elapsed_time
115+
self.save_score()
116+
117+
if self.start_timer:
118+
self.timer = time.time()
119+
self.start_timer = False
120+
self.elapsed_time = time.time() - self.timer
121+
122+
if self.start_shuffle:
123+
self.shuffle()
124+
self.draw_tiles()
125+
self.shuffle_time += 1
126+
if self.shuffle_time > 120:
127+
self.start_shuffle = False
128+
self.start_game = True
129+
self.start_timer = True
130+
131+
self.all_sprites.update()
132+
133+
def draw_grid(self):
134+
for row in range(-1, GAME_SIZE * TILESIZE, TILESIZE):
135+
pygame.draw.line(self.screen, LIGHTGREY, (row, 0), (row, GAME_SIZE * TILESIZE))
136+
for col in range(-1, GAME_SIZE * TILESIZE, TILESIZE):
137+
pygame.draw.line(self.screen, LIGHTGREY, (0, col), (GAME_SIZE * TILESIZE, col))
138+
139+
def draw(self):
140+
self.screen.fill(BGCOLOUR)
141+
self.all_sprites.draw(self.screen)
142+
self.draw_grid()
143+
for button in self.buttons_list:
144+
button.draw(self.screen)
145+
UIElement(550, 35, "%.3f" % self.elapsed_time).draw(self.screen)
146+
UIElement(430, 300, "High Score - %.3f" % (self.high_score if self.high_score > 0 else 0)).draw(self.screen)
147+
pygame.display.flip()
148+
149+
def events(self):
150+
for event in pygame.event.get():
151+
if event.type == pygame.QUIT:
152+
pygame.quit()
153+
quit(0)
154+
155+
if event.type == pygame.MOUSEBUTTONDOWN:
156+
mouse_x, mouse_y = pygame.mouse.get_pos()
157+
for row, tiles in enumerate(self.tiles):
158+
for col, tile in enumerate(tiles):
159+
if tile.click(mouse_x, mouse_y):
160+
if tile.right() and self.tiles_grid[row][col + 1] == 0:
161+
self.tiles_grid[row][col], self.tiles_grid[row][col + 1] = self.tiles_grid[row][col + 1], self.tiles_grid[row][col]
162+
163+
if tile.left() and self.tiles_grid[row][col - 1] == 0:
164+
self.tiles_grid[row][col], self.tiles_grid[row][col - 1] = self.tiles_grid[row][col - 1], self.tiles_grid[row][col]
165+
166+
if tile.up() and self.tiles_grid[row - 1][col] == 0:
167+
self.tiles_grid[row][col], self.tiles_grid[row - 1][col] = self.tiles_grid[row - 1][col], self.tiles_grid[row][col]
168+
169+
if tile.down() and self.tiles_grid[row + 1][col] == 0:
170+
self.tiles_grid[row][col], self.tiles_grid[row + 1][col] = self.tiles_grid[row + 1][col], self.tiles_grid[row][col]
171+
172+
self.draw_tiles()
173+
174+
for button in self.buttons_list:
175+
if button.click(mouse_x, mouse_y):
176+
if button.text == "Shuffle":
177+
self.shuffle_time = 0
178+
self.start_shuffle = True
179+
if button.text == "Reset":
180+
self.new()
181+
182+
183+
game = Game()
184+
while True:
185+
game.new()
186+
game.run()

Tiles Puzzle/settings.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# COLORS (r, g, b)
2+
WHITE = (255, 255, 255)
3+
BLACK = (0, 0, 0)
4+
DARKGREY = (40, 40, 40)
5+
LIGHTGREY = (100, 100, 100)
6+
BGCOLOUR = DARKGREY
7+
8+
# game settings
9+
WIDTH = 800
10+
HEIGHT = 641
11+
FPS = 60
12+
title = "Sliding Puzzle Game"
13+
TILESIZE = 128
14+
GAME_SIZE = 3

Tiles Puzzle/sprite.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import pygame
2+
from settings import *
3+
4+
pygame.font.init()
5+
6+
7+
class Tile(pygame.sprite.Sprite):
8+
def __init__(self, game, x, y, text):
9+
self.groups = game.all_sprites
10+
pygame.sprite.Sprite.__init__(self, self.groups)
11+
self.game = game
12+
self.image = pygame.Surface((TILESIZE, TILESIZE))
13+
self.x, self.y = x, y
14+
self.text = text
15+
self.rect = self.image.get_rect()
16+
if self.text != "empty":
17+
self.font = pygame.font.SysFont("Consolas", 50)
18+
font_surface = self.font.render(self.text, True, BLACK)
19+
self.image.fill(WHITE)
20+
self.font_size = self.font.size(self.text)
21+
draw_x = (TILESIZE / 2) - self.font_size[0] / 2
22+
draw_y = (TILESIZE / 2) - self.font_size[1] / 2
23+
self.image.blit(font_surface, (draw_x, draw_y))
24+
else:
25+
self.image.fill(BGCOLOUR)
26+
27+
def update(self):
28+
self.rect.x = self.x * TILESIZE
29+
self.rect.y = self.y * TILESIZE
30+
31+
def click(self, mouse_x, mouse_y):
32+
return self.rect.left <= mouse_x <= self.rect.right and self.rect.top <= mouse_y <= self.rect.bottom
33+
34+
def right(self):
35+
return self.rect.x + TILESIZE < GAME_SIZE * TILESIZE
36+
37+
def left(self):
38+
return self.rect.x - TILESIZE >= 0
39+
40+
def up(self):
41+
return self.rect.y - TILESIZE >= 0
42+
43+
def down(self):
44+
return self.rect.y + TILESIZE < GAME_SIZE * TILESIZE
45+
46+
47+
class UIElement:
48+
def __init__(self, x, y, text):
49+
self.x, self.y = x, y
50+
self.text = text
51+
52+
def draw(self, screen):
53+
font = pygame.font.SysFont("Consolas", 30)
54+
text = font.render(self.text, True, WHITE)
55+
screen.blit(text, (self.x, self.y))
56+
57+
58+
class Button:
59+
def __init__(self, x, y, width, height, text, colour, text_colour):
60+
self.colour, self.text_colour = colour, text_colour
61+
self.width, self.height = width, height
62+
self.x, self.y = x, y
63+
self.text = text
64+
65+
def draw(self, screen):
66+
pygame.draw.rect(screen, self.colour, (self.x, self.y, self.width, self.height))
67+
font = pygame.font.SysFont("Consolas", 30)
68+
text = font.render(self.text, True, self.text_colour)
69+
self.font_size = font.size(self.text)
70+
draw_x = self.x + (self.width / 2) - self.font_size[0] / 2
71+
draw_y = self.y + (self.height / 2) - self.font_size[1] / 2
72+
screen.blit(text, (draw_x, draw_y))
73+
74+
def click(self, mouse_x, mouse_y):
75+
return self.x <= mouse_x <= self.x + self.width and self.y <= mouse_y <= self.y + self.height

0 commit comments

Comments
 (0)