We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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.
Answers
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:
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
in google Enter:
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
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()
in setup()
this is somewhere down but must be called from
draw()
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[4];
andfloat snakeY[] = new snakeY[4];
Now we have a snake with four parts, but we need to set a start-location. We do that in setup() like this: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[0] and snakeY[0]) 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[0] = 370
. The new point we'll call dirX and we'll set it to 375. The distance between snakeX[0] and dirX can be calculated like this:float distX = snakeX[0] - dirX;
and if we then set snakeX[0]'s new location like this:snakeX[0] = snakeX[0] - 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: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:
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:
I know these are pretty radical changes, so I understand if you don't want to use them, but hopefully they have given you some new ideas or you've learned something new.