Skip to content

Instantly share code, notes, and snippets.

@jkwok91

jkwok91/Food.pde Secret

Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkwok91/a469a189bc7ac419e7f6 to your computer and use it in GitHub Desktop.
Save jkwok91/a469a189bc7ac419e7f6 to your computer and use it in GitHub Desktop.
snaaaaake
class Food {
PVector location;
float xrad,yrad;
Food() {
xrad = 5;
yrad = 5;
location = new PVector(random(xrad,width-xrad), random(yrad,height-yrad));
}
void display() {
ellipseMode(CENTER);
ellipse(location.x, location.y, 2*xrad, 2*yrad);
}
}
/*
snAaaaaaAAaAaAAke
*/
int W = 300;
int H = 300;
int cx = W/2;
int cy = H/2;
int highScore;
boolean gameOver;
float seconds;
Snake user;
Food f;
void setup() {
size(W, H);
background(100);
startGame();
highScore = 0;
}
void startGame() {
gameOver = false;
user = new Snake();
f = new Food();
}
void draw() {
background(100);
displayScore();
if (gameOver) {
text("click to restart", cx/2, cy/2);
}
else {
if (user.canEat(f)) {
user.eat();
f = new Food();
};
f.display();
user.move();
gameOver = user.isDead();
user.display();
}
}
void displayScore() {
String scoreTxt = "Score: "+user.eaten+"\nHigh Score: "+highScore;
text(scoreTxt, cx, cy);
}
void mousePressed() {
highScore = max(highScore,user.eaten);
startGame();
}
void keyPressed() {
if (key == CODED) {
user.changeDirection(keyCode);
}
}
class Snake {
PVector head;
PVector velocity;
int r, xside, yside;
ArrayList<PVector> body;
int eaten; //num food eaten
int lastCommand;
Snake() {
body = new ArrayList<PVector>();
head = new PVector(width/2, height/2);
velocity = new PVector(1.5, 1.5);
body.add(head);
r = 5;
xside = 2*r;
yside = 2*r;
eaten = 0;
lastCommand = UP;
}
boolean canEat(Food food) {
float fx = food.location.x;
float fy = food.location.y;
boolean withinX = (head.x+xside >= fx-food.xrad && fx+food.xrad >= head.x);
boolean withinY = (head.y+yside >= fy-food.yrad && fy+food.yrad >= head.y);
return (withinX && withinY);
}
void eat() {
println(body);
eaten++;
//add new segment
newSeg(eaten-1);
PVector accel = new PVector(1.0/(eaten+1), 1.0/(eaten+1));
velocity.add(accel);
println("aftereating: "+body);
}
boolean isDead() {
//death by hitting a wall
return (head.x >= width-xside || head.x <= 0 || head.y >= height-yside || head.y <= 0);
}
void move() {
newSeg(0);
head = body.get(0);
println("just added new head"+body);
body.remove(eaten+1);
}
void newSeg(int idx) {
PVector toClone = body.get(idx); //get segment to latch onto
PVector newPos = new PVector(toClone.x, toClone.y);
int factor = (idx == 0) ? 1 : -1;
if (lastCommand == UP) {
newPos.y-=(velocity.y*factor);
}
else if (lastCommand == DOWN) {
newPos.y+=(velocity.y*factor);
}
else if (lastCommand == RIGHT) {
newPos.x+=(velocity.x*factor);
}
else if (lastCommand == LEFT) {
newPos.x-=(velocity.x*factor);
}
else {
//do nothing
}
body.add(idx, newPos);
}
void changeDirection(int k) {
lastCommand = k;
}
void display() {
fill(255);
stroke(0);
for (int i = body.size()-1 ; i >= 0; i--) { //draws from tail to head so that head overlaps the following segments
PVector pv = body.get(i);
rect(pv.x, pv.y, xside, yside);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment