Code Sketch


Space Shooter (v:1.1)
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.radialGradient(0, 0, cm.white, 200, cm.silver, true))

//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

//Space
val space = 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)
}
space.setPosition(300, 0)
space.setFillColor(cm.black)
space.setPenColor(noColor)
draw(space)

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

//Stars
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))

//Asteroids
//Asteroid1
val asteroid1 = Picture.image("~/OneDrive/Pictures/Kojo Pics/Asteroid.png")
asteroid1.setPosition(-213, -150)
asteroid1.scale(0.25)
draw(asteroid1)

//Asteroid2
val asteroid2 = Picture.image("~/OneDrive/Pictures/Kojo Pics/Asteroid.png")
asteroid2.setPosition(-313, -180)
asteroid2.scale(0.25)
draw(asteroid2)

//Space Shooter
val spaceShooter = Picture.image("~/OneDrive/Pictures/Kojo Pics/SpaceShooter.png")
spaceShooter.setPosition(0, 150)
spaceShooter.scale(0.25)
draw(spaceShooter)

//Missile
val missile = Picture.image("~/OneDrive/Pictures/Kojo Pics/Missile.png")
missile.setPosition(spaceShooter.position.x - 100000, spaceShooter.position.y - 100)
missile.flipX()
missile.scale(0.3)
draw(missile)

//Intro
val intro = Picture.text("Crafted by \n Soham Dabral", Font("Arial", 20))
intro.setPenColor(cm.yellow)
intro.setPosition(-125, -125)
draw(intro)

//Game name
val gameName = Picture.text("Space Shooter", Font("Cambria", 60))
gameName.setPenColor(ColorMaker.hsl(188, 1.00, 0.50))
drawCentered(gameName)

//Pause
pause(3)

cleari()
setBackground(black)
disablePanAndZoom()

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

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

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

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

//Play button
val play = Picture.text("Play", Font("Serif", 40))
play.setPenColor(cm.black)
drawCentered(play)

//Quit Game button
val quitGame = Picture.text("Quit Game", Font("Serif", 40))
quitGame.setPenColor(cm.black)
quitGame.setPosition(-75, -25)
draw(quitGame)

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

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

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

    //Space
    val space = 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)
    }
    space.setPosition(300, 0)
    space.setFillColor(cm.black)
    space.setPenColor(noColor)
    draw(space)

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

    //Stars
    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))

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

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

    //Space Shooter
    val spaceShooter = Picture.image("~/OneDrive/Pictures/Kojo Pics/SpaceShooter.png")
    spaceShooter.setPosition(0, 150)
    spaceShooter.scale(0.25)
    draw(spaceShooter)

    //Missile
    val missile = Picture.image("~/OneDrive/Pictures/Kojo Pics/Missile.png")
    missile.setPosition(spaceShooter.position.x - 100000, spaceShooter.position.y - 100)
    missile.flipX()
    missile.scale(0.3)
    draw(missile)

    //Hearts
    val heart = Picture.image("~/OneDrive/Pictures/Kojo Pics/Hearts.png")
    heart.setPosition(-275, 170)
    heart.scale(0.2)
    draw(heart)

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

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

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

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

        if (isKeyPressed(Kc.VK_RIGHT)) {
            spaceShooter.translate(20, 0)
            missile.translate(20, 0)
        }

        //Calculating velocity
        velocity = velocity - gravity

        //Shoot missile!!!
        if (isKeyPressed(Kc.VK_SPACE)) {
            shootMissile = true
        }

        if (shootMissile == true) {
            missile.translate(-velocity)
        }

        //Collison part
        //Collison with stage
        if (missile.collidesWith(bg)) {
            missile.setPosition(spaceShooter.position.x + 60, spaceShooter.position.y + 30)
            shootMissile = false
        }

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

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

        //Collison with Moon
        if (asteroid1.collidesWith(Moon)) {
            asteroid1.setPosition(random(-500, -613), -150)
            missile.setPosition(spaceShooter.position.x + 60, spaceShooter.position.y + 30)
            lifes = lifes - 1
            LifeCount -= 1
            LifeCountText.update(LifeCount)
            shootMissile = false
        }

        if (asteroid2.collidesWith(Moon)) {
            asteroid2.setPosition(random(-500, -613), -150)
            missile.setPosition(spaceShooter.position.x + 60, spaceShooter.position.y + 30)
            lifes = lifes - 1
            LifeCount -= 1
            LifeCountText.update(LifeCount)
            shootMissile = false
        }

        if (missile.collidesWith(Moon)) {
            missile.setPosition(spaceShooter.position.x + 60, spaceShooter.position.y + 30)
            shootMissile = false
        }

        if (spaceShooter.collidesWith(Moon)) {
            missile.setPosition(spaceShooter.position.x + 60, spaceShooter.position.y + 30)
            shootMissile = false
        }

        //Lifes Part
        if (lifes == 0) {
            drawCenteredMessage(s"You Lose :( \n Your Score is $score", red, 40)
            activateEditor()
            shootMissile = 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
    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))

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

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

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

    //missile2
    val missile2 = Picture.image("~/OneDrive/Pictures/Kojo Pics/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("~/OneDrive/Pictures/Kojo Pics/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)

    //PART - 2 Animating and Controls
    animate {
        //Speed of Asetroids
        asteroid1.translate(random(1, 20), 0)
        asteroid2.translate(random(1, 20), 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()
}

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

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