import pygame,time,random
from pygame.locals import *
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
#Colores
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
blanco=255,255,255
negro=0,0,0
rojo= 192,57,43
azul=41,128,185
verde= 26,188,156
amarillo=255,255,0
naranja= 243,156,18
gris= 93,109,126
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
#pantalla
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
tamaño_pantalla=ancho_pantalla,alto_pantalla=1000,600
fps=pygame.time.Clock()
pantalla=pygame.display.set_mode((tamaño_pantalla))
pygame.display.set_caption('Snake')
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
#botones
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
boton1=[400,250]
tamboton=[250,50]
boton2=[400,350]
boton3=[400,450]
colorboton=[gris,rojo,negro,blanco]
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
#serpiente
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
posicions=[500,300]
tamaño=15
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
#manzana
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
posicions=[500,300]
tamaño=15
velocidads=[0,0]
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
pygame.init()
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
#Fuente
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
letra_pequeña= pygame.font.Font("Greywall.ttf",20)
letra_media= pygame.font.Font("Greywall.ttf",40)
letra_grande= pygame.font.Font("Greywall.ttf",120)
#------------------------------------------------------------------------------------------------------------------------------------------------------------------
def borde(superficie,color,pos,tam,grosor):
cursor=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
if pos[0]+tam[0]>cursor[0] > tam[0] and pos[1]+tam[1]>cursor[1]>tam[1] and pos[1]+tam[1]<cursor[1]+tam[1]:
marco=pygame.draw.lines(superficie,color[3],True,((pos[0],pos[1]),(pos[0]+tam[0],pos[1]),(pos[0]+tam[0],pos[1]+tam[1]),(pos[0],pos[1]+tam[1])),grosor)
else:
marco=pygame.draw.lines(superficie,color[2],True,((pos[0],pos[1]),(pos[0]+tam[0],pos[1]),(pos[0]+tam[0],pos[1]+tam[1]),(pos[0],pos[1]+tam[1])),grosor)
return marco
def botones(texto,superficie,color,pos,tam,identidad= None):
cursor=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
if pos[0]+tam[0]>cursor[0] > tam[0] and pos[1]+tam[1]>cursor[1]>tam[1] and pos[1]+tam[1]<cursor[1]+tam[1]:
if click[0] == 1:
if identidad == "jugar":
gameloop()
elif identidad == "config":
opciones()
elif identidad == "salir":
quit()
boton=pygame.draw.rect(superficie,color[0],(pos[0],pos[1],tam[0],tam[1]))
textboton(texto,color[3],pos,tam)
else:
boton=pygame.draw.rect(superficie,color[1],(pos[0],pos[1],tam[0],tam[1]))
textboton(texto,color[2],pos,tam)
return boton
def textboton(msg,color,pos,tam,tamaño="pequeño"):
texto_sup,textoRect=texto(msg,color,tamaño)
textoRect.center = pos[0]+tam[0]//2,pos[1]+tam[1]//2
pantalla.blit(texto_sup,textoRect)
def texto(texto,color,tamaño):
if tamaño == "pequeño":
texto_sup=letra_pequeña.render(texto,True,color)
if tamaño == "mediano":
texto_sup=letra_media.render(texto,True,color)
if tamaño == "grande":
texto_sup=letra_grande.render(texto,True,color)
return texto_sup,texto_sup.get_rect()
def mensaje(msg,color,desplazamientoY=0,tamaño="pequeño"):
texto_sup,textoRect=texto(msg,color,tamaño)
textoRect.center = (ancho_pantalla//2),(alto_pantalla//2)+desplazamientoY
pantalla.blit(texto_sup,textoRect)
def inicio():
intjuego=True
while intjuego:
pantalla.fill(azul)
for event in pygame.event.get():
if event.type==pygame.QUIT:
introjuego=False
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_ESCAPE:
quit()
mensaje("¡Snake!",naranja,-200,tamaño="grande")
botones("Iniciar juego",pantalla,colorboton,boton1,tamboton,identidad="jugar")
borde(pantalla,colorboton,boton1,tamboton,2)
botones("Opciones",pantalla,colorboton,boton2,tamboton,identidad="config")
borde(pantalla,colorboton,boton2,tamboton,2)
botones("Salir",pantalla,colorboton,boton3,tamboton,identidad="salir")
borde(pantalla,colorboton,boton3,tamboton,2)
pygame.display.update()
fps.tick(10)
def opciones():
retroceso=True
while retroceso:
pantalla.fill(blanco)
for event in pygame.event.get():
if event.type==pygame.QUIT:
retroceso=False
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_ESCAPE:
inicio()
retroceso=False
mensaje("Opciones",verde,-200,tamaño="mediano")
pygame.display.update()
fps.tick(10)
def gameloop():
salir=False
global posicions
global velocidads
while not salir:
pantalla.fill(negro)
for event in pygame.event.get():
if event.type==pygame.QUIT:
salir=True
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_ESCAPE:
inicio()
salir=True
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_LEFT:
velocidads=(-4,0)
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_RIGHT:
velocidads=(4,0)
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_UP:
velocidads=(0,-4)
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_DOWN:
velocidads=(0,4)
posicions[0] += velocidads[0]
posicions[1] += velocidads[1]
pygame.draw.circle(pantalla,verde,(posicions),tamaño,0)
#pygame.draw.circle(pantalla,rojo,(random.randint(0,1000),random.randint(0,600)),tamaño,0)
pygame.display.update()
fps.tick(60)
inicio()
quit()
gameloop()