Game
in
Programming Questions
•
18 days ago
when I press p the game does not pause and I do not know how to start a pause game with the space bar?
how can i display the score at the end?
col=color(57,185,247)
color foodColor = color(240,109,227);
float speed = 50;
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 = 200;
boolean gameOver = false;
PFont Font = createFont("Times New Roman",20, true);
void setup(){
size(int(windowSize), int(windowSize),P3D);
background(0);
speed = 50;
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%10 == 0){
background(0);
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-5 || snakeX < 5||snakeY > windowSize-5||snakeY < 5){
gameOver = true;
}
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();}
if(keyCode == 'P') {pause();}
}
1