# Snake Game

Printable View

• 03-08-2012, 06:48 AM
LuluMM
Snake Game
So I'm making a snake game, the one were the snake eats something and grows larger everytime. I have a basic outline, but now I'm stuck, when I run this code all I get is a red circle, I guess that's the snake's head. Any tips on were to begin or how to proceed from here?

class GridPos {
// instance variables
int x;
int y;

// one value shared by the whole class
static final int GRIDSIZE = 20;

GridPos (int initX, int initY) {
x = initX;
y = initY;
}

// sets the position to the indicated coordinates
void setTo (int xval, int yval) {
x = xval;
y = yval;
}

// copy the x and y values of the indicated GridPos
void setToPos(GridPos pos) {
x = pos.x;
y = pos.y;
}

// returns true if at same place as the passed GridPos
boolean equals (GridPos p) {
return (x == p.x && y == p.y);
}

// returns true if on the screen
boolean onScreen() {
return (x >= 0 && y >= 0 && x*GRIDSIZE < width && y*GRIDSIZE < height);
}

// updates the refrence position by adding in the x and y in the passed position to the x and
void shiftBy(GridPos p) {
x = x + p.x;
y = y + p.y;
}

// draws the position as a square on the screen
void paint (color c) {
rectMode(CORNER);
stroke(255);
fill(c);
rect(x*GRIDSIZE, y*GRIDSIZE, GRIDSIZE, GRIDSIZE);
}

// draws a circle instead of a square
void circle (color c) {
ellipseMode(CORNER);
stroke(255);
fill(c);
ellipse(x*GRIDSIZE, y*GRIDSIZE, GRIDSIZE, GRIDSIZE);
}

// updates the x and y coordinates to random values on the screen
void randomize() {
x = (int) random(0, width / GRIDSIZE);
y = (int) random(0, height / GRIDSIZE);
}

// returns the x-value of the position
int getX() {
return this.x; // using "this.x" instead of x for emphasis
}

// returns the y-value of the position
int getY() {
return this.y; // using "this.y" instead of y for emphasis
}

// prints the location with parens
public String toString() {
return "(" + x + ", " + y + ")";
}
}

Snake serpent = new Snake(100); // the snake controlled by the player
// 100 is the number of grid squares in display

GridPos food = new GridPos(0,0);

void setup() {
size(200, 200); // 10 by 10 gridsquares (each square is 20x20)

food = serpent.randGoal(); // initialize to a random spat not on the snake

println("click screen to start");
frameRate(30);
noLoop();
}

void draw() {
background(200);

// move serpent every 10 frames (1/3 of a second)
if (frameCount % 10 == 0) {
serpent.move();
}

// draw food and serpent
food.circle( color(255,0,0)) ;
serpent.display();

// check for serpent eating himself or leaving screen
if (serpent.headOffScreen() || serpent.headIntersect() ) {
noLoop();
println("The snake died after growing to size " + serpent.length);
}

// check for snake reaching the food
if (serpent.onSnake(food) ) {
food = serpent.randGoal();
serpent.grow(3); // the snake grows after eating
// the 3 causes it to grow by 3 squares
}
}

// read the arrow keys to change the snakes direction
void keyPressed() {
// println(" in KeyPressed");
if (key == CODED) { // a special key like arrows
if (keyCode == UP) {
// println("now moving up");
serpent.changeDirection(new GridPos(0,-1) );
}
else if (keyCode == DOWN) {
// println("now moving down");
serpent.changeDirection(new GridPos(0,1) );
}
else if (keyCode == RIGHT) {
serpent.changeDirection(new GridPos(1,0) );
}
else if (keyCode == LEFT) {
serpent.changeDirection(new GridPos(-1,0) );
}
else {
println("UNKNOWN CODED KEY: code = " + keyCode);
}
}
}

// clicking the mouse starts the game
void mousePressed() {
loop();
}

class Snake {

GridPos[] body;
int length;
GridPos direction; // this is a GridPos object holding the direction

int amountToGrow;

// start snake on left edge going down
Snake (int maxCells) {
body = new GridPos[maxCells];
length = 3;
for (int i=0; i < length; i++) {
body[i] = new GridPos(0,i);
}
direction = new GridPos(0,1);
amountToGrow = 0;
}

}

}

void move() {

}

// show the snake
void display() {

}

return false; // temporary return before it is implemented
}

// return true if the head goes off the screen, and false otherwise
boolean headOffScreen() {
// implement in phase 4
return false; // temporary return before full implementation
}

// generate a random Grid position that is *not* on the snake.
GridPos randGoal () {
GridPos result = new GridPos(0,0);
result.randomize();

return result;
}

// return true if the indicated GridPos is on the snake and false otherwise
boolean onSnake (GridPos pos) {
// implement in phase 0
return false;
}

// print information about snake for debugging
void debugInfo(String msg) {
print(msg);
println ("Snake: length = " + length + " direction=" + direction
+ " amountToGrow=" + amountToGrow);
print("Snake Body (from tail): ");
for (int i=0; i < length; i++) {
print(body[i]+" ");
}
println();
}
}