Processing beginner working on a unique Snake Game. HELP is needed !!! Thank you in advance !!!
in
Programming Questions
•
10 months ago
So i have been working on this game for a while now and i am unable to resolve the issue. Looked at multiple processing forums and feel more lost than ever. Please Help!!!
General Idea: To create a snake game
Details: The snake moves through the use of the cursor
It has to eat the apple
The apple is an image
The game ends if the snake hits the frame or itself
Problems: The snake does not eat the apple (Mostly because the code needs to explain when they overlap that the apple disappears and the snake grows and i do not know how to do that)
The game doe not end when it hits the border walls or itself.
What am i doing wrong?
//Creating variables like "time", "pos" and "apple" of the datatype "int"
int applex=(round(random(35))+1)*10;
int appley=(round(random(35))+1)*10;
int [] xpos;
int [] ypos;
int time=0;
int [] xpos;
int [] ypos;
int time=0;
Snake s0;
int snakesize= 15;
PImage apple;
//calls boolean function and make use of it if true
boolean gamecontinues=true;
//calls boolean function and make use of it if false
boolean gameover=false;
void setup()
{
restart();
size(400,400); //size of canvas
textAlign(CENTER);
smooth();
//initialise
s0 = new Snake (20);
apple = loadImage ("greeapple.jpg");
apple.resize (50,50);
}
void draw(){
background(255);
//updates the display
s0.update (mouseX-50,mouseY);
s0.display();
if (gameover)
{
//game stops
}
else
{
//draws fixed elements
fill(20,252,0);
stroke(1);
image(apple,10,10);
fill(167,240,196);
noStroke();
image(apple,0,0,10,10);
image(apple,0,height-10,width,10);
image(apple,0,0,10,height);
image(apple,width-10,0,10,height);
}
}
class Snake {
//Declares and creates the array
int[] xpos;
int[] ypos;
// the length of the snake can vary
Snake(int num) {
xpos = new int [num]; //Assigns the first value
ypos = new int [num]; //Assigns the second value
}
void update (int newX, int newY){
//shifts all elements down one spot
//xpos [o] = xpos [1], xpos [1] = xpos [2],...
for (int i = 0; i<xpos.length-1; i++){ //Getting Started With Processing p.143
xpos [i] = xpos [i+1];
ypos [i] = ypos [i+1];//p.
}
//updates the last spot on the array with the mouse location
xpos [xpos.length-1] = newX;
ypos [ypos.length-1] = newY;
}
//p.149 and Michael Smith Snake! (
http://www.openprocessing.org/sketch/43673)
void display(){
for(int i = 0; i<xpos.length; i++){
//draws an ellipse for each element in the arrays.
// colour and size are tied to the loops counter; i.
noStroke();
fill(98,227,106-i*5);
ellipse (xpos [i], ypos [i], i, i);//p.149
}
}
}
//controls:
void keyPressed()
{
if (key == CODED)
{
if (keyCode == SHIFT)
{
//restarts the game by pressing shift
restart();
}
}
}
void display()
{
//if snake eats the apple
if (xpos[1]==applex && ypos[1]==appley)
{
//snake grows and new apple is placed in a new location
s0 +=round(random(3)+1);
gamecontinues=true;
while(gamecontinues)
{
applex=(round(random(47))+1)*8;
appley=(round(random(47))+1)*8;
for(int i=1;i<snakesize;i++)
{
if (applex==xpos[i] && appley==ypos[i])
{
gamecontinues=true;
}
else
{
gamecontinues=false;
i=1000;
}
}
}
}
//new head is drawn
fill(0); //black fill
rect(xpos[1],ypos[1],8,8);
//the snake's tail is erased
fill(255);//white fill
rect(xpos[s0],ypos[s0],8,8);
}
void checkgameover()
{
for(int i=2; i<=snakesize; i++)//"Snake Game Explained" Youtube Tutorial
{
//if snake head and other part of the body collide
if (xpos[1]==xpos[i] && ypos[1]==ypos[i])
{
fill(255);
rect(125,125,160,100);
fill(0);
text("GAME OVER",207,150);
text("Score: "+str(snakesize-1)+" units long",207,175);
text("To restart, press Shift.",208,200);
gameover=true;
}
//if snake's head hits the walls
if (xpos[1]>=(width-8) || ypos[1]>=(height-8) || xpos[1]<=0 || ypos[1]<=0)
{
fill(255);
rect(125,125,160,100);//Draws a Box
fill(0);
text("GAME OVER",207,150);//Draws Text in a Box (Getting Started with Processing p.85)
text("Score: "+str(snakesize-1)+" units long",207,175);//p.85
text("To play again, press Shift.",208,200);//p.85
gameover=true;
}
}
}
void restart()
{
//pressing shift will reset all key variables to their defaults
background(255,229,188);
stroke(188,255,223);
strokeWeight(1.5);
gameover=false;
applex=(round(random(47))+1)*8;
appley=(round(random(47))+1)*8;
snakesize=10;
gamecontinues=true;
}
1