Code Sketch


Space Shooter
By: Aditya Pant
Category: Programming
// To run this game locally, you will need to copy the game assets
// from the following shared folder - into your Downloads directory
// https://drive.google.com/drive/folders/1xse42kGqTSNITcLxhnxEZt109aZ9t-k0

cleari()
drawStage(ColorMaker.hsl(192, 1.00, 0.50))
val cb = canvasBounds
val bg =  Picture.image("~/Downloads/space background.jpeg")
bg.setPosition(cb.x, cb.y)
bg.scale(1.03, 1.33)
draw(bg)

var score = 0

def newtarget = 
    Picture.image("~/Downloads/enemy spaceship model.png").withScaling(0.3).withRotation(90)
//    fillColor(black) * penColor(black) -> Picture.rectangle(55, 70)

case class TargetAt(target: Picture, at: Long)

val targets = HashSet.empty[TargetAt]

val shooter = Picture.image("~/Downloads/spaceship model.png").withScaling(0.7).withRotation(-90)
shooter.setPosition(cb.x, cb.y + cb.height / 2 - 25)
draw(shooter)

timer(1500) {
    val currTime = epochTimeMillis

    targets.foreach { ta =>
        if (currTime - ta.at > 1499) {
            val pic = ta.target
            pic.erase()
            targets.remove(ta)
        }
    }   

    val t = newtarget
    targets.add(TargetAt(t, epochTimeMillis))
    draw(t)
    t.setPosition(cb.x + cwidth - 10, cb.y + random(0, cheight - 40))
}
def newBullet =
    Picture.image("~/Downloads/lazer bullet model.png").withScaling(0.1)
//    fillColor(black) * penColor(black) -> Picture.circle(3)
      

val bullets = HashSet.empty[Picture]

val bulletVels = Vector2D(50, 0)
var gunPrimed = true
def shootbullet() {
    if (gunPrimed) {
        playMp3("~/Downloads/mixkit-short-laser-gun-shot-1670_OPxN07XU.mp3")
        val b = newBullet
        b.setPosition(shooter.position.x + 50, shooter.position.y - 75)
        bullets.add(b)
        draw(b)
        gunPrimed = false
        schedule(0.5) {
            gunPrimed = true
        }
    }
}
def msg = s"Score : $score"

val SL = Picture.textu(msg, 15, white)
SL.setPosition(cb.x, cb.y + cb.height)
def updateScore() {
    SL.update(msg)
}

draw(SL)

def Finalscore = s"Your score is $score"

val SL2 = Picture.textu(Finalscore, 40, white)
SL2.setPosition(cb.x+cb.width/2-140, cb.y + cb.height/2)
def updateScore2() {
    SL2.update(Finalscore)
}
animate {
    if (!shooter.collidesWith(stageTop)) {
        if (isKeyPressed(Kc.VK_UP)) {
            shooter.translate(-15, 0)
        }
    }
    if (!shooter.collidesWith(stageBot)) {
        if (isKeyPressed(Kc.VK_DOWN)) {
            shooter.translate(15, 0)
        }
    }
    if (isKeyPressed(Kc.VK_SPACE)) {
        shootbullet()
    }

    bullets.foreach { b =>
        b.offset(bulletVels)
        targets.foreach { ta =>
            val t = ta.target
            if (b.collidesWith(t) || t.contains(b)) {
                bullets.remove(b)
                b.erase()
                targets.remove(ta)
                t.erase()
                score = score + 1
                updateScore()
                updateScore2()
            }
        }
        if (!stageArea.contains(b)) {
            bullets.remove(b)
            b.erase()
        }

    }
}
showGameTimeCountdown(15, "", green, 50)
onAnimationStop {
    draw(SL2)
    SL.erase()   
}
activateCanvas()