Tutorial: Donkey Game

Code your own remake of the Donkey.bas game from 1981, originally co-written by Bill Gates. Touch screen to change lane.

Source Code

'-------------------------------------------------
' Source code for demo game: Donkey.bas
' Original game concept: IBM (1981)
'-------------------------------------------------
BGCOLOR #000000
DIM title, subtitle, credits, label1, label2, score1, score2, boom AS TEXT
DIM border1 AS RECTANGLE
DIM zone1, zone2 AS ZONE
DIM car, car1, car2, car3, car4 AS IMAGE
DIM donkey, donkey1, donkey2, donkey3, donkey4 AS IMAGE
DIM line1, line2 AS IMAGE
DIM isLeftLane AS BOOLEAN: isLeftLane = true
DIM posLeft, posRight AS INTEGER: posLeft = 215: posRight = 340
DIM posCar, posDonkey AS INTEGER: posCar = 400: posDonkey = 50
DIM scoreDriver, scoreDonkey AS INTEGER: scoreDriver = 0: scoreDonkey = 0
DIM donkeyTimer AS TIMER
DIM donkeyCount AS INTEGER
DIM isCrashed AS BOOLEAN

' Title screen
zone1 = ZONE (0,0)-(640,640)
border1 = RECT (100, 100)-(550, 250), #000000, "fade-in", 1000
COLOR border1, #000000, #55FF55, 3
title = TEXT "DONKEY", (_MIDDLEX, 120), "console3", 120, #55FF55, "fade-in", 1000
WAIT
credits = TEXT "Credits:\noriginal game by IBM (1981)\nremake by D2Soft Technologies (2024)", (_MIDDLEX, 550), "console3", 18, #ffffff, "in-down", 2000
WAIT
SLEEP 500
subtitle = TEXT "Press screen to continue", (_MIDDLEX, 350), "console2", 30, #FFFF55
ANIM subtitle, "blink"
MUSIC "intro_tune"
ON PRESS zone1, startGame, 1

'--------------------- SUBs ---------------------
SUB startGame
STOP MUSIC
HIDE border1: HIDE title : HIDE subtitle : HIDE credits
BGCOLOR #555555 ' Alternative color: #C0EF9E
LINE (0, 0)-(640,0), #AAAAAA, 10
LINE (640, 0)-(640,640), #AAAAAA, 10
LINE (640, 640)-(0,640), #AAAAAA, 10
LINE (0, 640)-(0,0), #AAAAAA, 10
RECT (15,15)-(180, 625), #00AAAA ' Left side road
RECT (460,15)-(625, 625), #00AAAA ' Right side road
RECT (40,45)-(150, 75), #555555 ' Left name
RECT (500,45)-(595, 75), #555555 ' Right name
RECT (70,90)-(110, 116), #555555 ' Left score
RECT (525,90)-(565, 116), #555555 ' Right name
label1 = TEXT "Donkey", (40, 40), "console2", 30, #AAAAAA
label2 = TEXT "Driver", (500, 40), "console2", 30, #AAAAAA
score1 = TEXT "0", (75, 90), "console2", 25, #AAAAAA
score2 = TEXT "0", (530, 90), "console2", 25, #AAAAAA
LINE (190, 0)-(190,640), #AAAAAA, 3
LINE (450, 0)-(450,640), #AAAAAA, 3
line1 = IMAGE "donkey_line", (319, 0)', "hide" ' To use when an accident
SOUND "car_start", "no-wait" '"engine_revving", "no-wait"
car = IMAGE "donkey_car", (posLeft, posCar), "in-down", 2000
WAIT
HIDE line1
line2 = IMAGE "donkey_line_anim", (319, 0)
donkey = IMAGE "donkey_animal", (0, 0), "offscreen" ' , "in-up", 1000
boom = TEXT "BOOM!", (0, 0), "console2", 30, #FFFFFF, "offscreen"
zone2 = ZONE (0,0)-(640,640)
ON PRESS zone2, changeLane
ON COLLIDE car, donkey, crash
MUSIC "speed_up", 30
restartPlay
END SUB

SUB restartPlay
posCar = 400 ' Y position of car, increase over time
donkeyCount = 1
resetCar
donkeyTimer = TIMER 200, resetDonkey
SHOW car
END SUB

SUB resetCar
udpateCarPos
END SUB

SUB speedCar
posCar = posCar - 16
udpateCarPos
END SUB

SUB udpateCarPos
IF isLeftLane THEN
MOVE car, (posLeft, posCar)
ELSE
MOVE car, (posRight, posCar)
END IF
END IF

SUB changeLane
IF NOT isCrashed THEN
IF isLeftLane THEN
isLeftLane = FALSE
ELSE
isLeftLane = TRUE
END IF
SOUND "horn", "no-wait"
udpateCarPos
END IF
END IF

SUB updateScore
SET TEXT score1, STR(scoreDonkey)
SET TEXT score2, STR(scoreDriver)
END SUB

SUB resetDonkey
STOP ANIM donkey
IF RND(1,2) = 1 THEN
MOVE donkey, (posLeft, posDonkey)
ELSE
MOVE donkey, (posRight, posDonkey)
END IF
SHOW donkey
isCrashed = FALSE
MOVE donkey, "out-down", 2000
WAIT
IF NOT isCrashed THEN
donkeyCount = donkeyCount + 1
IF donkeyCount = 12 THEN
playerWins
ELSE
speedCar
END IF
END IF
END SUB

SUB playerWins
STOP TIMER donkeyTimer
scoreDriver = scoreDriver + 1
SOUND "win"
WAIT
updateScore
restartPlay
END SUB

SUB crash
isCrashed = TRUE
STOP TIMER donkeyTimer
STOP ANIM donkey
HIDE car
HIDE donkey
HIDE line2
SHOW line1
MOVE boom, (POSX(car), POSY(car))
SHOW boom
SOUND "explosion2", "no-wait"
car1 = CROP IMAGE "donkey_car", (0, 0)-(40, 62), (POSX(car), POSY(car))
car2 = CROP IMAGE "donkey_car", (40, 0)-(80, 62), (POSX(car)+40, POSY(car))
car3 = CROP IMAGE "donkey_car", (0, 62)-(40, 125), (POSX(car), POSY(car)+62)
car4 = CROP IMAGE "donkey_car", (40, 62)-(80, 125), (POSX(car)+40, POSY(car)+62)
MOVE car1, (0, 0), 750
MOVE car2, (640, 0), 750
MOVE car3, (0, 640), 750
MOVE car4, (640, 640), 750
donkey1 = CROP IMAGE "donkey_animal", (0, 0)-(46, 27), (POSX(donkey), POSY(donkey))
donkey2 = CROP IMAGE "donkey_animal", (46, 0)-(92, 27), (POSX(donkey)+46, POSY(donkey))
donkey3 = CROP IMAGE "donkey_animal", (0, 27)-(46, 55), (POSX(donkey), POSY(donkey)+27)
donkey4 = CROP IMAGE "donkey_animal", (46, 27)-(92, 55), (POSX(donkey)+46, POSY(donkey)+27)
MOVE donkey1, (0, 0), 700
MOVE donkey2, (640, 0), 700
MOVE donkey3, (0, 640), 700
MOVE donkey4, (640, 640), 700
WAIT
HIDE car1: HIDE car2: HIDE car3: HIDE car4
HIDE donkey1: HIDE donkey2: HIDE donkey3: HIDE donkey4
scoreDonkey = scoreDonkey + 1
updateScore
restartPlay
HIDE line1
HIDE boom
SHOW line2
END SUB






Go back to list of Tutorials.