#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Snake game questions

edited May 2016

What to do next?

``````int snakeX=370;
int snakeY=370;
int foodX=int(random(1,780));
int foodY=int(random(1,780));
int score=1;
boolean up=false;
boolean down=false;
boolean right=false;
boolean left=false;
void setup(){
size(800,800);
}
void draw(){
fill(0);
stroke(255);
rect(-1,-1,801,801);
rect(snakeX,snakeY,29,29);
fill(255);
stroke(255);
rect(foodX,foodY,19,19);
fill(0);
if (up==true && down==false && right==false && left==false){
snakeY-=1;
}
if (down==true && up==false && right==false && left==false){
snakeY+=1;
}
if (right==true && up==false && down==false && left==false){
snakeX+=1;
}
if (left==true && up==false && down==false && right==false){
snakeX-=1;
}
if (up==true){
if (snakeX<foodX+20 && snakeX+30>foodX+20 && snakeY==foodY+20 || snakeX<foodX && snakeX+30>foodX && snakeY==foodY+20){
foodX=int(random(1,780));
foodY=int(random(1,780));
score+=1;
}
if (down==true){
if (snakeX<foodX+20 && snakeX+30>foodX+20 && snakeY+30==foodY || snakeX<foodX && snakeX+30>foodX && snakeY+30==foodY){
foodX=int(random(1,780));
foodY=int(random(1,780));
score+=1;
}
}
if(up==true){
rect(snakeX,snakeY,30,score*30);
}
if(down==true){
rect(score*snakeX,snakeY,30,30);
}
}
}
void keyPressed()
{
if  (key == CODED)
{
if (keyCode == UP)
{
if(down==false){
left=false;
right=false;
up=true;
}
}
if (keyCode == DOWN)
{
if(up==false){
up=false;
left=false;
right=false;
down=true;
}
}
if (keyCode == RIGHT)
{
if(left==false){
up=false;
down=false;
left=false;
right=true;
}
}
if (keyCode == LEFT)
{
if(right==false){
up=false;
down=false;
right=false;
left=true;
}
}
}
}
``````

Ok, sorry, just realized it didn't paste the whole code, here it is.

Tagged:

• look, don't get frustrated

I think you need keyPressed to get key input cursor up down etc.

Look at reference for keyPressed and keyCode

then set your up and down etc. accordingly

now I see you are checking the snake head aginst the food. Good. But do it always not only when up is true. You could also use dist instead, See Reference.

Now when you ate, give foodX and y a new random pos. foodX=random(0,width); And say score++; declare score at the top

And you need to shorten your sknake, right? Not sure.... you could have all Segments in a array

• Maybe use background(0); at start of draw()

• So I used dist and put background at the beginning of draw(), here it is:

``````int snakeX=370;
int snakeY=370;
int foodX=int(random(1,780));
int foodY=int(random(1,780));
int score=1;
boolean up=false;
boolean down=false;
boolean right=false;
boolean left=false;
void setup(){
size(800,800);
}
void draw(){
background(0);
fill(0);
stroke(255);
rect(-1,-1,801,801);
rect(snakeX,snakeY,29,29);
fill(255);
stroke(255);
rect(foodX,foodY,19,19);
fill(0);
if (up==true && down==false && right==false && left==false){
snakeY-=1;
}
if (down==true && up==false && right==false && left==false){
snakeY+=1;
}
if (right==true && up==false && down==false && left==false){
snakeX+=1;
}
if (left==true && up==false && down==false && right==false){
snakeX-=1;
}
if(dist(snakeX+15,snakeY+30,foodX+10,foodY)==0 || dist(snakeX+15,snakeY,foodX+10,foodY+20)==0 || dist(snakeX,snakeY+15,foodX+20,foodY+10)==0 || dist(snakeX+30,snakeY+15,foodX,foodY+10)==0){
rect(snakeX,snakeY,30,score*30);
foodX=int(random(1,780));
foodY=int(random(1,780));
score+=1;
}
}
void keyPressed()
{
if  (key == CODED)
{
if (keyCode == UP)
{
if(down==false){
left=false;
right=false;
up=true;
}
}
if (keyCode == DOWN)
{
if(up==false){
up=false;
left=false;
right=false;
down=true;
}
}
if (keyCode == RIGHT)
{
if(left==false){
up=false;
down=false;
left=false;
right=true;
}
}
if (keyCode == LEFT)
{
if(right==false){
up=false;
down=false;
right=false;
left=true;
}
}
}
}
``````
• well done!

now in line 35 you can insert a line break after each || sign

line 15 to 17 not needed

does the snake grow? you need this

make sure it dies when hits wall or itself

• The snake growth is the hardest part, do you have any tips? You've been a real help.

• Google snake in the forum

snake site:forum.processing.org

Or so

I think I'd do it with an array but not sure

• Use text(score, 19,19); to display the score (fill() for its color)

you could also say lives and say lives--; when you hit a wall or itself

• You should use empty lines between the functions

And in other places

Ctrl-t makes auto-indents - good!

Also you can make functions and put lines from draw() into them

Eg. manageSnake() could be one function for the lines 23 to 34

• Ok this is still screwed up, for some reason it's not registering an "eat". Here's the newest code

``````int snakeX=370;
int snakeY=370;
int foodX=int(random(1,780));
int foodY=int(random(1,780));
int score=1;
boolean up=false;
boolean down=false;
boolean right=false;
boolean left=false;
void setup(){
size(800,800);
}
void draw(){
background(0);
rect(snakeX,snakeY,29,29);
fill(255);
stroke(255);
rect(foodX,foodY,19,19);
fill(0);
if (up==true && down==false && right==false && left==false){
snakeY-=1;
}
if (down==true && up==false && right==false && left==false){
snakeY+=1;
}
if (right==true && up==false && down==false && left==false){
snakeX+=1;
}
if (left==true && up==false && down==false && right==false){
snakeX-=1;
}
if(dist(snakeX+15,snakeY+30,foodX+10,foodY)==0 ||
dist(snakeX+15,snakeY,foodX+10,foodY+20)==0 ||
dist(snakeX,snakeY+15,foodX+20,foodY+10)==0 ||
dist(snakeX+30,snakeY+15,foodX,foodY+10)==0){
rect(snakeX,snakeY,30,score*30);
foodX=int(random(1,780));
foodY=int(random(1,780));
score+=1;
}
}
void keyPressed()
{
if  (key == CODED)
{
if (keyCode == UP)
{
if(down==false){
left=false;
right=false;
up=true;
}
}
if (keyCode == DOWN)
{
if(up==false){
up=false;
left=false;
right=false;
down=true;
}
}
if (keyCode == RIGHT)
{
if(left==false){
up=false;
down=false;
left=false;
right=true;
}
}
if (keyCode == LEFT)
{
if(right==false){
up=false;
down=false;
right=false;
left=true;
}
}
}
}
``````
• use dist...... with `< 20`

• I looked up how to do that the snake gets longer

you can use 2 arrays (x and y position for each segment) which holds x and y position and you append one new segment (in x and y) and you copy / shift all elements one step forward

before setup()

``````int []snakesX;
int []snakesY;
int snakeSize = 1;
``````

in setup()

``````  snakesX = new int;
snakesY = new int;
``````

this is somewhere down but must be called from `draw()`

``````void drawSnake() {
fill(col);

for (int i = 0; i < snakeSize; i++) {
int X = snakesX[i];
int Y = snakesY[i];
rect(X-5, Y-5, 10, 10);
}
if (!paused) {
for (int i = snakeSize; i > 0; i--) {
snakesX[i] = snakesX[i-1];
snakesY[i] = snakesY[i-1];
}
}
}

void snakeMove() {
snakeX += moveX;
snakeY += moveY;
if (snakeX > windowSize-5 || snakeX < 5||
snakeY > windowSize-5||snakeY < 5) {
gameOver = true;
}
snakesX = snakeX;
snakesY = snakeY;
}
``````
• Did you figure it out? Do you need more help?

I think that if you focus on one thing at a time you'll be able to make sure one part of the game works before you add another. So make sure your core function of the game works (moving around the snake).

The way you are moving your snake around now is by changing the coordinates of the snake. But if you do it like that it will look a bit weird when you make a turn. So what you need is for the snake to a chain. And each part of the chain is headed towards the part in front of it. And when we control the snake, we just control the head.

So let's make snakeX and snakeY into two arrays. Before setup() we say: `float snakeX[] = new snakeX;` and `float snakeY[] = new snakeY;` Now we have a snake with four parts, but we need to set a start-location. We do that in setup() like this:

``````snakeX = 370;
snakeY = 370;
for(int i = 1; i<snakeX.length;i++){ //snakeX and snakeY are the same length.
snakeX[i] = snakeX;  //Because each part is following the next, the snake will
snakeY[i] = snakeY; //unfold as soon as it starts moving.
}
``````

Now we'll start drawing the snake, but in order to keep things nice and ordered I think it's best to move the snake-drawing out of draw and in to a new function that we can call.. snake()? In snake() we will draw a rect at each coordinate and we'll make the snake move. The first thing we'll move is the head of the snake (snakeX and snakeY) towards a new point. And we never want the snake to reach that point. We'll move it around forever. or until the snake dies.

So `snakeX = 370`. The new point we'll call dirX and we'll set it to 375. The distance between snakeX and dirX can be calculated like this: `float distX = snakeX - dirX;` and if we then set snakeX's new location like this: `snakeX = snakeX - distX;`, we'll appear at the new point. But we don't want to appear at the new point, we want to move towards it. So we'll say it like this:

``````float distX = snakeX - dirX;
snakeX = snakeX - distX/5;
``````

Now that we are moving toward the new point we need to make sure the rest of the body is following. We can use a for-loop and do it like this:

``````for (int i=0; i<bodyX.length; i++) {
float distX, distY;
if (i==0) {
distX = snakeX - dirX;
distY = snakeY - dirY;
} else {
distX = snakeX[i] - snakeX[i-1];
distY = snakeY[i] - snakeY[i-1];
}
snakeX[i] = snakeX[i] - distX/5;
snakeY[i] = snakeY[i] - distY/5;
ellipse(bodyX[i], bodyY[i], 10, 10);
}
``````

Now I would make a new variable called addX and addY and then add them to the dirX and dirY in snake(). If you want to change the direction of the snake you simply change addX and addY.

Here is what I have done for you so far:

``````float [] snakeX = new float ;
float [] snakeY = new float ;
float dirX = 370, dirY = 370, addX = 1, addY =0;
boolean dirX = true, dirY = false;
void setup() {
size(800, 600);
snakeX = dirX;
snakeY = dirY;
for (int i = 1; i<snakeX.length; i++) { //snakeX and snakeY are the same length.
snakeX[i] = snakeX;  //Because each part is following the next, the snake will
snakeY[i] = snakeY; //unfold as soon as it starts moving.
}
}

void draw() {
background(100);
snake();
}

void snake() {
fill(0);
for (int i=0; i<snakeX.length; i++) {
ellipse(snakeX[i], snakeY[i], 10, 10);
float distX, distY;
if (i==0) {
distX = snakeX - dirX;
distY = snakeY - dirY;
} else {
distX = snakeX[i] - snakeX[i-1];
distY = snakeY[i] - snakeY[i-1];
}
snakeX[i] = snakeX[i] - distX/5;
snakeY[i] = snakeY[i] - distY/5;
//println(snakeX[i]);
}