Code Sketch


Space Shooter (v:1.2)
By: Soham Dabral
Category: Programming
//https://drive.google.com/drive/folders/1Nn7MeKI4vKiCaqy8XN4q7CTRJheCQF9Q?usp=sharing
//Download the above files and put it in your downloads folder

// A shooting game
//Use left and right to control
//Press SPACE to shoot
cleari()
clearOutput()
disablePanAndZoom()
setBackground(cm.black)

//PART - 1 Defining objects
//Canvas Bounds
val cb = canvasBounds

//Life Stuff
var lifes = 3
var LifeCount = 3

//Help us to shoot missile
var shootMissile = false
var shootmissile2 = false

//Velocity
var velocity = Vector2D(0, -1)

//Gravity
val gravity = Vector2D(0, 0.1)

//Score
var score = 0

//Stars area
def height = random(10, 50)
def width = cb.width / 100

//Intro
val intro = Picture.image("~/Downloads/SD.png ")
drawCentered(intro)

//Creator
val creator = Picture.text("Soham Dabral", Font("Serif", 40))
creator.setPosition(-100, -150)
creator.setPenColor(cm.grey)
draw(creator)

showFps(green, 20)

//Pause
pause(3)

cleari()
disablePanAndZoom()
setBackground(cm.black)

//Intro's background
def Stars(num: Int, width: Double, height: Double) = picStack(
    for {
        n <- 1 to num
        x = randomDouble(0, width)
        y = randomDouble(0, height)
        p = Picture.circle(1).thatsTranslated(x, y).withPenColor(noColor)
    } yield p
)
draw(trans(-550, -700) -> Stars(1000, 1000, 1000).withFillColor(cm.silver))
showFps(green, 20)

//Box color
val boxclr = cm.linearGradient(0, 40, cm.white, 0, 0, cm.black, false)

//Box
val box1 = Picture.rectangle(500, 50).withPenColor(noColor).withFillColor(boxclr).withPenThickness(1).withOpacity(0.75)
drawCentered(box1)

val box2 = Picture.rectangle(500, 50).withPenColor(noColor).withFillColor(boxclr).withPenThickness(1).withOpacity(0.75)
draw(box2.withTranslation(-250, -80))

//Play button
val play = Picture.text("Play", Font("Space Game", 40))
play.setPenColor(cm.black)
//play.setOpacity(0.5)
drawCentered(play)

//Quit Game button
val quitGame = Picture.text("Quit Game", Font("Space Game", 40))
quitGame.setPenColor(cm.black)
quitGame.setPosition(-85, -30)
draw(quitGame)

play.onMouseClick { (x, y) =>
    cleari()
    setBackground(cm.radialGradient(0, 0, cm.white, 200, cm.silver, true))

    //Stage
    val bg2 = Picture.rectangle(cwidth * 1.5, cheight * 2)
    bg2.setPosition(-cwidth / 1.4, -cheight)
    bg2.setPenColor(noColor)
    draw(bg2)

    //space2
    val space2 = Picture {
        left()
        hop(cwidth * 4)
        left()
        hop(cheight * 2)
        right(180)
        setPenColor(noColor)
        setFillColor(cm.black)
        forward(cheight * 2.5)
        right()
        forward(cwidth * 4)
        right(180)
        left(180, 237.5)
    }
    space2.setPosition(300, 0)
    space2.setFillColor(cm.black)
    space2.setPenColor(noColor)
    draw(space2)

    //Celestial bodies
    //Moon2
    val Moon2 = Picture {
        right(180, 237.5)
    }
    Moon2.setPosition(300, -240)
    Moon2.rotate(90)
    Moon2.setPenColor(noColor)
    draw(Moon2)

    //Stars
    draw(trans(-550, -700) -> Stars(1000, 1000, 1000).withFillColor(cm.silver))

    //Asteroids
    //Asteroid1
    val asteroid1 = Picture.image("~/Downloads/Asteroid.png")
    asteroid1.setPosition(random(-500, -613), -150)
    asteroid1.scale(0.25)
    draw(asteroid1)

    //Asteroid2
    val asteroid2 = Picture.image("~/Downloads/Asteroid.png")
    asteroid2.setPosition(random(-500, -613), -150)
    asteroid2.scale(0.25)
    draw(asteroid2)

    //space2 Shooter
    val space2Shooter = Picture.image("~/Downloads/spaceShooter.png")
    space2Shooter.setPosition(0, 150)
    space2Shooter.scale(0.25)
    draw(space2Shooter)

    //missile2
    val missile2 = Picture.image("~/Downloads/missile.png")
    missile2.setPosition(space2Shooter.position.x - 100000, space2Shooter.position.y - 100)
    missile2.flipX()
    missile2.scale(0.3)
    draw(missile2)

    //heart2s
    val heart2 = Picture.image("~/Downloads/hearts.png")
    heart2.setPosition(-275, 170)
    heart2.scale(0.2)
    draw(heart2)

    //Column2
    val Column2 = Picture.text(":", Font("Cambria", 40))
    Column2.setPosition(-230, 220)
    Column2.setPenColor(ColorMaker.hsl(18, 0.60, 0.40))
    draw(Column2)

    //Life Count Text
    val LifeCountText2 = Picture.text(s"$LifeCount", Font("Serif", 40))
    LifeCountText2.setPosition(-210, 220)
    LifeCountText2.setPenColor(cm.silver)
    draw(LifeCountText2)

    showFps(green, 20)

    //PART - 2 Animating and Controls
    animate {
        //Speed of Asetroids
        asteroid1.translate(random(1, 25), 0)
        asteroid2.translate(random(1, 25), 0)

        // Controling space2Shooter and missile2 :)
        if (isKeyPressed(Kc.VK_LEFT)) {
            space2Shooter.translate(-20, 0)
            missile2.translate(-20, 0)
        }

        if (isKeyPressed(Kc.VK_RIGHT)) {
            space2Shooter.translate(20, 0)
            missile2.translate(20, 0)
        }

        //Calculating velocity
        velocity = velocity - gravity

        //Shoot missile2!!!
        if (isKeyPressed(Kc.VK_SPACE)) {
            shootmissile2 = true
        }

        if (shootmissile2 == true) {
            missile2.translate(-velocity)
        }

        //Collison part
        //Collison with stage
        if (missile2.collidesWith(bg2)) {
            missile2.setPosition(space2Shooter.position.x + 60, space2Shooter.position.y + 30)
            shootmissile2 = false
        }

        //Collison with missile2
        if (missile2.collidesWith(asteroid1)) {
            asteroid1.setPosition(random(-500, -613), -150)
            missile2.setPosition(space2Shooter.position.x, space2Shooter.position.y + 30)
            shootmissile2 = false
            score += 1
        }

        if (missile2.collidesWith(asteroid2)) {
            asteroid2.setPosition(random(-500, -613), -150)
            missile2.setPosition(space2Shooter.position.x, space2Shooter.position.y + 30)
            shootmissile2 = false
            score += 1
        }

        //Collison with Moon2
        if (asteroid1.collidesWith(Moon2)) {
            asteroid1.setPosition(random(-500, -613), -150)
            missile2.setPosition(space2Shooter.position.x + 60, space2Shooter.position.y + 30)
            lifes = lifes - 1
            LifeCount -= 1
            LifeCountText2.update(LifeCount)
            shootmissile2 = false
        }

        if (asteroid2.collidesWith(Moon2)) {
            asteroid2.setPosition(random(-500, -613), -150)
            missile2.setPosition(space2Shooter.position.x + 60, space2Shooter.position.y + 30)
            lifes = lifes - 1
            LifeCount -= 1
            LifeCountText2.update(LifeCount)
            shootmissile2 = false
        }

        if (missile2.collidesWith(Moon2)) {
            missile2.setPosition(space2Shooter.position.x + 60, space2Shooter.position.y + 30)
            shootmissile2 = false
        }

        if (space2Shooter.collidesWith(Moon2)) {
            missile2.setPosition(space2Shooter.position.x + 60, space2Shooter.position.y + 30)
            shootmissile2 = false
        }

        //Lifes Part
        if (lifes == 0) {
            drawCenteredMessage(s"You Lose :( \n Your Score is $score", red, 40)
            activateEditor()
            shootmissile2 = false
            stopAnimation()
        }

    }
    showGameTimeCountdown(30, s"You Win :) \n Your score is $score", green, 30, 20, 50)
    activateCanvas()
}

box1.onMouseClick { (x, y) =>
    cleari()
    setBackground(cm.radialGradient(0, 0, cm.white, 200, cm.silver, true))

    //Stage
    val bg2 = Picture.rectangle(cwidth * 1.5, cheight * 2)
    bg2.setPosition(-cwidth / 1.4, -cheight)
    bg2.setPenColor(noColor)
    draw(bg2)

    //space2
    val space2 = Picture {
        left()
        hop(cwidth * 4)
        left()
        hop(cheight * 2)
        right(180)
        setPenColor(noColor)
        setFillColor(cm.black)
        forward(cheight * 2.5)
        right()
        forward(cwidth * 4)
        right(180)
        left(180, 237.5)
    }
    space2.setPosition(300, 0)
    space2.setFillColor(cm.black)
    space2.setPenColor(noColor)
    draw(space2)

    //Celestial bodies
    //Moon2
    val Moon2 = Picture {
        right(180, 237.5)
    }
    Moon2.setPosition(300, -240)
    Moon2.rotate(90)
    Moon2.setPenColor(noColor)
    draw(Moon2)

    //Stars
    draw(trans(-550, -700) -> Stars(1000, 1000, 1000).withFillColor(cm.silver))

    //Asteroids
    //Asteroid1
    val asteroid1 = Picture.image("~/Downloads/Asteroid.png")
    asteroid1.setPosition(random(-500, -613), -150)
    asteroid1.scale(0.25)
    draw(asteroid1)

    //Asteroid2
    val asteroid2 = Picture.image("~/Downloads/Asteroid.png")
    asteroid2.setPosition(random(-500, -613), -150)
    asteroid2.scale(0.25)
    draw(asteroid2)

    //space2 Shooter
    val space2Shooter = Picture.image("~/Downloads/spaceShooter.png")
    space2Shooter.setPosition(0, 150)
    space2Shooter.scale(0.25)
    draw(space2Shooter)

    //missile2
    val missile2 = Picture.image("~/Downloads/missile.png")
    missile2.setPosition(space2Shooter.position.x - 100000, space2Shooter.position.y - 100)
    missile2.flipX()
    missile2.scale(0.3)
    draw(missile2)

    //heart2s
    val heart2 = Picture.image("~/Downloads/hearts.png")
    heart2.setPosition(-275, 170)
    heart2.scale(0.2)
    draw(heart2)

    //Column2
    val Column2 = Picture.text(":", Font("Cambria", 40))
    Column2.setPosition(-230, 220)
    Column2.setPenColor(ColorMaker.hsl(18, 0.60, 0.40))
    draw(Column2)

    //Life Count Text
    val LifeCountText2 = Picture.text(s"$LifeCount", Font("Serif", 40))
    LifeCountText2.setPosition(-210, 220)
    LifeCountText2.setPenColor(cm.silver)
    draw(LifeCountText2)

    showFps(green, 20)

    //PART - 2 Animating and Controls
    animate {
        //Speed of Asetroids
        asteroid1.translate(random(1, 25), 0)
        asteroid2.translate(random(1, 25), 0)

        // Controling space2Shooter and missile2 :)
        if (isKeyPressed(Kc.VK_LEFT)) {
            space2Shooter.translate(-20, 0)
            missile2.translate(-20, 0)
        }

        if (isKeyPressed(Kc.VK_RIGHT)) {
            space2Shooter.translate(20, 0)
            missile2.translate(20, 0)
        }

        //Calculating velocity
        velocity = velocity - gravity

        //Shoot missile2!!!
        if (isKeyPressed(Kc.VK_SPACE)) {
            shootmissile2 = true
        }

        if (shootmissile2 == true) {
            missile2.translate(-velocity * 2)
        }

        //Collison part
        //Collison with stage
        if (missile2.collidesWith(bg2)) {
            missile2.setPosition(space2Shooter.position.x + 60, space2Shooter.position.y + 30)
            shootmissile2 = false
        }

        //Collison with missile2
        if (missile2.collidesWith(asteroid1)) {
            asteroid1.setPosition(random(-500, -613), -150)
            missile2.setPosition(space2Shooter.position.x, space2Shooter.position.y + 30)
            shootmissile2 = false
            score += 1
        }

        if (missile2.collidesWith(asteroid2)) {
            asteroid2.setPosition(random(-500, -613), -150)
            missile2.setPosition(space2Shooter.position.x, space2Shooter.position.y + 30)
            shootmissile2 = false
            score += 1
        }

        //Collison with Moon2
        if (asteroid1.collidesWith(Moon2)) {
            asteroid1.setPosition(random(-500, -613), -150)
            missile2.setPosition(space2Shooter.position.x + 60, space2Shooter.position.y + 30)
            lifes = lifes - 1
            LifeCount -= 1
            LifeCountText2.update(LifeCount)
            shootmissile2 = false
        }

        if (asteroid2.collidesWith(Moon2)) {
            asteroid2.setPosition(random(-500, -613), -150)
            missile2.setPosition(space2Shooter.position.x + 60, space2Shooter.position.y + 30)
            lifes = lifes - 1
            LifeCount -= 1
            LifeCountText2.update(LifeCount)
            shootmissile2 = false
        }

        if (missile2.collidesWith(Moon2)) {
            missile2.setPosition(space2Shooter.position.x + 60, space2Shooter.position.y + 30)
            shootmissile2 = false
        }

        if (space2Shooter.collidesWith(Moon2)) {
            missile2.setPosition(space2Shooter.position.x + 60, space2Shooter.position.y + 30)
            shootmissile2 = false
        }

        //Lifes Part
        if (lifes == 0) {
            drawCenteredMessage(s"You Lose :( \n Your Score is $score", red, 40)
            activateEditor()
            shootmissile2 = false
            stopAnimation()
        }

    }
    showGameTimeCountdown(30, s"You Win :) \n Your score is $score", green, 30, 20, 50)
    activateCanvas()
}

quitGame.onMouseClick { (x, y) =>
    clear()
    activateEditor()
}

box2.onMouseClick { (x, y) =>
    clear()
    activateEditor()
}