viernes, 12 de mayo de 2017

Proyecto snake

Aquí os dejo el proyecto que más tarde hemos realizado ya que es una programación mucho mas laboriosa y por ello hemos tenido que buscar más información. En este juego se usa todo tiempo de estructuras en lo cual se demuestra lo aprendido a lo largo del curso.

Snake
Aquí os dejo el código:

int angulo=0;
int largura=5;
int tiempo=0;
int[] cabezax= new int[2500];
int[] cabezay= new int[2500];
int comidax=(round(random(47))+1)*8;
int comiday=(round(random(47))+1)*8;
boolean rehacer=true;
boolean terminar=false;
void setup()
{
  reiniciar();
  size(400,400);
  textAlign(CENTER);
}
void draw()
{
  if (terminar)
  {
  }
  else
  {
 
  tiempo+=1;
  fill(255,0,0);
  stroke(0);
  rect(comidax,comiday,8,8);
  fill(0);
  stroke(0);
  rect(0,0,width,8);
  rect(0,height-8,width,8);
  rect(0,0,8,height);
  rect(width-8,0,8,height);
 
  if ((tiempo % 5)==0)
  {
    desplazamiento();
    ampliar();
    muerte();
  }
  }
}
void keyPressed()
{
  if (key == CODED)
  {
 
    if (keyCode == UP && angulo!=270 && (cabezay[1]-8)!=cabezay[2])
    {
      angulo=90;
    }
    if (keyCode == DOWN && angulo!=90 && (cabezay[1]+8)!=cabezay[2])
    {
      angulo=270;
    }if (keyCode == LEFT && angulo!=0 && (cabezax[1]-8)!=cabezax[2])
    {
      angulo=180;
    }if (keyCode == RIGHT && angulo!=180 && (cabezax[1]+8)!=cabezax[2])
    {
      angulo=0;
    }
    if (keyCode == SHIFT)
    {
      reiniciar();
    }
  }
}
void desplazamiento()
{
  for(int i=largura;i>0;i--)
  {
    if (i!=1)
    {
      cabezax[i]=cabezax[i-1];
      cabezay[i]=cabezay[i-1];
    }
    else
    {
      switch(angulo)
      {
        case 0:
        cabezax[1]+=8;
        break;
        case 90:
        cabezay[1]-=8;
        break;
        case 180:
        cabezax[1]-=8;
        break;
        case 270:
        cabezay[1]+=8;
        break;
      }
    }
  }
 
}
void ampliar()
{
  if (cabezax[1]==comidax && cabezay[1]==comiday)
  {
    largura+=round(random(3)+1);
    rehacer=true;
    while(rehacer)
    {
      comidax=(round(random(47))+1)*8;
      comiday=(round(random(47))+1)*8;
      for(int i=1;i<largura;i++)
      {
       
        if (comidax==cabezax[i] && comiday==cabezay[i])
        {
          rehacer=true;
        }
        else
        {
          rehacer=false;
          i=1000;
        }
      }
    }
  }
  stroke(destello(1),destello(0),destello(.5));
  fill(0);
  rect(cabezax[1],cabezay[1],8,8);
  fill(255);
  rect(cabezax[largura],cabezay[largura],8,8);
 
}
void muerte()
{
  for(int i=2;i<=largura;i++)
  {
    if (cabezax[1]==cabezax[i] && cabezay[1]==cabezay[i])
    {
      fill(255);
      rect(125,125,160,100);
      fill(0);
      text("Has perdido",200,150);
      text("Para volver a jugar presione Shift.",200,200);
      terminar=true;
    }
    if (cabezax[1]>=(width-8) || cabezay[1]>=(height-8) || cabezax[1]<=0 || cabezay[1]<=0)
    {
      fill(255);
      rect(125,125,160,100);
      fill(0);
      text("HAS PERDIDO",200,150);
      text("Para volver a jugar presione Shift.",200,200);
      terminar=true;
    }
  }
}
void reiniciar()
{
  background(255);
  cabezax[1]=200;
  cabezay[1]=200;
  for(int i=2;i<1000;i++)
  {
    cabezax[i]=0;
    cabezay[i]=0;
  }
  terminar=false;
  comidax=(round(random(47))+1)*8;
  comiday=(round(random(47))+1)*8;
  largura=5;
  tiempo=0;
  angulo=0;
  rehacer=true;
}
float destello(float percent)
{
  float slime=(sin(radians((((tiempo +(255*percent)) % 255)/255)*360)))*255;
  return slime;
}


Los comandos del juego son los siguientes:
-El movimiento de la serpiente se realiza con las flechas del teclado.
-El juego consiste en coger el máximo número de cuadraditos rojos(representan manzanas). Estos aumentan el tamaño de la serpiente.
-Se pierde al tocar los lados de la pantalla.
















No hay comentarios:

Publicar un comentario