Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package snake
- case class Banana(
- val game: SnakeGame
- ) extends CanTeleport{
- nbrOfStepsSinceLastTeleport = 0
- var vertical = false
- val teleportAfterSteps: Int = 250
- override def update(): Unit = {
- nbrOfStepsSinceLastTeleport += 1
- if (nbrOfStepsSinceLastTeleport > teleportAfterSteps) reset()
- }
- override def reset(): Unit = {
- erase()
- nbrOfStepsSinceLastTeleport = 0
- pos = game.randomFreePos()
- vertical = scala.util.Random.nextBoolean()
- while(pos.x == 0 || pos.x == pos.dim.x - 1 || pos.y == 0 || pos.y == pos.dim.y - 1) {
- pos = game.randomFreePos()
- }
- draw()
- }
- def draw(): Unit = {
- if (vertical) {
- for(i <- -1 to 1) {
- game.drawBlock(pos.x, pos.y + i, Colors.Banana)
- }
- }
- else for(i <- -1 to 1) {
- game.drawBlock(pos.x + i, pos.y, Colors.Banana)
- }
- }
- def erase(): Unit = {
- if (vertical) {
- for(i <- -1 to 1){
- game.eraseBlock(pos.x, pos.y + i)
- }
- }
- else for(i <- -1 to 1){
- game.eraseBlock(pos.x + i, pos.y)
- }
- }
- override def isOccupyingBlockAt(p: Pos): Boolean = {
- p == pos
- if(vertical) {
- p==pos || p==pos + Pos(0, 1, pos.dim) || p==pos + Pos(0, -1, pos.dim )
- }
- else p==pos || p==pos + Pos(1, 0, pos.dim) || p==pos + Pos(-1, 0, pos.dim )
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement