Author Topic: Target Practice  (Read 4632 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Target Practice
« on: April 03, 2019, 12:57:52 pm »
... with Mouse Action Shooter.

Code: QB64: [Select]
  1. _TITLE "Mouse Action Shooter #2 Targets for MB triggers" 'B+ 2019-04-01
  2. 'Mouse Action Shooter started from eRATication 5 by bplus 2018-08-06"
  3. ' upodate Mouse Action Shooter with MB triggers 2019-04-01
  4. ' 2019-04-03 add some targets for practice
  5.  
  6. ' SCORE System:
  7. ' Right click to fire a bullet, they cost 10 points each.
  8. ' Targets range from 30 for biggest targets to 100 points for smallest Targets
  9. ' Get hit by target loose 100 points and delay of game. Just worry about aound the nose of the shooter.
  10.  
  11.  
  12. 'screen dimensions
  13. CONST xmax = 1200
  14. CONST ymax = 700
  15. CONST nBullets = 15
  16. CONST bSpeed = 30
  17. CONST nTargets = 3 'should probably depend on level of play
  18. CONST tSpeed = 5 'should probably depend on level of play
  19. CONST shooterRadius = 50
  20.  
  21. TYPE shooterType
  22.     x AS SINGLE
  23.     y AS SINGLE
  24.     a AS SINGLE
  25.  
  26. TYPE bulletType
  27.     x AS INTEGER
  28.     y AS INTEGER
  29.     dx AS INTEGER
  30.     dy AS INTEGER
  31.     live AS INTEGER
  32.  
  33. TYPE targetType
  34.     x AS SINGLE
  35.     y AS SINGLE
  36.     a AS SINGLE
  37.     r AS INTEGER
  38.  
  39. SCREEN _NEWIMAGE(xmax, ymax, 32)
  40. _SCREENMOVE 100, 20
  41.  
  42.  
  43. DIM SHARED GameOn, mb1, last1, score
  44.  
  45. 'targets
  46. DIM SHARED t(nTargets - 1) AS targetType
  47. FOR i = 0 TO nTargets - 1: newTarget i: NEXT
  48.  
  49. 'bullets
  50. DIM SHARED b(nBullets - 1) AS bulletType
  51.  
  52. 'shooter
  53. DIM SHARED shooter AS shooterType
  54. shooter.x = xmax / 2
  55. shooter.y = ymax / 2
  56. shooter.a = 0
  57. lastx = xmax / 2: lasty = ymax / 2
  58.  
  59. 'game
  60. GameOn = 1
  61. WHILE GameOn
  62.     CLS
  63.     _TITLE "Target Practice   Score:" + STR$(score)
  64.     mb1 = 0: mb2 = 0
  65.     shooter.x = _MOUSEX: shooter.y = _MOUSEY
  66.     IF ABS(lastx - shooter.x) > 7 OR ABS(lasty - shooter.y) > 7 THEN
  67.         shooter.a = _ATAN2(shooter.y - lasty, shooter.x - lastx)
  68.         lastx = shooter.x: lasty = shooter.y
  69.     END IF
  70.     t = TIMER(.001)
  71.     IF _MOUSEBUTTON(1) THEN 'when ship is heading north the left button should be left
  72.         IF t - last1 > .15 THEN mb1 = 1: last1 = t
  73.     END IF
  74.     handleTargets
  75.     drawshooter
  76.     handleBullets
  77.     _DISPLAY
  78.     _LIMIT 30
  79.  
  80. SUB handleTargets
  81.     FOR i = 0 TO nTargets - 1
  82.         'update position
  83.         t(i).x = t(i).x + tSpeed * COS(t(i).a)
  84.         t(i).y = t(i).y + tSpeed * SIN(t(i).a)
  85.         'inbounds?
  86.         IF t(i).x < 0 OR t(i).x > xmax OR t(i).y < 0 OR t(i).y > ymax THEN
  87.             newTarget i
  88.         ELSE
  89.             IF hitShooter(i) THEN
  90.                 'explosion
  91.                 CLS
  92.                 _PRINTSTRING (xmax / 2 - 40, ymax / 2 - 10), "Bang!... Ouch!"
  93.                 score = score - 100
  94.                 _DISPLAY
  95.                 _DELAY .2
  96.             ELSE
  97.                 drawTarget i
  98.             END IF
  99.         END IF
  100.     NEXT
  101.  
  102. SUB newTarget (i)
  103.     'pick edge
  104.     edge = INT(RND * 4)
  105.     SELECT CASE edge
  106.         CASE 0: t(i).x = 0: t(i).y = RND * (ymax - 300) + 150: t(i).a = RND * _PI
  107.         CASE 1: t(i).x = xmax: t(i).y = RND * (ymax - 300) + 150: t(i).a = _PI / 2 + RND * _PI
  108.         CASE 2: t(i).x = RND * xmax: t(i).y = 0: t(i).a = RND * _PI
  109.         CASE 3: t(i).x = RND * xmax: t(i).y = ymax: t(i).a = _PI + RND * _PI
  110.     END SELECT
  111.     t(i).r = (INT(RND * 8) + 3) * 10 '30 to 100 score 130 - radius 100 to 30
  112.  
  113. SUB drawTarget (i)
  114.     FOR r = t(i).r TO 0 STEP -t(i).r / 10
  115.         count = (count + 1) MOD 2
  116.         IF count THEN c~& = _RGB32(255, 0, 0) ELSE c~& = _RGB32(255, 255, 255)
  117.         fcirc t(i).x, t(i).y, r, c~&
  118.     NEXT
  119.  
  120. SUB handleBullets ()
  121.     FOR i = 0 TO nBullets - 1
  122.         IF b(i).live = 0 AND mb1 = 1 THEN 'have in active bullet index to use
  123.             b(i).x = shooter.x + .5 * shooterRadius * COS(shooter.a)
  124.             b(i).y = shooter.y + .5 * shooterRadius * SIN(shooter.a)
  125.             b(i).dx = bSpeed * COS(shooter.a)
  126.             b(i).dy = bSpeed * SIN(shooter.a)
  127.             b(i).live = 1
  128.             mb1 = 0
  129.             score = score - 10 'bullets cost 10 points
  130.             'ELSEIF b(i).live  = 0 AND mb2 = 1 THEN
  131.             '    b(i).x = shooter.x + (shooterRadius) * COS(shooter.a - _PI(7 / 8))
  132.             '    b(i).y = shooter.y + (shooterRadius) * SIN(shooter.a - _PI(7 / 8))
  133.             '    b(i).dx = bSpeed * COS(shooter.a)
  134.             '    b(i).dy = bSpeed * SIN(shooter.a)
  135.             '    b(i).live = 1
  136.             '    mb2 = 0
  137.  
  138.         ELSEIF b(i).live = 1 THEN 'new location
  139.             b(i).x = b(i).x + b(i).dx
  140.             b(i).y = b(i).y + b(i).dy
  141.             IF b(i).x > 0 AND b(i).x < xmax AND b(i).y > 0 AND b(i).y < ymax THEN 'in bounds draw it
  142.                 'check for collision with ...
  143.                 t = hitTarget(i)
  144.                 IF t > -1 THEN
  145.                     score = score + 130 - t(t).r
  146.                     b(i).live = 0
  147.                     newTarget t
  148.                 ELSE
  149.                     'draw bullet
  150.                     ba = _ATAN2(b(i).dy, b(i).dx): b = 15
  151.                     x1 = b(i).x + b * COS(ba)
  152.                     y1 = b(i).y + b * SIN(ba)
  153.                     x2 = b(i).x + b * COS(ba + _PI(5 / 6))
  154.                     y2 = b(i).y + b * SIN(ba + _PI(5 / 6))
  155.                     x3 = b(i).x + b * COS(ba + _PI(7 / 6))
  156.                     y3 = b(i).y + b * SIN(ba + _PI(7 / 6))
  157.                     fTri x1, y1, x2, y2, x3, y3, _RGB32(10, 160, 160)
  158.                     'fcirc b(i).x, b(i).y, 4, _RGB32(64, 0, 0)
  159.                 END IF
  160.             ELSE
  161.                 b(i).live = 0 'dectiveate
  162.             END IF
  163.         END IF
  164.     NEXT
  165.  
  166. FUNCTION hitTarget (bulletIndex)
  167.     hitTarget = -1
  168.     FOR i = 0 TO nTargets - 1
  169.         IF SQR((t(i).x - b(bulletIndex).x) ^ 2 + (t(i).y - b(bulletIndex).y) ^ 2) <= t(i).r THEN hitTarget = i: EXIT FUNCTION
  170.     NEXT
  171.  
  172. FUNCTION hitShooter (TargetIndex)
  173.     IF SQR((shooter.x - t(TargetIndex).x) ^ 2 + (shooter.y - t(TargetIndex).y) ^ 2) <= t(i).r THEN hitShooter = 1: EXIT FUNCTION
  174.  
  175. SUB drawshooter ()
  176.     'just a wedge
  177.     x1 = shooter.x + (shooterRadius - 30) * COS(shooter.a)
  178.     y1 = shooter.y + (shooterRadius - 30) * SIN(shooter.a)
  179.     x2 = shooter.x + (shooterRadius + 20) * COS(shooter.a + _PI(11 / 16))
  180.     y2 = shooter.y + (shooterRadius + 20) * SIN(shooter.a + _PI(11 / 16))
  181.     x3 = shooter.x + (shooterRadius + 20) * COS(shooter.a - _PI(11 / 16))
  182.     y3 = shooter.y + (shooterRadius + 20) * SIN(shooter.a - _PI(11 / 16))
  183.     fTri x1, y1, x2, y2, x3, y3, _RGB32(85, 45, 0)
  184.     x1 = shooter.x + shooterRadius * COS(shooter.a)
  185.     y1 = shooter.y + shooterRadius * SIN(shooter.a)
  186.     x2 = shooter.x + shooterRadius * COS(shooter.a + _PI(7 / 8))
  187.     y2 = shooter.y + shooterRadius * SIN(shooter.a + _PI(7 / 8))
  188.     x3 = shooter.x + shooterRadius * COS(shooter.a - _PI(7 / 8))
  189.     y3 = shooter.y + shooterRadius * SIN(shooter.a - _PI(7 / 8))
  190.     fTri x1, y1, x2, y2, x3, y3, _RGB32(0, 0, 200)
  191.     x2 = shooter.x + shooterRadius * COS(shooter.a + _PI(15 / 16))
  192.     y2 = shooter.y + shooterRadius * SIN(shooter.a + _PI(15 / 16))
  193.     x3 = shooter.x + shooterRadius * COS(shooter.a - _PI(15 / 16))
  194.     y3 = shooter.y + shooterRadius * SIN(shooter.a - _PI(15 / 16))
  195.     fTri x1, y1, x2, y2, x3, y3, _RGB32(255, 255, 255)
  196.  
  197. ' found at [abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]:    http://www.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/forum/index.php?topic=14425.0
  198. SUB fTri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  199.     a& = _NEWIMAGE(1, 1, 32)
  200.     _DEST a&
  201.     PSET (0, 0), K
  202.     _DEST 0
  203.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  204.     _FREEIMAGE a& '<<< this is important!
  205.  
  206. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  207.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  208.     DIM X AS INTEGER, Y AS INTEGER
  209.  
  210.     Radius = ABS(R)
  211.     RadiusError = -Radius
  212.     X = Radius
  213.     Y = 0
  214.  
  215.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  216.  
  217.     ' Draw the middle span here so we don't draw it twice in the main loop,
  218.     ' which would be a problem with blending turned on.
  219.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  220.  
  221.     WHILE X > Y
  222.         RadiusError = RadiusError + Y * 2 + 1
  223.         IF RadiusError >= 0 THEN
  224.             IF X <> Y + 1 THEN
  225.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  226.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  227.             END IF
  228.             X = X - 1
  229.             RadiusError = RadiusError - X * 2
  230.         END IF
  231.         Y = Y + 1
  232.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  233.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  234.     WEND
  235.  
  236.  

(A break from Traveling Salespeople) :)
« Last Edit: April 03, 2019, 12:58:57 pm by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Target Practice
« Reply #1 on: April 03, 2019, 02:26:44 pm »
I made an ASCII version of that game, too. <= for the ship, and (_|_) for the falling objects. You guessed it, it was called ass-teroids. I supposed I could have called it ASCII-roids, but oh well...

Hey neat, you accomplished a near clone of the 1980's arcade game asteroids, in under 250 lines. I found it a little hard to rotate the ship, but it fires and moves out of the way, great.

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

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Target Practice
« Reply #2 on: April 03, 2019, 02:36:58 pm »
Code: QB64: [Select]
  1. I found it a little hard to rotate the ship, but it fires and moves out of the way, great.

Yes, so do I. It is unnatural to fly at the thing you want to shoot at.

Maybe tinker with this line in main loop, it was originally > 7:
Code: QB64: [Select]
  1. IF ABS(lastx - shooter.x) > 3 OR ABS(lasty - shooter.y) > 3 THEN    
  2.  
« Last Edit: April 03, 2019, 02:46:40 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Target Practice
« Reply #3 on: April 03, 2019, 06:05:05 pm »
Hi Ashis
cool!
I must practice with mouse because I let it jump in rotation also to 50 degree.
Programming isn't difficult, only it's  consuming time and coffee

Offline pforpond

  • Newbie
  • Posts: 76
  • I am me
    • View Profile
Re: Target Practice
« Reply #4 on: April 04, 2019, 06:15:46 am »
hey, i don't know if i'm doing anything wrong but it keeps crashing for me :(
i attached a video to show :)
* vokoscreen-2019-04-04_11-11-18.mkv (Filesize: 636.05 KB, Downloads: 191)
Loading Signature...

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Target Practice
« Reply #5 on: April 04, 2019, 10:39:27 am »
hey, i don't know if i'm doing anything wrong but it keeps crashing for me :(
i attached a video to show :)

From your video, I see you are Running from an Option I don't have, "Start detached" ???
I just use the first option in Run  Menu. Don't know if that is difference that makes a difference.

If you are running on Linux, your Timer(.001) may be returning very different numbers than my settings from Windows 10.
Tinker with this part, that attempts a delay of rapid fire bullets.
Code: QB64: [Select]
  1.     t = TIMER(.001)
  2.     IF _MOUSEBUTTON(1) THEN 'when ship is heading north the left button should be left
  3.         IF t - last1 > .15 THEN mb1 = 1: last1 = t
  4.     END IF
  5.  

In case the targets are running into shooter way too often, before you can even see what's going on, replace this block:
Code: QB64: [Select]
  1.             IF hitShooter(i) THEN
  2.                 'explosion
  3.                 CLS
  4.                 _PRINTSTRING (xmax / 2 - 40, ymax / 2 - 10), "Bang!... Ouch!"
  5.                 score = score - 100
  6.                 _DISPLAY
  7.                 _DELAY .2
  8.             ELSE
  9.                 drawTarget i
  10.             END IF
  11.  

With just:
Code: QB64: [Select]
  1. drawTarget i

Also the screen doesn't look right, the shooter should start up in the middle of the screen. Why is it in top left corner?
I set screen width with const xmax and screen height with const ymax.


To all,

To point the shooter, you move it towards your target or anticipate a little where target is going and move mouse that way...
« Last Edit: April 04, 2019, 10:47:41 am by bplus »

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Target Practice
« Reply #6 on: April 04, 2019, 11:19:12 am »
Hi bplus! Nice one
I modified your code so that target appears perfectly from the window. That is, they only disappear when they fully move away from sight.
Lines modified - 115 to 118, 94.
Code: QB64: [Select]
  1. _TITLE "Mouse Action Shooter #2 Targets for MB triggers" 'B+ 2019-04-01
  2. 'Mouse Action Shooter started from eRATication 5 by bplus 2018-08-06"
  3. ' upodate Mouse Action Shooter with MB triggers 2019-04-01
  4. ' 2019-04-03 add some targets for practice
  5.  
  6. ' SCORE System:
  7. ' Right click to fire a bullet, they cost 10 points each.
  8. ' Targets range from 30 for biggest targets to 100 points for smallest Targets
  9. ' Get hit by target loose 100 points and delay of game. Just worry about aound the nose of the shooter.
  10.  
  11.  
  12. 'screen dimensions
  13. CONST xmax = 1200
  14. CONST ymax = 700
  15. CONST nBullets = 15
  16. CONST bSpeed = 30
  17. CONST nTargets = 3 'should probably depend on level of play
  18. CONST tSpeed = 5 'should probably depend on level of play
  19. CONST shooterRadius = 50
  20.  
  21. TYPE shooterType
  22.     x AS SINGLE
  23.     y AS SINGLE
  24.     a AS SINGLE
  25.  
  26. TYPE bulletType
  27.     x AS INTEGER
  28.     y AS INTEGER
  29.     dx AS INTEGER
  30.     dy AS INTEGER
  31.     live AS INTEGER
  32.  
  33. TYPE targetType
  34.     x AS SINGLE
  35.     y AS SINGLE
  36.     a AS SINGLE
  37.     r AS INTEGER
  38.  
  39. SCREEN _NEWIMAGE(xmax, ymax, 32)
  40. _SCREENMOVE 100, 20
  41.  
  42.  
  43. DIM SHARED GameOn, mb1, last1, score
  44.  
  45. 'targets
  46. DIM SHARED t(nTargets - 1) AS targetType
  47. FOR i = 0 TO nTargets - 1: newTarget i: NEXT
  48.  
  49. 'bullets
  50. DIM SHARED b(nBullets - 1) AS bulletType
  51.  
  52. 'shooter
  53. DIM SHARED shooter AS shooterType
  54. shooter.x = xmax / 2
  55. shooter.y = ymax / 2
  56. shooter.a = 0
  57. lastx = xmax / 2: lasty = ymax / 2
  58.  
  59. 'game
  60. GameOn = 1
  61. WHILE GameOn
  62.     CLS
  63.     _TITLE "Target Practice   Score:" + STR$(score)
  64.     mb1 = 0: mb2 = 0
  65.     shooter.x = _MOUSEX: shooter.y = _MOUSEY
  66.     IF ABS(lastx - shooter.x) > 7 OR ABS(lasty - shooter.y) > 7 THEN
  67.         shooter.a = _ATAN2(shooter.y - lasty, shooter.x - lastx)
  68.         lastx = shooter.x: lasty = shooter.y
  69.     END IF
  70.     t = TIMER(.001)
  71.     IF _MOUSEBUTTON(1) THEN 'when ship is heading north the left button should be left
  72.         IF t - last1 > .15 THEN mb1 = 1: last1 = t
  73.     END IF
  74.     handleTargets
  75.     drawshooter
  76.     handleBullets
  77.     _DISPLAY
  78.     _LIMIT 30
  79.  
  80. SUB handleTargets
  81.     FOR i = 0 TO nTargets - 1
  82.         'update position
  83.         t(i).x = t(i).x + tSpeed * COS(t(i).a)
  84.         t(i).y = t(i).y + tSpeed * SIN(t(i).a)
  85.         'inbounds?
  86.         IF t(i).x < -shooterRadius OR t(i).x > xmax + shooterRadius OR t(i).y < -shooterRadius OR t(i).y > ymax + shooterradiu THEN
  87.             newTarget i
  88.         ELSE
  89.             IF hitShooter(i) THEN
  90.                 'explosion
  91.                 CLS
  92.                 _PRINTSTRING (xmax / 2 - 40, ymax / 2 - 10), "Bang!... Ouch!"
  93.                 score = score - 100
  94.                 _DISPLAY
  95.                 _DELAY .2
  96.             ELSE
  97.                 drawTarget i
  98.             END IF
  99.         END IF
  100.     NEXT
  101.  
  102. SUB newTarget (i)
  103.     'pick edge
  104.     edge = INT(RND * 4)
  105.     SELECT CASE edge
  106.         CASE 0: t(i).x = -shooterRadius: t(i).y = RND * (ymax - 300) + 150: t(i).a = RND * _PI
  107.         CASE 1: t(i).x = xmax + shooterRadius: t(i).y = RND * (ymax - 300) + 150: t(i).a = _PI / 2 + RND * _PI
  108.         CASE 2: t(i).x = RND * xmax: t(i).y = -shooterRadius: t(i).a = RND * _PI
  109.         CASE 3: t(i).x = RND * xmax: t(i).y = ymax + shooterRadius: t(i).a = _PI + RND * _PI
  110.     END SELECT
  111.     t(i).r = (INT(RND * 8) + 3) * 10 '30 to 100 score 130 - radius 100 to 30
  112.  
  113. SUB drawTarget (i)
  114.     FOR r = t(i).r TO 0 STEP -t(i).r / 10
  115.         count = (count + 1) MOD 2
  116.         IF count THEN c~& = _RGB32(255, 0, 0) ELSE c~& = _RGB32(255, 255, 255)
  117.         fcirc t(i).x, t(i).y, r, c~&
  118.     NEXT
  119.  
  120. SUB handleBullets ()
  121.     FOR i = 0 TO nBullets - 1
  122.         IF b(i).live = 0 AND mb1 = 1 THEN 'have in active bullet index to use
  123.             b(i).x = shooter.x + .5 * shooterRadius * COS(shooter.a)
  124.             b(i).y = shooter.y + .5 * shooterRadius * SIN(shooter.a)
  125.             b(i).dx = bSpeed * COS(shooter.a)
  126.             b(i).dy = bSpeed * SIN(shooter.a)
  127.             b(i).live = 1
  128.             mb1 = 0
  129.             score = score - 10 'bullets cost 10 points
  130.             'ELSEIF b(i).live  = 0 AND mb2 = 1 THEN
  131.             '    b(i).x = shooter.x + (shooterRadius) * COS(shooter.a - _PI(7 / 8))
  132.             '    b(i).y = shooter.y + (shooterRadius) * SIN(shooter.a - _PI(7 / 8))
  133.             '    b(i).dx = bSpeed * COS(shooter.a)
  134.             '    b(i).dy = bSpeed * SIN(shooter.a)
  135.             '    b(i).live = 1
  136.             '    mb2 = 0
  137.  
  138.         ELSEIF b(i).live = 1 THEN 'new location
  139.             b(i).x = b(i).x + b(i).dx
  140.             b(i).y = b(i).y + b(i).dy
  141.             IF b(i).x > 0 AND b(i).x < xmax AND b(i).y > 0 AND b(i).y < ymax THEN 'in bounds draw it
  142.                 'check for collision with ...
  143.                 t = hitTarget(i)
  144.                 IF t > -1 THEN
  145.                     score = score + 130 - t(t).r
  146.                     b(i).live = 0
  147.                     newTarget t
  148.                 ELSE
  149.                     'draw bullet
  150.                     ba = _ATAN2(b(i).dy, b(i).dx): b = 15
  151.                     x1 = b(i).x + b * COS(ba)
  152.                     y1 = b(i).y + b * SIN(ba)
  153.                     x2 = b(i).x + b * COS(ba + _PI(5 / 6))
  154.                     y2 = b(i).y + b * SIN(ba + _PI(5 / 6))
  155.                     x3 = b(i).x + b * COS(ba + _PI(7 / 6))
  156.                     y3 = b(i).y + b * SIN(ba + _PI(7 / 6))
  157.                     fTri x1, y1, x2, y2, x3, y3, _RGB32(10, 160, 160)
  158.                     'fcirc b(i).x, b(i).y, 4, _RGB32(64, 0, 0)
  159.                 END IF
  160.             ELSE
  161.                 b(i).live = 0 'dectiveate
  162.             END IF
  163.         END IF
  164.     NEXT
  165.  
  166. FUNCTION hitTarget (bulletIndex)
  167.     hitTarget = -1
  168.     FOR i = 0 TO nTargets - 1
  169.         IF SQR((t(i).x - b(bulletIndex).x) ^ 2 + (t(i).y - b(bulletIndex).y) ^ 2) <= t(i).r THEN hitTarget = i: EXIT FUNCTION
  170.     NEXT
  171.  
  172. FUNCTION hitShooter (TargetIndex)
  173.     IF SQR((shooter.x - t(TargetIndex).x) ^ 2 + (shooter.y - t(TargetIndex).y) ^ 2) <= t(i).r THEN hitShooter = 1: EXIT FUNCTION
  174.  
  175. SUB drawshooter ()
  176.     'just a wedge
  177.     x1 = shooter.x + (shooterRadius - 30) * COS(shooter.a)
  178.     y1 = shooter.y + (shooterRadius - 30) * SIN(shooter.a)
  179.     x2 = shooter.x + (shooterRadius + 20) * COS(shooter.a + _PI(11 / 16))
  180.     y2 = shooter.y + (shooterRadius + 20) * SIN(shooter.a + _PI(11 / 16))
  181.     x3 = shooter.x + (shooterRadius + 20) * COS(shooter.a - _PI(11 / 16))
  182.     y3 = shooter.y + (shooterRadius + 20) * SIN(shooter.a - _PI(11 / 16))
  183.     fTri x1, y1, x2, y2, x3, y3, _RGB32(85, 45, 0)
  184.     x1 = shooter.x + shooterRadius * COS(shooter.a)
  185.     y1 = shooter.y + shooterRadius * SIN(shooter.a)
  186.     x2 = shooter.x + shooterRadius * COS(shooter.a + _PI(7 / 8))
  187.     y2 = shooter.y + shooterRadius * SIN(shooter.a + _PI(7 / 8))
  188.     x3 = shooter.x + shooterRadius * COS(shooter.a - _PI(7 / 8))
  189.     y3 = shooter.y + shooterRadius * SIN(shooter.a - _PI(7 / 8))
  190.     fTri x1, y1, x2, y2, x3, y3, _RGB32(0, 0, 200)
  191.     x2 = shooter.x + shooterRadius * COS(shooter.a + _PI(15 / 16))
  192.     y2 = shooter.y + shooterRadius * SIN(shooter.a + _PI(15 / 16))
  193.     x3 = shooter.x + shooterRadius * COS(shooter.a - _PI(15 / 16))
  194.     y3 = shooter.y + shooterRadius * SIN(shooter.a - _PI(15 / 16))
  195.     fTri x1, y1, x2, y2, x3, y3, _RGB32(255, 255, 255)
  196.  
  197. ' found at [abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]:    http://www.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/forum/index.php?topic=14425.0
  198. SUB fTri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  199.     a& = _NEWIMAGE(1, 1, 32)
  200.     _DEST a&
  201.     PSET (0, 0), K
  202.     _DEST 0
  203.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  204.     _FREEIMAGE a& '<<< this is important!
  205.  
  206. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  207.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  208.     DIM X AS INTEGER, Y AS INTEGER
  209.  
  210.     Radius = ABS(R)
  211.     RadiusError = -Radius
  212.     X = Radius
  213.     Y = 0
  214.  
  215.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  216.  
  217.     ' Draw the middle span here so we don't draw it twice in the main loop,
  218.     ' which would be a problem with blending turned on.
  219.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  220.  
  221.     WHILE X > Y
  222.         RadiusError = RadiusError + Y * 2 + 1
  223.         IF RadiusError >= 0 THEN
  224.             IF X <> Y + 1 THEN
  225.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  226.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  227.             END IF
  228.             X = X - 1
  229.             RadiusError = RadiusError - X * 2
  230.         END IF
  231.         Y = Y + 1
  232.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  233.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  234.     WEND
  235.  
  236.  
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: Target Practice
« Reply #7 on: April 04, 2019, 12:36:23 pm »
Hi Ashish,

Good idea. I extended active bullet range beyond the edge of screen as well, in case one was locked in on target you can get credit if it hits target before it disappears.

I will change shooter x, y calculations so it is off the nose and in center of shooter and post update.

Update:
Shooter x, y fixed long time ago.

So here is snippet to fix bullet range to follow target centers past edge of screen, add to Ashish code mod..
Code: QB64: [Select]
  1.         ELSEIF b(i).live = 1 THEN 'new location
  2.             b(i).x = b(i).x + b(i).dx
  3.             b(i).y = b(i).y + b(i).dy
  4.             IF b(i).x > -50 AND b(i).x < xmax + 50 AND b(i).y > -50 AND b(i).y < ymax + 50 THEN '<<< new range!
  5.                 'check for collision with ...
  6.  

 
« Last Edit: April 04, 2019, 01:29:00 pm by bplus »