3 # A simple Puzzle game written with Pygame
5 # Copyright (C) 2008 Luiz Fernando N. Capitulino <lcapitulino@gmail.com>
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
28 x
= random
.randint(self
.start
, self
.end
)
29 y
= random
.randint(self
.start
, self
.end
)
32 def __init__(self
, start
, end
):
38 def move_block(self
, pos
):
39 if self
.__click
_is
_valid
(pos
):
40 self
.__swap
_blocks
(pos
, self
.hidden
)
46 for i
in range(self
.ROWS
):
47 for j
in range(self
.COLS
):
48 if self
.block_m
[i
][j
] != self
.orig_order
[idx
]:
55 ev
= pygame
.event
.wait()
56 if ev
.type == pygame
.QUIT
:
58 if ev
.type == pygame
.MOUSEBUTTONDOWN
:
59 pos
= pygame
.mouse
.get_pos()
60 return self
.__mouse
_to
_block
_pos
(pos
)
62 def display_image(self
):
63 rect
= self
.image
.get_rect()
64 self
.screen
.blit(self
.image
, rect
)
68 block_m
= self
.block_m
69 for i
in range(self
.ROWS
):
70 for j
in range(self
.COLS
):
71 x
, y
= self
.random
.rand()
72 self
.__swap
_blocks
((i
, j
), (x
, y
))
73 self
.hidden
= self
.random
.rand()
75 def display_screen(self
):
78 for i
in range(self
.ROWS
):
79 for j
in range(self
.COLS
):
80 if (i
, j
) == self
.hidden
:
82 s
.blit(self
.block_m
[i
][j
], self
.rect_m
[i
][j
])
85 def title(self
, msg
= False):
86 title
= 'Quebra Cabeca'
89 pygame
.display
.set_caption(title
)
91 def __click_is_valid(self
, pos
):
94 if x
- 1 >= 0 and (x
- 1, y
) == self
.hidden
:
96 elif x
+ 1 < self
.ROWS
and (x
+ 1, y
) == self
.hidden
:
98 elif y
- 1 >= 0 and (x
, y
- 1) == self
.hidden
:
100 elif y
+ 1 < self
.COLS
and (x
, y
+ 1) == self
.hidden
:
104 def __mouse_to_block_pos(self
, mouse_pos
):
107 y
= self
.BLOCK_HEIGHT
108 for i
in range(self
.ROWS
):
109 for j
in range(self
.COLS
):
110 if mx
< x
and my
< y
:
112 x
+= self
.BLOCK_WIDTH
114 y
+= self
.BLOCK_HEIGHT
116 def __swap_blocks(self
, pos1
, pos2
):
119 block_m
= self
.block_m
120 tmp
= block_m
[x1
][y1
]
121 block_m
[x1
][y1
] = block_m
[x2
][y2
]
122 block_m
[x2
][y2
] = tmp
124 def __constants_setup(self
):
125 self
.BLOCK_WIDTH
= 200
126 self
.BLOCK_HEIGHT
= 160
127 self
.ROWS
= self
.COLS
= 3
128 self
.PIECE_NR
= self
.ROWS
* self
.COLS
129 self
.SCREEN_SIZE
= 600, 480
130 self
.BG_COLOR
= 255, 255, 255 # white
132 def __image_setup(self
, imgfile
):
133 image
= pygame
.image
.load(imgfile
)
137 width
= self
.BLOCK_WIDTH
138 height
= self
.BLOCK_HEIGHT
139 border
= (width
* 2) + (step
* 2)
145 for i
in range(self
.ROWS
):
148 for j
in range(self
.COLS
):
149 b
= image
.subsurface(x
, y
, width
,height
)
152 r
= pygame
.Rect(x
, y
, width
, height
)
160 self
.block_m
= block_m
161 self
.orig_order
= orig_order
163 def __init__(self
, imagefile
):
165 self
.__constants
_setup
()
166 self
.random
= PuzzleRand(0, self
.COLS
- 1)
167 self
.__image
_setup
(imagefile
)
168 self
.screen
= pygame
.display
.set_mode(self
.SCREEN_SIZE
)
171 puzzle
= Puzzle('default.jpg')
173 # Display the image as is
174 puzzle
.title(msg
= 'clique para inciar')
175 puzzle
.display_image()
180 puzzle
.display_screen()
185 pos
= puzzle
.wait_user()
186 puzzle
.move_block(pos
)
187 if puzzle
.finished():
190 puzzle
.title(msg
= 'parabens! Voce ganhou!')
191 puzzle
.display_image()
194 if __name__
== '__main__':