Code Sketch
sanavi dino game
Category: Programming
// 1. ??? ??????? ??? ???????????? ?????????? ?????
cleari()
// ????? ????? ????????? ?????????? (Sky Gradient Backdrop)
setBackground(cm.linearGradient(0, -200, Color(135, 206, 250), 0, 200, Color(245, 245, 250), false))
// ??????? ????? ???? (Styled Ground)
val groundY: Double = -100.0
val groundLine = Picture.rectangle(620, 10)
groundLine.setFillColor(Color(34, 139, 34)) // ????? ???? ?????
groundLine.setPenColor(noColor)
groundLine.setPosition(-310.0, groundY - 5.0)
draw(groundLine)
// 2. ???????????? ???????? ????????? (????? ??? ??)
val sun = Picture.circle(25)
sun.setFillColor(Color(255, 215, 0)) // ?????? ?????? ?????
sun.setPenColor(noColor)
sun.setPosition(180.0, 130.0)
draw(sun)
val cloud1 = Picture.circle(20)
cloud1.setFillColor(Color(255, 255, 255, 200)) // ???????? ?????? ??
cloud1.setPenColor(noColor)
cloud1.setPosition(-120.0, 100.0)
draw(cloud1)
val cloud2 = Picture.circle(15)
cloud2.setFillColor(Color(255, 255, 255, 200))
cloud2.setPenColor(noColor)
cloud2.setPosition(-100.0, 105.0)
draw(cloud2)
// 3. ????? ????? ??????????
// ????? ???? (Dino)
val dinoX: Double = -150.0
var dinoY: Double = groundY
val dino = Picture.rectangle(40, 40)
dino.setFillColor(Color(46, 139, 87)) // ??-????? ????
dino.setPenColor(noColor)
dino.setPosition(dinoX, dinoY)
draw(dino)
// ???????????? ?????? (Cactus)
var cactusX: Double = 300.0
val cactus = Picture.rectangle(25, 50)
cactus.setFillColor(Color(178, 34, 34)) // ??? ??? ??????
cactus.setPenColor(noColor)
cactus.setPosition(cactusX, groundY)
draw(cactus)
// 4. ??????? ??? ??? ???????????
var yVelocity: Double = 0.0
val gravity: Double = -1.2
var isJumping: Boolean = false
var cactusSpeed: Double = 6.0
var score: Int = 0
var bgChanged: Boolean = false
// ????? ???????? ?????
var scoreText = Picture.text(s"Score: $score", 22)
scoreText.setFillColor(Color(25, 25, 112)) // ?????? ???? ???
scoreText.setPosition(-260.0, 150.0)
draw(scoreText)
// ?? ??????? ???????? ?????? ????? ?????
var milestoneText = Picture.text("", 28)
milestoneText.setFillColor(Color(255, 69, 0))
milestoneText.setPosition(-120.0, 20.0)
draw(milestoneText)
println("??? ???? ????! ??? ?????????? SPACEBAR ?????.")
// 5. ????? ??????? ??? (Game Loop)
animate {
// ????? ?? ????????? ??? ?????
if (isKeyPressed(Kc.VK_SPACE) && !isJumping) {
yVelocity = 17.5
isJumping = true
}
// ??? ??? ?????????? ???????
if (isJumping) {
dinoY += yVelocity
yVelocity += gravity
if (dinoY <= groundY) {
dinoY = groundY
yVelocity = 0.0
isJumping = false
}
dino.setPosition(dinoX, dinoY)
}
// ???????? ??????? ?????? ??????
cactusX -= cactusSpeed
// ?????? ??? ???????? (Score Increments)
if (cactusX < -310.0) {
cactusX = 300.0
score += 1
cactusSpeed += 0.4 // ?????? ????? ?????
// ????? ??????? ??????????? ????? ????
scoreText.invisible()
scoreText = Picture.text(s"Score: $score", 22)
scoreText.setFillColor(Color(25, 25, 112))
scoreText.setPosition(-260.0, 150.0)
draw(scoreText)
// ?? ??????? ???????? ?????????? ????? "Night Mode" ????? ??? ????? ????? ?????!
if (score >= 50 && !bgChanged) {
// ????? ?????-????? ???? ??????????
setBackground(cm.linearGradient(0, -200, Color(25, 25, 112), 0, 200, Color(0, 0, 0), false))
// ???????? ??????????? ?????? ??? ????
sun.setFillColor(Color(240, 248, 255))
// ???????? ??? ?????
cactus.setFillColor(Color(255, 215, 0))
// "VERY GOOD" ????? ????????
milestoneText.invisible()
milestoneText = Picture.text("VERY GOOD! ?", 28)
milestoneText.setFillColor(Color(255, 215, 0))
milestoneText.setPosition(-120.0, 20.0)
draw(milestoneText)
bgChanged = true
}
}
cactus.setPosition(cactusX, groundY)
// 6. ??? ????? ??????? ???? (Collision)
if (dino.collidesWith(cactus)) {
stopAnimation()
val gameOverText = Picture.text("GAME OVER! ?", 32)
gameOverText.setFillColor(Color(220, 20, 60))
gameOverText.setPosition(-110.0, 60.0)
draw(gameOverText)
println("??? ?????! ????? ????? ?????: " + score)
}
}
// ??????? ??? ???? ??????? ??????????
activateCanvas()