Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorflavio <fdanesse@gmail.com>2012-10-19 17:29:31 (GMT)
committer flavio <fdanesse@gmail.com>2012-10-19 17:29:31 (GMT)
commit3afe8678a2ab75484cd65473ff8490ff8e45358d (patch)
treec821aaa19a47fe4d7edf2c9d366c9a2e3263a905
parentb6da9f85c8ce046a82ff01c5bafc2f901ec62083 (diff)
Que la nave no se salga de la pantalla
-rwxr-xr-xJuego.py16
-rw-r--r--Jugador.py20
-rwxr-xr-xNave.py29
3 files changed, 50 insertions, 15 deletions
diff --git a/Juego.py b/Juego.py
index d64a4cc..3edd7d3 100755
--- a/Juego.py
+++ b/Juego.py
@@ -7,7 +7,7 @@ import sys
import pygame
from pygame.locals import *
-from Nave import Nave
+from Jugador import Jugador
Ancho = 1024
Alto = 600
@@ -55,12 +55,14 @@ class Juego():
self.fondo = pygame.image.load(os.path.join(DIRECTORIOBASE,
'Imagenes', 'fondo.png'))
- self.protagonista = Nave()
+ 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)
+ self.naves.add(self.protagonista.nave)
self.estado = True
@@ -69,6 +71,8 @@ class Juego():
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:
@@ -76,11 +80,17 @@ class Juego():
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):
diff --git a/Jugador.py b/Jugador.py
new file mode 100644
index 0000000..8d8342b
--- /dev/null
+++ b/Jugador.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import os
+import sys
+import random
+
+import pygame
+from pygame.locals import *
+
+from Nave import Nave
+
+class Jugador ():
+
+ def __init__(self, nombre, imagen):
+
+ self.nombre = nombre
+ self.puntos = 0
+ self.nave = Nave(imagen, self)
+ \ No newline at end of file
diff --git a/Nave.py b/Nave.py
index 54676cc..c418b8b 100755
--- a/Nave.py
+++ b/Nave.py
@@ -7,7 +7,7 @@ import random
import pygame
from pygame.locals import *
-
+'''
DIRECTORIOBASE = os.path.dirname(__file__)
IMAGEN_NAVE = os.path.join(DIRECTORIOBASE,
@@ -17,22 +17,23 @@ IMAGEN_NAVE_ENEMIGA = os.path.join(DIRECTORIOBASE,
'Imagenes', 'Naves', 'enemy02.png')
IMAGEN_BALA = os.path.join(DIRECTORIOBASE,
- 'Imagenes', 'Bala.png')
-
+ 'Imagenes', 'Bala.png')'''
+
class Nave(pygame.sprite.Sprite):
"""Nave."""
- def __init__(self):
+ def __init__(self, imagen, jugador):
pygame.sprite.Sprite.__init__(self)
- self.image = pygame.image.load(IMAGEN_NAVE)
+ self.jugador = jugador
+ self.image = pygame.image.load(imagen)
self.rect = self.image.get_rect()
- w = pygame.display.Info().current_w
- h = pygame.display.Info().current_h
+ self.w = pygame.display.Info().current_w
+ self.h = pygame.display.Info().current_h
- self.rect.center = (w/2, h/2)
+ self.rect.center = (self.w/2, self.h/2)
def update(self):
"""Actualiza la nave, segĂșn eventos del teclado."""
@@ -40,16 +41,20 @@ class Nave(pygame.sprite.Sprite):
tecla = pygame.key.get_pressed()
if tecla[pygame.K_UP]:
- self.rect.y -= 5
+ if self.rect.y - 5 > 0:
+ self.rect.y -= 5
if tecla[pygame.K_DOWN]:
- self.rect.y += 5
+ if self.rect.y + self.rect.h + 5 < self.h:
+ self.rect.y += 5
if tecla[pygame.K_RIGHT]:
- self.rect.x += 5
+ if self.rect.x + self.rect.w + 5 < self.w:
+ self.rect.x += 5
if tecla[pygame.K_LEFT]:
- self.rect.x -= 5
+ if self.rect.x - 5 > 0:
+ self.rect.x -= 5
if tecla[pygame.K_SPACE]:
print "Disparar"