Code Sketch
dolly 24
Category: Art
cleari()
originBottomLeft()
var robotX = 100.0
var robotY = 100.0
var velocityY = 0.0
var grounded = true
var score = 0
var energy = 100
var tick = 0
var gameOver = false
var obstacleX = 650.0
var crystalX = 400.0
var crystalY = 120.0
var crystalTaken = false
def resetRun() {
robotX = 100.0
robotY = 100.0
velocityY = 0.0
grounded = true
score = 0
energy = 100
tick = 0
gameOver = false
obstacleX = 650.0
crystalX = 400.0
crystalY = 120.0
crystalTaken = false
}
onKeyPress { key =>
if (key == Kc.VK_R) {
resetRun()
}
if (!gameOver && key == Kc.VK_SPACE && grounded) {
velocityY = 16.0
grounded = false
}
if (!gameOver && key == Kc.VK_LEFT) {
robotX = math.max(35.0, robotX - 20.0)
}
if (!gameOver && key == Kc.VK_RIGHT) {
robotX = math.min(cwidth - 35.0, robotX + 20.0)
}
}
def drawWorld(c: CanvasDraw) {
c.fill(10, 12, 35)
c.noStroke()
c.rect(0, 0, cwidth, cheight)
for (i <- 0 until 45) {
val x = (i * 137) % cwidth.toInt
val y = (i * 97) % cheight.toInt
c.fill(130, 170, 255)
c.ellipse(x.toDouble, y.toDouble, 3, 3)
}
c.fill(30, 50, 75)
c.rect(0, 0, cwidth, 70)
c.fill(0, 220, 180)
c.rect(0, 70, cwidth, 4)
}
def drawRobot(c: CanvasDraw) {
c.fill(0, 220, 255)
c.stroke(255, 255, 255)
c.strokeWeight(2)
c.rect(robotX - 18, robotY - 10, 36, 38)
c.fill(20, 50, 100)
c.noStroke()
c.rect(robotX - 13, robotY + 2, 26, 12)
c.fill(255, 255, 0)
c.ellipse(robotX - 6, robotY + 8, 5, 5)
c.ellipse(robotX + 6, robotY + 8, 5, 5)
c.fill(255, 100, 30)
c.rect(robotX - 13, robotY - 20, 10, 10)
c.rect(robotX + 3, robotY - 20, 10, 10)
}
def drawObstacle(c: CanvasDraw) {
c.fill(255, 65, 65)
c.stroke(255, 150, 100)
c.strokeWeight(2)
c.rect(obstacleX - 18, 70, 36, 45)
}
def drawCrystal(c: CanvasDraw) {
if (!crystalTaken) {
c.fill(255, 220, 0)
c.noStroke()
c.ellipse(crystalX, crystalY, 20, 25)
c.fill(255, 255, 180)
c.ellipse(crystalX, crystalY, 7, 12)
}
}
def updateRun() {
if (!gameOver) {
tick += 1
velocityY -= 0.8
robotY += velocityY
if (robotY <= 100) {
robotY = 100
velocityY = 0
grounded = true
}
obstacleX -= 6.0
if (obstacleX < -30) {
obstacleX = cwidth + 50
score += 5
}
crystalX -= 5.0
if (crystalX < -20) {
crystalX = cwidth + 100
crystalY = 110.0 + (tick % 4) * 35.0
crystalTaken = false
}
if (
math.abs(robotX - obstacleX) < 30 &&
robotY < 135
) {
energy -= 25
obstacleX = cwidth + 100
if (energy <= 0) {
gameOver = true
}
}
if (
!crystalTaken &&
math.abs(robotX - crystalX) < 25 &&
math.abs(robotY - crystalY) < 30
) {
crystalTaken = true
score += 10
energy = math.min(100, energy + 10)
}
if (tick % 120 == 0) {
score += 5
}
}
}
def drawHUD(c: CanvasDraw) {
c.fill(255, 255, 255)
c.noStroke()
c.text(s"SCORE: $score", 20, cheight - 30)
c.text(s"ENERGY: $energy", 20, cheight - 55)
c.text("SPACE = JUMP", cwidth - 150, cheight - 30)
}
def drawEnd(c: CanvasDraw) {
c.fill(5, 5, 25)
c.noStroke()
c.rect(0, 0, cwidth, cheight)
c.fill(255, 70, 70)
c.text("RUN OVER!", cwidth / 2 - 40, cheight / 2 + 30)
c.fill(255, 255, 255)
c.text(s"FINAL SCORE: $score", cwidth / 2 - 55, cheight / 2)
c.text("Press R to Restart", cwidth / 2 - 60, cheight / 2 - 35)
}
animateWithCanvasDraw { c =>
updateRun()
if (!gameOver) {
drawWorld(c)
drawObstacle(c)
drawCrystal(c)
drawRobot(c)
drawHUD(c)
} else {
drawEnd(c)
}
}