Games Guns and shooting 2d

hookshy

Well-Known Member
Licensed User
Longtime User
Hey,
Can we start ading some shooting procedures and maybe some guns types ? Is there any gun specialist arownd here ?
Need some help to start ading some gun features, sample code to start over.

I do have one by now ...a rectangle that goes on x axis borowed from enemies exemple :)
Thank you
 

hookshy

Well-Known Member
Licensed User
Longtime User
This is my single wepon so far ...a rectangle what i did was to modify direction from the exemply posted in acccelerated surface lib space enemy :), hopeing other shooting specialist to help us with more shooting styles share some of his guns
B4X:
                    'shoot at surface touch or buton pressed
                    If firewait<=0 Then
                        Dim LaserShot As typLaser
                        LaserShot.X = Player.X +Player.Width - LaserShotSize
                        LaserShot.Y = Player.y + 0.5*Player.Height'position the gun
                        LaserShots.Add(LaserShot)
                        SP.Play(LaserSound, 1 - (LaserShot.X / 100%x), LaserShot.X / 100%x, 1, 0, 1)
                        firewait=500'postpone fire
                    End If


'take care of shoot, compute colision  and laser position usualy in surface update
  Dim Secs As Double = Elapsed / 1000
    firewait = firewait - Elapsed' calculate speed
  
    For i = LaserShots.Size - 1 To 0 Step -1
        Dim LaserShot As typLaser 'get the laser object from list
        LaserShot = LaserShots.Get(i)
  
       'colision missing

' destroy the laser when out of screen
            LaserShot.x = LaserShot.x + (Secs * LaserSpeed)
            If LaserShot.x > ACSF.Width Then LaserShots.RemoveAt(i)
    Next


    'Draws the laser shots
    Dim LaserShot As typLaser, R As Rect
    For i = 0 To LaserShots.Size - 1
        LaserShot = LaserShots.Get(i)
        R.Initialize(LaserShot.X, LaserShot.Y, LaserShot.X + LaserShotSize, LaserShot.Y + 0.5%x)
        AC.DrawRect(R, Colors.Cyan, True, 0, False)
    Next
 

andymc

Well-Known Member
Licensed User
Longtime User
Looks like you're on the right track. I would avoid using % of screen size though and stick to pixels.

You can download the source code for my earth defence fighter game here if it helps.
I would strongly advise using libgdx and then using vectors for moving objects like bullets, it makes using angles and speed much easier.
https://www.dropbox.com/s/95z9i2803c6cpex/Earth Defence Fighter.zip?dl=0
 

melonZgz

Active Member
Licensed User
Longtime User
Yep, soon or late you'll face a problem: not all the devices have the same aspect ratio. You can make your game with a fixed relation (lets say 16/9, wich is common in mobile), this will be fine in a lot of devices, but with a more square device (ie: tablet, wich can be 16/10), the game will look a little deformed (thats how I do the GUI).
You can also set the horizontal dimenson whatever you like, and calculate the vertical dimension depending on the screen ratio. This way you never strech the image, but in more square devices you'll show more world...
I also recommend using libGDX, the learning curve can look harder but it's woth the effort. And you'll be able to use Box2D! one of the best physics engine out there.
And another thing, when using lists instead of

B4X:
 'Draws the laser shots
    Dim LaserShot As typLaser, R As Rect
    For i = 0 To LaserShots.Size - 1
        LaserShot = LaserShots.Get(i)
        R.Initialize(LaserShot.X, LaserShot.Y, LaserShot.X + LaserShotSize, LaserShot.Y + 0.5%x)
        AC.DrawRect(R, Colors.Cyan, True, 0, False)
    Next

you can use

B4X:
 'Draws the laser shots
    Dim R As Rect
    For each LaserShot As typLaser in LaserShots
        R.Initialize(LaserShot.X, LaserShot.Y, LaserShot.X + LaserShotSize, LaserShot.Y + 0.5%x)
        AC.DrawRect(R, Colors.Cyan, True, 0, False)
    Next
It's supposed to be faster
 

hookshy

Well-Known Member
Licensed User
Longtime User
Thanks guys
Hope very soon to be able to release new game that I am working on, it will be a shooter variant
 

hookshy

Well-Known Member
Licensed User
Longtime User
Here is how I use the animation with all the objects, player waling,player runing, enemy shooting and so on ....
Will start new thread to game level desing because I did stucked with this new challange

B4X:
    'Draws enemy shoots
    Dim bombshot  As Physical_Object
    For i = 0 To bombshots.Size - 1
        bombshot = bombshots.Get(i)
       
        If animationindex=0 Then  bombshot.animationframe=bombshot.animationframe+1 'change actual frame of the bitmaps sprites, animation index is divided from app lifecycle
        AC.DrawBitmapAt(bmpax(bombshot.animationframe),bombshot.X,bombshot.Y)'render the bomb
        If bombshot.animationframe=7 Then bombshot.animationframe=0 'force the render to frame 0
    Next
 

hookshy

Well-Known Member
Licensed User
Longtime User
Looks like you're on the right track. I would avoid using % of screen size though and stick to pixels.

You can download the source code for my earth defence fighter game here if it helps.
I would strongly advise using libgdx and then using vectors for moving objects like bullets, it makes using angles and speed much easier.
https://www.dropbox.com/s/95z9i2803c6cpex/Earth Defence Fighter.zip?dl=0
I guess it will be easy to migrate to libgdx after mastering the game structure and concept
Your exemple is great and inspired a bit for some of the functions , thanks for sharing
 
Top