Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.62 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 tailsize = InitTailSize
  19.   var prePos : Pos = Pos(0,0)
  20.   var erasePrePos : Boolean = false
  21.   var drawNewPos : Boolean = false
  22.  
  23.   // var nbrOfStepsSinceReset = 0
  24.   // val growEvery = 10
  25.   // val startGrowingAfter = 400
  26.   // var nbrOfApples = 0
  27.  
  28.   def reset(game: BlockGame): Unit = {  // återställ starttillstånd, ge rätt svanslängd
  29.     for(i <- body.indices){
  30.       game.drawBlock(body(i),snake.Colors.Background)
  31.     }
  32.     body = initBody
  33. }
  34.  
  35.   def grow(): Unit = tailSize += 1   // väx i rätt riktning med extra svansposition
  36.  
  37.   def shrink(): Unit = { // krymp svansen om kroppslängden är större än 2
  38.     if(tailSize > 2) tailSize -= 1
  39. }
  40.  
  41.   def isOccupyingBlockAt(p: Pos): Boolean = {    // kolla om p finns i kroppen
  42.     return body.contains(p)
  43. }
  44.  
  45.   def isHeadCollision(other: Snake): Boolean = { // kolla om huvudena krockar
  46.     dir match{
  47.       case North => {
  48.     return this.body(0)._1 == other.body(0)._1 && this.body(0)._2 - 1 == other.body(0)._2
  49.       }
  50.       case West  => {
  51.     return this.body(0)._1 + 1 == other.body(0)._1 && this.body(0)._2 == other.body(0)._2
  52.       }
  53.       case East  => {
  54.     return this.body(0)._1 - 1 == other.body(0)._1 && this.body(0)._2 == other.body(0)._2
  55.       }
  56.       case South => {
  57.     return this.body(0)._1 == other.body(0)._1 && this.body(0)._2 + 1 == other.body(0)._2
  58.       }
  59.       case _     => return false
  60.     }
  61. }
  62.   def isTailCollision(other: Snake): Boolean = { // mitt huvud i annans svans
  63.     dir match{
  64.       case North => {
  65.     for(i <- other.body.indices){
  66.       if (this.body(0)._1 == other.body(i)._1 && this.body(0)._2 - 1 == other.body(i)._2) return true
  67.     }
  68.       }
  69.       case West  => {
  70.     for(i <- other.body.indices){
  71.       if (this.body(0)._1 + 1 == other.body(i)._1 && this.body(0)._2 == other.body(i)._2) return true
  72.     }
  73.       }
  74.       case East  => {
  75.     for(i <- other.body.indices){
  76.       if (this.body(0)._1 - 1 == other.body(i)._1 && this.body(0)._2 == other.body(i)._2) return true
  77.     }
  78.       }
  79.       case South => {
  80.     for(i <- other.body.indices){
  81.       if (this.body(0)._1 == other.body(i)._1 && this.body(0)._2 + 1 == other.body(i)._2) return true
  82.     }
  83.       }
  84.       case _     => return false
  85.     }
  86.     return false
  87.   }
  88.  
  89.   def move(): Unit = { // väx och krymp enl. regler; action om äter frukt
  90.  
  91.     if(body.length == tailSize) {
  92.       for(i => body.indices.reverse.dropRight){
  93.         body(i - 1) = body(i)
  94.       }
  95.       drawNewPos = true
  96.       body(0) = Pos(body(0)._1 + dir._1, body(0)._2 + dir._2)
  97.     }
  98.     else if(body.length > tailSize){
  99.       prePos = body(body.length - 1)
  100.       erasePrePos = true
  101.       body.dropRight
  102.     }
  103.     else if(body.length < tailSize){
  104.       drawNewPos = true
  105.       body.append(Pos(body(0)._1 + dir._1, body(0)._2 + dir._2))
  106.     }
  107. }
  108.  
  109.   def draw(game: BlockGame): Unit = {
  110.     if(drawNewPos){
  111.       drawNewPos = false
  112.       game.drawBlock(body(0), headColor)
  113.       game.drawBlock(body(1), blodyColor)
  114.     }
  115.   }
  116.  
  117.   def erase(game: BlockGame): Unit = {
  118.     if(erasePrePos){
  119.       erasePrePos = false
  120.       game.drawBlock(prePos,snake.Colors.Background)
  121.     }
  122.   }
  123.  
  124.   override def toString =  // bra vid println-debugging
  125.     body.map(p => (p.x, p.y)).mkString(">:)", "~", s" going $dir")
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement