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
|
import pygame
import gtk
import os
import imp
from gettext import gettext as _
import time
import sys
#constantes
EVENTORESPUESTA = pygame.USEREVENT+1
EVENTODESPEGUE = EVENTORESPUESTA+1
EVENTOREFRESCO = EVENTODESPEGUE+1
TIEMPOREFRESCO = 250
CAMINORECURSOS = "recursos"
CAMINOCOMUN = "comun"
CAMINOIMAGENES = "imagenes"
CAMINODATOS = "datos"
CAMINOFUENTES = "fuentes"
clock = pygame.time.Clock()
def wait_events():
""" Funcion para esperar por eventos de pygame sin consumir CPU """
global clock
clock.tick(20)
return pygame.event.get()
class YoButia():
def __init__(self):
pass
def principal(self):
print "Clase principal"
#Aca va el loop principal del juego.
global scale, shift_x, shift_y
pygame.time.set_timer(EVENTOREFRESCO,TIEMPOREFRESCO)
self.loadAll()
self.loadCommons()
self.presentacion()
time.sleep(2)
#Main principal del juego
while 1:
self.pantallaInicial()
time.sleep(2)
def presentacion(self):
self.pantalla.fill((0,0,0))
self.pantalla.blit(self.fondo1,
(int(75*scale+shift_x),int(75*scale+shift_y)))
self.mostrarTexto(_("Presione una tecla para continuar"),
self.fuente32,
(int(600*scale+shift_x),int(800*scale+shift_y)),
(255,155,155))
pygame.display.flip()
def mostrarTexto(self,texto,fuente,posicion,color):
"""Muestra texto en una determinada posicion"""
text = fuente.render(texto, 1, color)
textrect = text.get_rect()
textrect.center = posicion
self.pantalla.blit(text, textrect)
def loadAll(self):
#Metodo que carga las imagenes y sonidos
global scale, shift_x, shift_y, xo_resolution
pygame.init()
pygame.display.init()
# crear pantalla
self.anchoPantalla = gtk.gdk.screen_width()
self.altoPantalla = gtk.gdk.screen_height()
self.pantalla = pygame.display.get_surface()
if not(self.pantalla):
# prevent hide zones
self.anchoPantalla = self.anchoPantalla - 50
self.altoPantalla = self.altoPantalla - 100
self.pantalla = pygame.display.set_mode((self.anchoPantalla,
self.altoPantalla))
pygame.display.flip()
if self.anchoPantalla==1200 and self.altoPantalla==900:
xo_resolution = True
scale = 1
shift_x = 0
shift_y = 0
else:
xo_resolution = False
if self.anchoPantalla/1200.0<self.altoPantalla/900.0:
scale = self.anchoPantalla/1200.0
shift_x = 0
shift_y = int((self.altoPantalla-scale*900)/2)
else:
scale = self.altoPantalla/900.0
shift_x = int((self.anchoPantalla-scale*1200)/2)
shift_y = 0
# cargar imagenes generales
self.camino_imagenes = os.path.join(CAMINORECURSOS,
CAMINOCOMUN,
CAMINOIMAGENES)
# fondo presentacion
self.fondo1 = self.cargarImagen("fondo1.png")
# cargar fuentes
self.fuente60 = pygame.font.Font(os.path.join(CAMINORECURSOS,\
CAMINOCOMUN,\
CAMINOFUENTES,\
"Share-Regular.ttf"),
int(60*scale))
self.fuente40 = pygame.font.Font(os.path.join(CAMINORECURSOS,\
CAMINOCOMUN,\
CAMINOFUENTES,\
"Share-Regular.ttf"),
int(34*scale))
self.fuente9 = pygame.font.Font(os.path.join(CAMINORECURSOS,\
CAMINOCOMUN,\
CAMINOFUENTES,\
"Share-Regular.ttf"),
int(20*scale))
self.fuente32 = pygame.font.Font(None, int(30*scale))
self.fuente24 = pygame.font.Font(None, int(24*scale))
def loadCommons(self):
self.listaPrefijos = list()
r_path = os.path.join(CAMINORECURSOS, CAMINOCOMUN, CAMINODATOS, 'commons.py')
a_path = os.path.abspath(r_path)
f = None
try:
f = imp.load_source('commons', a_path)
except:
print _('Cannot open %s') % 'commons'
if f:
if hasattr(f, 'PREFIX'):
for e in f.PREFIX:
e1 = unicode(e, 'UTF-8')
self.listaPrefijos.append(e1)
self.numeroPrefijos = len(self.listaPrefijos)
def cargarImagen(self,nombre):
"""Carga una imagen y la escala de acuerdo a la resolucion"""
global scale, xo_resolution
if xo_resolution:
imagen = pygame.image.load( \
os.path.join(self.camino_imagenes,nombre))
else:
imagen0 = pygame.image.load( \
os.path.join(self.camino_imagenes,nombre))
imagen = pygame.transform.scale(imagen0,
(int(imagen0.get_width()*scale),
int(imagen0.get_height()*scale)))
del imagen0
return imagen
def pantallaInicial(self):
"""Pantalla con el menu principal del juego"""
global scale, shift_x, shift_y
self.pantalla.fill((0,0,0))
self.mostrarTexto(unicode(_("MENU PRINCIPAL"), "UTF-8"),
self.fuente60,
(int(50*scale+shift_x),
int(50*scale+shift_y)),
(255,255,255))
self.mostrarTexto(_("About this game"),
self.fuente40,
(int(300*scale+shift_x),int(825*scale+shift_y)),
(100,200,100))
pygame.display.flip()
while 1:
# Pump GTK messages.
while gtk.events_pending():
gtk.main_iteration()
for event in wait_events():
if event.type == pygame.KEYDOWN:
if event.key == 27: # escape: volver
sys.exit()
return
def main():
juego = YoButia()
juego.principal()
if __name__ == "__main__":
main()
|