Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package snake
- class Snake (
- val initPos: Pos,
- val initDir: Dir,
- val headColor: java.awt.Color,
- val tailColor: java.awt.Color,
- val game: SnakeGame
- ) extends CanMove {
- var dir: Dir = initDir
- val initBody: List[Pos] = List(initPos + initDir, initPos)
- val body: scala.collection.mutable.Buffer[Pos] = initBody.toBuffer
- val InitTailSize: Int = 10 // välj själv vad som är lagom svårt
- var tailsize = InitTailSize
- var prePos : Pos = Pos(0,0)
- var erasePrePos : Boolean = false
- var drawNewPos : Boolean = false
- // var nbrOfStepsSinceReset = 0
- // val growEvery = 10
- // val startGrowingAfter = 400
- // var nbrOfApples = 0
- def reset(game: BlockGame): Unit = { // återställ starttillstånd, ge rätt svanslängd
- for(i <- body.indices){
- game.drawBlock(body(i),snake.Colors.Background)
- }
- body = initBody
- }
- def grow(): Unit = tailSize += 1 // väx i rätt riktning med extra svansposition
- def shrink(): Unit = { // krymp svansen om kroppslängden är större än 2
- if(tailSize > 2) tailSize -= 1
- }
- def isOccupyingBlockAt(p: Pos): Boolean = { // kolla om p finns i kroppen
- return body.contains(p)
- }
- def isHeadCollision(other: Snake): Boolean = { // kolla om huvudena krockar
- dir match{
- case North => {
- return this.body(0)._1 == other.body(0)._1 && this.body(0)._2 - 1 == other.body(0)._2
- }
- case West => {
- return this.body(0)._1 + 1 == other.body(0)._1 && this.body(0)._2 == other.body(0)._2
- }
- case East => {
- return this.body(0)._1 - 1 == other.body(0)._1 && this.body(0)._2 == other.body(0)._2
- }
- case South => {
- return this.body(0)._1 == other.body(0)._1 && this.body(0)._2 + 1 == other.body(0)._2
- }
- case _ => return false
- }
- }
- def isTailCollision(other: Snake): Boolean = { // mitt huvud i annans svans
- dir match{
- case North => {
- for(i <- other.body.indices){
- if (this.body(0)._1 == other.body(i)._1 && this.body(0)._2 - 1 == other.body(i)._2) return true
- }
- }
- case West => {
- for(i <- other.body.indices){
- if (this.body(0)._1 + 1 == other.body(i)._1 && this.body(0)._2 == other.body(i)._2) return true
- }
- }
- case East => {
- for(i <- other.body.indices){
- if (this.body(0)._1 - 1 == other.body(i)._1 && this.body(0)._2 == other.body(i)._2) return true
- }
- }
- case South => {
- for(i <- other.body.indices){
- if (this.body(0)._1 == other.body(i)._1 && this.body(0)._2 + 1 == other.body(i)._2) return true
- }
- }
- case _ => return false
- }
- return false
- }
- def move(): Unit = { // väx och krymp enl. regler; action om äter frukt
- if(body.length == tailSize) {
- for(i => body.indices.reverse.dropRight){
- body(i - 1) = body(i)
- }
- drawNewPos = true
- body(0) = Pos(body(0)._1 + dir._1, body(0)._2 + dir._2)
- }
- else if(body.length > tailSize){
- prePos = body(body.length - 1)
- erasePrePos = true
- body.dropRight
- }
- else if(body.length < tailSize){
- drawNewPos = true
- body.append(Pos(body(0)._1 + dir._1, body(0)._2 + dir._2))
- }
- }
- def draw(game: BlockGame): Unit = {
- if(drawNewPos){
- drawNewPos = false
- game.drawBlock(body(0), headColor)
- game.drawBlock(body(1), blodyColor)
- }
- }
- def erase(game: BlockGame): Unit = {
- if(erasePrePos){
- erasePrePos = false
- game.drawBlock(prePos,snake.Colors.Background)
- }
- }
- override def toString = // bra vid println-debugging
- body.map(p => (p.x, p.y)).mkString(">:)", "~", s" going $dir")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement