diff options
-rwxr-xr-x | Juego.py | 24 | ||||
-rwxr-xr-x | Nave.py | 42 |
2 files changed, 32 insertions, 34 deletions
@@ -8,6 +8,7 @@ import pygame from pygame.locals import * from Jugador import Jugador +from Nave import Bala Ancho = 1024 Alto = 600 @@ -85,6 +86,8 @@ class Juego(): self.eventos() self.naves.update() + self.balas.update() + self.explosiones.update() pygame.event.clear() self.naves.draw(self.ventana) @@ -102,6 +105,25 @@ class Juego(): 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() @@ -7,17 +7,17 @@ import random import pygame from pygame.locals import * -''' -DIRECTORIOBASE = os.path.dirname(__file__) +DIRECTORIOBASE = os.path.dirname(__file__) +''' IMAGEN_NAVE = os.path.join(DIRECTORIOBASE, 'Imagenes', 'Naves', 'Nave.png') IMAGEN_NAVE_ENEMIGA = os.path.join(DIRECTORIOBASE, - 'Imagenes', 'Naves', 'enemy02.png') + 'Imagenes', 'Naves', 'enemy02.png')''' IMAGEN_BALA = os.path.join(DIRECTORIOBASE, - 'Imagenes', 'Bala.png')''' + 'Imagenes', 'Bala.png') class Nave(pygame.sprite.Sprite): """Nave.""" @@ -34,31 +34,7 @@ class Nave(pygame.sprite.Sprite): self.h = pygame.display.Info().current_h self.rect.center = (self.w/2, self.h/2) - - def update(self): - """Actualiza la nave, segĂșn eventos del teclado.""" - - tecla = pygame.key.get_pressed() - - if tecla[pygame.K_UP]: - if self.rect.y - 5 > 0: - self.rect.y -= 5 - - if tecla[pygame.K_DOWN]: - if self.rect.y + self.rect.h + 5 < self.h: - self.rect.y += 5 - - if tecla[pygame.K_RIGHT]: - if self.rect.x + self.rect.w + 5 < self.w: - self.rect.x += 5 - - if tecla[pygame.K_LEFT]: - if self.rect.x - 5 > 0: - self.rect.x -= 5 - - if tecla[pygame.K_SPACE]: - print "Disparar" - + class Enemigo(Nave): """Nave.""" @@ -95,10 +71,10 @@ class Bala(pygame.sprite.Sprite): self.image = pygame.image.load(IMAGEN_BALA) self.rect = self.image.get_rect() - xnave = self.propietario.rect.x - ynave = self.propietario.rect.y - wnave = self.propietario.rect.w - hnave = self.propietario.rect.h + xnave = self.propietario.nave.rect.x + ynave = self.propietario.nave.rect.y + wnave = self.propietario.nave.rect.w + hnave = self.propietario.nave.rect.h self.rect.x = xnave + wnave / 2 - self.rect.w / 2 self.rect.y = ynave - self.rect.h/2 |