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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import pygame
from pygame.locals import *
from Jugador import Jugador
Ancho = 1024
Alto = 600
DIRECTORIOBASE = os.path.dirname(__file__)
IMAGEN_NAVE = os.path.join(DIRECTORIOBASE,
'Imagenes', 'Naves', 'Nave.png')
class Juego():
""" Juego de Naves. """
def __init__(self):
self.ventana = None
self.fondo = None
self.reloj = None
self.estado = None
self.protagonista = None
self.naves = None
self.preset()
self.load()
self.run()
def preset(self):
"""Se inicia pygame y se configura el entorno general."""
pygame.init()
pygame.display.set_mode((Ancho, Alto))
pygame.display.set_caption("El super juego")
pygame.display.set_icon(pygame.image.load(IMAGEN_NAVE))
pygame.mouse.set_visible(False)
pygame.event.set_allowed([KEYDOWN, KEYUP])
pygame.key.set_repeat(15, 15)
def load(self):
"""Se crean los objetos del juego."""
self.ventana = pygame.display.get_surface()
self.fondo = pygame.image.load(os.path.join(DIRECTORIOBASE,
'Imagenes', 'fondo.png'))
self.protagonista = Jugador("Cristian", IMAGEN_NAVE)
self.reloj = pygame.time.Clock()
self.naves = pygame.sprite.OrderedUpdates()
self.balas = pygame.sprite.OrderedUpdates()
self.explosiones = pygame.sprite.OrderedUpdates()
self.naves.add(self.protagonista.nave)
self.estado = True
def run(self):
"""El Juego corre."""
self.ventana.blit(self.fondo, (0, 0))
self.naves.draw(self.ventana)
self.balas.draw(self.ventana)
self.explosiones.draw(self.ventana)
pygame.display.update()
while self.estado:
self.reloj.tick(35)
self.naves.clear(self.ventana, self.fondo)
self.balas.clear(self.ventana, self.fondo)
self.explosiones.clear(self.ventana, self.fondo)
self.eventos()
self.naves.update()
pygame.event.clear()
self.naves.draw(self.ventana)
self.balas.draw(self.ventana)
self.explosiones.draw(self.ventana)
pygame.display.update()
def eventos(self):
"""Manejar los eventos Generales del Juego."""
tecla = pygame.key.get_pressed()
if tecla[pygame.K_ESCAPE]:
pygame.quit()
sys.exit(0)
if __name__ == "__main__":
Juego()
|