300x250 AD TOP

jueves, 15 de agosto de 2013

Etiquetado Como: , ,

Codigo Simple Snake con Pygame basado en pygame.Rect

Una de las primeras cosas que hice con Pygame en el clásico juego Snake que en otro tutorial voy a explicar paso a paso su desarrollo, ahora les comparto el código fuente utilizando los Rectángulos de pygame - pygame.Rect



#!/usr/bin/env python
import sys,pygame,random
from pygame.locals import *
def cerrar():
    pygame.quit()
    sys.exit(0)
def cxy(max,min=3):
    x1 = random.randint(min, max)
    y1 = random.randint(min, max)
    return x1,y1
def main():
    w = pygame.display.set_mode((300,300))
    reloj = pygame.time.Clock()
    bucle = True
    snake_color = (103,97,97)
    carnada_color = (228,38,38)
    head_color = (39,98,20)
    direc = random.randint(0,3)
    ancho = 10
    max = (300 / ancho) - 1
    
    if direc == 0:
        x1,y1 = cxy(max)
        xysnake = [(x1,y1),(x1,y1-1),(x1,y1-2)]
    if direc == 1:
        x1,y1 = cxy(max)
        xysnake = [(x1-2,y1),(x1-1,y1),(x1,y1)]
    if direc == 3:
        x1,y1 = cxy(max)
        xysnake = [(x1,y1),(x1-1,y1),(x1-2,y1)]
    if direc == 2:
        x1,y1 = cxy(max)
        xysnake = [(x1,y1-2),(x1,y1-1),(x1,y1)]
        
    x0,y0 = cxy(max,0)
    carnada = pygame.Rect(x0 * 10,y0 * 10,ancho,ancho)
    
    while bucle == True:
        w.fill((255,255,255))
        pygame.draw.rect(w,carnada_color,carnada,1)
        x,y = xysnake[0]
        snake_head = pygame.Rect(x*ancho,y*ancho,ancho,ancho)
        pygame.draw.rect(w,head_color,snake_head,1)
        for xy in xysnake[1:]:
            x , y = xy
            snake_body = pygame.Rect(x * ancho,y * ancho,ancho,ancho)
            pygame.draw.rect(w,snake_color,snake_body,1)
            
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                bucle = False
                break
            if e.type == pygame.KEYDOWN:
                if e.key == pygame.K_UP and direc != 0:
                    direc = 2
                if e.key == pygame.K_DOWN and direc != 2:
                    direc = 0
                if e.key == pygame.K_LEFT and direc != 3:
                    direc = 1
                if e.key == pygame.K_RIGHT and direc != 1:
                    direc = 3
       
        if direc == 3:
            x,y = xysnake[0]
            x = x + 1
            if x > max:
                x = 0
            xysnake.insert(0, (x,y))
            xydel = xysnake.pop()
        if direc == 2:
            x,y = xysnake[0]
            y = y -1
            if y < 0:
                y = max
            xysnake.insert(0, (x,y))
            xydel = xysnake.pop()
        if direc == 1:
            x,y = xysnake[0]
            x = x - 1
            if x < 0:
                x = max
            xysnake.insert(0, (x,y))
            xydel = xysnake.pop()
        if direc == 0:
            x,y = xysnake[0]
            y = y +1
            if y > max:
                y = 0
            xysnake.insert(0, (x,y))
            xydel = xysnake.pop()
        if xysnake[0] in xysnake[1:]:
            print "Que pena perdiste"
            cerrar()
        
        if snake_head.colliderect(carnada):
            carnada = pygame.Rect( random.randint(0, max) * 10, random.randint(0, max) * 10,ancho,ancho)
            if direc == 1:
                xysnake.insert(0,(x-1,y))
            if direc == 3:
                xysnake.insert(0,(x+1,y))
            if direc == 0:
                xysnake.insert(0,(x,y+1))
            if direc == 2:
                xysnake.insert(0,(x,y-1))
        pygame.display.update()
        reloj.tick(15)
    cerrar()
if __name__ == '__main__':
    pygame.init()
    main()

0 comentarios:

Publicar un comentario

Gracias por comentarnos