예제 #1
0
 // This method is called once a second, and it is a good place to
 // do things like check if the snake has collided into the wall
 // or the apple.
 public void onTimer() {
   if ((snake.hasHitTail() == false) && (snake.hasHitWall(gameBoard) == false)) {
     snake.move(gameBoard);
     if (snake.hasHitApple(apple)) {
       apple.setPos(gameBoard.getRandomXCoord(), gameBoard.getRandomYCoord());
       System.out.println("It hit the apple");
       scoreBoard.incrementPlayerScore();
     }
   } else gameOver = true;
 }
예제 #2
0
 // This method is called whenever a keyboard button is pressed
 // within your game. The keyCode represents the key the actual key pushed.
 // You can check which keyCode using 'KeyEvent' constants.
 // i.e keyCode == KeyEvent.VK_G
 protected void onKeyPress(int keyCode) {
   // Move Up
   if (keyCode == KeyEvent.VK_UP) {
     snake.setDirection("up");
   }
   // Move Right
   if (keyCode == KeyEvent.VK_RIGHT) {
     snake.setDirection("right");
   }
   // Move Down
   if (keyCode == KeyEvent.VK_DOWN) {
     snake.setDirection("down");
   }
   // Move Left
   if (keyCode == KeyEvent.VK_LEFT) {
     snake.setDirection("left");
   }
 }
예제 #3
0
 // This method is called once a second to redraw the canvas,
 // so you can do things like draw the snake.
 public void redrawCanvas(Graphics canvas) {
   gameBoard.draw(canvas);
   scoreBoard.drawScore(canvas, gameOver);
   snake.draw(canvas);
   apple.draw(canvas);
 }