Can you help with my snake game ?

I am new to processing and I'm making a snake game but I don't know where to start or go about getting the snake the eat the food and create the food in the first place. If you have an suggestions or code to offer it would be very helpful thank you.

final int titleScreen = 1;
final int game = 2;
final int gameOver = 3;
int rectX = 250;
int rectY = 250;
int screen;
int xspeed;
int yspeed;



void setup() {
  size(500, 500);
  background(255);
  smooth();

  screen = titleScreen;
  rectX = 250;
  rectY = 250;          
  xspeed=0;     
  yspeed=0;
}

void draw() {

  switch(screen) {

  case titleScreen:
    titleScreen();
    break;

  case game:
    gameScreen();
    break;

  case gameOver:
    gameOver();
    break;
  }
} 


void titleScreen() {
  background(0);
  smooth();

  fill(255);
  textSize(48);
  rect(135, 355, 227, 60);
  fill(0);
  textAlign(CENTER, TOP);
  text("Play Now!", 250, 355);

  fill(random(0, 255), random(0, 255), random(0, 255));
  textSize(93);
  text("SNAKE!", 250, 115);


  if (mousePressed == true) {
    if ((mouseX > 135)&&(mouseX < 362))
      if ((mouseY > 355)&&(mouseY < 413))
        screen=2;
  }
}

void gameScreen() {

  background(255);

  rect(0, 0, 5, 495);//Left
  rect(0, 0, 495, 5);//Top
  rect(0, 495, 500, 10);//Bottom
  rect(495, 0, 10, 500);//right

  rect(rectX, rectY, 10, 10);

  rectX = rectX +xspeed; 
  rectY = rectY +yspeed;






  if (( rectX == 0)||( rectX > 495)) {
    screen = 3;
  }
  if (( rectY == 0)||( rectY > 495)) {
    screen = 3;
  }
}
void reset() {

  rectX = 250;
  rectY = 250;          
  xspeed=0;     
  yspeed=0;
}
void gameOver() {
  background(0);
  fill(255);
  textSize(48);
  text(" Game over", 250, 150);
  rect(90, 400, 320, 45);
  textSize(32);
  fill(0);
  text(" Press To Play Again", 250, 400);
  if (mousePressed) {  
    reset();
    screen=1;
  }
}

void mousePressed() {
  print("x: " + mouseX + " y: "+ mouseY +"\n");
}
void keyPressed() { 

  if (keyCode==UP || key == 'w') 
  { 
    xspeed=0; 
    yspeed=-2;
  } else if (keyCode==DOWN || key =='s') 
  { 
    xspeed=0; 
    yspeed=2;
  } else if (keyCode==LEFT || key=='a') 
  { 
    xspeed=-2; 
    yspeed=0;
  } else if (keyCode==RIGHT || key =='d') 
  { 
    xspeed=2; 
    yspeed=0;
  }
} 
Tagged:

Answers

Sign In or Register to comment.