Tutorials › dbc Classic › 9
Sprites
A scooter dodging rocks on the sea: the start of any 2D game.
What you need

sea.bmpexamples/dbc/tutorials/sprite/
scooter.bmpexamples/dbc/tutorials/sprite/
rock.bmpexamples/dbc/tutorials/sprite/A background
LOAD BITMAP loads a picture into an off-screen bitmap. Bitmap 0 is the screen, so switch back to
it with SET CURRENT BITMAP, then COPY BITMAP the background onto it every frame:
sprites_background.dba excerptDownload
load bitmap "sea.bmp", 1
set current bitmap 0
...
rem every frame, the background covers the last one
copy bitmap 1, 0Sprites
A sprite shows an image at a position. SPRITE creates it the first time and moves it after.
Black pixels in the image are transparent.
sprites_sprite.dba excerptDownload
load image "scooter.bmp", 1
...
sprite 1, x#, h - 90, 1 : rem sprite number, x, y, imageCollisions
SPRITE HIT is 1 on the first frame two sprites touch; SPRITE COLLISION is 1 as long as
they overlap. Use the first to count hits, the second to keep a shield on.
sprites.dbaDownload
rem Sprites - OpenDBC tutorial (DarkBASIC Classic)
rem Media: sea.bmp, scooter.bmp and rock.bmp from examples/dbc/tutorials/sprite
sync on : sync rate 60
w = screen width() : h = screen height()
load bitmap "sea.bmp", 1
set current bitmap 0
load image "scooter.bmp", 1
load image "rock.bmp", 2
rem Sprite 1 is the scooter; black pixels are transparent
x# = w / 2
sprite 1, x#, h - 90, 1
rem Sprite 2 is a rock falling from the top
rx = rnd(w - 48) : ry# = -50
sprite 2, rx, ry#, 2
hits = 0
do
rem Draw the sea, then the sprites appear on top
copy bitmap 1, 0
rem Steer with the arrow keys, or follow the rock on your own
if leftkey() = 1 then x# = x# - 5
if rightkey() = 1 then x# = x# + 5
if leftkey() + rightkey() = 0 then x# = x# + (rx - x#) * 0.02
sprite 1, x#, h - 90, 1
ry# = ry# + 4
if ry# > h then ry# = -50 : rx = rnd(w - 48)
sprite 2, rx, ry#, 2
rem SPRITE HIT is 1 on the first frame two sprites touch
if sprite hit(1, 2) > 0 then hits = hits + 1
text 10, 10, "Hits: " + str$(hits)
sync
loopTry this
- Several rocks with arrays:
dim rockx(5). - Flip the scooter when it turns with
MIRROR SPRITE. - Scroll the sea by copying the bitmap in two slices with the long form of
COPY BITMAP.
The repository's rock_dodge.dba
is the full game, scrolling sea included.