Kill mega-sena.sh
[lcapit-junk-code.git] / games / puzzle / quebra-cabeca
blob04b3b6a4b34894c1fb86f89f9695443a5e83a5e2
1 #!/usr/bin/python
3 # A simple Puzzle game written with Pygame
5 # Copyright (C) 2008 Luiz Fernando N. Capitulino <lcapitulino@gmail.com>
6 #
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,
20 # MA 02110-1301, USA.
22 import pygame
23 from time import time
24 from sys import exit, argv
25 from random import seed, randint
27 class PuzzleRand:
28 def rand(self):
29 x = randint(self.start, self.end)
30 y = randint(self.start, self.end)
31 return (x, y)
33 def __init__(self, start, end):
34 self.start = start
35 self.end = end
36 seed(time())
38 class Puzzle:
39 def move_block(self, pos):
40 if self.click_from == None:
41 self.click_from = pos
42 return
43 self.__swap_blocks(self.click_from, pos)
44 self.display_screen()
45 self.__click_reset()
47 def finished(self):
48 idx = 0
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]:
52 return False
53 idx += 1
54 return True
56 def wait_user(self):
57 while True:
58 ev = pygame.event.wait()
59 if ev.type == pygame.QUIT:
60 return 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)
68 pygame.display.flip()
70 def mix_up(self):
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):
78 s = self.screen
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])
83 pygame.display.flip()
85 def title(self, msg = False):
86 title = 'Quebra Cabeca'
87 if msg:
88 title += ': ' + msg
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):
95 mx, my = 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:
101 return (i, j)
102 x += self.__BLOCK_WIDTH
103 x = self.__BLOCK_WIDTH
104 y += self.__BLOCK_HEIGHT
106 def __swap_blocks(self, pos1, pos2):
107 x1, y1 = pos1
108 x2, y2 = 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):
115 x = 680
116 y = 400
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)
127 step = 1
128 x = y = 0
129 width = self.__BLOCK_WIDTH
130 height = self.__BLOCK_HEIGHT
131 border = (width * 2) + (step * 2)
133 block_m = []
134 rect_m = []
135 orig_order = []
137 for i in range(self.__ROWS):
138 block_m.append([])
139 rect_m.append([])
140 for j in range(self.__COLS):
141 b = image.subsurface(x, y, width,height)
142 block_m[i].append(b)
143 orig_order.append(b)
144 r = pygame.Rect(x, y, width, height)
145 rect_m[i].append(r)
146 x += (width + step)
147 x = 0
148 y += (height + step)
150 self.image = image
151 self.rect_m = rect_m
152 self.block_m = block_m
153 self.orig_order = orig_order
155 def __init__(self, imagefile):
156 pygame.init()
157 self.__click_reset()
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)
163 def main():
164 file = 'default.jpg'
165 if len(argv) == 2:
166 file = argv[1]
168 puzzle = Puzzle(file)
170 # Display the image as is
171 puzzle.title(msg = 'clique para inciar')
172 puzzle.display_image()
173 puzzle.wait_user()
175 # Mix it up!
176 puzzle.mix_up()
177 puzzle.display_screen()
178 puzzle.title()
180 # Main loop:
181 while True:
182 ret = puzzle.wait_user()
183 if ret == pygame.QUIT:
184 pygame.quit()
185 exit(0)
186 puzzle.move_block(ret)
187 if puzzle.finished():
188 break
190 puzzle.title(msg = 'parabens! Voce ganhou!')
191 puzzle.display_image()
192 puzzle.wait_user()
193 pygame.quit()
195 if __name__ == '__main__':
196 main()