[VB.net] Multiple Bullets

AnthraxXx

New Member
Reaction score
25
So basically I'm creating a 2D jet fighter game for my IPT class, and I was wondering how I would go about firing more than 1 bullet on-screen at a time.

I had a system which could fire a bullet, but if I fired again it would just get the bullet I just fired and re-fire it, meaning only 1 bullet could be on-screen at a time.

My group partner and I have thought about it, and have narrowed it down to two possible methods:

1. Bullet Recycling
2. Using a Bullet Class

Bullet Recycling would consist of having multiple arrays, and as a bullet is fired, it is put into an "in" array, as it leaves the game area it is now "out", all bullets in the "out" array are ready to be re-fired. An analogy could be having a real-life gun whose bullets are put into it's magazine after being shot (theoretically), meaning it would never run out of ammunition.

Second is the bullet class, however we have no idea where to begin. Surely you would 'create' the bullet after each shot is fired, and then it would have to move. Does this mean we have to create a timer for EVERY shot fired. Or is it possible to use a single timer for all bullets on-screen by putting them into an array. How would you 'destroy' a bullet after it reaches the game bounds, to avoid bloating the game.

So confusing.

Here's random snippets of what we have/had:

Code:
'If InCount > 0 Then
        '    For p = 0 To InCount
        '        If Collision(bulletX(p), enemytest) Then
        '            bulletismoving(bulletsOut(0)) = False
        '            bulletX(p).Visible = False
        '            bulletismoving(p) = False
        '            CentreOn(enemytest, picExplosion)
        '            picExplosion.Visible = True
        '            enemytest.Visible = False
        '            tmrExplosion.Start()
        '        End If
        '    Next

        '    For i = 0 To InCount
        '        Dim bulletPos(i) As Point
        '        If bulletismoving(i) = True Then
        '            bulletX(i).Show()
        '            bulletPos(i) = New Point(0, -5)
        '            bulletX(i).Location += bulletPos(i)
        '        End If

        '        If bulletPos(i).Y >= boundary1.Y And bulletismoving(i) = False Then
        '            bulletX(i).Show()
        '            bulletX(i).Location += bulletPos(i)
        '        ElseIf bulletiscomputed(i) = False Then
        '            'bulletX(i).Hide()
        '            bulletsOut(outcount) = i
        '            bulletiscomputed(i) = True
        '            bulletismoving(i) = False
        '            InCount -= 1
        '            outcount += 1
        '            For o = 0 To InCount
        '                If bulletsIn(o) = i Then
        '                    For p = (o + 1) To InCount
        '                        bulletsIn(p) = bulletsIn(p - 1)
        '                    Next
        '                End If
        '            Next
        '        End If
        '    Next
        'End If

        '''''''''''''''''''
        'Old Bullet System'
        '''''''''''''''''''
        If bulletPos.Y >= boundary1.Y Then
            SingleBullet.Show()
            bulletPos = New Point(0, -5)
            SingleBullet.Location += bulletPos
        Else
            tmrBullet.Stop()
            SingleBullet.Hide()
        End If

Half of it doesn't make sense because I didn't write it, but have a look anyway.

I'm not asking for answers I just need a push in the right direction.
 

The Helper

Necromancy Power over 9000
Staff member
Reaction score
1,697
I like the bullet recycling. Just make sure you destroy and deallocate the space for those arrays you are creating when you are done with them. You have a potential for big leaks if you dont do that.
 

UndeadDragon

Super Moderator
Reaction score
447
If I have to do things like this my preferred method is the bullet class. It makes it much easier to organise too.
 

celerisk

When Zerg floweth, life is good
Reaction score
62
" Or is it possible to use a single timer for all bullets on-screen by putting them into an array "

Sure, why not?
You need a list somewhere anyway, so why not use it?


" Just make sure you destroy and deallocate the space "

Depends on the overall plan.
You could settle for something like "there's never more than 100" per player and preinitialize one huge structure.
Then just remember the current count, and, as a bullet hits a target or goes off-screen, put the last one in its place, update the counter.
Or, when a new one comes, increment the counter and set the new one up at the end.

And, yes, you somewhat really need to know where each bullet currently is and what direction it was flying to.
 

AnthraxXx

New Member
Reaction score
25
And, yes, you somewhat really need to know where each bullet currently is and what direction it was flying to.

The direction it is going is the easy part because all bullets can only go in 1 direction which is upwards (It's like a scroller game) :)

+rep to all above
 

celerisk

When Zerg floweth, life is good
Reaction score
62
" bullets can only go in 1 direction which is upwards "

:eek:
I was expecting something like a powerup, triple-shot for 20 seconds or so, about once every 30 kills (or whatever really).

Code:
\  |  /
 \ | /  <- Shooting
  \|/
   O    <- You

For each bullet in (x, y), all that's really needed is some additional (dx, dy), the variation in position at each timer interval.

As in:
Up:
dx = 0
dy = 10

Diagonal right:
dx = 7
dy = 7

Diagonal left:
dx = -7
dy = 7

... and whatever else directions there might be.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top