Kill mega-sena.sh
[lcapit-junk-code.git] / games / puzzle / quebra-cabeca2
blobd8cf00f90b19785c310438e3246437f80200e1e4
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 import random
24 from sys import exit
26 class PuzzleRand:
27 def rand(self):
28 x = random.randint(self.start, self.end)
29 y = random.randint(self.start, self.end)
30 return (x, y)
32 def __init__(self, start, end):
33 self.start = start
34 self.end = end
35 random.seed(1)
37 class Puzzle:
38 def move_block(self, pos):
39 if self.__click_is_valid(pos):
40 self.__swap_blocks(pos, self.hidden)
41 self.hidden = pos
42 self.display_screen()
44 def finished(self):
45 idx = 0
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]:
49 return False
50 idx += 1
51 return True
53 def wait_user(self):
54 while True:
55 ev = pygame.event.wait()
56 if ev.type == pygame.QUIT:
57 exit(0)
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)
65 pygame.display.flip()
67 def mix_up(self):
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):
76 s = self.screen
77 s.fill(self.BG_COLOR)
78 for i in range(self.ROWS):
79 for j in range(self.COLS):
80 if (i, j) == self.hidden:
81 continue
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_is_valid(self, pos):
92 x, y = pos
94 if x - 1 >= 0 and (x - 1, y) == self.hidden:
95 return True
96 elif x + 1 < self.ROWS and (x + 1, y) == self.hidden:
97 return True
98 elif y - 1 >= 0 and (x, y - 1) == self.hidden:
99 return True
100 elif y + 1 < self.COLS and (x, y + 1) == self.hidden:
101 return True
102 return False
104 def __mouse_to_block_pos(self, mouse_pos):
105 mx, my = mouse_pos
106 x = self.BLOCK_WIDTH
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:
111 return (i, j)
112 x += self.BLOCK_WIDTH
113 x = self.BLOCK_WIDTH
114 y += self.BLOCK_HEIGHT
116 def __swap_blocks(self, pos1, pos2):
117 x1, y1 = pos1
118 x2, y2 = 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)
135 step = 1
136 x = y = 0
137 width = self.BLOCK_WIDTH
138 height = self.BLOCK_HEIGHT
139 border = (width * 2) + (step * 2)
141 block_m = []
142 rect_m = []
143 orig_order = []
145 for i in range(self.ROWS):
146 block_m.append([])
147 rect_m.append([])
148 for j in range(self.COLS):
149 b = image.subsurface(x, y, width,height)
150 block_m[i].append(b)
151 orig_order.append(b)
152 r = pygame.Rect(x, y, width, height)
153 rect_m[i].append(r)
154 x += (width + step)
155 x = 0
156 y += (height + step)
158 self.image = image
159 self.rect_m = rect_m
160 self.block_m = block_m
161 self.orig_order = orig_order
163 def __init__(self, imagefile):
164 pygame.init()
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)
170 def main():
171 puzzle = Puzzle('default.jpg')
173 # Display the image as is
174 puzzle.title(msg = 'clique para inciar')
175 puzzle.display_image()
176 puzzle.wait_user()
178 # Mix it up!
179 puzzle.mix_up()
180 puzzle.display_screen()
181 puzzle.title()
183 # Main loop:
184 while True:
185 pos = puzzle.wait_user()
186 puzzle.move_block(pos)
187 if puzzle.finished():
188 break
190 puzzle.title(msg = 'parabens! Voce ganhou!')
191 puzzle.display_image()
192 puzzle.wait_user()
194 if __name__ == '__main__':
195 main()