# Untitled

unknown
plain_text
3 years ago
4.7 kB
1
Indexable
Never
```//BY Ian151 (ChromeWing)
//inspired by the original snake game
//this is a test for creating small games in processing.

int angle=0;
int snakesize=5;
int time=0;
int speed = 5;
int applex=(round(random(47))+1)*8;
int appley=(round(random(47))+1)*8;
boolean redo=true;
boolean stopgame=false;
void setup()
{
restart();
size(400,400);
textAlign(CENTER);
}
void draw()
{
if (stopgame)
{
//do nothing because of game over (stop playing)
}
else
{
//draw stationary stuff
time+=1;
fill(255,0,0);
stroke(0);
rect(applex,appley,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);
//by modulating time by 5, we create artificial frames each 5 frames
//(otherwise the game would go WAY too fast!)
if ((time % speed)==0)
{
travel();
display();
}
}
}
//controls:
void keyPressed()
{
if (key == CODED)
{
//what key was pressed? is the previous angle the opposite direction?
//we wouldn't want to go backwards into our body, that would hurt!
//also, is the previous body segment in front of the direction?
//(based on the previous question, but added for secure turning without suicide)
{
angle=90;
}
{
angle=270;
{
angle=180;
{
angle=0;
}
if (keyCode == SHIFT)
{
//restart the game by pressing shift
restart();
}
}
}
void travel()
{
for(int i=snakesize;i>0;i--)
{
if (i!=1)
{
//shift all the coordinates back one array
}
else
{
//move the new spot for the head of the snake, which is
switch(angle)
{
case 0:
break;
case 90:
break;
case 180:
break;
case 270:
break;
}
}
}

}
void display()
{
//is the head of the snake eating the apple?
{
//grow and spawn the apple somewhere away from the snake
//(currently some of the code below might not be working, but the game still works.)
spawnApple();
}
//draw the new head of the snake...
stroke(sinecolor(1),sinecolor(0),sinecolor(.5));
fill(0);
//...then erase the back end of the snake.
fill(255);

}
void spawnApple()
{
snakesize+=round(random(3)+1);
redo=true;
while(redo)
{
applex=(round(random(47))+1)*8;
appley=(round(random(47))+1)*8;
for(int i=1;i<snakesize;i++)
{

{
redo=true;
}
else
{
redo=false;
i=1000;
}
}
}
}
{
for(int i=2;i<=snakesize;i++)
{
//is the head of the snake occupying the same spot as any of the snake chunks?
{
fill(255);
rect(125,125,160,100);
fill(0);
text("GAME OVER",200,150);
text("Score:  "+str(snakesize-1)+" units long",200,175);
text("To restart, press Shift.",200,200);
stopgame=true;
}
//is the head of the snake hitting the walls?
{
fill(255);
rect(125,125,160,100);
fill(0);
text("GAME OVER",200,150);
text("Score:  "+str(snakesize-1)+" units long",200,175);
text("To restart, press Shift.",200,200);
stopgame=true;
}
}
}
void restart()
{
//by pressing shift, all of the main variables reset to their defaults.
background(255);
for(int i=2;i<1000;i++)
{
}
stopgame=false;
applex=(round(random(47))+1)*8;
appley=(round(random(47))+1)*8;
snakesize=5;
time=0;
angle=0;
redo=true;
}
float sinecolor(float percent)
{