package com.codegym.games.snake;
import com.codegym.engine.cell.*;
public class SnakeGame extends Game {
public static final int WIDTH = 15;
public static final int HEIGHT = 15;
private Snake snake ;
private int turnDelay;
@Override
public void onKeyPress(Key key){
switch (key){
case LEFT ://my mistake is that i used == to compare two object , i should
//use equals() instead !
if (this.snake.getDirection().equals(Direction.RIGHT)){
break;
}
snake.setDirection(Direction.LEFT);
break;
case RIGHT :
if (this.snake.getDirection().equals(Direction.LEFT)){
break;
}
snake.setDirection(Direction.RIGHT);
break;
case UP:
if (this.snake.getDirection().equals(Direction.DOWN)){
break;
}
snake.setDirection(Direction.UP);
break;
case DOWN:
if (this.snake.getDirection().equals(Direction.UP)){
break;
}
snake.setDirection(Direction.DOWN);
break;
}
}
@Override
public void initialize() {
setScreenSize(WIDTH, HEIGHT);
createGame();
}
private void createGame(){
turnDelay=300;
snake= new Snake(WIDTH/2,HEIGHT/2);//why should the snake be intialized
//before drawing the Scene ?
drawScene();
//Apple apple = new Apple(7,7);
//apple.draw(this);//this tarje3 3al class mta3 apple (Apple)
setTurnTimer(turnDelay);
}
private void drawScene(){
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j++) {
//setCellColor(i, j, Color.GRAY);replaced
setCellValueEx(i,j,Color.GRAY , "");//n7ot chaine vide fi kol cell??
}
}
snake.draw(this);//this here refers to the snake class(Snake)
}
//the requirements said : this method should be overriden but the compiler said
//there is no such function in super class
@Override
public void onTurn(int step){
//Everything that should happen in the game during one turn is described here
snake.move();
drawScene();// drawScene() after every move ?
}
}