Hello, I am new to programming and I’m trying to create the snake game. I have been able to create the food and the snake with some help from google but I cant seem to get the snake to move. Any help or tips would be greatly appreciated. Here is my code so far. Thank you.
int direction=0;
int size=4;
int snakeX;
int snakeY;
int time=1;
float foodX =(round(random(50)+1)*10);
float foodY =(round(random(50)+1)*10);
int[] sHeadX= new int[2500];
int[] sHeadY= new int[2500];
void setup() {
size (500, 500);
}
void draw() {
time+=1;
background(255);
rect(foodX, foodY, 10, 10);
fill(0,0,0);
rect(snakeX, snakeY, 15, 15);
if ((time % 5)==0) //framerate
{
move();
display();
}
}
void keyPressed()
{
if (key == CODED)
{
if (keyCode == UP && direction!=270 && (sHeadY[1]-8)!=sHeadY[2])
{
direction=90;
}
if (keyCode == DOWN && direction!=90 && (sHeadY[1]+8)!=sHeadY[2])
{
direction=270;
}if (keyCode == LEFT && direction!=0 && (sHeadX[1]-8)!=sHeadX[2])
{
direction=180;
}if (keyCode == RIGHT && direction!=180 && (sHeadX[1]+8)!=sHeadX[2])
{
}
}
}
void move(){
for(int i=size;i>0;i--)
{
if (i!=1)
{
//shift back by 1 array
sHeadX[i]=sHeadX[i-1];
sHeadY[i]=sHeadY[i-1];
}
else
{
fill(0,0,0);
//set new spot for snake head
switch(direction)
{
case 0:
sHeadX[1]+=8;
break;
case 90:
sHeadY[1]-=8;
break;
case 180:
sHeadX[1]-=8;
break;
case 270:
sHeadY[1]+=8;
break;
}
}
}
}
void display()
{
if (sHeadX[1]==foodX && sHeadY[1]==foodY)
{
size+=round(random(3)+1);
{
foodX=(round(random(50)+1)*10);
foodY=(round(random(50)+1)*10);
for(int i=1;i<size;i++)
{
if (foodX==sHeadX[i] && foodY==sHeadY[i])
{
}
else
{
i=1000;
}
}
}
}
}
In draw(), you are drawing one rectangle for the snake, at the position (snakeX, snakeY).
The values for snakeX and snakeY are never assigned any values! Like, at all! And since these values never change (at all) the snake’s rectangle never moves (at all)!
I think you’ve bitten off too much at once. Forget about a snake for now, forget about using an array to track all the positions for it, and just work on getting a single square moving about.
Or go wild and draw all the segments of you snake using a for loop and the arrays you have that are storing the values for the actual snake’s position.
I also have some sample snake code myself, which I think is quite good, but doesn’t do snake-snake collision detecting. If you’re down with classes, this might be easier to use as a starting point:
// Useful arrays to decode/use direction.
int[] dx = {1, 0, -1, 0};
int[] dy = {0, 1, 0, -1};
int[] dk = { RIGHT, DOWN, LEFT, UP };
class Snake {
int x, y; // Current head position.
ArrayList<PVector> tail; // Other segments.
int d; // Direction of movement;
boolean isGrowing = false;
Snake() {
d = 0;
tail = new ArrayList();
}
void draw() {
fill(50,200,50);
stroke(150,200,50);
rect(x,y,20,20);
for( int i = 0; i < tail.size(); i++){
rect(tail.get(i).x,tail.get(i).y,20,20);
}
}
void update() {
tail.add( new PVector(x,y,d) );
if( isGrowing ){
isGrowing = false;
} else {
tail.remove(0);
}
x += 20*dx[d];
y += 20*dy[d];
x += width;
y += height;
x %= width;
y %= height;
}
void grow() {
isGrowing = true;
}
void onKeyPressed() {
for( int i = 0; i < dk.length; i++){
if( key == CODED && keyCode == dk[i] ){
d = i;
}
}
}
}
class Food {
int x, y;
Food() {
newPosition();
}
void draw() {
fill(255,0,0);
stroke(200,0,200);
ellipse(x+10,y+10,20,20);
}
boolean wasEaten(int ox, int oy) {
if ( x == ox && y == oy ) {
newPosition();
return true;
}
return false;
}
void newPosition() {
x = 20 * int(random(width/20));
y = 20 * int(random(height/20));
}
}
class Timer {
int time;
int duration;
Timer() {
duration = 100;
reset();
}
void reset() {
time = millis() + duration;
}
boolean alarm() {
if ( millis() > time ) {
time = millis() + duration;
return true;
}
return false;
}
}
// --- Main driving code follows. Notice how simple its logic is to understand!
Snake snake;
Food food;
Timer timer;
void setup() {
size(600, 400);
snake = new Snake();
food = new Food();
timer = new Timer();
}
void draw() {
background(0);
if ( timer.alarm() ) {
snake.update();
if ( food.wasEaten( snake.x, snake.y ) ) {
snake.grow();
}
}
food.draw();
snake.draw();
}
void keyPressed() {
snake.onKeyPressed();
}
Ok, I’ve started again and this is what I’ve come up with. I am still having some issues with getting it to move. TfGuy44 you said to forget about using arrays but I have tried searching and coming up with solutions but cant find any without using it. Please correct me if I’m wrong but I’m imagining the program to draw a new rectangle at a new position which gets added to the array which I am having trouble with. Then some code which essentially gets the position of the snake -1 to remove it.
I also keep getting this error
The method rect(float, float, float, float) in the type PApplet is not applicable for the arguments (float, float, float, float)
int width = 10;
int height = 10;
int size = 10;
float [] snakeX = new float [10];
float [] snakeY = new float [10];
int direction = 0;
void setup() {
size(400,400);
}
void draw(){
background(255);
fill(0);
rect(snakeX,snakeY,10,10);
}
void keyPressed()
{
if (key == CODED)
{
if (keyCode == UP && direction!=270 && (snakeY[1]-8)!=snakeY[2])
{
direction=90;
}
if (keyCode == DOWN && direction!=90 && (snakeY[1]+8)!=snakeY[2])
{
direction=270;
}if (keyCode == LEFT && direction!=0 && (snakeX[1]-8)!=snakeX[2])
{
direction=180;
}if (keyCode == RIGHT && direction!=180 && (snakeX[1]+8)!=snakeX[2])
{
}
}
}