1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Copyright (C) 2007 by ULPM: Alexandre Yukio Harano
# Fábio Cassarotti Parronchi Navarro
# Gabriel Geraldo França Marcondes
# Luiz Carlos Irber Júnior
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import sys
import os
import gtk
import pygame
from command import Play, Help, Quit
from gamemenu import GameMenu
import levels as Levels
from gettext import gettext as _
from objects import Esteira, check_collision
class Game():
def __init__(self, play_sounds=True, running_sugar=True):
self.play_sounds = play_sounds
self.running_sugar = running_sugar
# controle do jogo
self.fps = 30
self.running = True
self.playing = False
self.clock = None
self.level = 0
self.levels = []
self.selected_element = None
self._showed_help = False
self.count = 0
# elementos do jogo
self.screen = None
self.menu = None
self.congrats = None
self.congrats_snd = None
def load_all(self):
pygame.init()
self.screen = pygame.display.get_surface()
if self.screen is None:
self.screen = pygame.display.set_mode((1200, 900))
pygame.display.flip()
pygame.display.set_caption(_('Gambiarra'))
self.clock = pygame.time.Clock()
self.levels = Levels.init_levels()
self.menu = GameMenu()
self.congrats = pygame.image.load(os.path.join("data","images",
"fim_fase.png"))
self._fuente = pygame.font.Font(None, 80)
msg = self._fuente.render(_('Congratulations'), 1, (0, 0, 0))
self.congrats.blit(msg, (80, 200))
if self.play_sounds:
pygame.mixer.init()
snd_file = os.path.join("data", "snd", "Congrats.wav")
self.congrats_snd = pygame.mixer.Sound(snd_file)
def run(self):
self.load_all()
self.main_loop()
def event_handler(self):
#GTK events
while gtk.events_pending():
gtk.main_iteration()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
self.mouse_event( pygame.mouse.get_pos() )
elif (event.type == pygame.QUIT) and not self.running_sugar:
self.running = False
def update_screen(self, fps):
#update dos elementos da tela
if self.playing:
# executa a simulacao
objs = check_collision(self.levels[self.level].simulator.objects,
self.levels[self.level].simulator.static_objs)
self.levels[self.level].simulator.objects = objs
for obj in self.levels[self.level].simulator.objects:
obj.update()
else:
if self.selected_element:
if self.selected_element.editable:
self.selected_element.rect.center = pygame.mouse.get_pos()
def mouse_event(self, mouse_pos):
if not self.selected_element:
mouse_move = (0, 0)
mouse_move = pygame.mouse.get_rel()
for element in self.levels[self.level].simulator.objects:
if element.rect.collidepoint(mouse_pos):
self.selected_element = element
break
if not self.selected_element:
for element in self.levels[self.level].simulator.static_objs:
if element.rect.collidepoint(mouse_pos):
self.selected_element = element
if isinstance(element, Esteira) and element.editable:
self.count += 1
if self.count == 1:
element.sentido = -element.sentido
self.count = 0
break
if not self.selected_element: #se nao encontrou no for anterior
for element in self.levels[self.level].objbar.objects:
if element.rect.collidepoint(mouse_pos):
element.remove(self.levels[self.level].objbar.objects)
self.levels[self.level].simulator.add(element)
self.selected_element = element
break
if not self.selected_element: #se nao encontrou no for anterior
for element in self.levels[self.level].cmdbar.commands:
if element.rect.collidepoint(mouse_pos):
if isinstance(element, Play):
element.image = pygame.transform.flip(element.image,
True, False)
self.playing = not self.playing
if not self.playing:
objs = self.levels[self.level].simulator.objects
for obj in objs:
obj.speed = [0, 0]
obj.rect.topleft = obj.initial_pos
elif isinstance(element, Help):
self.levels[self.level].show_help(self.screen)
elif isinstance(element, Quit):
sys.exit()
break
else:
if self.selected_element.editable and not self.playing:
mouse_move = pygame.mouse.get_rel()
if mouse_move != (0, 0):
self.count -= 1
self.selected_element.rect.center = pygame.mouse.get_pos()
new_pos = self.selected_element.rect.topleft
self.selected_element.initial_pos = new_pos
self.selected_element = None
def show_congratulations(self):
if self.play_sounds:
pygame.mixer.stop()
self.congrats_snd.play()
self.screen.blit(self.congrats, (600 - self.congrats.get_width()/2,
450 - self.congrats.get_height()/2) )
pygame.display.flip()
while True:
#GTK events
while gtk.events_pending():
gtk.main_iteration()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
return
def main_loop(self):
self.menu.run()
while self.running and self.level < len(self.levels):
while not self.levels[self.level].goal_reached() and self.running:
self.event_handler()
self.clock.tick(self.fps)
self.update_screen(self.fps)
self.levels[self.level].draw()
if not self._showed_help:
self.levels[self.level].show_help(self.screen)
self._showed_help = True
pygame.display.flip()
if self.running:
self.playing = False
self._showed_help = False
self.level += 1
self.show_congratulations()
if (len(self.levels) == self.level) :
self.running = False
if __name__ == "__main__":
g = Game(running_sugar=False)
g.run()
|