sábado, 17 de octubre de 2015

Un gato no tan libre


#Este es un programa ya admitidamente algo largo, pero tenemos ahora la inclusion
#de un "score" para contar el numero de pelotas (temblorosas) y agregando también algunos
#obstáculos para impedir que el gato deambule impunemente. Tambien he agregado
#un ejemplo de musica de fondo; todos estos aditamentos en principio no lesionan
#la eficiencia en general.

#En este estado de desarrollo, existen diversas variantes que originarian juegos interesantes
#con las pertinentes modificaciones.
# Un video correspondiente a la ejecucion de este programa está en:
# VIDEO             https://youtu.be/GJMdnwv2VvI
import pygame,time,random
from pygame.locals import *

SCREENRECT = Rect(0,0,840,680)
SCORE=0
BOMBS=3

WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
BLUE = (0, 0, 128)
BLUER = (0, 255, 128)


class Wall(pygame.sprite.Sprite):
    # Constructor function
    def __init__(self,height,width,x,y):
        # Call the parent's constructor
        pygame.sprite.Sprite.__init__(self,self.containers)
         # Make a blue wall, of the size specified in the parameters
        self.image = pygame.Surface([width, height])
        self.image.fill((BLUER))
         # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)


class Wall2(pygame.sprite.Sprite):
    images=[]
    def __init__(self,x,y,width,height):
        pygame.sprite.Sprite.__init__(self,self.containers)
        self.image=self.images[0]
        self.rect=self.image.get_rect()
        self.x=x
        self.y=y
        self.rect.center = (self.x,self.y)
#    def update(self):
#        self.rect.center = (self.x,self.y)

class Score(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.font = pygame.font.Font(None,60)
        self.font.set_italic(1)
        self.color = Color('white')
        self.lastscore=-1
        self.update()
        self.rect=self.image.get_rect().move(10,550)
    def update(self):
        if SCORE != self.lastscore:
            self.lastscore = SCORE
            msg = "Score: %d" % SCORE
            self.image = self.font.render(msg,0,self.color)



class Gato(pygame.sprite.Sprite):
    speed = 5
    bounce = 24
    images = []
    frame = 0
    animcycle=12
    def __init__(self):
        pygame.sprite.Sprite.__init__(self,self.containers)
        self.image=self.images[0]
        self.rect = self.image.get_rect()
        self.facing=-1
        self.origtop=self.rect.top
        self.change_x=0
        self.change_y=0
       
    def move(self,horver,direction):
        if horver==1:
            if direction==-1:
                self.image=self.images[0]
                self.rect.move_ip(direction*self.speed,0)
            if direction==1:
                self.image=self.images[1]
#                self.image=self.images[0]
                self.rect.move_ip(direction*self.speed,0)
        if horver==-1:
            if direction==-1:
                self.image=self.images[2]
                self.rect.move_ip(0,direction*self.speed)
            if direction==1:
                self.image=self.images[3]
                self.rect.move_ip(0,direction*self.speed)
#    def update(self):
        self.change_x=self.rect.left      
        self.change_y=self.rect.top      

#        self.frame=self.frame+1
#        self.rect.top=self.origtop-(self.rect.left/self.bounce%2)


    def inf(self):
        print self.speed
        print self.bounce
        print self.images

class Ball(pygame.sprite.Sprite):
    images=[]
    images=[]
    frame=0
    animcycle=3
    defaultlife=20

#    x,y=random.randint(1,200),random.randint(1,200)
    def __init__(self,x,y):
        pygame.sprite.Sprite.__init__(self,self.containers)
        self.image=self.images[0]
        self.rect=self.image.get_rect()
        self.x=x
        self.y=y
        self.life=self.defaultlife
       
    def update(self):
#        self.rect.move(300,300)
        self.rect.center = (self.x,self.y)
        self.life=self.life-1
        self.image = self.images[self.life%5]
#        if self.life<=0: self.kill()


class Explosion(pygame.sprite.Sprite):
    images=[]
    frame=0
    animcycle=3
    defaultlife=20

    def __init__(self,actor):
        pygame.sprite.Sprite.__init__(self,self.containers)
        self.image=self.images[0]
        self.rect=self.image.get_rect()
        self.rect.center=actor.rect.center
        self.life=self.defaultlife

    def update(self):
#        self.frame = self.frame+1
        self.life=self.life-1
        self.image = self.images[self.life/self.animcycle%5]
        if self.life<=0: self.kill()

def main():
    global SCORE
    pygame.init()
    clock=pygame.time.Clock()
    windowStyle=0 #FULLSCREEN

    boom = pygame.mixer.Sound('./DataSounds/wahoo.wav')
    meow = pygame.mixer.Sound('./DataSounds/meow.wav')
    bus = pygame.mixer.music.load('./DataSounds/grieg.wav')
    pygame.mixer.music.play(-1,0.0)

    bestDepth=pygame.display.mode_ok(SCREENRECT.size,windowStyle,32)
    disp=pygame.display.set_mode(SCREENRECT.size,windowStyle,bestDepth)
    pygame.display.set_caption("!!!! *** Un GATO de verdad!!!!!... *** ")
    pygame.mouse.set_visible(0)

    background= pygame.Surface(SCREENRECT.size)
    img11=pygame.image.load('./DataImgs/cat.png')
    img=pygame.transform.scale(img11,(90,70))
    ball=pygame.image.load('./DataImgs/ball.png')
    imgExplosion1=pygame.image.load('./DataImgs/frogMio.png')
    imgExplosion=pygame.transform.scale(imgExplosion1,(100,100))
    wall2=pygame.image.load('./DataImgs/brick.png')
    bgdtile=pygame.image.load('./DataImgs/land.png')
 #   for yT in range(0,500,20):
#        for xT in range(0,SCREENRECT.width,bgdtile.get_width()):
#            background.blit(bgdtile,(xT,yT))
    background.blit(bgdtile,(0,400))
    disp.blit(background,(0,0))
   

    all = pygame.sprite.RenderUpdates()
    Gato.containers = all
    bombs=pygame.sprite.Group()
    walls2=pygame.sprite.Group()
    walls=pygame.sprite.Group()
    explosions=pygame.sprite.Group()
    Ball.containers = bombs, all
    Wall2.containers = walls2, all
    Wall.containers=walls,all
    Explosion.containers=explosions,all
    Score.containers=all
    if pygame.font:
        all.add(Score())

    img2 = pygame.transform.flip(img,1,0)
    #...1,0) equiv ...TrueX, FalseY)
    img3 = pygame.transform.rotate(img,-90)
    img4 = pygame.transform.rotate(img,90)
    Gato.images=[img,img2,img3,img4]

    Wall2.images=[wall2]

    ball01=pygame.transform.rotate(ball,2)
    ball02=pygame.transform.rotate(ball01,2)
    ball03=pygame.transform.rotate(ball02,2)
    ball04=pygame.transform.rotate(ball03,2)
    Ball.images=[ball,ball01,ball02,ball03,ball04]

    imE2=pygame.transform.rotate(imgExplosion,15)
    imE3=pygame.transform.rotate(imgExplosion,35)
    imE4=pygame.transform.rotate(imgExplosion,45)
    imE5=pygame.transform.rotate(imgExplosion,95)
    imE6=pygame.transform.rotate(imgExplosion,135)
    Explosion.images=[imgExplosion,imE2,imE3,imE4,imE5]

    pygame.display.flip()

    gato = Gato()

    bomb = Ball(100,200)
    bomb1 = Ball(40,300)
    bomb2 = Ball(200,500)
    wall2 = Wall2(130,180,5000,5000)
    wall_list=[]
#    wall_list.append(Wall(600,10,0,0))
 #   wall_list.append(Wall(10,790,10,0))
    wall_list.append(Wall(10,100,10,100))
    wall_list.append(Wall(30,200,210,300))
    wall_list.append(Wall(20,100,150,220))
    wall_list.append(Wall(100,30,100,350))
    wall_list.append(Wall(100,10,400,350))
    wall_list.append(Wall(10,100,40,400))
    wall_list.append(Wall(15,200,500,210))
    wall_list.append(Wall(120,10,250,320))
    wall_list.append(Wall(14,180,150,300))
    wall_list.append(Wall(160,19,350,200))


    walls3=pygame.sprite.RenderPlain(wall_list)


    gato.inf()

#    disp.blit(img,(100,00))

    while True:
        old_x=gato.rect.topleft[0]
        old_y=gato.rect.topleft[1]

        for event in pygame.event.get():
            if event.type==QUIT or \
                    (event.type==KEYDOWN and event.key==K_ESCAPE):
                return
            keystate=pygame.key.get_pressed()      
#                pygame.quit()
       
        all.clear(disp,background)
        all.update()
        if keystate[K_RIGHT]:
            gato.move(1,1)
        if keystate[K_LEFT]:
            gato.move(1,-1)
        if keystate[K_UP]:
#            for i in range(1,10):
            gato.move(-1,-1)
#            for i in range(1,9):
#                gato.move(-1,1)    
        if keystate[K_DOWN]:
            gato.move(-1,1)

        for wall2 in pygame.sprite.spritecollide(gato,walls2,False):
#            gato.speed=gato.speed+1
#             Wall(30,150,5000,5000)
             gato.move(0,0)
                     
        # Update position according to our speed (vector)
        new_x=gato.change_x
        new_y=gato.change_y
   
        # Put the player in the new spot
        gato.rect.topleft = (new_x,new_y)
         # Did this update cause us to hit a wall?
        collide = pygame.sprite.spritecollide(gato, walls3, False)
        if collide:
            # Whoops, hit a wall. Go back to the old position
            gato.rect.topleft=(old_x,old_y)


           
#True, remover sprite
        for bomb in pygame.sprite.spritecollide(gato,bombs,True):
            Explosion(gato)
            SCORE=SCORE+1
            x,y=random.randint(1,800),random.randint(1,600)
            if (x>1 and y <200) or (x>1 and y>400):
                meow.play()
            exists=random.randint(0,1)
            if exists==0:
                Ball(x,y)          
            boom.play()

        dirty=all.draw(disp)
        pygame.display.update(dirty)
        clock.tick(40)



if __name__=='__main__': main()




No hay comentarios:

Publicar un comentario