-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGameView.py
346 lines (282 loc) · 10.7 KB
/
GameView.py
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# GameView.py por:
# Flavio Danesse <[email protected]>
# Uruguay
# 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import os
import sys
import gobject
import gtk
import pango
installed_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(1, os.path.join(installed_dir, "Lib/"))
import sugargame2
import sugargame2.canvas
import spyral
import pygame
from ConfigParser import SafeConfigParser
from Globales import COLORES
class GameMenu(gtk.EventBox):
__gsignals__ = {
"video": (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, (gobject.TYPE_STRING, ))}
def __init__(self):
gtk.EventBox.__init__(self)
vb = gtk.VBox()
self.modify_bg(gtk.STATE_NORMAL, COLORES["contenido"])
self.set_border_width(4)
self.inside_vb = gtk.VBox()
self.ug1 = gtk.Button()
imagen = gtk.Image()
imagen.set_from_file("Imagenes/juego1_banner.png")
self.ug1.modify_bg(gtk.STATE_NORMAL, COLORES["toolbar"])
self.ug1.add(imagen)
self.ug2 = gtk.Button()
imagen = gtk.Image()
imagen.set_from_file("Imagenes/juego2_banner.png")
self.ug2.modify_bg(gtk.STATE_NORMAL, COLORES["toolbar"])
self.ug2.add(imagen)
self.ug3 = gtk.Button()
imagen = gtk.Image()
imagen.set_from_file("Imagenes/juego3_banner.png")
self.ug3.modify_bg(gtk.STATE_NORMAL, COLORES["toolbar"])
self.ug3.add(imagen)
butt = gtk.Button()
img = gtk.Image()
img.set_from_file("Imagenes/go_back_disabled.png")
butt.set_relief(gtk.RELIEF_NONE)
butt.set_image(img)
butt.set_label("")
butt.modify_bg(gtk.STATE_NORMAL, COLORES["toolbar"])
butt.connect("clicked", self.__emit_video)
butt.connect("enter-notify-event", self.__color)
butt.connect("leave-notify-event", self.__decolor)
img.show()
butt.show()
self.back = gtk.Alignment(1, 1, 0, 0)
self.back.add(butt)
self.titulo = gtk.Label("Título")
self.titulo.set_property("justify", gtk.JUSTIFY_CENTER)
self.titulo.modify_font(pango.FontDescription("DejaVu Sans Bold 20"))
self.titulo.modify_fg(gtk.STATE_NORMAL, COLORES["window"])
self.titulo.set_padding(xpad=20, ypad=50)
index = 0
for butt in self.ug1, self.ug2, self.ug3:
butt.modify_bg(gtk.STATE_NORMAL, COLORES["toolbar"])
butt.modify_fg(gtk.STATE_NORMAL, COLORES["text"])
butt.child.modify_font(pango.FontDescription(
"DejaVu Sans Bold 12"))
align = gtk.Alignment(0.5, 0.5, 0.3, 0.2)
align.add(butt)
self.inside_vb.add(align)
butt.connect("clicked", self.start_game, index)
index += 1
self.inside_vb.add(self.back)
vb.pack_start(self.titulo, expand=False, fill=False)
vb.add(self.inside_vb)
self.gameview = GameView()
vb.pack_end(self.gameview, True, True, 0)
self.add(vb)
def stop(self):
self.gameview.stop()
self.hide()
def run(self, topic):
parser = SafeConfigParser()
metadata = os.path.join(topic, "topic.ini")
parser.read(metadata)
self.titulo.set_text("Play Practice: " + parser.get('topic', 'title'))
self.topic = topic
self.show_all()
self.gameview.hide()
def start_game(self, widget, index):
self.inside_vb.hide()
self.titulo.hide()
self.gameview.run(self.topic, index)
def __decolor(self, widget, event):
widget.get_image().set_from_file("Imagenes/go_back_disabled.png")
def __color(self, widget, event):
widget.get_image().set_from_file("Imagenes/go_back.png")
def __emit_video(self, widget):
self.emit("video", self.topic)
class GameView(gtk.EventBox):
__gsignals__ = {
"video": (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, (gobject.TYPE_STRING, ))}
def __init__(self):
gtk.EventBox.__init__(self)
self.modify_bg(gtk.STATE_NORMAL, COLORES["contenido"])
self.set_border_width(4)
self.game = False
self.pump = False
self.firstrun = True
self.pygamecanvas = sugargame2.canvas.PygameCanvas(self)
grupo1 = gtk.Alignment(0.5, 1, 0, 0)
separador = gtk.HSeparator()
grupo1.add(separador)
grupo2 = gtk.Alignment(1, 0.5, 0, 0)
grupo2.add(self.pygamecanvas)
grupo3 = gtk.Alignment(1, 1, 0, 0)
vbox = gtk.VBox()
butt = gtk.Button()
img = gtk.Image()
img.set_from_file("Imagenes/go_back_disabled.png")
butt.set_relief(gtk.RELIEF_NONE)
butt.set_image(img)
butt.set_label("")
butt.modify_bg(gtk.STATE_NORMAL, COLORES["toolbar"])
butt.connect("clicked", self.__reset_menu)
butt.connect("enter-notify-event", self.__color)
butt.connect("leave-notify-event", self.__decolor)
img.show()
butt.show()
vbox.add(butt)
self.score_label = gtk.Label("SCORE\n0")
self.score_label.set_property("justify", gtk.JUSTIFY_RIGHT)
self.score_label.modify_font(pango.FontDescription(
"DejaVu Sans Mono 22"))
self.score_label.modify_fg(gtk.STATE_NORMAL, COLORES["window"])
self.score_label.set_padding(xpad=30, ypad=30)
self.score_label.show()
vbox.add(self.score_label)
butt = gtk.ToggleButton()
butt.set_active(False)
butt.set_relief(gtk.RELIEF_NONE)
butt.modify_bg(gtk.STATE_NORMAL, COLORES["toolbar"])
img = gtk.Image()
img.set_from_file("Iconos/stock_volume-max.svg")
butt.set_image(img)
butt.set_label("")
img.show()
butt.show()
butt.connect("toggled", self.update_volume)
self.volbtn = butt
vbox.add(butt)
grupo3.add(vbox)
hb = gtk.HBox()
hb.pack_start(grupo1)
hb.add(grupo2)
hb.pack_end(grupo3)
self.add(hb)
self.connect("size-allocate", self.__reescalar)
self.show_all()
def update_score(self, score):
self.score_label.set_text("SCORE\n%s" % str(score))
def update_volume(self, widget):
if not widget.get_active():
if self.game:
self.game.mute(False)
iconfile = "Iconos/stock_volume-max.svg"
self.pygamecanvas.grab_focus()
else:
self.game.mute(True)
iconfile = "Iconos/stock_volume-mute.svg"
pygame.mixer.fadeout(300)
self.pygamecanvas.grab_focus()
widget.get_image().set_from_file(iconfile)
def __decolor(self, widget, event):
widget.get_image().set_from_file("Imagenes/go_back_disabled.png")
def __color(self, widget, event):
widget.get_image().set_from_file("Imagenes/go_back.png")
def __reset_menu(self, widget):
self.stop()
self.parent.show_all()
self.hide()
def __reescalar(self, widget, event):
if self.game:
rect = self.get_allocation()
# FIXME: El juego debe reescalarse a: rect.width, rect.height
def __run_game_1(self):
from Games.ug1.runme import Intro
rect = self.get_allocation()
self.lado = min(rect.width-8, rect.height-8)
print self.lado
self.pygamecanvas.set_size_request(self.lado, self.lado)
spyral.director.init((self.lado, self.lado),
fullscreen=False, max_fps=30)
self.game = Intro(self.topic, self)
spyral.director.push(self.game)
if self.pump:
gobject.source_remove(self.pump)
self.pump = False
self.pump = gobject.timeout_add(300, self.__pump)
try:
spyral.director.run(sugar=True)
except pygame.error:
pass
def __run_game_2(self):
from Games.ug2.runme import Escena
rect = self.get_allocation()
self.lado = min(rect.width-8, rect.height-8)
print self.lado
self.pygamecanvas.set_size_request(self.lado, self.lado)
spyral.director.init((self.lado, self.lado),
fullscreen=False, max_fps=30)
self.game = Escena(self.topic, self)
spyral.director.push(self.game)
if self.pump:
gobject.source_remove(self.pump)
self.pump = False
self.pump = gobject.timeout_add(300, self.__pump)
try:
spyral.director.run(sugar=True)
except pygame.error:
pass
def __run_game_3(self):
from Games.ug3.runme import Escena
rect = self.get_allocation()
self.lado = min(rect.width-8, rect.height-8)
print self.lado
self.pygamecanvas.set_size_request(self.lado, self.lado)
spyral.director.init((self.lado, self.lado),
fullscreen=False, max_fps=30)
self.game = Escena(self.topic, gameview=self)
spyral.director.push(self.game)
if self.pump:
gobject.source_remove(self.pump)
self.pump = False
self.pump = gobject.timeout_add(300, self.__pump)
try:
spyral.director.run(sugar=True)
except pygame.error:
pass
def __pump(self):
pygame.event.pump()
return True
def stop(self):
if self.pump:
gobject.source_remove(self.pump)
self.pump = False
if self.game:
pygame.mixer.music.stop()
spyral.director.pop()
self.game = False
self.hide()
def run(self, topic, game):
self.update_score(0)
self.volbtn.set_active(False)
self.volbtn.get_image().set_from_file("Iconos/stock_volume-max.svg")
self.topic = topic
self.pygamecanvas.grab_focus()
self.show()
if game==0:
gamestart=self.__run_game_1
elif game==1:
gamestart=self.__run_game_2
elif game==2:
gamestart=self.__run_game_3
if self.firstrun:
self.firstrun = False
gobject.idle_add(self.pygamecanvas.run_pygame(gamestart))
else:
gobject.idle_add(gamestart())