Code Sketch
yadnesh
Category: Programming
cleari()
drawStage(ColorMaker.darkGreen)
disablePanAndZoom()
val cb = canvasBounds
// ============================================================
// ? ULTRA LEGEND CRICKET - 100 FEATURE EDITION
// ============================================================
//
// CONTROLS
// SPACE = Bowl
// W = Pull Shot
// S = Straight Drive
// A = Cover Drive
// D = Defence
// Q = Lofted Shot
// E = Special Shot
// P = Pause
// R = Restart
//
// ============================================================
// ============================================================
// 1 - 20 : GAME STATE
// ============================================================
var score = 0
var wickets = 0
var balls = 0
var fours = 0
var sixes = 0
var singles = 0
var dotBalls = 0
var combo = 0
var bestCombo = 0
var target = 0
var innings = 1
var matchOver = false
var paused = false
var ballActive = false
var ballHit = false
var shotPlayed = false
var totalBalls = 30
var maxWickets = 5
var message = "SPACE = BOWL"
var scoreText = "SCORE 0/0 | OVER 0.0"
var difficulty = 2
var selectedPlayer = "Legend XI"
var boundaryCount = 0
var perfectShots = 0
var wicketsLost = 0
// ============================================================
// 21 - 30 : LEGEND PLAYERS
// ============================================================
case class Player(
name: String,
power: Int,
timing: Int,
speed: Int
)
val players = List(
Player("Master Blaster", 98, 96, 88),
Player("Captain Cool", 92, 94, 80),
Player("King Batter", 99, 98, 92),
Player("Universe Boss", 100, 90, 86),
Player("Yorker King", 80, 88, 100),
Player("Cover Drive Legend", 95, 99, 90),
Player("Six Machine", 100, 91, 95),
Player("Spin Wizard", 88, 94, 83),
Player("Power Hitter", 97, 87, 97),
Player("Young Superstar", 91, 95, 94)
)
var currentPlayer =
players(0)
// ============================================================
// 31 - 40 : STADIUM
// ============================================================
val stadium =
Picture.circle(650)
stadium.setFillColor(ColorMaker.green)
stadium.setPosition(
cb.x + cb.width / 2,
cb.y + 240
)
draw(stadium)
val boundary =
Picture.circle(530)
boundary.setFillColor(ColorMaker.darkGreen)
boundary.setPosition(
cb.x + cb.width / 2,
cb.y + 240
)
draw(boundary)
val pitch =
Picture.rectangle(160, 500)
pitch.setFillColor(ColorMaker.tan)
pitch.setPosition(
cb.x + cb.width / 2 - 80,
cb.y + 25
)
draw(pitch)
// ============================================================
// 41 - 45 : PITCH DETAILS
// ============================================================
val crease1 =
Picture.rectangle(160, 5)
crease1.setFillColor(ColorMaker.white)
crease1.setPosition(
cb.x + cb.width / 2 - 80,
cb.y + 90
)
draw(crease1)
val crease2 =
Picture.rectangle(160, 5)
crease2.setFillColor(ColorMaker.white)
crease2.setPosition(
cb.x + cb.width / 2 - 80,
cb.y + 400
)
draw(crease2)
// ============================================================
// 46 - 50 : STUMPS
// ============================================================
val stump1 =
Picture.rectangle(6, 45)
stump1.setFillColor(ColorMaker.saddleBrown)
stump1.setPosition(
cb.x + cb.width / 2 - 15,
cb.y + 50
)
draw(stump1)
val stump2 =
Picture.rectangle(6, 45)
stump2.setFillColor(ColorMaker.saddleBrown)
stump2.setPosition(
cb.x + cb.width / 2 - 3,
cb.y + 50
)
draw(stump2)
val stump3 =
Picture.rectangle(6, 45)
stump3.setFillColor(ColorMaker.saddleBrown)
stump3.setPosition(
cb.x + cb.width / 2 + 9,
cb.y + 50
)
draw(stump3)
// ============================================================
// 51 - 60 : SCOREBOARD
// ============================================================
val scoreDisplay =
Picture.text(scoreText, 20)
scoreDisplay.setFillColor(ColorMaker.white)
scoreDisplay.setPosition(
cb.x + 20,
cb.y + cb.height - 30
)
draw(scoreDisplay)
val messageDisplay =
Picture.text(message, 15)
messageDisplay.setFillColor(ColorMaker.yellow)
messageDisplay.setPosition(
cb.x + 20,
cb.y + cb.height - 60
)
draw(messageDisplay)
val statsDisplay =
Picture.text(
"4s: 0 | 6s: 0 | Combo: 0",
14
)
statsDisplay.setFillColor(ColorMaker.white)
statsDisplay.setPosition(
cb.x + 20,
cb.y + cb.height - 88
)
draw(statsDisplay)
// ============================================================
// 61 - 70 : BATSMAN
// ============================================================
class Batsman {
val body =
Picture.rectangle(25, 50)
body.setFillColor(ColorMaker.blue)
val head =
Picture.circle(22)
head.setFillColor(ColorMaker.saddleBrown)
head.setPosition(
0,
-35
)
val helmet =
Picture.circle(25)
helmet.setFillColor(ColorMaker.black)
helmet.setPosition(
0,
-37
)
val bat =
Picture.rectangle(8, 65)
bat.setFillColor(
ColorMaker.saddleBrown
)
bat.setPosition(
-18,
-20
)
val pic =
picBatch(
body,
head,
helmet,
bat
)
pic.setPosition(
cb.x + cb.width / 2 - 12,
cb.y + 70
)
var swinging = false
var timer = 0
var swingType = ""
def swing(kind: String): Unit = {
if (!swinging) {
swinging = true
timer = 8
swingType = kind
if (kind == "pull") {
pic.rotate(-55)
}
if (kind == "straight") {
pic.rotate(45)
}
if (kind == "cover") {
pic.rotate(-40)
}
if (kind == "defence") {
pic.rotate(15)
}
if (kind == "loft") {
pic.rotate(70)
}
if (kind == "special") {
pic.rotate(-75)
}
}
}
def update(): Unit = {
if (timer > 0) {
timer -= 1
if (timer == 0) {
if (swingType == "pull") {
pic.rotate(55)
}
if (swingType == "straight") {
pic.rotate(-45)
}
if (swingType == "cover") {
pic.rotate(40)
}
if (swingType == "defence") {
pic.rotate(-15)
}
if (swingType == "loft") {
pic.rotate(-70)
}
if (swingType == "special") {
pic.rotate(75)
}
swinging = false
}
}
}
}
val batsman =
new Batsman()
draw(batsman.pic)
// ============================================================
// 71 - 80 : BALL SYSTEM
// ============================================================
val ball =
Picture.circle(12)
ball.setFillColor(ColorMaker.white)
var ballX =
cb.x + cb.width / 2
var ballY =
cb.y + cb.height - 100
var ballVX = 0.0
var ballVY = 0.0
ball.setPosition(
ballX,
ballY
)
draw(ball)
// Ball shadow
val shadow =
Picture.ellipse(25, 8)
shadow.setFillColor(ColorMaker.black)
shadow.setPosition(
ballX,
ballY + 10
)
draw(shadow)
// ============================================================
// 81 - 85 : SCORE CALCULATIONS
// ============================================================
def updateScore(): Unit = {
val over =
balls / 6
val ballNo =
balls % 6
scoreText =
"SCORE " +
score +
"/" +
wickets +
" | OVER " +
over +
"." +
ballNo +
" / 5.0"
}
// ============================================================
// 86 - 90 : BOWLING
// ============================================================
def bowl(): Unit = {
if (
!ballActive &&
!matchOver &&
!paused
) {
ballX =
cb.x + cb.width / 2
ballY =
cb.y + cb.height - 100
ballVX = 0
ballVY =
-5.5 - math.random * 2.0
ballActive = true
ballHit = false
shotPlayed = false
message =
"? BALL COMING!"
}
}
// ============================================================
// 91 - 95 : SHOT SYSTEM
// ============================================================
def playShot(
kind: String
): Unit = {
if (
ballActive &&
!shotPlayed
) {
shotPlayed = true
ballHit = true
combo += 1
if (combo > bestCombo) {
bestCombo = combo
}
if (kind == "pull") {
batsman.swing("pull")
score += 4
fours += 1
boundaryCount += 1
perfectShots += 1
ballVX = 4
ballVY = 5
message =
"? PERFECT PULL! FOUR!"
}
if (kind == "straight") {
batsman.swing("straight")
score += 6
sixes += 1
boundaryCount += 1
perfectShots += 1
ballVX = 0
ballVY = 7
message =
"? STRAIGHT DRIVE! SIX!"
}
if (kind == "cover") {
batsman.swing("cover")
score += 4
fours += 1
boundaryCount += 1
ballVX = -4
ballVY = 5
message =
"? COVER DRIVE! FOUR!"
}
if (kind == "defence") {
batsman.swing("defence")
score += 1
singles += 1
ballVX = 1
ballVY = 6
message =
"? DEFENCE! 1 RUN"
}
if (kind == "loft") {
batsman.swing("loft")
score += 6
sixes += 1
boundaryCount += 1
perfectShots += 1
ballVX = 2
ballVY = 7
message =
"? LOFTED SIX!"
}
if (kind == "special") {
batsman.swing("special")
score += 6
sixes += 1
boundaryCount += 1
ballVX = -3
ballVY = 8
message =
"? LEGENDARY SPECIAL SIX!"
}
updateScore()
}
}
// ============================================================
// 96 - 98 : BALL RESET + WICKET
// ============================================================
def resetBall(): Unit = {
ballActive = false
ballHit = false
shotPlayed = false
balls += 1
ballX =
cb.x + cb.width / 2
ballY =
cb.y + cb.height - 100
ballVX = 0
ballVY = 0
ball.setPosition(
ballX,
ballY
)
shadow.setPosition(
ballX,
ballY + 10
)
updateScore()
if (
balls >= totalBalls ||
wickets >= maxWickets
) {
matchOver = true
message =
"? MATCH OVER! FINAL " +
score +
"/" +
wickets
} else {
message =
"SPACE = NEXT BALL"
}
}
def wicket(): Unit = {
wickets += 1
wicketsLost += 1
combo = 0
message =
"? WICKET!"
resetBall()
}
// ============================================================
// 99 : RESTART
// ============================================================
def restartGame(): Unit = {
score = 0
wickets = 0
balls = 0
fours = 0
sixes = 0
singles = 0
dotBalls = 0
combo = 0
bestCombo = 0
boundaryCount = 0
perfectShots = 0
wicketsLost = 0
ballActive = false
ballHit = false
shotPlayed = false
matchOver = false
paused = false
ballX =
cb.x + cb.width / 2
ballY =
cb.y + cb.height - 100
ball.setPosition(
ballX,
ballY
)
shadow.setPosition(
ballX,
ballY + 10
)
message =
"SPACE = BOWL"
updateScore()
}
// ============================================================
// 100 : MAIN GAME LOOP
// ============================================================
animate {
// Restart
if (isKeyPressed(Kc.VK_R)) {
restartGame()
}
// Pause
if (isKeyPressed(Kc.VK_P)) {
paused = !paused
}
if (
!paused &&
!matchOver
) {
// Bowl
if (isKeyPressed(Kc.VK_SPACE)) {
bowl()
}
// ------------------------------------------
// BALL PHYSICS
// ------------------------------------------
if (ballActive) {
ballX += ballVX
ballY += ballVY
ball.setPosition(
ballX,
ballY
)
shadow.setPosition(
ballX,
ballY + 10
)
// ----------------------------------------
// BATTING ZONE
// ----------------------------------------
if (
ballY > cb.y + 75 &&
ballY < cb.y + 145 &&
!shotPlayed
) {
if (isKeyPressed(Kc.VK_W)) {
playShot("pull")
} else if (isKeyPressed(Kc.VK_S)) {
playShot("straight")
} else if (isKeyPressed(Kc.VK_A)) {
playShot("cover")
} else if (isKeyPressed(Kc.VK_D)) {
playShot("defence")
} else if (isKeyPressed(Kc.VK_Q)) {
playShot("loft")
} else if (isKeyPressed(Kc.VK_E)) {
playShot("special")
}
}
// ----------------------------------------
// MISSED BALL
// ----------------------------------------
if (
ballY < cb.y + 40 &&
!shotPlayed
) {
wicket()
}
// ----------------------------------------
// BALL FINISHED
// ----------------------------------------
if (
ballY > cb.y + cb.height - 40 &&
ballHit
) {
resetBall()
}
}
batsman.update()
}
// ----------------------------------------------------------
// IMPORTANT KOJO TEXT API
// ----------------------------------------------------------
scoreDisplay.setText(scoreText)
messageDisplay.setText(message)
statsDisplay.setText(
"4s: " +
fours +
" | 6s: " +
sixes +
" | Combo: " +
combo +
" | Best: " +
bestCombo
)
}
// ============================================================
// START SCREEN
// ============================================================
println("================================================")
println(" ? ULTRA LEGEND CRICKET ?")
println("================================================")
println("SPACE = Bowl")
println("W = Pull Shot -> FOUR")
println("S = Straight Drive -> SIX")
println("A = Cover Drive -> FOUR")
println("D = Defence -> ONE")
println("Q = Lofted Shot -> SIX")
println("E = Special Shot -> LEGEND SIX")
println("P = Pause")
println("R = Restart")
println("================================================")
println("10 LEGEND PLAYERS")
println("30 BALLS | 5 WICKETS")
println("100-FEATURE GAME ENGINE")
println("================================================")