Author Topic: Coin Race!  (Read 3868 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Coin Race!
« on: August 16, 2020, 03:05:20 am »
Try to beat the computer in gathering coins between the grid of blocks in 60 seconds by driving your future car around against his future car. Use the arrow keys. After 60 seconds it asks if you want to play again. The score and the time are kept track on the title bar. This is a project I've been working on for 2 days and is the first time I've ever made a grid game or app that is anything like this. It's not perfect, there's some bugs here and there. For example, once in a great while you can get stuck on a block but if you hold down an arrow key you can drive through it, but it's not too often.
Press the S key to turn sound off and on during play. It's a pretty simple game. The computer doesn't have A.I., just random directional changes. So if you can get used to the arrow keys, you can win every time. I might make some kind of A.I. in the future, but I kinda rushed for this first version. But please tell me what you think, thanks.

Code: QB64: [Select]
  1. 'Coin Race! By SierraKen
  2. 'August 16, 2020
  3. 'Use the arrow keys to race against the computer to collect coins.
  4. 'Press the S key to turn the sound off and on.
  5.  
  6. start:
  7. SCREEN _NEWIMAGE(620, 620, 32)
  8. DIM xx(1000), yy(1000)
  9. _LIMIT 1000
  10. score = 0
  11. score$ = ""
  12. score2 = 0
  13. score2$ = ""
  14. soun = 1
  15. coin = 0
  16. x = 5
  17. y = 5
  18. cc = 0
  19. co = 0
  20. ex = 300: ey = 300
  21. dir = INT(RND * 4) + 1
  22. PAINT (2, 2), _RGB32(0, 0, 255)
  23. 'Setup Coins
  24. FOR yy = 20 TO 600 STEP 36
  25.     FOR xx = 20 TO 600 STEP 36
  26.         co = co + 1
  27.         xx(co) = xx: yy(co) = yy
  28.         FOR sz = .25 TO 3 STEP .25
  29.             CIRCLE (xx(co), yy(co)), sz, _RGB32(255, 255, 128)
  30.         NEXT sz
  31.         coin = coin + 1
  32.     NEXT xx
  33. NEXT yy
  34. coin = coin - 67 'Delete the coin count in the red boxes.
  35. t2 = TIMER + 60
  36.     t3 = TIMER
  37.     t1 = INT(t3 - t2)
  38.     IF t1 = 0 THEN GOTO done:
  39.     a$ = INKEY$
  40.     IF a$ = CHR$(0) + CHR$(72) THEN d = 1
  41.     IF a$ = CHR$(0) + CHR$(80) THEN d = 2
  42.     IF a$ = CHR$(0) + CHR$(75) THEN d = 3
  43.     IF a$ = CHR$(0) + CHR$(77) THEN d = 4
  44.     IF a$ = CHR$(27) THEN END
  45.     IF a$ = "s" OR a$ = "S" THEN soun = soun + 1
  46.     'Erase Future Vehicle
  47.     LINE (oldx, oldy)-(oldx + 15, oldy + 15), _RGB32(0, 0, 255), BF
  48.     FOR tires = 1 TO 4
  49.         IF tires = 2 THEN ty = 15
  50.         IF tires = 3 THEN tx = 15
  51.         IF tires = 4 THEN ty = 15: tx = 15
  52.         FOR sz = .25 TO 3 STEP .25
  53.             CIRCLE (oldx + tx, oldy + ty), sz, _RGB32(0, 0, 255)
  54.         NEXT sz
  55.         ty = 0: tx = 0
  56.     NEXT tires
  57.  
  58.     'Check to see if you hit a red square.
  59.     FOR iy = 40 TO 560 STEP 72
  60.         FOR ix = 40 TO 560 STEP 72
  61.             FOR t = -18 TO 33
  62.                 FOR tt = -18 TO 33
  63.                     IF x = ix + tt AND y = iy + t THEN
  64.                         IF d = 3 THEN x = x + 5
  65.                         IF d = 4 THEN x = x - 5
  66.                         IF d = 1 THEN y = y + 5
  67.                         IF d = 2 THEN y = y - 5
  68.                         d = 0
  69.                         GOTO nex:
  70.                     END IF
  71.                 NEXT tt
  72.             NEXT t
  73.         NEXT ix
  74.     NEXT iy
  75.  
  76.     'Check to see if you got a coin.
  77.     FOR yy = 20 TO 600 STEP 36
  78.         FOR xx = 20 TO 600 STEP 36
  79.             cc = cc + 1
  80.             FOR cx = -18 TO 18 STEP 1
  81.                 FOR cy = -18 TO 18 STEP 1
  82.                     IF x + cx = xx(cc) AND y + cy = yy(cc) THEN
  83.                         IF POINT(xx(cc), yy(cc)) = _RGB32(255, 0, 0) THEN GOTO nex3:
  84.                         FOR sz = .25 TO 3 STEP .25
  85.                             CIRCLE (xx(cc), yy(cc)), sz, _RGB32(0, 0, 255)
  86.                         NEXT sz
  87.                         score = score + 1
  88.                         score$ = STR$(score)
  89.                         t1$ = STR$(t1)
  90.                         _TITLE "Coin Race! Time: " + t1$ + "    You: " + score$ + "    Computer: " + score2$
  91.                         xx(cc) = -100: yy(cc) = -100
  92.                         IF soun / 2 <> INT(soun / 2) THEN FOR snd = 400 TO 500 STEP 25: SOUND snd, .5: NEXT snd
  93.                         GOTO nex3:
  94.                     END IF
  95.                 NEXT cy
  96.             NEXT cx
  97.         NEXT xx
  98.     NEXT yy
  99.     nex3:
  100.     cc = 0
  101.     'Direction of vehicle and movement.
  102.     IF d = 1 THEN y = y - 5
  103.     IF d = 2 THEN y = y + 5
  104.     IF d = 3 THEN x = x - 5
  105.     IF d = 4 THEN x = x + 5
  106.     oldx = x
  107.     oldy = y
  108.     nex:
  109.     'If you go off the screen.
  110.     IF x < -15 THEN x = 615
  111.     IF y < -15 THEN y = 615
  112.     IF x > 615 THEN x = -15
  113.     IF y > 615 THEN y = -15
  114.  
  115.     'Draw Future Vehicle
  116.     LINE (x, y)-(x + 15, y + 15), _RGB32(0, 255, 0), BF
  117.     FOR tires = 1 TO 4
  118.         IF tires = 2 THEN ty = 15
  119.         IF tires = 3 THEN tx = 15
  120.         IF tires = 4 THEN ty = 15: tx = 15
  121.         FOR sz = .25 TO 3 STEP .25
  122.             CIRCLE (x + tx, y + ty), sz, _RGB32(128, 255, 127)
  123.         NEXT sz
  124.         ty = 0: tx = 0
  125.     NEXT tires
  126.  
  127.     'Blocks (red squares)
  128.     FOR iy = 40 TO 560 STEP 72
  129.         FOR ix = 40 TO 560 STEP 72
  130.             LINE (ix, iy)-(ix + 30, iy + 30), _RGB32(255, 0, 0), BF
  131.         NEXT ix
  132.     NEXT iy
  133.  
  134.     'Enemy A.I. -------------------------------------------------------
  135.  
  136.     'If enemy goes off the screen.
  137.     IF ex < -15 THEN ex = 615
  138.     IF ey < -15 THEN ey = 615
  139.     IF ex > 615 THEN ex = -15
  140.     IF ey > 615 THEN ey = -15
  141.  
  142.     'Erase the older enemy.
  143.     LINE (oldex, oldey)-(oldex + 15, oldey + 15), _RGB32(0, 0, 255), BF
  144.     FOR tires = 1 TO 4
  145.         IF tires = 2 THEN ty = 15
  146.         IF tires = 3 THEN tx = 15
  147.         IF tires = 4 THEN ty = 15: tx = 15
  148.         FOR sz = .25 TO 3 STEP .25
  149.             CIRCLE (oldex + tx, oldey + ty), sz, _RGB32(0, 0, 255)
  150.         NEXT sz
  151.         ty = 0: tx = 0
  152.     NEXT tires
  153.  
  154.     'Check to see if enemy went over a coin.
  155.     FOR yy = 20 TO 600 STEP 36
  156.         FOR xx = 20 TO 600 STEP 36
  157.             cc2 = cc2 + 1
  158.             FOR cx = -18 TO 18 STEP 1
  159.                 FOR cy = -18 TO 18 STEP 1
  160.                     IF oldex + cx = xx(cc2) AND oldey + cy = yy(cc2) THEN
  161.                         xx(cc2) = -200: yy(cc2) = -200
  162.                         IF POINT(xx(cc), yy(cc)) = _RGB32(255, 0, 0) THEN GOTO nex6:
  163.                         FOR sz = .25 TO 7 STEP .25
  164.                             CIRCLE (xx(cc), yy(cc)), sz, _RGB32(0, 0, 255)
  165.                         NEXT sz
  166.                         score2 = score2 + 1
  167.                         score2$ = STR$(score2)
  168.                         t1$ = STR$(t1)
  169.                         _TITLE "Coin Race! Time: " + t1$ + "    You: " + score$ + "    Computer: " + score2$
  170.                         GOTO nex5:
  171.                     END IF
  172.                     nex6:
  173.                 NEXT cy
  174.             NEXT cx
  175.         NEXT xx
  176.     NEXT yy
  177.     nex5:
  178.     cc2 = 0
  179.  
  180.     ee = INT(RND * 1000) + 1
  181.     IF ee > 920 THEN 'This number is how often the computer changes direction, the smaller the more frequent.
  182.         dir = INT(RND * 4) + 1
  183.     END IF
  184.     oldex = ex: oldey = ey
  185.     IF dir = 1 THEN ex = ex + 5
  186.     IF dir = 2 THEN ex = ex - 5
  187.     IF dir = 3 THEN ey = ey + 5
  188.     IF dir = 4 THEN ey = ey - 5
  189.  
  190.     'Check to see if the enemy hit a square.
  191.     FOR iy = 40 TO 560 STEP 72
  192.         FOR ix = 40 TO 560 STEP 72
  193.             FOR t = -18 TO 33
  194.                 FOR tt = -18 TO 33
  195.                     IF ex = ix + tt AND ey = iy + t THEN
  196.                         IF dir = 1 THEN dir = 3: ex = ex - 5
  197.                         IF dir = 2 THEN dir = 4: ex = ex + 5
  198.                         IF dir = 3 THEN dir = 1: ey = ey - 5
  199.                         IF dir = 4 THEN dir = 2: ey = ey + 5
  200.                         GOTO nex4:
  201.                     END IF
  202.                 NEXT tt
  203.             NEXT t
  204.         NEXT ix
  205.     NEXT iy
  206.     nex4:
  207.  
  208.     'Draw the computer.
  209.     LINE (ex, ey)-(ex + 15, ey + 15), _RGB32(255, 133, 0), BF
  210.     FOR tires = 1 TO 4
  211.         IF tires = 2 THEN ty = 15
  212.         IF tires = 3 THEN tx = 15
  213.         IF tires = 4 THEN ty = 15: tx = 15
  214.         FOR sz = .25 TO 3 STEP .25
  215.             CIRCLE (ex + tx, ey + ty), sz, _RGB32(128, 255, 127)
  216.         NEXT sz
  217.         ty = 0: tx = 0
  218.     NEXT tires
  219.     t1$ = STR$(t1)
  220.     _TITLE "Coin Race! Time: " + t1$ + "    You: " + score$ + "    Computer: " + score2$
  221.     _DISPLAY
  222.  
  223. done:
  224. _TITLE "T H E    E N D........ You: " + score$ + "    Computer: " + score2$ + "    Again? (Y/N)"
  225. again3:
  226. ag$ = INKEY$
  227. IF LEFT$(ag$, 1) = "y" OR LEFT$(ag$, 1) = "Y" THEN GOTO start:
  228. IF LEFT$(ag$, 1) = "n" OR LEFT$(ag$, 1) = "N" THEN END
  229. GOTO again3:
  230.  


Offline dajan

  • Newbie
  • Posts: 41
    • View Profile
Re: Coin Race!
« Reply #1 on: August 16, 2020, 07:00:43 am »
Nice game SierraKen, a bit easy to win, though. Challenging AI would make the code much longer. Maybe you could try something simple, like storing unpicked coin counts in rows/columns in array and turning the AI car into the one with the highest value on its way direction.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Coin Race!
« Reply #2 on: August 16, 2020, 10:15:37 am »
Good work! Of course, the game AI could be improved and it will be more fun to play it.
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Coin Race!
« Reply #3 on: August 16, 2020, 11:55:15 am »
Amazing what you can turn into a game!

I have the usual problem of being able to turn on a dime, maybe you can come up with something for old codgers with keyboards that have 4 arrows in the space of 3 keys. (I was robbed!)  :)
« Last Edit: August 16, 2020, 11:56:33 am by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Coin Race!
« Reply #4 on: August 16, 2020, 12:54:46 pm »
Thanks guys! B+, I'll add keys to turn for you if you want. Would W, A, S, Z work? Or W,A, D, X?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Coin Race!
« Reply #5 on: August 16, 2020, 01:29:54 pm »
Thanks guys! B+, I'll add keys to turn for you if you want. Would W, A, S, Z work? Or W,A, D, X?

Thanks but no thanks, I like WASZ system even less, I am severely right handed. :-)

I was thinking press an arrow once = move one step, not shoot off in that direction but something with mouse would be better! (I know you don't want to do things like that, don't worry about it.)

Or press arrows to set direction and use spacebar to take steps, can you even do that?

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Coin Race!
« Reply #6 on: August 16, 2020, 03:41:44 pm »
I'm not sure how the space bar would help. Your car moves automatically without pressing anything, you just press the arrow once and it changes direction and keeps going. I might try mouse control as a selection. Like if you press M you can steer with the mouse. I think I'll try that. :)

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Coin Race!
« Reply #7 on: August 16, 2020, 03:58:03 pm »
I just tried using a mouse 2 different ways. One just to change direction, which didn't work well at all, the car kept going all kinds of weird ways lol. So then I tried direct control of the car wherever the pointer was and that was not good either, was a mess because it didn't erase the last car's position hardly at all and you could go inside the red blocks. lol Oh well I tried. If there's anything else, go right ahead. Or of course you can add to it yourself if you want. But I'm still thinking about that A.I. a little bit, or maybe adding more competitors if it won't slow down too much.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Coin Race!
« Reply #8 on: August 16, 2020, 04:35:11 pm »
Thanks for trying Ken, I am trying to help you think of different approaches is all.

I don't know how you guys can turn so quickly down one street or another I keep crashing into red blocks or borders, mostly borders.

AI for car? how to get it to cover whole block with little repetition? Go down one lane, turn and go up next... until whole square is covered then repeat :)

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Coin Race!
« Reply #9 on: August 16, 2020, 05:48:31 pm »
I think I got it down pact now. Instead of 1 enemy car (computer) I made 2. Sometimes I win, sometimes I lose. :D No A.I. needed except it took a lot of tries to get the right number for using Random on how often they change direction. The more often they change direction, the harder it is. Bplus, would it be easier if I added the number pad? 8 = up, 2 = down, 4 = left, 6 = right ?

Code: QB64: [Select]
  1. 'Coin Race! By SierraKen
  2. 'August 16, 2020
  3. 'Use the arrow keys to race against the computer to collect coins.
  4. 'Press the S key to turn the sound off and on.
  5.  
  6. start:
  7. SCREEN _NEWIMAGE(620, 620, 32)
  8. DIM xx(1000), yy(1000)
  9. _LIMIT 1000
  10. score = 0
  11. score$ = ""
  12. score2 = 0
  13. score2$ = ""
  14. soun = 1
  15. coin = 0
  16. x = 5
  17. y = 5
  18. cc = 0
  19. co = 0
  20. ry = 0: rx = 0
  21. ex = 270: ey = 300
  22. ex2 = 330: ey2 = 300
  23. dir = INT(RND * 4) + 1
  24. dir2 = INT(RND * 4) + 1
  25. d = INT(RND * 4) + 1
  26. PAINT (2, 2), _RGB32(0, 0, 255)
  27. 'Setup Coins
  28. FOR yy = 20 TO 600 STEP 36
  29.     FOR xx = 20 TO 600 STEP 36
  30.         co = co + 1
  31.         xx(co) = xx: yy(co) = yy
  32.         FOR sz = .25 TO 3 STEP .25
  33.             CIRCLE (xx(co), yy(co)), sz, _RGB32(255, 255, 128)
  34.         NEXT sz
  35.         coin = coin + 1
  36.     NEXT xx
  37. NEXT yy
  38.  
  39. coin = coin - 67 'Delete the coin count in the red boxes.
  40. t2 = TIMER + 60
  41.     t3 = TIMER
  42.     t1 = INT(t3 - t2)
  43.     IF t1 = 0 THEN GOTO done:
  44.     a$ = INKEY$
  45.     IF a$ = CHR$(0) + CHR$(72) THEN d = 1
  46.     IF a$ = CHR$(0) + CHR$(80) THEN d = 2
  47.     IF a$ = CHR$(0) + CHR$(75) THEN d = 3
  48.     IF a$ = CHR$(0) + CHR$(77) THEN d = 4
  49.     IF a$ = CHR$(27) THEN END
  50.     IF a$ = "s" OR a$ = "S" THEN soun = soun + 1
  51.     'Erase Future Vehicle
  52.     LINE (oldx, oldy)-(oldx + 15, oldy + 15), _RGB32(0, 0, 255), BF
  53.     FOR tires = 1 TO 4
  54.         IF tires = 2 THEN ty = 15
  55.         IF tires = 3 THEN tx = 15
  56.         IF tires = 4 THEN ty = 15: tx = 15
  57.         FOR sz = .25 TO 3 STEP .25
  58.             CIRCLE (oldx + tx, oldy + ty), sz, _RGB32(0, 0, 255)
  59.         NEXT sz
  60.         ty = 0: tx = 0
  61.     NEXT tires
  62.  
  63.     'Check to see if you hit a red square.
  64.     FOR iy = 40 TO 560 STEP 72
  65.         FOR ix = 40 TO 560 STEP 72
  66.             FOR t = -18 TO 33
  67.                 FOR tt = -18 TO 33
  68.                     IF x = ix + tt AND y = iy + t THEN
  69.                         IF d = 3 THEN x = x + 5
  70.                         IF d = 4 THEN x = x - 5
  71.                         IF d = 1 THEN y = y + 5
  72.                         IF d = 2 THEN y = y - 5
  73.                         d = 0
  74.                         GOTO nex:
  75.                     END IF
  76.                 NEXT tt
  77.             NEXT t
  78.         NEXT ix
  79.     NEXT iy
  80.  
  81.     'Check to see if you got a coin.
  82.     FOR yy = 20 TO 600 STEP 36
  83.         FOR xx = 20 TO 600 STEP 36
  84.             cc = cc + 1
  85.             FOR cx = -18 TO 18 STEP 1
  86.                 FOR cy = -18 TO 18 STEP 1
  87.                     IF x + cx = xx(cc) AND y + cy = yy(cc) THEN
  88.                         IF POINT(xx(cc), yy(cc)) = _RGB32(255, 0, 0) THEN GOTO nex3:
  89.                         FOR sz = .25 TO 3 STEP .25
  90.                             CIRCLE (xx(cc), yy(cc)), sz, _RGB32(0, 0, 255)
  91.                         NEXT sz
  92.                         score = score + 1
  93.                         score$ = STR$(score)
  94.                         t1$ = STR$(t1)
  95.                         _TITLE "Coin Race! Time: " + t1$ + "    You: " + score$ + "    Computer: " + score2$
  96.                         xx(cc) = -100: yy(cc) = -100
  97.                         IF soun / 2 <> INT(soun / 2) THEN FOR snd = 400 TO 500 STEP 25: SOUND snd, .5: NEXT snd
  98.                         GOTO nex3:
  99.                     END IF
  100.                 NEXT cy
  101.             NEXT cx
  102.         NEXT xx
  103.     NEXT yy
  104.     nex3:
  105.     cc = 0
  106.     'Direction of vehicle and movement.
  107.     IF d = 1 THEN y = y - 5
  108.     IF d = 2 THEN y = y + 5
  109.     IF d = 3 THEN x = x - 5
  110.     IF d = 4 THEN x = x + 5
  111.     oldx = x
  112.     oldy = y
  113.     nex:
  114.     'If you go off the screen.
  115.     IF x < -15 THEN x = 615
  116.     IF y < -15 THEN y = 615
  117.     IF x > 615 THEN x = -15
  118.     IF y > 615 THEN y = -15
  119.  
  120.     'Draw Future Vehicle
  121.     LINE (x, y)-(x + 15, y + 15), _RGB32(0, 255, 0), BF
  122.     FOR tires = 1 TO 4
  123.         IF tires = 2 THEN ty = 15
  124.         IF tires = 3 THEN tx = 15
  125.         IF tires = 4 THEN ty = 15: tx = 15
  126.         FOR sz = .25 TO 3 STEP .25
  127.             CIRCLE (x + tx, y + ty), sz, _RGB32(128, 255, 127)
  128.         NEXT sz
  129.         ty = 0: tx = 0
  130.     NEXT tires
  131.  
  132.     'Blocks (red squares)
  133.     FOR iy = 40 TO 560 STEP 72
  134.         FOR ix = 40 TO 560 STEP 72
  135.             LINE (ix, iy)-(ix + 30, iy + 30), _RGB32(255, 0, 0), BF
  136.         NEXT ix
  137.     NEXT iy
  138.  
  139.     'Computer -------------------------------------------------------
  140.  
  141.     'If computer goes off the screen.
  142.     IF ex < -15 THEN ex = 615
  143.     IF ey < -15 THEN ey = 615
  144.     IF ex > 615 THEN ex = -15
  145.     IF ey > 615 THEN ey = -15
  146.     IF ex2 < -15 THEN ex2 = 615
  147.     IF ey2 < -15 THEN ey2 = 615
  148.     IF ex2 > 615 THEN ex2 = -15
  149.     IF ey2 > 615 THEN ey2 = -15
  150.  
  151.  
  152.     'Erase the older computer.
  153.     LINE (oldex, oldey)-(oldex + 15, oldey + 15), _RGB32(0, 0, 255), BF
  154.     FOR tires = 1 TO 4
  155.         IF tires = 2 THEN ty = 15
  156.         IF tires = 3 THEN tx = 15
  157.         IF tires = 4 THEN ty = 15: tx = 15
  158.         FOR sz = .25 TO 3 STEP .25
  159.             CIRCLE (oldex + tx, oldey + ty), sz, _RGB32(0, 0, 255)
  160.         NEXT sz
  161.         ty = 0: tx = 0
  162.     NEXT tires
  163.  
  164.     LINE (oldex2, oldey2)-(oldex2 + 15, oldey2 + 15), _RGB32(0, 0, 255), BF
  165.     FOR tires = 1 TO 4
  166.         IF tires = 2 THEN ty = 15
  167.         IF tires = 3 THEN tx = 15
  168.         IF tires = 4 THEN ty = 15: tx = 15
  169.         FOR sz = .25 TO 3 STEP .25
  170.             CIRCLE (oldex2 + tx, oldey2 + ty), sz, _RGB32(0, 0, 255)
  171.         NEXT sz
  172.         ty = 0: tx = 0
  173.     NEXT tires
  174.  
  175.  
  176.     'Check to see if computer went over a coin.
  177.     FOR yy = 20 TO 600 STEP 36
  178.         FOR xx = 20 TO 600 STEP 36
  179.             cc2 = cc2 + 1
  180.             FOR cx = -18 TO 18 STEP 1
  181.                 FOR cy = -18 TO 18 STEP 1
  182.                     IF oldex + cx = xx(cc2) AND oldey + cy = yy(cc2) THEN
  183.                         xx(cc2) = -200: yy(cc2) = -200
  184.                         IF POINT(xx(cc), yy(cc)) = _RGB32(255, 0, 0) THEN GOTO nex6:
  185.                         FOR sz = .25 TO 7 STEP .25
  186.                             CIRCLE (xx(cc), yy(cc)), sz, _RGB32(0, 0, 255)
  187.                         NEXT sz
  188.                         score2 = score2 + 1
  189.                         score2$ = STR$(score2)
  190.                         t1$ = STR$(t1)
  191.                         _TITLE "Coin Race! Time: " + t1$ + "    You: " + score$ + "    Computer: " + score2$
  192.                     END IF
  193.                     nex6:
  194.                     IF oldex2 + cx = xx(cc2) AND oldey2 + cy = yy(cc2) THEN
  195.                         xx(cc2) = -200: yy(cc2) = -200
  196.                         IF POINT(xx(cc), yy(cc)) = _RGB32(255, 0, 0) THEN GOTO nex6:
  197.                         FOR sz = .25 TO 7 STEP .25
  198.                             CIRCLE (xx(cc), yy(cc)), sz, _RGB32(0, 0, 255)
  199.                         NEXT sz
  200.                         score2 = score2 + 1
  201.                         score2$ = STR$(score2)
  202.                         t1$ = STR$(t1)
  203.                         _TITLE "Coin Race! Time: " + t1$ + "    You: " + score$ + "    Computer: " + score2$
  204.                         GOTO nex5:
  205.                     END IF
  206.                 NEXT cy
  207.             NEXT cx
  208.         NEXT xx
  209.     NEXT yy
  210.     nex5:
  211.     cc2 = 0
  212.  
  213.     ee = INT(RND * 1000) + 1
  214.     IF ee > 970 THEN 'This number is how often the computer changes direction, the smaller the more frequent.
  215.         dir = INT(RND * 4) + 1
  216.     END IF
  217.     oldex = ex: oldey = ey
  218.     IF dir = 1 THEN ex = ex + 5
  219.     IF dir = 2 THEN ex = ex - 5
  220.     IF dir = 3 THEN ey = ey + 5
  221.     IF dir = 4 THEN ey = ey - 5
  222.  
  223.     ee2 = INT(RND * 1000) + 1
  224.     IF ee2 > 970 THEN 'This number is how often the computer changes direction, the smaller the more frequent.
  225.         dir2 = INT(RND * 4) + 1
  226.     END IF
  227.     oldex2 = ex2: oldey2 = ey2
  228.     IF dir2 = 1 THEN ex2 = ex2 + 5
  229.     IF dir2 = 2 THEN ex2 = ex2 - 5
  230.     IF dir2 = 3 THEN ey2 = ey2 + 5
  231.     IF dir2 = 4 THEN ey2 = ey2 - 5
  232.  
  233.  
  234.     'Check to see if the computer hit a square.
  235.     FOR iy = 40 TO 560 STEP 72
  236.         FOR ix = 40 TO 560 STEP 72
  237.             FOR t = -18 TO 33
  238.                 FOR tt = -18 TO 33
  239.                     IF ex = ix + tt AND ey = iy + t THEN
  240.                         IF dir = 1 THEN dir = 3: ex = ex - 5
  241.                         IF dir = 2 THEN dir = 4: ex = ex + 5
  242.                         IF dir = 3 THEN dir = 1: ey = ey - 5
  243.                         IF dir = 4 THEN dir = 2: ey = ey + 5
  244.                     END IF
  245.                     IF ex2 = ix + tt AND ey2 = iy + t THEN
  246.                         IF dir = 1 THEN dir = 3: ex2 = ex2 - 5
  247.                         IF dir = 2 THEN dir = 4: ex2 = ex2 + 5
  248.                         IF dir = 3 THEN dir = 1: ey2 = ey2 - 5
  249.                         IF dir = 4 THEN dir = 2: ey2 = ey2 + 5
  250.                         GOTO nex4:
  251.                     END IF
  252.                 NEXT tt
  253.             NEXT t
  254.         NEXT ix
  255.     NEXT iy
  256.     nex4:
  257.  
  258.     'Draw the computer.
  259.     LINE (ex, ey)-(ex + 15, ey + 15), _RGB32(255, 133, 0), BF
  260.     FOR tires = 1 TO 4
  261.         IF tires = 2 THEN ty = 15
  262.         IF tires = 3 THEN tx = 15
  263.         IF tires = 4 THEN ty = 15: tx = 15
  264.         FOR sz = .25 TO 3 STEP .25
  265.             CIRCLE (ex + tx, ey + ty), sz, _RGB32(128, 255, 127)
  266.         NEXT sz
  267.         ty = 0: tx = 0
  268.     NEXT tires
  269.  
  270.     LINE (ex2, ey2)-(ex2 + 15, ey2 + 15), _RGB32(255, 133, 0), BF
  271.     FOR tires = 1 TO 4
  272.         IF tires = 2 THEN ty = 15
  273.         IF tires = 3 THEN tx = 15
  274.         IF tires = 4 THEN ty = 15: tx = 15
  275.         FOR sz = .25 TO 3 STEP .25
  276.             CIRCLE (ex2 + tx, ey2 + ty), sz, _RGB32(128, 255, 127)
  277.         NEXT sz
  278.         ty = 0: tx = 0
  279.     NEXT tires
  280.  
  281.     t1$ = STR$(t1)
  282.     _TITLE "Coin Race! Time: " + t1$ + "    You: " + score$ + "    Computer: " + score2$
  283.     _DISPLAY
  284.  
  285. done:
  286. _TITLE "T H E    E N D........ You: " + score$ + "    Computer: " + score2$ + "    Again? (Y/N)"
  287. again3:
  288. ag$ = INKEY$
  289. IF ag$ = "y" OR ag$ = "Y" THEN GOTO start:
  290. IF ag$ = "n" OR ag$ = "N" THEN END
  291. GOTO again3:
  292.  
« Last Edit: August 16, 2020, 05:52:12 pm by SierraKen »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Coin Race!
« Reply #10 on: August 16, 2020, 05:56:41 pm »
Bplus, you also might want to slow it down. I've seen before on my stuff that your computer might run faster than mine for some reason. You can slow down the _LIMIT or you can reduce the numbers that x, y, ex, ey, ex2, and ey2 move every loop. They are set on 5 right now I believe. The car is easier to control slowed down I've seen.  _LIMIT is set at 1000 on line 10.

Note: I made an update that changes speed as you like it on a post below.



 
« Last Edit: August 16, 2020, 07:03:00 pm by SierraKen »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Coin Race!
« Reply #11 on: August 16, 2020, 07:04:50 pm »
This update lets you change speed. It starts out in the middle ( 5 ) and by pressing + or - it speeds up or slows down the game. The slowest it goes is 1 and the fastest is 10. It's almost impossible to play at 10. lol Bplus, see if this helps you play it at around 3 or 4 I bet. I first tried _LIMIT but for some reason that does nothing (I even moved _LIMIT to the loop). So instead I sped up the cars and slowed them down. I think Limit won't speed up the program anymore because of so many FOR/NEXT loops I use. But this is fine instead.

Code: QB64: [Select]
  1. 'Coin Race! By SierraKen
  2. 'August 16, 2020
  3. 'Use the arrow keys to race against the computer to collect coins.
  4. 'Press the S key to turn the sound off and on.
  5. 'Control speed of game with + and - keys.
  6.  
  7. start:
  8. SCREEN _NEWIMAGE(620, 620, 32)
  9. DIM xx(1000), yy(1000)
  10. _LIMIT 1000
  11. score = 0
  12. score$ = ""
  13. score2 = 0
  14. score2$ = ""
  15. soun = 1
  16. coin = 0
  17. speed = 5
  18. x = 5
  19. y = 5
  20. cc = 0
  21. co = 0
  22. ry = 0: rx = 0
  23. ex = 270: ey = 300
  24. ex2 = 330: ey2 = 300
  25. dir = INT(RND * 4) + 1
  26. dir2 = INT(RND * 4) + 1
  27. d = INT(RND * 4) + 1
  28. PAINT (2, 2), _RGB32(0, 0, 255)
  29. 'Setup Coins
  30. FOR yy = 20 TO 600 STEP 36
  31.     FOR xx = 20 TO 600 STEP 36
  32.         co = co + 1
  33.         xx(co) = xx: yy(co) = yy
  34.         FOR sz = .25 TO 3 STEP .25
  35.             CIRCLE (xx(co), yy(co)), sz, _RGB32(255, 255, 128)
  36.         NEXT sz
  37.         coin = coin + 1
  38.     NEXT xx
  39. NEXT yy
  40.  
  41. coin = coin - 67 'Delete the coin count in the red boxes.
  42. t2 = TIMER + 60
  43.     t3 = TIMER
  44.     t1 = INT(t3 - t2)
  45.     IF t1 = 0 THEN GOTO done:
  46.     a$ = INKEY$
  47.     IF a$ = CHR$(0) + CHR$(72) THEN d = 1
  48.     IF a$ = CHR$(0) + CHR$(80) THEN d = 2
  49.     IF a$ = CHR$(0) + CHR$(75) THEN d = 3
  50.     IF a$ = CHR$(0) + CHR$(77) THEN d = 4
  51.     IF a$ = CHR$(27) THEN END
  52.     IF a$ = "s" OR a$ = "S" THEN soun = soun + 1
  53.     IF a$ = "-" THEN speed = speed - 1
  54.     IF a$ = "+" THEN speed = speed + 1
  55.     IF speed > 10 THEN speed = 10
  56.     IF speed < 1 THEN speed = 1
  57.     'Erase Future Vehicle
  58.     LINE (oldx, oldy)-(oldx + 15, oldy + 15), _RGB32(0, 0, 255), BF
  59.     FOR tires = 1 TO 4
  60.         IF tires = 2 THEN ty = 15
  61.         IF tires = 3 THEN tx = 15
  62.         IF tires = 4 THEN ty = 15: tx = 15
  63.         FOR sz = .25 TO 3 STEP .25
  64.             CIRCLE (oldx + tx, oldy + ty), sz, _RGB32(0, 0, 255)
  65.         NEXT sz
  66.         ty = 0: tx = 0
  67.     NEXT tires
  68.  
  69.     'Check to see if you hit a red square.
  70.     FOR iy = 40 TO 560 STEP 72
  71.         FOR ix = 40 TO 560 STEP 72
  72.             FOR t = -18 TO 33
  73.                 FOR tt = -18 TO 33
  74.                     IF x = ix + tt AND y = iy + t THEN
  75.                         IF d = 3 THEN x = x + speed
  76.                         IF d = 4 THEN x = x - speed
  77.                         IF d = 1 THEN y = y + speed
  78.                         IF d = 2 THEN y = y - speed
  79.                         d = 0
  80.                         GOTO nex:
  81.                     END IF
  82.                 NEXT tt
  83.             NEXT t
  84.         NEXT ix
  85.     NEXT iy
  86.  
  87.     'Check to see if you got a coin.
  88.     FOR yy = 20 TO 600 STEP 36
  89.         FOR xx = 20 TO 600 STEP 36
  90.             cc = cc + 1
  91.             FOR cx = -18 TO 18 STEP 1
  92.                 FOR cy = -18 TO 18 STEP 1
  93.                     IF x + cx = xx(cc) AND y + cy = yy(cc) THEN
  94.                         IF POINT(xx(cc), yy(cc)) = _RGB32(255, 0, 0) THEN GOTO nex3:
  95.                         FOR sz = .25 TO 3 STEP .25
  96.                             CIRCLE (xx(cc), yy(cc)), sz, _RGB32(0, 0, 255)
  97.                         NEXT sz
  98.                         score = score + 1
  99.                         score$ = STR$(score)
  100.                         t1$ = STR$(t1)
  101.                         speed$ = STR$(speed)
  102.                         _TITLE "Coin Race! Time: " + t1$ + "    You: " + score$ + "    Computer: " + score2$ + "    Speed Of Game (Keys + / -): " + speed$
  103.                         xx(cc) = -100: yy(cc) = -100
  104.                         IF soun / 2 <> INT(soun / 2) THEN FOR snd = 400 TO 500 STEP 25: SOUND snd, .5: NEXT snd
  105.                         GOTO nex3:
  106.                     END IF
  107.                 NEXT cy
  108.             NEXT cx
  109.         NEXT xx
  110.     NEXT yy
  111.     nex3:
  112.     cc = 0
  113.     'Direction of vehicle and movement.
  114.     IF d = 1 THEN y = y - speed
  115.     IF d = 2 THEN y = y + speed
  116.     IF d = 3 THEN x = x - speed
  117.     IF d = 4 THEN x = x + speed
  118.     oldx = x
  119.     oldy = y
  120.     nex:
  121.     'If you go off the screen.
  122.     IF x < -15 THEN x = 615
  123.     IF y < -15 THEN y = 615
  124.     IF x > 615 THEN x = -15
  125.     IF y > 615 THEN y = -15
  126.  
  127.     'Draw Future Vehicle
  128.     LINE (x, y)-(x + 15, y + 15), _RGB32(0, 255, 0), BF
  129.     FOR tires = 1 TO 4
  130.         IF tires = 2 THEN ty = 15
  131.         IF tires = 3 THEN tx = 15
  132.         IF tires = 4 THEN ty = 15: tx = 15
  133.         FOR sz = .25 TO 3 STEP .25
  134.             CIRCLE (x + tx, y + ty), sz, _RGB32(128, 255, 127)
  135.         NEXT sz
  136.         ty = 0: tx = 0
  137.     NEXT tires
  138.  
  139.     'Blocks (red squares)
  140.     FOR iy = 40 TO 560 STEP 72
  141.         FOR ix = 40 TO 560 STEP 72
  142.             LINE (ix, iy)-(ix + 30, iy + 30), _RGB32(255, 0, 0), BF
  143.         NEXT ix
  144.     NEXT iy
  145.  
  146.     'Computer -------------------------------------------------------
  147.  
  148.     'If computer goes off the screen.
  149.     IF ex < -15 THEN ex = 615
  150.     IF ey < -15 THEN ey = 615
  151.     IF ex > 615 THEN ex = -15
  152.     IF ey > 615 THEN ey = -15
  153.     IF ex2 < -15 THEN ex2 = 615
  154.     IF ey2 < -15 THEN ey2 = 615
  155.     IF ex2 > 615 THEN ex2 = -15
  156.     IF ey2 > 615 THEN ey2 = -15
  157.  
  158.     'Erase the older computer.
  159.     LINE (oldex, oldey)-(oldex + 15, oldey + 15), _RGB32(0, 0, 255), BF
  160.     FOR tires = 1 TO 4
  161.         IF tires = 2 THEN ty = 15
  162.         IF tires = 3 THEN tx = 15
  163.         IF tires = 4 THEN ty = 15: tx = 15
  164.         FOR sz = .25 TO 3 STEP .25
  165.             CIRCLE (oldex + tx, oldey + ty), sz, _RGB32(0, 0, 255)
  166.         NEXT sz
  167.         ty = 0: tx = 0
  168.     NEXT tires
  169.  
  170.     LINE (oldex2, oldey2)-(oldex2 + 15, oldey2 + 15), _RGB32(0, 0, 255), BF
  171.     FOR tires = 1 TO 4
  172.         IF tires = 2 THEN ty = 15
  173.         IF tires = 3 THEN tx = 15
  174.         IF tires = 4 THEN ty = 15: tx = 15
  175.         FOR sz = .25 TO 3 STEP .25
  176.             CIRCLE (oldex2 + tx, oldey2 + ty), sz, _RGB32(0, 0, 255)
  177.         NEXT sz
  178.         ty = 0: tx = 0
  179.     NEXT tires
  180.  
  181.     'Check to see if computer went over a coin.
  182.     FOR yy = 20 TO 600 STEP 36
  183.         FOR xx = 20 TO 600 STEP 36
  184.             cc2 = cc2 + 1
  185.             FOR cx = -18 TO 18 STEP 1
  186.                 FOR cy = -18 TO 18 STEP 1
  187.                     IF oldex + cx = xx(cc2) AND oldey + cy = yy(cc2) THEN
  188.                         xx(cc2) = -200: yy(cc2) = -200
  189.                         IF POINT(xx(cc), yy(cc)) = _RGB32(255, 0, 0) THEN GOTO nex6:
  190.                         FOR sz = .25 TO 7 STEP .25
  191.                             CIRCLE (xx(cc), yy(cc)), sz, _RGB32(0, 0, 255)
  192.                         NEXT sz
  193.                         score2 = score2 + 1
  194.                         score2$ = STR$(score2)
  195.                         t1$ = STR$(t1)
  196.                         speed$ = STR$(speed)
  197.                         _TITLE "Coin Race! Time: " + t1$ + "    You: " + score$ + "    Computer: " + score2$ + "    Speed Of Game (Keys + / -): " + speed$
  198.                     END IF
  199.                     nex6:
  200.                     IF oldex2 + cx = xx(cc2) AND oldey2 + cy = yy(cc2) THEN
  201.                         xx(cc2) = -200: yy(cc2) = -200
  202.                         IF POINT(xx(cc), yy(cc)) = _RGB32(255, 0, 0) THEN GOTO nex6:
  203.                         FOR sz = .25 TO 7 STEP .25
  204.                             CIRCLE (xx(cc), yy(cc)), sz, _RGB32(0, 0, 255)
  205.                         NEXT sz
  206.                         score2 = score2 + 1
  207.                         score2$ = STR$(score2)
  208.                         t1$ = STR$(t1)
  209.                         speed$ = STR$(speed)
  210.                         _TITLE "Coin Race! Time: " + t1$ + "    You: " + score$ + "    Computer: " + score2$ + "    Speed Of Game (Keys + / -): " + speed$
  211.                         GOTO nex5:
  212.                     END IF
  213.                 NEXT cy
  214.             NEXT cx
  215.         NEXT xx
  216.     NEXT yy
  217.     nex5:
  218.     cc2 = 0
  219.  
  220.     ee = INT(RND * 1000) + 1
  221.     IF ee > 970 THEN 'This number is how often the computer changes direction, the smaller the more frequent.
  222.         dir = INT(RND * 4) + 1
  223.     END IF
  224.     oldex = ex: oldey = ey
  225.     IF dir = 1 THEN ex = ex + speed
  226.     IF dir = 2 THEN ex = ex - speed
  227.     IF dir = 3 THEN ey = ey + speed
  228.     IF dir = 4 THEN ey = ey - speed
  229.  
  230.     ee2 = INT(RND * 1000) + 1
  231.     IF ee2 > 970 THEN 'This number is how often the computer changes direction, the smaller the more frequent.
  232.         dir2 = INT(RND * 4) + 1
  233.     END IF
  234.     oldex2 = ex2: oldey2 = ey2
  235.     IF dir2 = 1 THEN ex2 = ex2 + speed
  236.     IF dir2 = 2 THEN ex2 = ex2 - speed
  237.     IF dir2 = 3 THEN ey2 = ey2 + speed
  238.     IF dir2 = 4 THEN ey2 = ey2 - speed
  239.  
  240.  
  241.     'Check to see if the computer hit a square.
  242.     FOR iy = 40 TO 560 STEP 72
  243.         FOR ix = 40 TO 560 STEP 72
  244.             FOR t = -18 TO 33
  245.                 FOR tt = -18 TO 33
  246.                     IF ex = ix + tt AND ey = iy + t THEN
  247.                         IF dir = 1 THEN dir = 3: ex = ex - 5
  248.                         IF dir = 2 THEN dir = 4: ex = ex + 5
  249.                         IF dir = 3 THEN dir = 1: ey = ey - 5
  250.                         IF dir = 4 THEN dir = 2: ey = ey + 5
  251.                     END IF
  252.                     IF ex2 = ix + tt AND ey2 = iy + t THEN
  253.                         IF dir = 1 THEN dir = 3: ex2 = ex2 - 5
  254.                         IF dir = 2 THEN dir = 4: ex2 = ex2 + 5
  255.                         IF dir = 3 THEN dir = 1: ey2 = ey2 - 5
  256.                         IF dir = 4 THEN dir = 2: ey2 = ey2 + 5
  257.                         GOTO nex4:
  258.                     END IF
  259.                 NEXT tt
  260.             NEXT t
  261.         NEXT ix
  262.     NEXT iy
  263.     nex4:
  264.  
  265.     'Draw the computer.
  266.     LINE (ex, ey)-(ex + 15, ey + 15), _RGB32(255, 133, 0), BF
  267.     FOR tires = 1 TO 4
  268.         IF tires = 2 THEN ty = 15
  269.         IF tires = 3 THEN tx = 15
  270.         IF tires = 4 THEN ty = 15: tx = 15
  271.         FOR sz = .25 TO 3 STEP .25
  272.             CIRCLE (ex + tx, ey + ty), sz, _RGB32(128, 255, 127)
  273.         NEXT sz
  274.         ty = 0: tx = 0
  275.     NEXT tires
  276.  
  277.     LINE (ex2, ey2)-(ex2 + 15, ey2 + 15), _RGB32(255, 133, 0), BF
  278.     FOR tires = 1 TO 4
  279.         IF tires = 2 THEN ty = 15
  280.         IF tires = 3 THEN tx = 15
  281.         IF tires = 4 THEN ty = 15: tx = 15
  282.         FOR sz = .25 TO 3 STEP .25
  283.             CIRCLE (ex2 + tx, ey2 + ty), sz, _RGB32(128, 255, 127)
  284.         NEXT sz
  285.         ty = 0: tx = 0
  286.     NEXT tires
  287.  
  288.     t1$ = STR$(t1)
  289.     speed$ = STR$(speed)
  290.     _TITLE "Coin Race! Time: " + t1$ + "    You: " + score$ + "    Computer: " + score2$ + "    Speed Of Game (Keys + / -): " + speed$
  291.     _DISPLAY
  292.  
  293. done:
  294. _TITLE "T H E    E N D........ You: " + score$ + "    Computer: " + score2$ + "    Again? (Y/N)"
  295. again3:
  296. ag$ = INKEY$
  297. IF ag$ = "y" OR ag$ = "Y" THEN GOTO start:
  298. IF ag$ = "n" OR ag$ = "N" THEN END
  299. GOTO again3:
  300.  
« Last Edit: August 16, 2020, 07:10:46 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Coin Race!
« Reply #12 on: August 16, 2020, 09:53:47 pm »
Quote
lol Bplus, see if this helps you play it at around 3 or 4 I bet.

Actually for a break, I messed around with the one, 2 posts up, and got the hang of things by not expecting to turn 1 block off from where I'm at, to time it for 5 or 6 blocks down the line, not always a turn but much fewer crashes.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Coin Race!
« Reply #13 on: August 16, 2020, 11:04:22 pm »
Yeah that happens when it goes too fast. Try my newest code and slow it down a little bit. It should help. If it does help, you can modify the speed variable to whatever number works for you, that way you won't have to press + or - every time.  The speed variable is set on line 19.
« Last Edit: August 16, 2020, 11:06:56 pm by SierraKen »