Author Topic: Bump N Go Car Toy  (Read 4240 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Bump N Go Car Toy
« on: March 01, 2022, 08:19:18 am »
Spriggsy suggested this to me at Discord:
Code: QB64: [Select]
  1. _Title "Bump N Go - Drive Car" 'b+ 2022-03-01
  2. ' b+ mod of Drive Car (Graphics Dir) 'b+ 2021-09-29
  3.  
  4. Type car
  5.     As Single x, y, w, h, ra, speed
  6.     As _Unsigned Long c
  7.  
  8. Dim blue As car
  9. blue.x = _Width / 2: blue.y = _Height / 2
  10. blue.w = 20: blue.h = 40
  11. blue.ra = _Pi / 2: blue.speed = 2
  12. blue.c = 9
  13.  
  14. While _KeyDown(27) = 0
  15.     If blue.speed = 0 Then
  16.         blue.ra = _Atan2(blue.y - _Height / 2, blue.x - _Width / 2) + _Pi + Rnd * -.5
  17.         blue.speed = .1
  18.     End If
  19.     blue.speed = blue.speed + .5
  20.     Cls
  21.     blue.x = blue.x + blue.speed * Cos(blue.ra)
  22.     blue.y = blue.y + blue.speed * Sin(blue.ra)
  23.     If blue.x < 10 Or blue.x > _Width - 10 Then blue.speed = 0
  24.     If blue.y < 10 Or blue.y > _Height - 10 Then blue.speed = 0
  25.     drawCar blue
  26.     _Display ' stop flicker
  27.     _Limit 30 ' loops per sec
  28.  
  29. Sub drawCar (a As car)
  30.     ' code not optimized for speed  just proff of concept
  31.     X1 = a.x + a.h / 2 * Cos(a.ra)
  32.     Y1 = a.y + a.h / 2 * Sin(a.ra)
  33.     X2 = X1 + a.w / 2 * Cos(a.ra + _Pi / 2)
  34.     Y2 = Y1 + a.w / 2 * Sin(a.ra + _Pi / 2)
  35.     X3 = X1 + a.w / 2 * Cos(a.ra - _Pi / 2)
  36.     Y3 = Y1 + a.w / 2 * Sin(a.ra - _Pi / 2)
  37.  
  38.     x4 = a.x + a.h / 2 * Cos(a.ra - _Pi)
  39.     y4 = a.y + a.h / 2 * Sin(a.ra - _Pi)
  40.     x5 = x4 + a.w / 2 * Cos(a.ra + _Pi / 2)
  41.     y5 = y4 + a.w / 2 * Sin(a.ra + _Pi / 2)
  42.     x6 = x4 + a.w / 2 * Cos(a.ra - _Pi / 2)
  43.     y6 = y4 + a.w / 2 * Sin(a.ra - _Pi / 2)
  44.  
  45.     Line (X2, Y2)-(X3, Y3), a.c
  46.     Line (X3, Y3)-(x6, y6), a.c
  47.     Line (x6, y6)-(x5, y5), a.c
  48.     Line (x5, y5)-(X2, Y2), a.c
  49.     Paint (a.x, a.y), a.c, a.c
  50.     Line (X2, Y2)-(X3, Y3), 15 ' give car a front
  51.  
  52.     ' white top  for all cars for future numbers maybe
  53.     X1 = a.x + a.h / 4 * Cos(a.ra)
  54.     Y1 = a.y + a.h / 4 * Sin(a.ra)
  55.     X2 = X1 + (a.w / 2 - 3) * Cos(a.ra + _Pi / 2)
  56.     Y2 = Y1 + (a.w / 2 - 3) * Sin(a.ra + _Pi / 2)
  57.     X3 = X1 + (a.w / 2 - 3) * Cos(a.ra - _Pi / 2)
  58.     Y3 = Y1 + (a.w / 2 - 3) * Sin(a.ra - _Pi / 2)
  59.  
  60.     x4 = a.x + a.h / 4 * Cos(a.ra - _Pi)
  61.     y4 = a.y + a.h / 4 * Sin(a.ra - _Pi)
  62.     x5 = x4 + (a.w / 2 - 3) * Cos(a.ra + _Pi / 2)
  63.     y5 = y4 + (a.w / 2 - 3) * Sin(a.ra + _Pi / 2)
  64.     x6 = x4 + (a.w / 2 - 3) * Cos(a.ra - _Pi / 2)
  65.     y6 = y4 + (a.w / 2 - 3) * Sin(a.ra - _Pi / 2)
  66.  
  67.     Line (X2, Y2)-(X3, Y3), 15
  68.     Line (X3, Y3)-(x6, y6), 15
  69.     Line (x6, y6)-(x5, y5), 15
  70.     Line (x5, y5)-(X2, Y2), 15
  71.     Paint (a.x, a.y), 15, 15
  72.  
  73.  
  74.  

;-))

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Bump N Go Car Toy
« Reply #1 on: March 01, 2022, 06:17:04 pm »
Is that vehicle insured?
It works better if you plug it in.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Bump N Go Car Toy
« Reply #2 on: March 02, 2022, 08:24:40 pm »
LOL that's really neat B+. This code can teach me and others how to change turns. I made a little mod to use either the left and right arrow keys or the "a" and "d" keys to turn with while the car toy is moving. I remembered that your computer or laptop doesn't use arrow keys so I added the letters. Experimenting a bit I found out the code to turn is easier than I thought. My mod code is between lines 18 and 22.

Code: QB64: [Select]
  1. _Title "Bump N Go - Drive Car" 'b+ 2022-03-01
  2. ' b+ mod of Drive Car (Graphics Dir) 'b+ 2021-09-29
  3. ' SierraKen's Mod to use left and right arrow keys, or "a" and "d" keys, to turn.
  4.  
  5. Type car
  6.     As Single x, y, w, h, ra, speed
  7.     As _Unsigned Long c
  8.  
  9. Dim blue As car
  10. blue.x = _Width / 2: blue.y = _Height / 2
  11. blue.w = 20: blue.h = 40
  12. blue.ra = _Pi / 2: blue.speed = 2
  13. blue.c = 9
  14.  
  15. While _KeyDown(27) = 0
  16.     'SierraKen's Mod
  17.     a$ = InKey$
  18.     If a$ = Chr$(0) + Chr$(77) Or a$ = "d" Or a$ = "D" Then blue.ra = blue.ra + .5
  19.     If a$ = Chr$(0) + Chr$(75) Or a$ = "a" Or a$ = "A" Then blue.ra = blue.ra - .5
  20.     '-----------
  21.     If blue.speed = 0 Then
  22.         blue.ra = _Atan2(blue.y - _Height / 2, blue.x - _Width / 2) + _Pi + Rnd * -.5
  23.         blue.speed = .1
  24.     End If
  25.     blue.speed = blue.speed + .5
  26.     Cls
  27.     blue.x = blue.x + blue.speed * Cos(blue.ra)
  28.     blue.y = blue.y + blue.speed * Sin(blue.ra)
  29.     If blue.x < 10 Or blue.x > _Width - 10 Then blue.speed = 0
  30.     If blue.y < 10 Or blue.y > _Height - 10 Then blue.speed = 0
  31.     drawCar blue
  32.     _Display ' stop flicker
  33.     _Limit 30 ' loops per sec
  34.  
  35. Sub drawCar (a As car)
  36.     ' code not optimized for speed  just proff of concept
  37.     X1 = a.x + a.h / 2 * Cos(a.ra)
  38.     Y1 = a.y + a.h / 2 * Sin(a.ra)
  39.     X2 = X1 + a.w / 2 * Cos(a.ra + _Pi / 2)
  40.     Y2 = Y1 + a.w / 2 * Sin(a.ra + _Pi / 2)
  41.     X3 = X1 + a.w / 2 * Cos(a.ra - _Pi / 2)
  42.     Y3 = Y1 + a.w / 2 * Sin(a.ra - _Pi / 2)
  43.  
  44.     x4 = a.x + a.h / 2 * Cos(a.ra - _Pi)
  45.     y4 = a.y + a.h / 2 * Sin(a.ra - _Pi)
  46.     x5 = x4 + a.w / 2 * Cos(a.ra + _Pi / 2)
  47.     y5 = y4 + a.w / 2 * Sin(a.ra + _Pi / 2)
  48.     x6 = x4 + a.w / 2 * Cos(a.ra - _Pi / 2)
  49.     y6 = y4 + a.w / 2 * Sin(a.ra - _Pi / 2)
  50.  
  51.     Line (X2, Y2)-(X3, Y3), a.c
  52.     Line (X3, Y3)-(x6, y6), a.c
  53.     Line (x6, y6)-(x5, y5), a.c
  54.     Line (x5, y5)-(X2, Y2), a.c
  55.     Paint (a.x, a.y), a.c, a.c
  56.     Line (X2, Y2)-(X3, Y3), 15 ' give car a front
  57.  
  58.     ' white top  for all cars for future numbers maybe
  59.     X1 = a.x + a.h / 4 * Cos(a.ra)
  60.     Y1 = a.y + a.h / 4 * Sin(a.ra)
  61.     X2 = X1 + (a.w / 2 - 3) * Cos(a.ra + _Pi / 2)
  62.     Y2 = Y1 + (a.w / 2 - 3) * Sin(a.ra + _Pi / 2)
  63.     X3 = X1 + (a.w / 2 - 3) * Cos(a.ra - _Pi / 2)
  64.     Y3 = Y1 + (a.w / 2 - 3) * Sin(a.ra - _Pi / 2)
  65.  
  66.     x4 = a.x + a.h / 4 * Cos(a.ra - _Pi)
  67.     y4 = a.y + a.h / 4 * Sin(a.ra - _Pi)
  68.     x5 = x4 + (a.w / 2 - 3) * Cos(a.ra + _Pi / 2)
  69.     y5 = y4 + (a.w / 2 - 3) * Sin(a.ra + _Pi / 2)
  70.     x6 = x4 + (a.w / 2 - 3) * Cos(a.ra - _Pi / 2)
  71.     y6 = y4 + (a.w / 2 - 3) * Sin(a.ra - _Pi / 2)
  72.  
  73.     Line (X2, Y2)-(X3, Y3), 15
  74.     Line (X3, Y3)-(x6, y6), 15
  75.     Line (x6, y6)-(x5, y5), 15
  76.     Line (x5, y5)-(X2, Y2), 15
  77.     Paint (a.x, a.y), 15, 15
  78.  
  79.  



 


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Bump N Go Car Toy
« Reply #3 on: March 02, 2022, 09:04:16 pm »
Ja, that Bump N Go code came from here (I did use arrow keys, I do have them but up and down are between left and right and only half size keys. Not the best setup for arrow pounding games.) Up and down are for gas and breaks.

Code: QB64: [Select]
  1. _Title "Drive Car" 'b+ 2021-09-29
  2. Type car
  3.     As Single x, y, w, h, ra, speed
  4.     As _Unsigned Long c
  5.  
  6. Dim blue As car
  7. blue.x = _Width / 2: blue.y = _Height / 2
  8. blue.w = 20: blue.h = 40
  9. blue.ra = _Pi / 2: blue.speed = 2
  10. blue.c = 9
  11.  
  12. While _KeyDown(27) = 0
  13.     CarAngle = CarAngle + _D2R(s)
  14.     K& = _KeyHit
  15.     Select Case K&
  16.         Case 18432 'arrow UP is gas
  17.             blue.speed = blue.speed + .1
  18.             If blue.speed > 5 Then blue.speed = 5
  19.         Case 20480 'arrow DOWN slow down
  20.             blue.speed = blue.speed - .1
  21.             If blue.speed < 0 Then blue.speed = 0
  22.         Case 19712
  23.             s = s + 2 'to right
  24.         Case 19200
  25.             s = s - 2 'to left
  26.     End Select
  27.     Cls
  28.     blue.ra = _D2R(s)
  29.     blue.x = blue.x + blue.speed * Cos(blue.ra)
  30.     blue.y = blue.y + blue.speed * Sin(blue.ra)
  31.     If blue.x < 10 Or blue.x > _Width - 10 Then blue.speed = 0
  32.     If blue.y < 10 Or blue.y > _Height - 10 Then blue.speed = 0
  33.     drawCar blue
  34.     _Display ' stop flicker
  35.     _Limit 30 ' loops per sec
  36.  
  37. Sub drawCar (a As car)
  38.     ' code not optimized for speed  just proff of concept
  39.     X1 = a.x + a.h / 2 * Cos(a.ra)
  40.     Y1 = a.y + a.h / 2 * Sin(a.ra)
  41.     X2 = X1 + a.w / 2 * Cos(a.ra + _Pi / 2)
  42.     Y2 = Y1 + a.w / 2 * Sin(a.ra + _Pi / 2)
  43.     X3 = X1 + a.w / 2 * Cos(a.ra - _Pi / 2)
  44.     Y3 = Y1 + a.w / 2 * Sin(a.ra - _Pi / 2)
  45.  
  46.     x4 = a.x + a.h / 2 * Cos(a.ra - _Pi)
  47.     y4 = a.y + a.h / 2 * Sin(a.ra - _Pi)
  48.     x5 = x4 + a.w / 2 * Cos(a.ra + _Pi / 2)
  49.     y5 = y4 + a.w / 2 * Sin(a.ra + _Pi / 2)
  50.     x6 = x4 + a.w / 2 * Cos(a.ra - _Pi / 2)
  51.     y6 = y4 + a.w / 2 * Sin(a.ra - _Pi / 2)
  52.  
  53.     Line (X2, Y2)-(X3, Y3), a.c
  54.     Line (X3, Y3)-(x6, y6), a.c
  55.     Line (x6, y6)-(x5, y5), a.c
  56.     Line (x5, y5)-(X2, Y2), a.c
  57.     Paint (a.x, a.y), a.c, a.c
  58.     Line (X2, Y2)-(X3, Y3), 15 ' give car a front
  59.  
  60.     ' white top  for all cars for future numbers maybe
  61.     X1 = a.x + a.h / 4 * Cos(a.ra)
  62.     Y1 = a.y + a.h / 4 * Sin(a.ra)
  63.     X2 = X1 + (a.w / 2 - 3) * Cos(a.ra + _Pi / 2)
  64.     Y2 = Y1 + (a.w / 2 - 3) * Sin(a.ra + _Pi / 2)
  65.     X3 = X1 + (a.w / 2 - 3) * Cos(a.ra - _Pi / 2)
  66.     Y3 = Y1 + (a.w / 2 - 3) * Sin(a.ra - _Pi / 2)
  67.  
  68.     x4 = a.x + a.h / 4 * Cos(a.ra - _Pi)
  69.     y4 = a.y + a.h / 4 * Sin(a.ra - _Pi)
  70.     x5 = x4 + (a.w / 2 - 3) * Cos(a.ra + _Pi / 2)
  71.     y5 = y4 + (a.w / 2 - 3) * Sin(a.ra + _Pi / 2)
  72.     x6 = x4 + (a.w / 2 - 3) * Cos(a.ra - _Pi / 2)
  73.     y6 = y4 + (a.w / 2 - 3) * Sin(a.ra - _Pi / 2)
  74.  
  75.     Line (X2, Y2)-(X3, Y3), 15
  76.     Line (X3, Y3)-(x6, y6), 15
  77.     Line (x6, y6)-(x5, y5), 15
  78.     Line (x5, y5)-(X2, Y2), 15
  79.     Paint (a.x, a.y), 15, 15
  80.  
  81.  
  82.  

I think I had plans to put car(s) on track and drive them for best time? Every time you go off the track you add to your time unless you crash and die, then times up, game over you've gone home.

Don't buy insurance don't want the wife too happy you are dead.

Nice job with the turning Ken!