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