We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have done the part which controls the snake to move, but I am still trying to figure out a way to duplicate the snake's tail after eating the apple. Here below is my code. Looking out for any suggestion and inspiration. Thanks a lot.
int x;
int y;
int a;
int b;
int a2;
int b2;
int aa,bb;
float dis;
int xpos=200;
int ypos=300;
int applex=int(random(1, 20))*20;
int appley=int(random(1, 30))*20;
void setup() {
size(600, 600);
smooth();
}
void draw() {
background(225);
//drawlines();
drawsnake();
eatfood();
drawfood();
}
void drawlines() {
fill(0);
stroke(0);
x+=20;
y+=20;
if (x<=600) {
line(x, 0, x, 600);
}
if (y<=600) {
line(0, y, 600, y);
}
}
void keyPressed() {
if (keyCode == LEFT) {
a=-5;
b=0;
a2=28;
b2=0;
} else if (keyCode == RIGHT) {
a=5;
b=0;
a2=-28;
b2=0;
} else if (keyCode == UP) {
a=0;
b=-5;
b2=28;
a2=0;
} else if (keyCode == DOWN) {
a=0;
b=5;
b2=-28;
a2=0;
}
}
void drawsnake() {
fill(0);
rect(xpos, ypos, 20, 20);
xpos+=a;
if (xpos>600) {
xpos=0;
}
if (xpos<0) {
xpos=600;
}
ypos+=b;
if (ypos>600) {
ypos=0;
}
if (ypos<0) {
ypos=600;
}
}
void drawfood() {
fill(0, 225, 225);
rect(applex, appley, 20, 20);
}
void eatfood(){
dis=dist(xpos,ypos,applex,appley);
if (dis<15){
applex=int(random(1, 20))*20;
appley=int(random(1, 30))*20;
clonesnake();
}
}
void clonesnake(){
fill(0);
rect(xpos+a2, ypos+b2, 20, 20);
}
Answers
Format your code -
Press the gear icon to edit your post.
Select your code, ctrl + o to indent, leave a line above and below.
what should i do if I use a mac.
no duplicate threads please
first in processing hit ctrl-t
then copy paste to forum
in forum:
then Select your code, press with mouse the C icon in the small command bar to indent, leave a line above and below the code
Read this page.
ctrl + o should work on Mac also (latest OS version).
afaik the key ctrl on mac is denoted as CMD
I just read this post - https://forum.processing.org/two/discussion/19520/update-the-readme-post#latest
the format question is solved, anyone can help me with the actual problem?
Did you read the Objects tutorial?
And how about doing a Google search on how to make a snake game in Processing?
Those online versions are too complicated for a bigginer
https://forum.processing.org/one/topic/the-snake-game-help.html
I there a solution on the code I am working on to make the tails?
is there a way to just add functions to my code in order to complete a snake game?
Yes.
yes