Code Sketch
sniper_gamer
Category: Art
cleari()
disablePanAndZoom()
setBackground(darkGray)
var score = 0
var gameRunning = true
val roadWidth = 300.0
val speed = 6.0
val cb = canvasBounds
// Draw the Road Track
val road = fillColor(lightGray) -> Picture.rectangle(roadWidth, cb.height)
road.setPosition(-roadWidth / 2, cb.y)
draw(road)
// --- PLAYER LUXURY CAR (Built using trans combinators) ---
def createLuxuryCar(): Picture = {
val body = fillColor(red) -> Picture.rectangle(36, 75)
val roof = trans(0, 10) * fillColor(darkGray) -> Picture.rectangle(28, 30)
val windshield = trans(0, 20) * fillColor(cyan) -> Picture.rectangle(24, 12)
val wheel1 = trans(-18, -25) * fillColor(black) -> Picture.rectangle(8, 14)
val wheel2 = trans(18, -25) * fillColor(black) -> Picture.rectangle(8, 14)
val wheel3 = trans(-18, 25) * fillColor(black) -> Picture.rectangle(8, 14)
val wheel4 = trans(18, 25) * fillColor(black) -> Picture.rectangle(8, 14)
// Combine all parts together into a single picture stack
picStack(body, roof, windshield, wheel1, wheel2, wheel3, wheel4)
}
var carX = 0.0
var carY = -150.0
val car = createLuxuryCar()
car.setPosition(carX, carY)
draw(car)
// --- OBSTACLE CAR ---
def createObstacleCar(): Picture = {
val body = fillColor(blue) -> Picture.rectangle(38, 70)
val roof = trans(0, 10) * fillColor(darkGray) -> Picture.rectangle(26, 28)
val wheel1 = trans(-20, -22) * fillColor(black) -> Picture.rectangle(8, 12)
val wheel2 = trans(20, -22) * fillColor(black) -> Picture.rectangle(8, 12)
val wheel3 = trans(-20, 22) * fillColor(black) -> Picture.rectangle(8, 12)
val wheel4 = trans(20, 22) * fillColor(black) -> Picture.rectangle(8, 12)
picStack(body, roof, wheel1, wheel2, wheel3, wheel4)
}
var obsX = (math.random * (roadWidth - 60)) - ((roadWidth - 60) / 2)
var obsY = 250.0
val obstacle = createObstacleCar()
obstacle.setPosition(obsX, obsY)
draw(obstacle)
// Scoreboard Text
var scoreText = Picture.text("Score: " + score)
scoreText.setPenColor(white)
scoreText.setPosition(-180, 200)
draw(scoreText)
// Main Animation Loop and Keyboard Controls
animate {
if (gameRunning) {
// Steer Left / Right with Boundaries
if (isKeyPressed(Kc.VK_LEFT) && carX > -roadWidth / 2 + 20) {
carX -= 6.0
}
if (isKeyPressed(Kc.VK_RIGHT) && carX < roadWidth / 2 - 20) {
carX += 6.0
}
car.setPosition(carX, carY)
// Move Obstacle Downwards
obsY -= speed
if (obsY < cb.y - 50) {
obsY = cb.y + cb.height + 50
obsX = (math.random * (roadWidth - 60)) - ((roadWidth - 60) / 2)
score += 1
// Update Score Display
scoreText.erase()
scoreText = Picture.text("Score: " + score)
scoreText.setPenColor(white)
scoreText.setPosition(-180, 200)
draw(scoreText)
}
obstacle.setPosition(obsX, obsY)
// Collision Detection
if (car.collidesWith(obstacle)) {
gameRunning = false
val gameOverText = Picture.text("GAME OVER! Final Score: " + score)
gameOverText.setPenColor(yellow)
gameOverText.setPosition(-140, 0)
draw(gameOverText)
stopAnimation()
}
}
}
// Focus Canvas for Instant Keyboard Response
activateCanvas()
println("Game Ready! Use your Left and Right Arrow Keys to drive.")