Tutorial: How to write games

Go to page << 1 2 3 4 5 6 7 8 9 10 11 >>

Where to now?

Don't worry too much if this all seems like a lot to take in in one setting. It's not important to absorb it all the first time through. The best way to learn is by example and experimentation, so don't be afraid to play around with the examples or try and invent some of your own. You won't break anything!

You may want to try skimming through the Programmer's guide (choose "Programmer's guide" from the "Help" menu in Basic4GL), which lists a number of functions and commands used in Basic4GL for things like joystick and mouse input, resizing the text area or changing the font.

You may also want to play with the demo programs that are distributed with Basic4GL (click "Open..." from the "Program" menu in Basic4GL). Just keep in mind that some of the demo programs use programming features that we haven't learnt yet, and many of them use OpenGL which is a huge topic in it's own right.)

We will finish off this first part of the tutorial with some more example programs/games to try out and experiment with. See if you can figure out how they work, or modify/extend them into your own customised versions.

Bricks

dim score, balls
dim ballx#, bally#, ballxd#, ballyd#
dim paddlex
dim col, line, a$

paddlex = 18
ballx# = 20
bally# = 10
ballxd# = 0
ballyd# = 1
score = 0
balls = 3

color (255, 255, 255)
locate 0, 0: print "Score: 0"
locate 30, 0: print "Balls: 3"
locate 0, 2
color (255, 100, 100)
for line = 1 to 7
    for col = 1 to 20
        print "[]"
    next
next

TextMode (TEXT_BUFFERED)

while true
    color (255, 255, 255)
    locate 0, 0: print "Score: " + score
    locate 30, 0: print "Balls: " + balls
    locate paddlex, 23: print"     "
    if ScanKeyDown (VK_LEFT) and paddlex > 0 then
        paddlex = paddlex - 1
    endif
    if ScanKeyDown (VK_RIGHT) and paddlex < 35 then
        paddlex = paddlex + 1
    endif
    Color (200, 200, 200)
    locate paddlex, 23: print"-----"
    
    Color (255, 255, 255)
    locate ballx#, bally#: print " "
    a$ = CharAt$ (ballx# + ballxd#, bally# + ballyd#)
    if a$ = "-" then 
        ballyd# = -ballyd#
        ballxd# = (int (ballx#) - paddlex - 2) / 2.0
        a$ = " "
    endif
    ballx# = ballx# + ballxd#
    bally# = bally# + ballyd#
    if ballx# <= 0 or ballx# > 39 then ballxd# = -ballxd# endif
    if bally# <= 0 then ballyd# = -ballyd# endif
    if bally# > 23 then
        DrawText ()
        Sleep (1000)
        balls = balls - 1
        if balls > 0 then
            locate paddlex, 23: print"     "
            paddlex = 18
            ballx# = 20
            bally# = 10
            ballxd# = 0
            ballyd# = 1
        else
            Color (255, 255, 255)
            locate 15, 12: print "Game Over!"
            DrawText ()
            end
        endif
    endif           

    locate ballx#, bally#: print "o"
    if a$ = "[" or a$ = "]" then 
        ballyd# = -ballyd# 
        score = score + 1    
    endif
    DrawText ()
    Sleep (50)
wend

Road

dim roadx, carx, score
dim delay#
dim i

roadx = 13
carx = 20
delay# = 100

Color (200, 200, 200)
for i = 1 to 23
    locate roadx, i: print "**          **"
next

TextMode (TEXT_BUFFERED)

while true
    roadx = roadx + rnd () % 3 - 1
    if roadx < 0 then roadx = 0 endif
    if roadx > 40 - 14 then roadx = 40 - 14 endif
    Color (200, 200, 200)
    locate 0, 24: printr
    locate roadx, 23: print "**          **"
    
    score = score + 1
    Color (255, 255, 0)
    locate 0, 0: print "Score = " + score

    if ScanKeyDown (VK_LEFT) then
        carx = carx - 1
    endif
    if ScanKeyDown (VK_RIGHT) then 
        carx = carx + 1
    endif
    if CharAt$ (carx, 12) <> " " then
        Color (255, 100, 10)
        locate carx - 1, 11: print "X X"
        locate carx - 1, 12: print " X "
        locate carx - 1, 13: print "X X"
        Color (255, 255, 255)
        locate 15, 12: print "Game Over!"
        DrawText ()        
        end
    endif
    Color (255, 255, 255)
    locate carx, 12: print "O"
    
    DrawText ()
    Sleep (delay#)
    delay# = delay# - 0.25
wend

Guess a number

dim guesses, number, guess
number = rnd () % 100 + 1

Color (255, 255, 255)
printr "I've chosen a number between 1 and 100"
printr "See if you can guess it in 10 guesses"
printr "To make a guess, type in the number and"
printr "press Enter"
printr

for guesses = 1 to 10
    print "Guess " + guesses + ": "
    guess = val (Input$ ())
    if guess = number then
        printr
        printr "Correct!"
        end
    endif
    if number > guess then 
        printr "The number is greater than " + guess
    else
        printr "The number is less than " + guess
    endif
next

printr
printr "Out of guesses!"
printr "The number was " + number

Worm

dim x, y, xd, yd, key, score
x = 20: y = 12
xd = 0: yd = -1
Color (0, 255, 0)
while true
    key = InScanKey ()
    if key = VK_LEFT and xd <> 1 then
        xd = -1: yd = 0
    endif
    if key = VK_RIGHT and xd <> -1 then
        xd = 1: yd = 0
    endif
    if key = VK_UP and yd <> 1 then 
        xd = 0: yd = -1
    endif
    if key = VK_DOWN and yd <> -1 then
        xd = 0: yd = 1
    endif
    x = x + xd
    y = y + yd
    score = score + 1
    if CharAt$ (x, y) <> " " then
        Color (255, 255, 255)
        locate 15, 11: print "Game Over!"
        locate 15, 13: print "You scored " + score
        end
    endif
    locate x, y: print "X"
    Sleep (75)
wend
Go to page << 1 2 3 4 5 6 7 8 9 10 11 >>