QB64 Phoenix Edition
Little ASCII Race Car Game - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Programs (https://qb64phoenix.com/forum/forumdisplay.php?fid=7)
+---- Thread: Little ASCII Race Car Game (/showthread.php?tid=2121)



Little ASCII Race Car Game - TerryRitchie - 10-27-2023

I'm working on some very simple games for users of the tutorial to tear apart and learn from.

The very first computer game I ever played was a text race car game on the TRS-80 Model I in 1980.

I've recreated it here for your ASCII enjoyment.

Code: (Select All)
'* Race Day!
'* Inspired by the first computer game I ever played on a TRS-80 Model I in 1980
'* Terry Ritchie
'* for the QB64 tutorial ( www.qb64tutorial.com )
'*
'* RIGHT/LEFT arrow keys to steer car, ESC to exit.
'*
DIM RoadWidth AS INTEGER ' width of roadway
DIM RoadX AS INTEGER '     left location of roadway
DIM Road AS STRING '       one segment of roadway
DIM CarX AS INTEGER '      car location on roadway
DIM CarY AS INTEGER
DIM Travel AS INTEGER '    distance traveled counter
DIM Speed AS SINGLE '      current speed of car
DIM Direction AS INTEGER ' next turn direction on roadway
DIM Turn AS INTEGER '      distance to turn in direction
DIM Marker AS STRING * 2 ' road side markers
DIM Crash AS INTEGER '     crash indicator
DIM Score AS INTEGER '     player's score

RANDOMIZE TIMER '                                    seed the random number generator
RoadWidth = 44 '                                     initial width of roadway
RoadX = 18 '                                         initial left side of roadway
CarX = 40 '                                          intitial car location
CarY = 1
Speed = .2 '                                         initial speed
Turn = 25 '                                          turn counter
Marker = "||" '                                      initial road markers
DO '                                                 BEGIN MAIN PROGRAM LOOP
    Travel = Travel + 1 '                            increment travel counter
    IF Travel = 25 THEN '                            25 frames gone by?
        Travel = 0 '                                 reset travel counter
        IF Speed > .07 THEN Speed = Speed - .02 '    increase speed
        IF RoadWidth > 7 THEN '                      road width at least 8?
            RoadWidth = RoadWidth - 2 '              yes, decrease width of roadway
            RoadX = RoadX + 1 '                      move roadway to right one position
        END IF
        IF CarY < 14 THEN CarY = CarY + 1 '          increase car position on roadway
    END IF
    Turn = Turn - 1 '                                decrement turn counter
    IF Turn = 1 THEN Marker = "||" '                 make a smooth transition between curves
    IF Turn = 0 THEN '                               has this turn ended?
        Direction = INT(RND * 3) - 1 '               which new direction? (-1, 0, or 1)
        Turn = INT(RND * 7) + 2 '                    how long should turn last?
        IF Direction = -1 THEN Marker = "//" '       create appropriate turn road markers
        IF Direction = 0 THEN Marker = "||"
        IF Direction = 1 THEN Marker = "\\"
    END IF
    RoadX = RoadX + Direction '                      move road in direction
    IF RoadX < 1 THEN '                              too far left?
        RoadX = 1 '                                  yes, keep here
        Marker = "||" '                              straighten the road out
    ELSEIF RoadX > 80 - LEN(Road) THEN '             too far right?
        RoadX = 80 - LEN(Road) '                     yes, keep here
        Marker = "||" '                              straighten the road out
    END IF
    IF _KEYDOWN(19200) THEN CarX = CarX - 1 '        move car left when left arrow pressed
    IF _KEYDOWN(19712) THEN CarX = CarX + 1 '        move car right when right arrow pressed
    Road = Marker + SPACE$(RoadWidth) + Marker '     create road segment
    LOCATE 24, RoadX '                               position cursor for road segment
    PRINT Road '                                     print road segment
    IF SCREEN(CarY, CarX, 0) = 32 THEN '             is the road clear?
        LOCATE CarY, CarX '                          yes, position cursor for car
        PRINT "V" '                                  print the car
        LOCATE 1, 1 '                                position cursor at top left corner
        Score = Score + 1 '                          increment score
        PRINT "Score:"; Score; '                     print player's score
        _DELAY Speed '                               pause at current speed
    ELSE
        Crash = -1 '                                 no, kaboom
    END IF
LOOP UNTIL _KEYDOWN(27) OR Crash '                   leave when player crashes or presses ESC
IF Crash THEN '                                      did player crash?
    LOCATE CarY, CarX '                              yes, position cursor at car
    PRINT "*" '                                      print wrecked car
    LOCATE 1, 1 '                                    position cursor at top left
    PRINT "---------------------" '                  print player's final score
    PRINT "------ !CRASH! ------"
    PRINT "---------------------"
    PRINT " Final Score:"; Score
    PRINT "---------------------"
    PRINT "- Press ESC to exit -"
    PRINT "---------------------"
    DO '                                             begin exit loop
        _LIMIT 10 '                                  don't hog cpu
    LOOP UNTIL _KEYDOWN(27) '                        leave when player presses ESC
END IF
SYSTEM '                                             return to operating system