08-14-2024, 04:18 AM
This was one of my early QB64 games from 2019. I wanted to simulate a handheld electronics toy I had as a kid and I think I did a pretty good job.
Tonight I updated this game to not only use the arrow keys but also the letters a, d, w, and x, to steer and speed up and slow down. The track is only a straightaway but it can be fun with the obstacles of other cars and oil slicks. It makes the file: toptenracers.txt for the top ten scores. To erase them, just delete the file and it will start over.
Tonight I updated this game to not only use the arrow keys but also the letters a, d, w, and x, to steer and speed up and slow down. The track is only a straightaway but it can be fun with the obstacles of other cars and oil slicks. It makes the file: toptenracers.txt for the top ten scores. To erase them, just delete the file and it will start over.
Code: (Select All)
'I've wanted to make this game since I was a kid when I owned a little hand-held race car game where you steered the little wheel"
'to avoid the other cars. That toy broke when I was a kid so I've wanted a similar one on the computer.
'Enjoy!
'
'Version 3 was made on July 16, 2019.
'Updated on August 13, 2024 for added keys and sound.
'Freeware only.
'
'Thanks to the QB64.org forum guys for the help!
_Title "RACE CAR"
_ScreenMove _Middle
Screen _NewImage(640, 480, 32)
begin:
Cls
Print " R A C E C A R"
Print
Print " V. 3"
Print: Print
Print " By SierraKen"
Print: Print
Print " Use your arrow keys or 'a' and 'd' to steer your car"
Print " around the other cars and go as far as you"
Print " can without hitting them, as well as the round"
Print " oil slicks. You get an extra car every 3000 points."
Print
Print " Press the up and down arrow keys or 'w' and 'x' to move"
Print " faster or slower and also to stop turning."
Print
Print " You get 5 cars."
Print " Every time you hit a street edge, you"
Print " lose 200 points."
Print " Every time you hit an oil slick your car"
Print " moves a random direction."
Print " Every time you hit another car you lose a car."
Print
Print " To reset the Top Ten score sheet, delete the file"
Print " named toptenracers.txt and play a full game."
Print
Input " Press Enter to play!", a$
Cls
Dim name$(50), score(50), nm$(50), scc(50)
Line (0, 240)-(640, 240), _RGB32(127, 255, 127)
tim = .02
t2 = 0
rc = 5
x = 320
y = 250: yy = 450
cx = 350: cy = 420
u = 1
sc = 0
one = 0
collision = 0
o = 0
other = 0
sct = 0
go:
_Delay tim
_Limit 200
a$ = InKey$
If a$ = Chr$(27) Then End
If a$ = Chr$(0) + Chr$(72) Or a$ = "w" Or a$ = "W" Then
u = 1
d = 0
r = 0
L = 0
tim = tim - .002
If tim < .008 Then tim = .008
End If
If a$ = Chr$(0) + Chr$(80) Or a$ = "x" Or a$ = "X" Then
u = 0
d = 1
r = 0
L = 0
tim = tim + .002
If tim > .02 Then tim = .02
End If
If a$ = Chr$(0) + Chr$(77) Or a$ = "d" Or a$ = "D" Then r = 1: L = 0
If a$ = Chr$(0) + Chr$(75) Or a$ = "a" Or a$ = "A" Then L = 1: r = 0
If u = 1 Then
GoSub linesmoving:
End If
If d = 1 Then
GoSub linesmoving:
End If
If r = 1 Then
ocx = cx: ocy = cy
GoSub erasecar:
cx = cx + 2
'Check street edge.
If cx > 570 Then
cx = 550
Sound 800, .25
sc = sc - 200
End If
GoSub drawcar:
End If
If L = 1 Then
ocx = cx: ocy = cy
GoSub erasecar:
cx = cx - 2
'Check street edge.
If cx < 40 Then
cx = 60
Sound 800, .25
sc = sc - 200
End If
GoSub drawcar:
End If
'Street Sides
Line (320, 240)-(0, 480), _RGB32(127, 255, 127)
Line (320, 240)-(640, 480), _RGB32(127, 255, 127)
GoSub drawcar:
'Other Car
Randomize Timer
oc = Int(Rnd * 1000) + 1
If oc > 980 And occ = 0 Then
occ = 1
Randomize Timer
oxx2 = (Rnd * 16) - 8
oyyy = 250
oxxx = 305
End If
If occ = 1 Then
oldoyyy = oyyy
oldoxxx = oxxx
oyyy = oyyy + 5
oxxx = oxxx + oxx2
Line (oldoxxx, oldoyyy)-(oldoxxx + 30, oldoyyy + 30), _RGB32(0, 0, 0), BF
Circle (oldoxxx, oldoyyy + 5), 5, _RGB32(0, 0, 0)
Circle (oldoxxx + 30, oldoyyy + 5), 5, _RGB32(0, 0, 0)
Circle (oldoxxx + 30, oldoyyy + 25), 5, _RGB32(0, 0, 0)
Circle (oldoxxx, oldoyyy + 25), 5, _RGB32(0, 0, 0)
Line (oxxx, oyyy)-(oxxx + 30, oyyy + 30), _RGB32(127, 249, 127), BF
Circle (oxxx, oyyy + 5), 5, _RGB32(127, 227, 127)
Circle (oxxx + 30, oyyy + 5), 5, _RGB32(127, 227, 127)
Circle (oxxx + 30, oyyy + 25), 5, _RGB32(127, 227, 127)
Circle (oxxx, oyyy + 25), 5, _RGB32(127, 227, 127)
If oyyy > 640 Then
occ = 0
Line (oxxx, oyyy)-(oxxx + 20, oyyy + 20), _RGB32(0, 0, 0), BF
Circle (oldoxxx, oldoyyy + 5), 5, _RGB32(0, 0, 0)
Circle (oldoxxx + 30, oldoyyy + 5), 5, _RGB32(0, 0, 0)
Circle (oldoxxx + 30, oldoyyy + 25), 5, _RGB32(0, 0, 0)
Circle (oldoxxx, oldoyyy + 25), 5, _RGB32(0, 0, 0)
If collision = 0 Then
other = other + 1
Locate 1, 20: Print "Passed Up Cars:"; other
End If
oyyy = 250
oxxx = 320
collision = 0
GoTo nex:
End If
End If
nex:
'Detect Collision
If cx > oxxx - 10 And cx < oxxx + 40 And cy > oyyy - 5 And cy < oyyy + 35 Then
Sound 800, .25
If collision = 0 Then rc = rc - 1
If rc = 0 Then GoTo done:
collision = 1
End If
'Oil Slick
Randomize Timer
oil = Int(Rnd * 100) + 1
If oil > 98 And oil2 = 0 Then
oil2 = 1
Randomize Timer
oilx2 = (Rnd * 12) - 6
oilyyy = 250
oilxxx = 305
End If
If oil2 = 1 Then
oldoilyyy = oilyyy
oldoilxxx = oilxxx
oilyyy = oilyyy + 5
oilxxx = oilxxx + oilx2
o = o + 1
If o = 1 Then GoTo oil:
For ooo = 1 To 25 Step 5
Circle (oldoilxxx, oldoilyyy + 25), ooo, _RGB32(0, 0, 0)
Next ooo
oil:
For ooo = 1 To 25 Step 5
Circle (oilxxx, oilyyy + 25), ooo, _RGB32(127, 227, 127)
Next ooo
If oilyyy > 640 Then
For ooo = 1 To 25 Step 5
Circle (oldoilxxx, oldoilyyy + 25), ooo, _RGB32(0, 0, 0)
Next ooo
oilyyy = 250
oilxxx = 330
o = 0
oil2 = 0
GoTo nex2:
End If
End If
nex2:
'Detect Collision With Oil Slick
If cx > oilxxx - 5 And cx < oilxxx + 35 And cy > oilyyy - 5 And cy < oilyyy + 35 Then
Sound 800, .25
Randomize Timer
skid = Int(Rnd * 100) + 1
If skid > 50 Then r = 1: L = 0
If skid <= 50 Then L = 1: r = 0
tim = .02
Sound 900, .25
End If
'Buildings
Randomize Timer
b = Int(Rnd * 100) + 1
If b > 80 And b2 = 0 Then
b2 = 1
Randomize Timer
bx2 = (Rnd * 4) - 2
If bx2 > 0 Then
Randomize Timer
bxxx = Int(Rnd * 200) + 370
bx3 = 10
End If
If bx2 <= 0 Then
Randomize Timer
bxxx = Int(Rnd * 150) + 10
bx3 = -10
End If
Randomize Timer
bsz = Int(Rnd * 30) + 10
Randomize Timer
bsz2 = Int(Rnd * 30) + 10
byyy = 255
End If
If b2 = 1 Then
oldbyyy = byyy
oldbxxx = bxxx
byyy = byyy + 5
bxxx = bxxx + bx3
ob = ob + 1
If ob = 1 Then GoTo building:
'old erase
Line (oldbxxx, oldbyyy)-(oldbxxx + bsz, oldbyyy + bsz2), _RGB32(0, 0, 0), B
building:
'new
Line (bxxx, byyy)-(bxxx + bsz, byyy + bsz2), _RGB32(127, 227, 127), B
If byyy > 640 Then
'old erase again
Line (oldbxxx, oldbyyy)-(oldbxxx + bsz, oldbyyy + bsz2), _RGB32(0, 0, 0), B
byyy = 250
ob = 0
b2 = 0
GoTo nex3:
End If
End If
nex3:
Randomize Timer
bb = Int(Rnd * 100) + 1
If bb > 80 And bb2 = 0 Then
bb2 = 1
Randomize Timer
bbx2 = (Rnd * 4) - 2
If bbx2 > 0 Then
Randomize Timer
bbxxx = Int(Rnd * 200) + 350
bbx3 = 10
End If
If bbx2 <= 0 Then
Randomize Timer
bbxxx = Int(Rnd * 150) + 10
bbx3 = -10
End If
Randomize Timer
bbsz = Int(Rnd * 30) + 10
Randomize Timer
bbsz2 = Int(Rnd * 30) + 10
bbyyy = 255
End If
If bb2 = 1 Then
oldbbyyy = bbyyy
oldbbxxx = bbxxx
bbyyy = bbyyy + 5
bbxxx = bbxxx + bbx3
obb = obb + 1
If obb = 1 Then GoTo building:
'old erase
Line (oldbbxxx, oldbbyyy)-(oldbbxxx + bbsz, oldbbyyy + bbsz2), _RGB32(0, 0, 0), B
building2:
'new
Line (bbxxx, bbyyy)-(bbxxx + bbsz, bbyyy + bbsz2), _RGB32(127, 227, 127), B
If bbyyy > 640 Then
'old erase again
Line (oldbbxxx, oldbbyyy)-(oldbbxxx + bbsz, oldbbyyy + bbsz2), _RGB32(0, 0, 0), B
bbyyy = 250
obb = 0
bb2 = 0
GoTo nex4:
End If
End If
nex4:
'Extra Car
If sc / 3000 = Int(sc / 3000) And sc / 3000 <> 0 Then
rc = rc + 1
Locate 15, 35: Print "EXTRA CAR!"
Sound 400, 2
Sound 400, 2
Sound 400, 2
t2 = 1
End If
If t2 > 500 Then
Locate 15, 35: Print " "
t2 = 0
End If
If t2 > 0 Then t2 = t2 + 1
'Speed Indicator
Locate 3, 1: Print "Speed: "; Int(1.5 / tim)
Locate 3, 13: Print "mph"
'Score
If sc < 1 Then sc = 1
Locate 1, 1: Print "Score: "; sc
sc = sc + 1
'Cars You Have Leftover
Locate 1, 73: Print "Cars:"; rc
Sound 150, .2
GoTo go:
linesmoving:
oy = y
y = y + 5
If y > 640 Then y = 250
If y < 240 Then y = 640
Line (x, oy)-(x, oy + 10), _RGB32(0, 0, 0)
Line (x, y)-(x, y + 10), _RGB32(127, 255, 127)
oyy = yy
yy = yy + 5
If yy > 640 Then yy = 250
If yy < 240 Then yy = 640
Line (x, oyy)-(x, oyy + 10), _RGB32(0, 0, 0)
Line (x, yy)-(x, yy + 10), _RGB32(127, 255, 127)
Return
drawcar:
'Car
Line (cx, cy)-(cx + 30, cy + 30), _RGB32(127, 227, 127), BF
Line (cx + 5, cy - 5)-(cx + 25, cy), _RGB32(127, 227, 127), BF
Circle (cx, cy + 5), 5, _RGB32(127, 227, 127)
Circle (cx + 30, cy + 5), 5, _RGB32(127, 227, 127)
Circle (cx + 30, cy + 25), 5, _RGB32(127, 227, 127)
Circle (cx, cy + 25), 5, _RGB32(127, 227, 127)
Return
erasecar:
Line (ocx, ocy)-(ocx + 30, ocy + 30), _RGB32(0, 0, 0), BF
Line (ocx + 5, ocy - 5)-(ocx + 25, ocy), _RGB32(0, 0, 0), BF
Circle (ocx, ocy + 5), 5, _RGB32(0, 0, 0)
Circle (ocx + 30, ocy + 5), 5, _RGB32(0, 0, 0)
Circle (ocx + 30, ocy + 25), 5, _RGB32(0, 0, 0)
Circle (ocx, ocy + 25), 5, _RGB32(0, 0, 0)
Return
done:
Locate 10, 30: Print "G A M E O V E R"
Locate 1, 1: Print "Score: "; sc
scc = sc * other
Locate 14, 1: Print sc; " x "; other; " Passed Up Cars = Total Score: "; scc
Locate 1, 73: Print "Cars:"; rc
Locate 20, 1: Input "Press Enter to see Top Ten list.", topten$
Cls
'Top Ten Racers
If _FileExists("toptenracers.txt") Then
Else
Open "toptenracers.txt" For Output As #1
Restore toptendata
Do
Read toptenname$, toptenscore!
If toptenname$ = "EOF" Then Exit Do
Print #1, toptenname$
Print #1, toptenscore!
Loop
Close #1
End If
Open "toptenracers.txt" For Input As #1
For n = 1 To 10
If EOF(1) Then GoTo nex5:
Input #1, name$(n)
Input #1, score(n)
If scc > score(n) And sct = 0 Then
nn = n
Print "You have made the Top Ten!"
typename:
Input "Type your name here (25 letters and spaces maximum.):", nm$(nn)
If Len(nm$(nn)) > 25 Then
Print "Name too long, try again."
GoTo typename:
End If
sccc(nn) = scc
sct = 1
End If
If n = 10 And sct = 0 Then Close #1: GoTo nex7:
Next n
Close #1
nex5:
Close #1
Open "toptenracers.txt" For Output As #1
For n = 1 To nn
If n <> nn Then Print #1, name$(n): Print #1, score(n)
If n = nn Then
Print #1, nm$(n)
Print #1, sccc(n)
End If
Next n
nex6:
For n = nn To 10
Print #1, name$(n): Print #1, score(n)
Next n
Close #1
nex7:
Cls
Print: Print: Print
Print " T O P T E N R A C E R S"
Print: Print: Print
Open "toptenracers.txt" For Input As #1
For n = 1 To 10
If EOF(1) Then GoTo nex8:
Input #1, name$(n)
Input #1, score(n)
Print " "; n; ". "; name$(n); score(n)
Next n
nex8:
Close #1
For n = 1 To 10
nm$(n) = ""
score(n) = 0
name$(n) = ""
sccc(n) = 0
Next n
Locate 23, 1
Input "Would you like to play again? (Yes/No)", ag$
If ag$ = "y" Or ag$ = "Y" Or ag$ = "YES" Or ag$ = "yes" Or ag$ = "Yes" Or ag$ = "yES" Or ag$ = "yeS" Then GoTo begin:
End
toptendata:
Data Electro Joe,500000
Data Flying Felice,450000
Data Speedy Spencer,400000
Data Super Sam,350000
Data Ralph Runner,300000
Data Suzy Swift,275000
Data Quick Ken,250000
Data Tiger Tessa,225000
Data Brisk Bob,200000
Data Jalopy Jay,175000
Data EOF,0