Advertisement
Guest User

Banana.scala

a guest
Nov 15th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.37 KB | None | 0 0
  1. package snake
  2.  
  3. case class Banana(
  4.             val game: SnakeGame
  5.  
  6.             ) extends CanTeleport{
  7.  
  8.  
  9.   nbrOfStepsSinceLastTeleport = 0
  10.   var vertical = false
  11.  
  12.   val teleportAfterSteps: Int = 250
  13.  
  14.   override def update(): Unit = {
  15.     nbrOfStepsSinceLastTeleport += 1
  16.     if (nbrOfStepsSinceLastTeleport > teleportAfterSteps) reset()
  17.   }
  18.  
  19.   override def reset(): Unit = {
  20.     erase()
  21.     nbrOfStepsSinceLastTeleport = 0
  22.     pos = game.randomFreePos()
  23.     vertical = scala.util.Random.nextBoolean()
  24.     while(pos.x == 0 || pos.x == pos.dim.x - 1 || pos.y == 0 || pos.y == pos.dim.y - 1) {
  25.       pos = game.randomFreePos()
  26.     }
  27.     draw()
  28.  
  29.   }
  30.   def draw(): Unit = {
  31.     if (vertical) {
  32.       for(i <- -1 to 1) {
  33.         game.drawBlock(pos.x, pos.y + i, Colors.Banana)
  34.       }
  35.     }
  36.     else for(i <- -1 to 1) {
  37.       game.drawBlock(pos.x + i, pos.y, Colors.Banana)
  38.     }
  39.  
  40.  
  41.   }
  42.   def erase(): Unit = {
  43.     if (vertical) {
  44.       for(i <- -1 to 1){
  45.         game.eraseBlock(pos.x, pos.y + i)
  46.       }
  47.     }
  48.     else for(i <- -1 to 1){
  49.       game.eraseBlock(pos.x + i, pos.y)
  50.     }
  51.   }
  52.  
  53.   override def isOccupyingBlockAt(p: Pos): Boolean = {
  54.     p == pos
  55.     if(vertical) {
  56.       p==pos || p==pos + Pos(0, 1, pos.dim) || p==pos + Pos(0, -1, pos.dim )
  57.     }
  58.     else p==pos || p==pos + Pos(1, 0, pos.dim) || p==pos + Pos(-1, 0, pos.dim )
  59.   }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement