본문 바로가기
[ChatGPT] Sample Code 샘플 코드

[ChatGPT][Scala.js] Scala.js 2D 게임 프로그래밍 샘플 코드

by byungwoo733 2023. 1. 22.
728x90
반응형
SMALL

Scala.js 2D 게임 프로그래밍 샘플 코드

 

/*Here is an example of a simple 2D game program written in Scala.js */
import org.scalajs.dom.{document, window}
import scala.scalajs.js.annotation.JSExport
import scala.scalajs.js.timers._

@JSExport
object Game {
  val canvas = document.createElement("canvas")
  val ctx = canvas.getContext("2d")
  document.body.appendChild(canvas)

  var x = 0
  var y = 0

  @JSExport
  def main(): Unit = {
    canvas.width = window.innerWidth
    canvas.height = window.innerHeight

    setInterval(1000 / 60) {
      update()
      draw()
    }
  }

  def update(): Unit = {
    x += 1
    y += 1
  }

  def draw(): Unit = {
    ctx.clearRect(0, 0, canvas.width, canvas.height)
    ctx.fillRect(x, y, 50, 50)
  }
}
728x90
LIST

댓글