Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scalafx.Includes._
- import scalafx.application.JFXApp
- import scalafx.scene.Scene
- import scalafx.scene.canvas._
- import scalafx.event.ActionEvent
- import scalafx.scene.control._
- import scalafx.scene.input._
- import scalafx.scene.image._
- import scalafx.scene.paint._
- import scalafx.animation._
- import scalafx.scene.shape._
- import scalafx.scene.image._
- import scalafx.scene.layout._
- import scalafx.scene.Group
- import io.Source
- import java.io.{PrintWriter, FileNotFoundException}
- var scores = try{
- val scoreSource = Source.fromFile("snakeScores.txt")
- val scoreLines = scoreSource.getLines
- val scores = scoreLines.map(score => HighScore(score.substring(0,score.indexOf(",")).toInt, score.substring(score.indexOf(",")+1))).toArray
- scoreSource.close
- scores
- } catch {
- case e:FileNotFoundException => Array.fill(10)(HighScore(0, ""))
- }
- val sWidth = 600
- val sHeight = 600
- var gameOver = false
- var direction = "up"
- case class HighScore(score:Int, name:String)
- var currentScore = 0
- val app = new JFXApp {
- stage = new JFXApp.PrimaryStage {
- title = "*+:。.。* Snake *。.。:+*"
- scene = new Scene(sWidth, sHeight) {
- val bg = new ImageView("https://i.imgur.com/F2wDuiV.png")
- val nameField = new TextField
- nameField.layoutX = 150; nameField.layoutY = 400
- nameField.promptText = "Enter your name!"
- nameField.visible = false
- //snakeHead WxL = 17x17
- //val snakeHead = Rectangle(17,17)
- //snakeHead.fill = Color.rgb(87,81,77)
- val snakeHead = new ImageView("https://i.imgur.com/yYqa1Mk.png")
- snakeHead.layoutX = 300
- snakeHead.layoutY = 300
- var snakeTail = List[ImageView](new ImageView("https://i.imgur.com/yYqa1Mk.png"))
- snakeTail.foreach(img => img.visible = false)
- val gameOverImg = new ImageView("https://i.imgur.com/kCfn9EY.png")
- gameOverImg.visible = false
- //initial score label
- val scoreLabel = new Label(currentScore.toString)
- scoreLabel.textFill = Color.rgb(87,81,77)
- scoreLabel.style = "-fx-font-size: 23pt"
- scoreLabel.layoutX = 18
- scoreLabel.layoutY = 3
- def overlaps(square1:ImageView, square2:ImageView): Boolean = {
- val dx = square1.layoutX.value-square2.layoutX.value
- val dy = square1.layoutY.value-square2.layoutY.value
- dx*dx+dy*dy <= (17+17)*(17+17)
- }
- //moves "forward" based on direction
- def move(dir:String, u:ImageView):Unit = {
- direction = dir
- if (dir == "up") u.layoutY = u.layoutY.value - 17
- else if (dir == "down") u.layoutY = u.layoutY.value + 17
- else if (dir == "left") u.layoutX = u.layoutX.value - 17
- else if (dir == "right") u.layoutX = u.layoutX.value + 17
- }
- //pear WxL = 14x17
- val pear = new ImageView("https://i.imgur.com/liCzXUT.png")
- //val pear = Rectangle(15, 15); pear.fill = Color.Green
- pear.layoutX = 50 + scala.util.Random.nextInt(500)
- pear.layoutY = 50 + scala.util.Random.nextInt(500)
- val moveInterval = 0.1
- var delaySum = 0.0
- var lastTime = 0L
- val snakeSpeed = 9.0
- val timer = AnimationTimer(t => {
- val delta = (t - lastTime)/1e9
- if (lastTime > 0) {
- delaySum += delta
- //movement:
- if (!gameOver && delaySum > moveInterval){
- onKeyPressed = (ke: KeyEvent) => {
- ke.code match {
- case KeyCode.Up => if (direction != "up" && direction != "down") move("up", snakeHead)
- case KeyCode.Down => if (direction != "down" && direction != "up") move("down", snakeHead)
- case KeyCode.Left => if (direction != "left" && direction != "right") move("left", snakeHead)
- case KeyCode.Right => if (direction != "right" && direction != "left") move("right", snakeHead)
- case _ =>
- }
- }
- val newTail = new ImageView("https://i.imgur.com/yYqa1Mk.png")
- newTail.layoutX = snakeHead.layoutX.value
- newTail.layoutY = snakeHead.layoutY.value
- content += newTail
- content -= snakeTail.last
- snakeTail = newTail :: snakeTail.init
- move(direction, snakeHead)
- delaySum = 0.0
- }
- }
- lastTime = t
- //check for intersection:
- //if (!Shape.intersect(snakeHead, pear).boundsInLocal.value.isEmpty){
- if(overlaps(snakeHead, pear)){
- snakeTail.foreach(img => img.visible = true)
- //add to the tail/body
- val newTail = new ImageView("https://i.imgur.com/yYqa1Mk.png")
- newTail.layoutX = snakeHead.layoutX.value
- newTail.layoutY = snakeHead.layoutY.value
- content += newTail
- snakeTail = newTail :: snakeTail
- //spawn a new pear
- pear.layoutX = 50 + scala.util.Random.nextInt(550)
- pear.layoutY = 50 + scala.util.Random.nextInt(550)
- //update score if you intersect
- currentScore += 100
- scoreLabel.text.update(currentScore.toString)
- }
- for (i <- snakeTail.indices){
- if (overlaps(snakeHead, snakeTail(i))) gameOver = true
- }
- //ends game if you run into the edge of the window
- if (snakeHead.layoutX.value >= 600-17 || snakeHead.layoutX.value <= 0 || snakeHead.layoutY.value <= 0 || snakeHead.layoutY.value >= 600-17) gameOver = true
- if (gameOver){
- gameOverImg.visible = true
- nameField.visible = true
- scores ++= Array(HighScore(currentScore, nameField.text.value))
- //sorting the scores
- def insertionSort(a:Array[HighScore]):Unit = {
- for (i <- 1 until a.length){
- var j = i - 1
- var temp = a(i)
- while( j >= 0 && temp.score < a(j).score){
- a(j+1) = a(j)
- j -= 1
- }
- a(j+1) = temp
- }
- }
- insertionSort(scores)
- scores = scores.reverse
- //saving the top 10 scores to "snakeScores.txt"
- val PW = new PrintWriter("snakeScores.txt")
- if (scores.length > 10){
- for (i <- 0 to 9) PW.println(scores(i).score + "," + scores(i).name)
- }
- else{
- for (i <- scores.indices) PW.println(scores(i).score + "," + scores(i).name)
- for (i <- 1 to 10 - scores.length) PW.println("0,NONE")
- }
- PW.close
- //resets the game (if you press Enter)
- onKeyPressed = (ke: KeyEvent) => {
- if (ke.code == KeyCode.Enter){
- gameOver = false
- gameOverImg.visible = false
- nameField.visible = false
- currentScore = 0
- scoreLabel.text.update(currentScore.toString)
- snakeHead.layoutX = 300
- snakeHead.layoutY = 300
- snakeTail = List[ImageView](new ImageView("https://i.imgur.com/yYqa1Mk.png")); snakeTail.foreach(img => img.visible = false)
- direction = "up"
- }
- }
- }
- })
- timer.start
- content ++= List(bg, snakeHead, scoreLabel, pear, nameField, gameOverImg)
- content ++= snakeTail.map(_.delegate)
- fill = Color.rgb(209, 237, 255)
- }
- }
- }
- app.main(args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement