04-25-2024, 02:08 AM
(04-25-2024, 01:42 AM)bplus Wrote: i approached that problem by timing bullets firedI use a similar approach. The code below is a snippet from my Widescreen Asteroids game that controls bullet fire. I tend to set up frame counters as timers.
also you could limit number of bullets, waiting until bullet off screen before reactivating that bullet slot to fire again.
In the second line of code I first check to see if Clock.shipBullet is zero. If it is that means I can fire again. If not toward the end of the snippet you'll see I decrement Clock.shipBullet by one with each passing frame.
The game runs at 60 frames per second and setting Clock.shipBullet to 10 after each bullet fired ensures that the player can fire no more than 6 bullets per second.
Code: (Select All)
IF Clock.warp = 0 THEN ' warping to next level?
IF Clock.shipBullet = 0 THEN ' no, ready to fire again?
IF _KEYDOWN(Keys.fire) THEN ' yes, fire bullet key pressed?
Clock.shipBullet = 10 ' yes, set bullet interval
c = 0
DO ' cycle through bullets
IF ShipBullet(c).life = 0 THEN ' bullet active?
'*******************
'* Add ship bullet *
'*******************
'**
'** NOTE: player ship bullet lifespan below (70)
'**
ShipBullet(c).life = 70 ' set bullet lifespan
ShipBullet(c).x = ObjX1(Ship, 0) ' set bullet X,Y origin
ShipBullet(c).y = ObjY1(Ship, 0) ' (front tip of player ship)
'**
'** NOTE: player ship bullet speed below (9)
'**
ShipBullet(c).xv = ObjVectorX(Ship) + 9 * ObjPSine(ObjAngle(Ship)) ' bullet X,Y vectors
ShipBullet(c).yv = ObjVectorY(Ship) + 9 * -ObjPCosine(ObjAngle(Ship))
PlaySound Sounds.fire, TRUE
c = 4
END IF
c = c + 1
LOOP UNTIL c = 5
END IF
ELSE
Clock.shipBullet = Clock.shipBullet - 1 ' decrement bullet interval timer
END IF
END IF