Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Juego.py
blob: e02860c99d88bb0b9a2789c04d31c563bb7c48ed (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys

import pygame
from pygame.locals import *

from Jugador import Jugador
from Nave import Bala

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()
            self.balas.update()
            self.explosiones.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 tecla[pygame.K_UP]:
            if self.protagonista.nave.rect.y - 5 > 0:
                self.protagonista.nave.rect.y -= 5
            
        if tecla[pygame.K_DOWN]:
            if self.protagonista.nave.rect.y + self.protagonista.nave.rect.h + 5 < self.protagonista.nave.h:
                self.protagonista.nave.rect.y += 5
            
        if tecla[pygame.K_RIGHT]:
            if self.protagonista.nave.rect.x + self.protagonista.nave.rect.w + 5 < self.protagonista.nave.w:
                self.protagonista.nave.rect.x += 5
            
        if tecla[pygame.K_LEFT]:
            if self.protagonista.nave.rect.x - 5 > 0:
                self.protagonista.nave.rect.x -= 5
            
        if tecla[pygame.K_SPACE]:
            bala = Bala(self.protagonista)
            self.balas.add(bala)
            
if __name__ == "__main__":
    Juego()