We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello guys! I have troubles with this code of Snake. I was trying to teleport the snake to the other side and actually, the code I wrote mostly works (line 114). The problem is once the snake goes through the wall and is teleported to the other side, it doesn't eat the food anymore. I'm a beginner so I don't have any clue of why. Any suggestion? :( Thanks in advance!
color col=color(0);
color foodColor = color(0,255,255);
float speed = 100;
int cx, cy;
int moveX = 0;
int moveY = 0;
int snakeX = 0;
int snakeY = 0;
int foodX = -1;
int foodY = -1;
boolean check = true;
int []snakesX;
int []snakesY;
int snakeSize = 1;
int windowSize = 600;
boolean gameOver = false;
PFont Font = createFont("Arial",20, true);
void setup(){
size(int(windowSize), int(windowSize),P3D);
background(255);
noStroke();
speed = 100;
speed=speed/frameRate;
snakesX = new int[100];
snakesY = new int[100];
cx = width/2;
cy = height/2;
snakeX = cx-5;
snakeY = cy-5;
foodX = -1;
foodY = -1;
gameOver = false;
check = true;
snakeSize =1;
}
void draw(){
if(speed%5 == 0){
background(255);
runGame();}
speed++;
}
void reset(){
snakeX = cx-5;
snakeY = cy-5;
gameOver = false;
check = true;
snakeSize =1;
moveY = 0;
moveX = 0;
}
void runGame(){
if(gameOver== false){
drawfood();
drawSnake();
snakeMove();
ateFood();
checkHitSelf();
}else{
String modelString = "game over";
textAlign (CENTER);
textFont(Font);
text(modelString,100,100,40);}
}
void checkHitSelf(){
for(int i = 1; i < snakeSize; i++){
if(snakeX == snakesX[i] && snakeY== snakesY[i]){
gameOver = true;}
}
}
void ateFood(){
if(foodX == snakeX && foodY == snakeY){
check = true;
snakeSize++;
}
}
void drawfood(){
fill(foodColor);
while(check){
int x = (int)random(1,windowSize/10);
int y = (int)random(1,windowSize/10);
foodX = 5+x*10;
foodY = 5+y*10;
for(int i = 0; i < snakeSize; i++){
if(x == snakesX[i] && y == snakesY[i]){
check = true;
i = snakeSize;
}else{
check = false; }}
}
rect(foodX-5, foodY-5, 10, 10);
}
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);
}
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){snakeX = 0;}
if (snakeX < 0){snakeX = windowSize;}
if (snakeY < 0){snakeY = windowSize;}
if (snakeY > windowSize){snakeY = 0;}
snakesX[0] = snakeX;
snakesY[0] = snakeY;
}
void keyPressed() {
if(keyCode == UP){
if(snakesY[1] != snakesY[0]-10){moveY = -10; moveX = 0;}}
if(keyCode == DOWN){
if(snakesY[1] != snakesY[0]+10){moveY = 10; moveX = 0;}}
if(keyCode == LEFT){
if(snakesX[1] != snakesX[0]-10){moveX = -10; moveY = 0;}}
if(keyCode == RIGHT){
if(snakesX[1] != snakesX[0]+10){moveX = 10; moveY = 0;}}
if(keyCode == 'R') {reset();}
}
Answers
For debugging, you can use the debugging features in the Processing IDE or use something like the following lines, which needs to be added in your keyPressed() function:
Where p is to pause/resume your game and c is for "checking". I notice your values are right even when you wrapped around the screen. Then I notice you have this:
snakeX
andsnakesX[]
I guess your bug is in drawfood(). Give it a try and see if this solves your problem.
Kf
Hey kfrajer, thank you for the tip but unfortunately, it didn't work :( I wrote the code again using another one as a reference. In this case, I'm not able to apply the same method for the teleport (line 48) and I don't really understand why. The terminal tells me "The left-hand side of an assignment must be a variable". Any idea? :(
Solved!