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:
snakeXandsnakesX[]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? :(
ArrayList x = new ArrayList(), y = new ArrayList(); int w = 24, h = 24, bs = 25, dir = 2, foodx = 12, foody = 10; int[] dx = {0,0,1,-1}, dy = {1,-1,0,0}; boolean gameover = false; int highScore; // millis keys lock boolean keys_broken; int time_to_toggle_keys_broken; void setup() { size (600,600); //size(w*bs,h*bs); x.add(5); y.add(5); keys_broken = false; time_to_toggle_keys_broken = millis() + int(random(20000, 3000)); }void draw() { background(255); if(millis() > time_to_toggle_keys_broken){ keys_broken = !keys_broken; time_to_toggle_keys_broken = millis() + int(random(20000, 3000)); } for(int i = 0; i < w; i++) line(i*bs, 0, i*bs, height); for(int i = 0; i < h; i++) line(0, i*bs, width, i*bs); for(int i = 0; i < x.size(); i++){ fill(0); noStroke(); rect(x.get(i)*bs, y.get(i)*bs, bs, bs); } if (!gameover) { fill(255,0,0); rect(foodx*bs, foody*bs, bs, bs); if(frameCount%8==0) { x.add(0,x.get(0) + dx[dir]); y.add(0,y.get(0) + dy[dir]); //if(x.get(0) < 0 || y.get(0) < 0 || x.get(0) >= w || y.get(0) >= h) gameover = true; if(x.get(0) < 0) { x.get(0) = w;} if(y.get(0) < 0) { y.get(0) = h;} if(x.get(0) >= w) { x.get(0) = 0;} if(y.get(0) >= h) { y.get(0) = 0;} for(int i = 1; i < x.size(); i++) if (x.get(0)==x.get(i) && y.get(0) == y.get(i)) gameover = true; if(x.get(0)==foodx && y.get(0)==foody) { foodx = (int)random(0,w); foody = (int)random(0,h); }else{ x.remove(x.size()-1); y.remove(y.size()-1); } } if (x.size() > highScore) // If there's a new highscore highScore = x.size(); // Change the highscore to the new record fill(0); textSize(18); text("Score " + x.size()*10 + "\nHighscore 680", 20,20); } else { fill(0); textSize(30); textAlign(CENTER); text("GAME OVER" + "\nPRESS SPACE", width/2, height/2); if(keyPressed && key==' ') { x.clear(); y.clear(); x.add(5); y.add(5); gameover = false; } } } void keyPressed() { if (keys_broken) {return; } int newdir = keyCode==DOWN ? 0 : (keyCode==UP ? 1 : (keyCode==RIGHT ? 2 : (keyCode==LEFT ? 3 : -1))); if(newdir != -1 && (x.size()
Solved!