Advertisement
Guest User

Snake.scala

a guest
Nov 15th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.46 KB | None | 0 0
  1. package snake
  2.  
  3. class Snake (
  4.   val initPos: Pos,
  5.   val initDir: Dir,
  6.   val headColor: java.awt.Color,
  7.   val tailColor: java.awt.Color,
  8.   val game: SnakeGame
  9. ) extends CanMove {
  10.   var dir: Dir = initDir
  11.  
  12.   val initBody: List[Pos] = List(initPos + initDir, initPos)
  13.  
  14.   val body: scala.collection.mutable.Buffer[Pos] = initBody.toBuffer
  15.  
  16.   val initTailSize: Int = 10 // välj själv vad som är lagom svårt
  17.  
  18.    var nbrOfStepsSinceReset = 0
  19.    val growEvery = 50
  20.    val startGrowingAfter = 20
  21.    var nbrOfApples = 0
  22.    var tailSize: Int = initTailSize
  23.  
  24.   def reset(): Unit = {
  25.     body.remove(0, body.length)
  26.     initBody.indices.foreach(i => body.append(initBody(i)))
  27.     tailSize = initTailSize
  28.     dir = initDir
  29.  
  30.   }
  31.        // återställ starttillstånd, ge rätt svanslängd
  32.  
  33.   def grow(): Unit = {    //koden är skriven som så att huvudet ligger vid index (0)!
  34.     val nextPos = body.head + dir
  35.     body.prepend(nextPos)
  36.  
  37.   }    // väx i rätt riktning med extra svansposition
  38.  
  39.   def shrink(): Unit = {
  40.     if (body.length > tailSize) body.remove(body.length - 1)
  41.   } // krymp svansen om kroppslängden är större än 2
  42.  
  43.   def isOccupyingBlockAt(p: Pos): Boolean = {
  44.      body.contains(p)
  45.   }    // kolla om p finns i kroppen
  46.  
  47.   def isHeadCollision(other: Snake): Boolean = {
  48.     body.head == other.body.head
  49.   } // kolla om huvudena krockar
  50.  
  51.   def isTailCollision(other: Snake): Boolean = {
  52.     if (other.body.tail.contains(body.head)) true
  53.     else false
  54.   } // mitt huvud i annans svans
  55.  
  56.   def move(): Unit = {
  57.     val nextPos = body.head + dir
  58.     val growagain = nbrOfStepsSinceReset%growEvery
  59.  
  60.     // if (ÄPPLEPOSITION.isOccupyingBlockAt(nextPos)) grow()
  61.     for (e <- game.entities) {
  62.       if (e.isOccupyingBlockAt(nextPos)) {
  63.         e match {
  64.           case Banana(_) => {tailSize+=2; e.reset();}
  65.           case _ =>
  66.         }
  67.       }
  68.     }
  69.     if (growagain == 0 && nbrOfStepsSinceReset > startGrowingAfter) tailSize += 1
  70.     shrink()
  71.     grow()
  72.     nbrOfStepsSinceReset += 1
  73.  
  74.   } // väx och krymp enl. regler; action om äter frukt
  75.  
  76.   def draw(): Unit = {
  77.     body.tail.foreach(pos => game.drawBlock(pos.x, pos.y, tailColor))
  78.     game.drawBlock(body.head.x, body.head.y, headColor)
  79.   }
  80.  
  81.   def erase(): Unit = {
  82.     body.foreach(pos => game.drawBlock(pos.x, pos.y, game.background))
  83.   }
  84.  
  85.   override def toString =  // bra vid println-debugging
  86.     body.map(p => (p.x, p.y)).mkString(">:)", "~", s" going $dir")
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement