Author Topic: Race Car Game  (Read 15686 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Race Car Game
« on: July 14, 2019, 10:57:44 pm »
Hi all,

Today I made a game I've wanted to make since I was a kid when my toy hand-held race car game broke one day.
You start with 5 cars and you move the arrow keys. You can go backwards but it doesn't do anything besides make the lines in the
road move back. I'll put this on my website http://KensProgras.com soon. I know things can be added like oil slick or shooting or whatever
but I think I accomplished a good game in 1 afternoon. :) Tell me what you think. Oh, and I tried to add engine sound but it messed up the sounds
when you hit another car or the side of the road.

(NOTE: This is Version 1, Version 2, which includes many things, is posted on the next page of this forum topic.)

Code: QB64: [Select]
  1. '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"
  2. 'to avoid the other cars. That toy broke when I was a kid so I've wanted a similar one on the computer. :)
  3. 'Enjoy!
  4. '
  5. 'Made on July 14, 2019 by Ken G.
  6. 'Freeware only.
  7.  
  8. _TITLE "RACE CAR"
  9. SCREEN _NEWIMAGE(640, 480, 32)
  10. begin:
  11. PRINT "                             R  A  C  E    C  A  R"
  12. PRINT "                                  By Ken G."
  13. PRINT "                    Use your arrow keys to steer your car"
  14. PRINT "                    around the other cars as far as you can"
  15. PRINT "                    without hitting them."
  16. PRINT "                    You get 5 cars."
  17. PRINT "                    Every time you hit a street edge, you"
  18. PRINT "                    lose 200 points."
  19. INPUT "                    Press Enter to play!", a$
  20.  
  21.  
  22. LINE (0, 240)-(640, 240), _RGB32(127, 255, 127)
  23. rc = 5
  24. x = 320
  25. y = 250: yy = 450
  26. cx = 350: cy = 420
  27. u = 1
  28. sc = 0
  29. collision = 0
  30. go:
  31. _LIMIT 100
  32. a$ = INKEY$
  33. IF a$ = CHR$(27) THEN END
  34. IF a$ = CHR$(0) + CHR$(72) THEN u = 1: d = 0: r = 0: L = 0
  35. IF a$ = CHR$(0) + CHR$(80) THEN u = 0: d = 1: r = 0: L = 0
  36. IF a$ = CHR$(0) + CHR$(77) THEN r = 1: L = 0
  37. IF a$ = CHR$(0) + CHR$(75) THEN L = 1: r = 0
  38. IF u = 1 THEN
  39.     oy = y
  40.     y = y + 5
  41.     IF y > 640 THEN y = 250
  42.     LINE (x, oy)-(x, oy + 10), _RGB32(0, 0, 0)
  43.     LINE (x, y)-(x, y + 10), _RGB32(127, 255, 127)
  44.     oyy = yy
  45.     yy = yy + 5
  46.     IF yy > 640 THEN yy = 250
  47.     IF yy < 240 THEN yy = 640
  48.     LINE (x, oyy)-(x, oyy + 10), _RGB32(0, 0, 0)
  49.     LINE (x, yy)-(x, yy + 10), _RGB32(127, 255, 127)
  50.  
  51. IF d = 1 THEN
  52.     oy = y
  53.     y = y - 5
  54.     IF y < 240 THEN y = 640
  55.     LINE (x, oy)-(x, oy + 10), _RGB32(0, 0, 0)
  56.     LINE (x, y)-(x, y + 10), _RGB32(127, 255, 127)
  57.     oyy = yy
  58.     yy = yy - 5
  59.     IF yy < 240 THEN yy = 640
  60.     IF yy > 640 THEN yy = 250
  61.     LINE (x, oyy)-(x, oyy + 10), _RGB32(0, 0, 0)
  62.     LINE (x, yy)-(x, yy + 10), _RGB32(127, 255, 127)
  63.  
  64. IF r = 1 THEN
  65.     ocx = cx: ocy = cy
  66.     GOSUB erasecar:
  67.     cx = cx + 2
  68.     'Check street edge.
  69.     IF cx > 570 THEN
  70.         cx = 550
  71.         SOUND 800, .25
  72.         sc = sc - 200
  73.     END IF
  74.     GOSUB drawcar:
  75.  
  76.  
  77. IF L = 1 THEN
  78.     ocx = cx: ocy = cy
  79.     GOSUB erasecar:
  80.     cx = cx - 2
  81.     'Check street edge.
  82.     IF cx < 40 THEN
  83.         cx = 60
  84.         SOUND 800, .25
  85.         sc = sc - 200
  86.     END IF
  87.     GOSUB drawcar:
  88. 'Street Sides
  89. LINE (320, 240)-(0, 480), _RGB32(127, 255, 127)
  90. LINE (320, 240)-(640, 480), _RGB32(127, 255, 127)
  91. GOSUB drawcar:
  92.  
  93. 'Other Car
  94. oc = INT(RND * 1000) + 1
  95. IF oc > 980 AND occ = 0 THEN
  96.     occ = 1
  97.     oxx2 = (RND * 12) - 6
  98.     oyyy = 250
  99.     oxxx = 305
  100. IF occ = 1 THEN
  101.     oldoyyy = oyyy
  102.     oldoxxx = oxxx
  103.     oyyy = oyyy + 5
  104.     oxxx = oxxx + oxx2
  105.     LINE (oldoxxx, oldoyyy)-(oldoxxx + 30, oldoyyy + 30), _RGB32(0, 0, 0), BF
  106.     CIRCLE (oldoxxx, oldoyyy + 5), 5, _RGB32(0, 0, 0)
  107.     CIRCLE (oldoxxx + 30, oldoyyy + 5), 5, _RGB32(0, 0, 0)
  108.     CIRCLE (oldoxxx + 30, oldoyyy + 25), 5, _RGB32(0, 0, 0)
  109.     CIRCLE (oldoxxx, oldoyyy + 25), 5, _RGB32(0, 0, 0)
  110.  
  111.     LINE (oxxx, oyyy)-(oxxx + 30, oyyy + 30), _RGB32(127, 249, 127), BF
  112.     CIRCLE (oxxx, oyyy + 5), 5, _RGB32(127, 227, 127)
  113.     CIRCLE (oxxx + 30, oyyy + 5), 5, _RGB32(127, 227, 127)
  114.     CIRCLE (oxxx + 30, oyyy + 25), 5, _RGB32(127, 227, 127)
  115.     CIRCLE (oxxx, oyyy + 25), 5, _RGB32(127, 227, 127)
  116.  
  117.     IF oyyy > 640 THEN
  118.         occ = 0
  119.         LINE (oxxx, oyyy)-(oxxx + 20, oyyy + 20), _RGB32(0, 0, 0), BF
  120.         CIRCLE (oldoxxx, oldoyyy + 5), 5, _RGB32(0, 0, 0)
  121.         CIRCLE (oldoxxx + 30, oldoyyy + 5), 5, _RGB32(0, 0, 0)
  122.         CIRCLE (oldoxxx + 30, oldoyyy + 25), 5, _RGB32(0, 0, 0)
  123.         CIRCLE (oldoxxx, oldoyyy + 25), 5, _RGB32(0, 0, 0)
  124.  
  125.         oyyy = 250
  126.         oxxx = 320
  127.         collision = 0
  128.         GOTO nex:
  129.     END IF
  130. nex:
  131.  
  132. 'Detect Collision
  133. IF cx > oxxx - 5 AND cx < oxxx + 35 AND cy > oyyy - 5 AND cy < oyyy + 35 THEN
  134.     SOUND 800, .25
  135.     IF collision = 0 THEN rc = rc - 1
  136.     IF rc = 0 THEN GOTO done:
  137.     collision = 1
  138.  
  139.  
  140. LOCATE 1, 1: PRINT "Score: "; sc
  141. sc = sc + 1
  142.  
  143. LOCATE 1, 70: PRINT "Cars:"; rc
  144.  
  145. GOTO go:
  146.  
  147. drawcar:
  148. 'Car
  149. LINE (cx, cy)-(cx + 30, cy + 30), _RGB32(127, 227, 127), BF
  150. LINE (cx + 5, cy - 5)-(cx + 25, cy), _RGB32(127, 227, 127), BF
  151. CIRCLE (cx, cy + 5), 5, _RGB32(127, 227, 127)
  152. CIRCLE (cx + 30, cy + 5), 5, _RGB32(127, 227, 127)
  153. CIRCLE (cx + 30, cy + 25), 5, _RGB32(127, 227, 127)
  154. CIRCLE (cx, cy + 25), 5, _RGB32(127, 227, 127)
  155.  
  156. erasecar:
  157. LINE (ocx, ocy)-(ocx + 30, ocy + 30), _RGB32(0, 0, 0), BF
  158. LINE (ocx + 5, ocy - 5)-(ocx + 25, ocy), _RGB32(0, 0, 0), BF
  159. CIRCLE (ocx, ocy + 5), 5, _RGB32(0, 0, 0)
  160. CIRCLE (ocx + 30, ocy + 5), 5, _RGB32(0, 0, 0)
  161. CIRCLE (ocx + 30, ocy + 25), 5, _RGB32(0, 0, 0)
  162. CIRCLE (ocx, ocy + 25), 5, _RGB32(0, 0, 0)
  163.  
  164. done:
  165. LOCATE 10, 30: PRINT "G A M E    O V E R"
  166. LOCATE 14, 30: PRINT "Score: "; sc
  167. LOCATE 1, 70: PRINT "Cars:"; rc
  168. again:
  169. LOCATE 17, 30
  170. INPUT "Would you like to play again? (Yes/No)", ag$
  171. IF ag$ = "y" OR ag$ = "Y" OR ag$ = "YES" OR ag$ = "yes" OR ag$ = "Yes" OR ag$ = "yES" OR ag$ = "yeS" THEN GOTO begin:
  172.  
  173.  
  174.  
« Last Edit: July 16, 2019, 01:55:17 pm by SierraKen »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Race Car Game
« Reply #1 on: July 14, 2019, 11:04:12 pm »
Was that an early arcade game? It seems very familiar.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Race Car Game
« Reply #2 on: July 14, 2019, 11:32:57 pm »
Here is the original. I got it around 1982 or so. It used paper to scroll inside the track with the cars going by. lol When you hit a car it flashed a light at you and made a sound. :) My game looks a bit more like the arcade game Pole Position, but with no turns, just a straight track, like my old toy was.  lol
tomydigitalderbyautoraceway002-1978.jpg
* tomydigitalderbyautoraceway002-1978.jpg (Filesize: 115.13 KB, Dimensions: 1000x750, Views: 293)

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Race Car Game
« Reply #3 on: July 15, 2019, 02:30:57 am »
Hi SierraKen, i like it!

I'm not at home now, but I'm going to look at it more deeply at home. Acceleration and braking fluency can be resolved with the TIMER function, that is, when you add throttle, record time and add a higher speed after some time limit, the same for braking. Definitely a very interesting program. The sounds .... well, I would change the frequency of the sound by the Y-axis distance in the loop.

Something as this:

Code: QB64: [Select]
  1.  
  2. SCREEN _NEWIMAGE(1024, 768, 256)
  3. CarWidth = 70
  4. CarHeight = 115
  5.  
  6.  
  7.     CLS
  8.  
  9.     LINE (512 - CarWidth, 384 - CarHeight)-(512 + CarWidth, 384 + CarHeight), 14, B 'static car
  10.  
  11.     LINE (_MOUSEX - CarWidth, _MOUSEY - CarHeight)-(_MOUSEX + CarWidth, _MOUSEY + CarHeight), 15, B 'car used with mouse
  12.     distance = ABS(_MOUSEY - 384) / 30 'return always positive value
  13.     LOCATE 1
  14.     PRINT distance
  15.  
  16.     FOR S = 150 - distance TO 170 - distance STEP 10
  17.         lenght = S / 900
  18.         SOUND S, lenght
  19.     NEXT S
  20.     _DISPLAY
  21.     _LIMIT 40
  22.  
  23. there muss be _LIMIT used for applying sound difference in time. Or is harder way with SNDLOOP used with SNDSETPOS.
  24.  

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Race Car Game
« Reply #4 on: July 15, 2019, 09:11:11 am »
4498, much better than I would expect with my reflexes. Very cool old school rendering. Takes me back...

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Race Car Game
« Reply #5 on: July 15, 2019, 09:14:48 am »
Hi Ken,

Your best game yet!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Race Car Game
« Reply #6 on: July 15, 2019, 12:59:12 pm »
Wow thanks guys! Petr, yeah I used _DELAY for a long time with it, I'll try it again. I would rather use that than a TIMER loop if I can. I'll also try to figure out a way to add engine sounds but most likely I won't be able to because 2 SOUND commands at the same time don't work well together on my computer for some reason. I wonder if a mp3 sound would work in the background? I'll look into it. I decided that I'm also going to try and add oil slicks and possibly other details. :) So version 2 will be coming soon, I'll post it here when I'm ready.
« Last Edit: July 15, 2019, 01:23:13 pm by SierraKen »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Race Car Game
« Reply #7 on: July 15, 2019, 01:39:38 pm »
This is another one of those cases where I wish there was a way to get programs like this on phone and PDA devices. Windows Phone crapped out, so that pretty much blew out the possibility of a QB64 C/C++ translator at least working on a Windows phone. Android is more JAVA based, but there is an OpenGL for Android. I recall Rob had a JAVA written version of QB64 as something on his wish list, if he ever redid QB64.

This one is remarkably under 200 lines, so I don't know, maybe it could be ported to SmallBasic for Android. It's been a couple of years since I've programmed in that language, so, I really just guessing.

I'm looking forward to the new additions to this race car app. I'm trying to recall if Pole Position had acceleration and deceleration capabilities. I know, I know, if it gets over-complicated, it loses its appeal. Anyway, I do recall a spinning car if it hit an oil slick. I think that would be a great addition.

Pete

Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Race Car Game
« Reply #8 on: July 15, 2019, 01:49:10 pm »
Awesome Pete! Yeah this would be perfect for cell phones, if it was possible. If you ever do use SmallBasic again, feel free to try and import it. So far I just added acceleration and deceleration using the _delay command, it was easier than I thought. lol Since the street is black I will try to add something like "OIL" in green or something. lol I'm also going to add up the amount of cars you pass so at the end it times the score with the amount of cars because someone can drive at like 5 mph all day and never get hit. LOL I might also add an accelerometer to tell people the speed. I'm having a blast with this! I just have to make time for my daily chores. lol

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Race Car Game
« Reply #9 on: July 15, 2019, 04:35:29 pm »
THIS is a cool game. Very simple layout. Continuous movement of the player makes for a challenging game. Unfortunately, my reflexes are not what they used to be and was "killed off" quite quickly, but still a fun game. Nicely done! (not getting killed off.... I meant the making of the game... lol)
Logic is the beginning of wisdom.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Race Car Game
« Reply #10 on: July 15, 2019, 06:49:58 pm »
Thanks Johno! Version 2 should be out soon. I've added oil slicks and acceleration. :) Might add some more things.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Race Car Game
« Reply #11 on: July 15, 2019, 07:37:28 pm »
Looking forward to it...
Logic is the beginning of wisdom.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Race Car Game
« Reply #12 on: July 15, 2019, 09:27:58 pm »
OK it is JUST about done! I got the acceleration/deceleration and speed indicator (You can't go backwards anymore, just slow down to 40 mph). I got 2 buildings at once on the sides of the road, can be 2 on each side or 1 on each side. And I got the oil slick that randomly slides you a direction. Now the last thing I want to add (you can laugh if you want), a Top Ten score list that makes a text file on your computer of the top ten scores so far. Has any of you made one of those yet with QB64? I know how to make a text file and read one, I just have to play with it some to detect the numbers and write the new ones. What I haven't done with QB64 yet is use the APPEND command for the text file and it's been decades since I attempted it, but I will give it a shot. Hold your horses for a little longer folks, I think you all will be surprised on how much the game has changed, I sure am. Oh, in case you are wondering, it's still all green lines, but I like it that way, it sorta gives a green screen 1980's flashback or like TRON or something. :) Just wanted to update ya, will be coming real soon.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Race Car Game
« Reply #13 on: July 15, 2019, 10:30:29 pm »
Do me a favor. When you go to post it, post it by editing the original post, but don't overwrite the original code. Name this new one something like version 2, and post it in another code box, below the first version. Just include some comments above the new version code box, which explains the additions to your newer version. Sound good?

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Race Car Game
« Reply #14 on: July 15, 2019, 11:06:15 pm »
That's fine Pete, I'll leave the first one but will add to the post to scroll down to version 2. :)