We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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;
}
}
Answers
Ok.
step 1: hit ctrl-t in processing for auto-format
Step 2: go back edit your post
Select entire code and leave empty line before and after and hit ctrl-o to format it as code
There, sorry first time using the forums on here
Now have a food position like
float foodX,foodY;
Use
random
to set both (see reference)Display a red
ellipse()
or animage
at this positionCheck snake head with
==
ordist()
against this fruitGoogle snake here or use the link in the tag
@Its_Stunz -- as @Chrisir suggests, for other examples see also: