From 3afe8678a2ab75484cd65473ff8490ff8e45358d Mon Sep 17 00:00:00 2001 From: flavio Date: Fri, 19 Oct 2012 17:29:31 +0000 Subject: Que la nave no se salga de la pantalla --- 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" -- cgit v0.9.1