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,
24 from sys
import exit
, argv
25 from random
import seed
, randint
29 x
= randint(self
.start
, self
.end
)
30 y
= randint(self
.start
, self
.end
)
33 def __init__(self
, start
, end
):
39 def move_block(self
, pos
):
40 if self
.click_from
== None:
43 self
.__swap
_blocks
(self
.click_from
, pos
)
49 for i
in range(self
.__ROWS
):
50 for j
in range(self
.__COLS
):
51 if self
.block_m
[i
][j
] != self
.orig_order
[idx
]:
58 ev
= pygame
.event
.wait()
59 if ev
.type == pygame
.QUIT
:
61 if ev
.type == pygame
.MOUSEBUTTONDOWN
:
62 pos
= pygame
.mouse
.get_pos()
63 return self
.__mouse
_to
_block
_pos
(pos
)
65 def display_image(self
):
66 rect
= self
.image
.get_rect()
67 self
.screen
.blit(self
.image
, rect
)
71 block_m
= self
.block_m
72 for i
in range(self
.__ROWS
):
73 for j
in range(self
.__COLS
):
74 x
, y
= self
.random
.rand()
75 self
.__swap
_blocks
((i
, j
), (x
, y
))
77 def display_screen(self
):
79 s
.fill(self
.__BG
_COLOR
)
80 for i
in range(self
.__ROWS
):
81 for j
in range(self
.__COLS
):
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_reset(self
):
92 self
.click_from
= None
94 def __mouse_to_block_pos(self
, mouse_pos
):
96 x
= self
.__BLOCK
_WIDTH
97 y
= self
.__BLOCK
_HEIGHT
98 for i
in range(self
.__ROWS
):
99 for j
in range(self
.__COLS
):
100 if mx
< x
and my
< y
:
102 x
+= self
.__BLOCK
_WIDTH
103 x
= self
.__BLOCK
_WIDTH
104 y
+= self
.__BLOCK
_HEIGHT
106 def __swap_blocks(self
, pos1
, pos2
):
109 block_m
= self
.block_m
110 tmp
= block_m
[x1
][y1
]
111 block_m
[x1
][y1
] = block_m
[x2
][y2
]
112 block_m
[x2
][y2
] = tmp
114 def __constants_setup(self
):
117 self
.__SCREEN
_SIZE
= (x
, y
)
118 self
.__ROWS
= self
.__COLS
= 3
119 self
.__BLOCK
_WIDTH
= x
/ self
.__ROWS
120 self
.__BLOCK
_HEIGHT
= y
/ self
.__COLS
121 self
.__PIECE
_NR
= self
.__ROWS
* self
.__COLS
122 self
.__BG
_COLOR
= 255, 255, 255 # white
124 def __image_setup(self
, imgfile
):
125 image
= pygame
.image
.load(imgfile
)
129 width
= self
.__BLOCK
_WIDTH
130 height
= self
.__BLOCK
_HEIGHT
131 border
= (width
* 2) + (step
* 2)
137 for i
in range(self
.__ROWS
):
140 for j
in range(self
.__COLS
):
141 b
= image
.subsurface(x
, y
, width
,height
)
144 r
= pygame
.Rect(x
, y
, width
, height
)
152 self
.block_m
= block_m
153 self
.orig_order
= orig_order
155 def __init__(self
, imagefile
):
158 self
.__constants
_setup
()
159 self
.random
= PuzzleRand(0, self
.__COLS
- 1)
160 self
.__image
_setup
(imagefile
)
161 self
.screen
= pygame
.display
.set_mode(self
.__SCREEN
_SIZE
)
168 puzzle
= Puzzle(file)
170 # Display the image as is
171 puzzle
.title(msg
= 'clique para inciar')
172 puzzle
.display_image()
177 puzzle
.display_screen()
182 ret
= puzzle
.wait_user()
183 if ret
== pygame
.QUIT
:
186 puzzle
.move_block(ret
)
187 if puzzle
.finished():
190 puzzle
.title(msg
= 'parabens! Voce ganhou!')
191 puzzle
.display_image()
195 if __name__
== '__main__':