xxxxxxxxxx
snake test;
food food1;
int highScore;
void setup(){
size(1000, 600);
frameRate(14);
test = new snake();
food1 = new food();
rectMode(CENTER);
textAlign(CENTER, CENTER);
highScore = 0;
}
void draw(){
background(250, 250, 250);
drawScoreboard();
test.move();
test.display();
food1.display();
if( dist(food1.xpos, food1.ypos, test.xpos.get(0), test.ypos.get(0)) < test.sidelen ){
food1.reset();
test.addLink();
}
if(test.len > highScore){
highScore= test.len;
}
}
void keyPressed(){
if(key == CODED){
if(keyCode == LEFT){
test.dir = "left";
}
if(keyCode == RIGHT){
test.dir = "right";
}
if(keyCode == UP){
test.dir = "up";
}
if(keyCode == DOWN){
test.dir = "down";
}
}
}
void drawScoreboard(){
// All of the scode for code and title
fill(250, 0, 250);
textSize(65);
text( "Snake Game", width/2, 80);
fill(250, 0, 250);
textSize(20);
text( "By: Katherine<3Quinlivan", width/2, 140);
// draw scoreboard
stroke(179, 140, 198);
fill(255, 0 ,255);
rect(90, 70, 160, 80);
fill(118, 22, 167);
textSize(17);
text( "Score: " + test.len, 70, 50);
fill(118, 22, 167);
textSize(17);
text( "High Score: " + highScore, 70, 70);
}
class food{
// define varibles
float xpos, ypos;
//constructor
food(){
xpos = random(100, width - 100);
ypos = random(100, height - 100);
}
// functions
void display(){
fill(190,0,100);
ellipse(xpos, ypos,17,17);
}
void reset(){
xpos = random(100, width - 100);
ypos = random(100, height - 100);
}
}
class snake{
//define varibles
int len;
float sidelen;
String dir;
ArrayList <Float> xpos, ypos;
// constructor
snake(){
len = 1;
sidelen = 17;
dir = "right";
xpos = new ArrayList();
ypos = new ArrayList();
xpos.add( random(width) );
ypos.add( random(height) );
}
// functions
void move(){
for(int i = len - 1; i > 0; i = i -1 ){
xpos.set(i, xpos.get(i - 1));
ypos.set(i, ypos.get(i - 1));
}
if(dir == "left"){
xpos.set(0, xpos.get(0) - sidelen);
}
if(dir == "right"){
xpos.set(0, xpos.get(0) + sidelen);
}
if(dir == "up"){
ypos.set(0, ypos.get(0) - sidelen);
}
if(dir == "down"){
ypos.set(0, ypos.get(0) + sidelen);
}
xpos.set(0, (xpos.get(0) + width) % width);
ypos.set(0, (ypos.get(0) + height) % height);
void display(){
for(int i = 0; i <len; i++){
stroke(179, 140, 198);
fill(100, 0, 100, map(i-1, 0, len-1, 250, 50));
rect(xpos.get(i), ypos.get(i), sidelen, sidelen);
}
}
void addLink(){
xpos.add( xpos.get(len-1) + sidelen);
ypos.add( ypos.get(len-1) + sidelen);
len++;
}
boolean checkHit(){
for(int i = 1; i < len; i++){
if( dist(xpos.get(0), ypos.get(0), xpos.get(i), ypos.get(i)) < sidelen){
return true;
}
}
return false;
}
}