Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit29d93f3

Browse files
committed
add slize puzzle game tutorial
1 parenta30de41 commit29d93f3

File tree

17 files changed

+210
-0
lines changed

17 files changed

+210
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
287287
-[How to Build a Tic Tac Toe Game in Python](https://www.thepythoncode.com/article/make-a-tic-tac-toe-game-pygame-in-python). ([code](gui-programming/tictactoe-game))
288288
-[How to Make a Checkers Game with Pygame in Python](https://www.thepythoncode.com/article/make-a-checkers-game-with-pygame-in-python). ([code](gui-programming/checkers-game))
289289
-[How to Make a Snake Game in Python](https://www.thepythoncode.com/article/make-a-snake-game-with-pygame-in-python). ([code](gui-programming/snake-game))
290+
-[How to Create a Slide Puzzle Game in Python](https://www.thepythoncode.com/article/slide-puzzle-game-in-python). ([code](gui-programming/slide-puzzle))
290291

291292

292293
For any feedback, please consider pulling requests.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#[How to Create a Slide Puzzle Game in Python](https://www.thepythoncode.com/article/slide-puzzle-game-in-python)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
importpygame
2+
3+
classCell:
4+
def__init__(self,row,col,cell_size,c_id):
5+
self.row=row
6+
self.col=col
7+
self.cell_size=cell_size
8+
self.width=self.cell_size[0]
9+
self.height=self.cell_size[1]
10+
self.abs_x=row*self.width
11+
self.abs_y=col*self.height
12+
13+
self.c_id=c_id
14+
15+
self.rect=pygame.Rect(
16+
self.abs_x,
17+
self.abs_y,
18+
self.width,
19+
self.height
20+
)
21+
22+
self.occupying_piece=None
23+
24+
defdraw(self,display):
25+
pygame.draw.rect(display, (0,0,0),self.rect)
26+
ifself.occupying_piece!=Noneandself.occupying_piece.p_id!=8:
27+
centering_rect=self.occupying_piece.img.get_rect()
28+
centering_rect.center=self.rect.center
29+
display.blit(self.occupying_piece.img,centering_rect.topleft)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
importpygame
2+
importrandom
3+
4+
fromcellimportCell
5+
frompieceimportPiece
6+
7+
classFrame:
8+
def__init__(self,frame_size):
9+
self.grid_size=3
10+
self.cell_width=frame_size//self.grid_size
11+
self.cell_height=frame_size//self.grid_size
12+
self.cell_size= (self.cell_width,self.cell_height)
13+
14+
self.grid=self._generate_cell()
15+
self.pieces=self._generate_piece()
16+
17+
self._setup()
18+
19+
def_generate_cell(self):
20+
cells= []
21+
c_id=0
22+
forcolinrange(self.grid_size):
23+
forrowinrange(self.grid_size):
24+
cells.append(Cell(row,col,self.cell_size,c_id))
25+
c_id+=1
26+
returncells
27+
28+
def_generate_piece(self):
29+
puzzle_pieces= []
30+
p_id=0
31+
forcolinrange(self.grid_size):
32+
forrowinrange(self.grid_size):
33+
puzzle_pieces.append(Piece(self.cell_size,p_id))
34+
p_id+=1
35+
returnpuzzle_pieces
36+
37+
def_setup(self):
38+
forcellinself.grid:
39+
piece_choice=random.choice(self.pieces)
40+
cell.occupying_piece=piece_choice
41+
self.pieces.remove(piece_choice)
42+
43+
def_get_cell_from_id(self,given_id):
44+
forcellinself.grid:
45+
ifcell.c_id==given_id:
46+
returncell
47+
48+
def_is_move_valid(self,click):
49+
moves= {
50+
79:1,
51+
80:-1,
52+
81:3,
53+
82:-3
54+
}
55+
forcellinself.grid:
56+
move_id=cell.c_id+moves[click.scancode]
57+
ifmove_id>=0andmove_id<=8:
58+
new_cell=self._get_cell_from_id(move_id)
59+
ifnew_cell.occupying_piece.img==None:
60+
return (cell,new_cell)
61+
else:
62+
continue
63+
64+
defhandle_click(self,click):
65+
c=self._is_move_valid(click)
66+
try:
67+
# print(c[0].c_id, c[1].c_id)
68+
c[0].occupying_piece,c[1].occupying_piece=c[1].occupying_piece,c[0].occupying_piece
69+
except:
70+
returnFalse
71+
72+
defdraw(self,display):
73+
forcellinself.grid:
74+
cell.draw(display)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
importpygame
2+
3+
pygame.font.init()
4+
5+
classGame:
6+
def__init__(self):
7+
self.font=pygame.font.SysFont("Courier New",35)
8+
self.background_color= (255,174,66)
9+
self.message_color= (17,53,165)
10+
11+
defarrow_key_clicked(self,click):
12+
try:
13+
ifclick.key==pygame.K_LEFTorclick.key==pygame.K_RIGHTorclick.key==pygame.K_UPorclick.key==pygame.K_DOWN:
14+
return(True)
15+
except:
16+
return(False)
17+
18+
defis_game_over(self,frame):
19+
forcellinframe.grid:
20+
piece_id=cell.occupying_piece.p_id
21+
# print(piece_id, cell.c_id)
22+
ifcell.c_id==piece_id:
23+
is_arranged=True
24+
else:
25+
is_arranged=False
26+
break
27+
returnis_arranged
28+
29+
defmessage(self,screen):
30+
screen.fill(self.background_color, (5,460,440,35))
31+
instructions=self.font.render('You Win!!',True,self.message_color)
32+
screen.blit(instructions,(125,460))
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
importpygame
2+
3+
fromframeimportFrame
4+
fromgameimportGame
5+
6+
pygame.init()
7+
pygame.font.init()
8+
9+
classPuzzle:
10+
def__init__(self,screen):
11+
self.screen=screen
12+
self.running=True
13+
self.FPS=pygame.time.Clock()
14+
self.is_arranged=False
15+
self.font=pygame.font.SysFont("Courier New",33)
16+
self.background_color= (255,174,66)
17+
self.message_color= (17,53,165)
18+
19+
def_draw(self,frame):
20+
frame.draw(self.screen)
21+
pygame.display.update()
22+
23+
def_instruction(self):
24+
instructions=self.font.render('Use Arrow Keys to Move',True,self.message_color)
25+
screen.blit(instructions,(5,460))
26+
27+
defmain(self,frame_size):
28+
self.screen.fill("white")
29+
frame=Frame(frame_size)
30+
game=Game()
31+
self._instruction()
32+
whileself.running:
33+
34+
ifgame.is_game_over(frame):
35+
self.is_arranged=True
36+
game.message(self.screen)
37+
38+
foreventinpygame.event.get():
39+
ifevent.type==pygame.QUIT:
40+
self.running=False
41+
42+
ifevent.type==pygame.KEYDOWN:
43+
ifnotself.is_arranged:
44+
ifgame.arrow_key_clicked(event):
45+
frame.handle_click(event)
46+
47+
self._draw(frame)
48+
self.FPS.tick(30)
49+
50+
pygame.quit()
51+
52+
53+
if__name__=="__main__":
54+
window_size= (450,500)
55+
screen=pygame.display.set_mode(window_size)
56+
pygame.display.set_caption("Slide Puzzle")
57+
58+
game=Puzzle(screen)
59+
game.main(window_size[0])
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
importpygame
2+
3+
classPiece:
4+
def__init__(self,piece_size,p_id):
5+
self.piece_size=piece_size
6+
self.p_id=p_id
7+
8+
ifself.p_id!=8:
9+
img_path=f'puzz-pieces/{self.p_id}.jpg'
10+
self.img=pygame.image.load(img_path)
11+
self.img=pygame.transform.scale(self.img,self.piece_size)
12+
else:
13+
self.img=None
2.39 KB
Loading
4.39 KB
Loading
2.34 KB
Loading

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp