Hello everyone,
I need some help with the code I am writing. this is a simple snake game but I don`t know how to make the snake eat the apples besides there is one more problem. whenever the snake moves from left to right it suddenly jumps.
import processing.core.*;
public class Problem04 extends PApplet {
float scale = height / 18f;
float foodX;
float foodY;
float circleX;
float circleY;
float circleR;
int score = 0;
String scoreTxt = "Score: " + score;
public void settings() {
fullScreen();
}
public void setup() {
scale = height / 18f;
foodX = width / 2f + width / 24f;
foodY = height / 5.5f;
circleX = width / 1.4f;
circleY = height / 2.4f;
circleR = scale;
frameRate(5);
}
public void draw() {
background(0, 0, 0);
fill(0, 0, 0, 10);
//snake
fill(225, 0, 0);
circle(circleX, circleY, circleR);
// texts
fill(0, 225, 0);
text("Game: Collect Yellow Circles Using Arow Button", width / 2f, height / 12f);
textSize(20);
textAlign(CENTER, CENTER);
text(scoreTxt, width / 2f, height / 1.1f);
// adding the table
for (float r = 0; r <= width / 1.4f; r += scale) {
for (float c = 0; c <= (height / 2f + height / 8f); c += scale) {
noFill();
stroke(0, 0, 225);
rect(r + width / 7f, c + height / 6f, scale, scale);
}
}
// key control
if (key == CODED) {
switch (keyCode) {
case UP:
circleY -= circleR;
if (circleY - circleR / 2 <= height / 6f) {
circleY = height / 5.1f;
}
break;
case DOWN:
circleY += circleR;
if (circleY >= (height / 2f) + (height / 8f)) {
circleY = (height / 2f) + (height / 3.3f);
}
break;
case RIGHT:
circleX += circleR;
if (circleX - circleR / 2 >= width / 1.4f) {
circleX = width / 1.15f - circleR / 2;
}
break;
case LEFT:
circleX -= circleR;
if (circleX <= width / 7f) {
circleX = width / 6.3f;
}
break;
}
}
float gridWidth = (int) (width / 7f) + 21 * scale;
float gridHeight = (int) (height / 6f) + 12 * scale;
float x = gridWidth / 2;
float y = gridHeight / 2;
fill(255, 255, 0);
circle(x, y, circleR);
}
public static void main(String[] args) {
PApplet.main("Problem04");
}
}