Aun está un poco soso sin imágenes ni nada, tengo que añadirlo esto además de que imprima un mensaje cuando pierdas, musica, etc.
- Código: Seleccionar todo
import pygame
from pygame.locals import *
from random import randrange
pygame.init()
Anchura, Altura = 350,350
Screen = pygame.display.set_mode((Anchura,Altura))
pygame.display.set_caption("Nath Snake")
class Snake(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.Posicion = [160,304]
self.Body = []
self.Direccion = 0
#0 es arriba, 1 derecha, 2 abajo, 3 izquierda.
self.Length = 3
def Update(self):
self.Body.insert(0,list(self.Posicion))
self.Body = self.Body[0:self.Length]
if self.Direccion == 0:
self.Posicion[1] -= 16
elif self.Direccion == 1:
self.Posicion[0] += 16
elif self.Direccion == 2:
self.Posicion[1] += 16
elif self.Direccion == 3:
self.Posicion[0] -= 16
if self.Posicion[0] not in range(349):
exit()
if self.Posicion[1] not in range(349):
exit()
for b in self.Body:
if self.Posicion == b:
exit()
Screen.fill((0, 0, 255), (self.Posicion[0], self.Posicion[1], 19, 19))
for b in self.Body:
Screen.fill((0, 0, 255), (b[0], b[1], 19, 19))
class Food(pygame.sprite.Sprite):
def __init__(self):
self.Position()
def Position(self):
self.Posicion = [randrange(1,18)*16,randrange(1,18)*16]
def Update(self):
Screen.fill((255, 0, 0), (self.Posicion[0], self.Posicion[1], 16, 16))
Snake = Snake()
SnakeGroup = pygame.sprite.Group()
SnakeGroup.add(Snake)
Food = Food()
Fuente = pygame.font.SysFont("Arial", 30)
Running = True
while Running:
for event in pygame.event.get():
if event.type == QUIT:
Running = False
elif event.type == KEYDOWN:
if event.key == K_UP:
Snake.Direccion = 0
elif event.key == K_RIGHT:
Snake.Direccion = 1
elif event.key == K_DOWN:
Snake.Direccion = 2
elif event.key == K_LEFT:
Snake.Direccion = 3
if Food.Posicion == Snake.Posicion:
Food.Position()
Snake.Length += 1
Screen.fill((255,255,255))
Snake.Update()
Food.Update()
Render = Fuente.render("Score: %d" % (Snake.Length-3), 1, (0,0,0))
Screen.blit(Render,(5,5))
pygame.display.update()
pygame.time.wait(100)