#!/usr/bin/python
# -*- coding: cp1252 -*-
#Importacion de modulos
import pygame
from pygame.locals import*
#Datos de pantallo
ANCHO = 749
ALTO = 523
RESOLUCION = (ANCHO,ALTO)
#Colores-----R----G---B-
AMARILLO = (255, 255, 0)
#Clase para procesar y trabajar con el texto
class Texto(pygame.sprite.Sprite):
texto = ""
imagen = None
rect = None
valor = 0
fuente = None
def __init__(self,fuente,texto,rect,valor=0):
pygame.sprite.Sprite.__init__(self)
self.texto = texto
self.fuente = fuente
self.valor = valor
self.imagen = fuente.render(texto,True,(AMARILLO))
self.rect = self.imagen.get_rect()
self.rect = self.rect.move(rect)
def actualizar(self,nuevovalor):
if self.valor <> nuevovalor:
self.valor = nuevovalor
palabra = self.texto+" "+str(self.valor)
self.imagen = self.fuente.render(palabra,True,(255,255,255))
class Nave(pygame.sprite.Sprite):
vida = True
imagen = None
imagen_normal = None
fueguito = None
vel = 0.0
pos = 0.0
aterrizo = False
fuego = 0.0
nafta = 1000
def __init__(self,pos=400):
pygame.sprite.Sprite.__init__(self)
if not self.imagen_normal:
Nave.imagen_normal = pygame.image.load("navesita.png").convert_alpha()
Nave.imagen = Nave.imagen_normal
self.rect = self.imagen.get_rect()
if not self.fueguito:
Nave.fueguito = pygame.image.load("fuegodelanave.png")
self.rect.left = pos
self.height = self.rect.height
def actualizar(self,tiempo,gravedad):
self.estado_teclado()
self.vel += (gravedad+self.fuego)/tiempo
self.pos -= self.vel/tiempo
self.rect.top = int(self.pos)
if self.fuego <> 0.0:
self.imagen = self.fueguito
else:
self.imagen = self.imagen_normal
tamano = self.imagen.get_rect()[2:]
self.rect.size = tamano
self.fuego = 0.0
def estado_teclado(self):
tecla = pygame.key.get_pressed()
if tecla[pygame.K_0]:
self.fuego = 20.0
self.nafta -= 0.5
elif tecla[pygame.K_1]:
self.fuego = 20.0
self.nafta -= 1
elif tecla[pygame.K_3]:
self.fuego = 20.0
self.nafta -= 1.5
elif tecla[pygame.K_4]:
self.fuego = 20.0
self.nafta -= 2
elif tecla[pygame.K_5]:
self.fuego = 20.0
self.nafta -= 2.5
elif tecla[pygame.K_6]:
self.fuego = 20.0
self.nafta -= 3
elif tecla[pygame.K_7]:
self.fuego = 20.0
self.nafta -= 3.5
elif tecla[pygame.K_8]:
self.fuego = 20.0
self.nafta -= 4
elif tecla[pygame.K_9]:
self.fuego = 20.0
self.nafta -= 4.5
elif tecla[pygame.K_p]:
def PauseUntilKeyboard():
e = pygame.event.wait()
if e.type in (pygame.QUIT, pygame.KEYDOWN):
return
class Alunizador:
gravedad = -20.0
andando = True
VELOCIDAD = -50.0
tiempo = 200
def __init__(self,tiempo):
pygame.init()
self.tiempo = tiempo
self.pantalla = pygame.display.set_mode(RESOLUCION)
pygame.display.set_caption("Alunizador 1.0")
self.reloj = pygame.time.Clock()
self.fuente = pygame.font.SysFont("",40)
self.fondo = pygame.image.load("fondito.png").convert_alpha()
self.pantalla.blit(self.fondo,(0,0))
self.nave = Nave()
self.vel = Texto(self.fuente,"Velocidad",(50,50))
self.altura = Texto(self.fuente,"Altura",(50,100))
self.nafta = Texto(self.fuente, "Nafta",(50,150))
self.todo = pygame.sprite.RenderUpdates()
self.todo.add(self.nave)
self.todo.add(self.vel)
self.todo.add(self.altura)
self.todo.add(self.nafta)
def eventos(self):
a = pygame.event.poll()
while a.type != NOEVENT:
if a.type == QUIT:
self.andando = False
elif a.type == KEYDOWN:
self.evento_cierre(a.key)
a = pygame.event.poll()
def evento_cierre(self,k):
if k == K_ESCAPE:
self.andando = False
def actualizar(self):
self.nave.actualizar(self.tiempo,self.gravedad)
self.vel.actualizar (self.nave.vel)
self.altura.actualizar(480 - self.nave.pos - self.nave.height)
self.nafta.actualizar(self.nave.nafta)
def draw(self):
dibu = self.todo.draw(self.pantalla)
pygame.display.update(dibu)
self.todo.clear(self.pantalla,self.fondo)
def quit(self):
pygame.quit()
def chequeo(self):
nave = self.nave
if nave.rect.top + nave.height >= 480:
if nave.vel < self.VELOCIDAD:
lander.vida = False
else:
lander.aterrizo = True
def pausa(self):
self.reloj.tick(self.tiempo)
def desvanecer_pantalla(self,color,alpha):
desv = pygame.Surface((ANCHO,ALTO))
desv.fill(color)
desv.set_alpha(alpha)
self.todo.dibujo(self.pantalla)
self.pantalla.blit(desv(0,0))
def gana(self):
if self.andando:
nave = self.nave
if not nave.vida:
self.desv_pantalla((128,0,0),128)
imagen = pygame.image.load("perdiste,png").convert_alpha()
print "Perdiste!"
elif nave.aterrizo:
self.desv_pantalla((0,0,128),128)
image = pygame.image.load("Ganste.png").convert_alpha()
print "Aterrizaste sano y salvo"
rect = imagen.get_rect()
pos = self.fondo.get_rect().centerx - rect.ancho /2 , self.fondo.get_rect().centery - rect.height /2
self.pantalla.blit(imagen,pos)
pygame.display.update()
while self.andando:
self.eventos()
self.reloj.tick(self.tiempo)
def jugar(self):
nave = self.nave
while self.andando and nave.vida and not nave.aterrizo:
self.pausa()
self.eventos()
self.chequeo()
self.actualizar()
self.draw()
self.gana()
self.quit()
if __name__ == "__main__":
jueguillo = Alunizador(50)
jueguillo.jugar()
Ese es el codigo, y el error que me tira es:
Traceback (most recent call last):
File "C:\Users\Jorge Kleinman\Desktop\Pygame\Alunizador .py", line 234, in <module>
jueguillo.jugar()
File "C:\Users\Jorge Kleinman\Desktop\Pygame\Alunizador .py", line 228, in jugar
self.draw()
File "C:\Users\Jorge Kleinman\Desktop\Pygame\Alunizador .py", line 172, in draw
dibu = self.todo.draw(self.pantalla)
File "C:\Python27\lib\site-packages\pygame\sprite.py", line 574, in draw
newrect = surface_blit(s.image, s.rect)
AttributeError: 'Texto' object has no attribute 'image'
>>>
SI ALGUIEN ME AYUDA SE LO AGRADECERIA!!!