Spawning Bullets?

Started by Baggey, December 21, 2024, 10:50:10

Previous topic - Next topic

Baggey

Im trying to just make a small program to get the idea of spawning Bullets.

Ive created some code but not sure where to go with it?

SuperStrict

Graphics 800,600

' Create Bullets

Global Bullet_Quantity:Int=0 'current number of bullets
Global Bullet_Rad:Int=2   'bullet radius (used For collision purposes)
Global Bullet_Speed:Int=4 'the bullet speed

Global Bullet_Spr:Int'`the number of the sprite used For the "master" bullet (see later)

' This defines the Type we will use To store our individual bullet information

Type TBullets

Field Tyype : Int ' Type of bullet
Field X : Int ' x pos of bullet
Field Y : Int ' y pos of bullet

Field SpriteNUM : Int ' sprite number of bullet

Method Draw()
        DrawRect( X, Y, 2, 2 )
    End Method

    Method Move()
        X:+Bullet_Speed
        Y:+Bullet_Speed
    End Method

    Method Create()

       newObject = New TBullets

       Bullet_Quantity:+1
     
       newObject.X = 50
       newObject.Y = 400
      
       BulletList.AddLast(newObject)

    End Method

    Method Destroy()

       BulletList.RemoveLast()

    End Method

EndType

Global BulletList:TList = CreateList()

Global newObject:TBullets


Repeat

Cls

If AppTerminate() Or KeyHit(Key_ESCAPE) Then End


If KeyHit(Key_SPACE) Then newObject.Create()

Flip(1)

Forever


Kind Regards Baggey
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on 2 x HP Z24's . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!

Dabz

#1
Going of your example, I would do it something like this:
Const SCREEN_WIDTH:Int = 800
Const SCREEN_HEIGHT:Int = 600

Type TBullet
Global bulletList:TList = New TList
    Field x:Int
    Field y:Int
    Field bulletSize:Int = 2
    Field bulletSpeed:Int = 4
  
    Function AddBullet(x:Int, y:Int)
Local bullet:TBullet = New TBullet
bullet.x = x
bullet.y = y
bulletList.addLast(bullet)
    End Function
  
    Function ProcessBullets()
        For Local bullet:TBullet = EachIn bulletList
            If bullet.x < 0 Or bullet.y < 0 Or bullet.x > SCREEN_WIDTH Or bullet.y > SCREEN_HEIGHT
                bulletList.RemoveLink(bulletList.FindLink(bullet))
            Else
                'This obviously makes the bullet go down and right, not
                'sure if that is what your after, but that's what it looks
                'like in your code
                bullet.x:+ bullet.bulletSpeed
                bullet.y:+ bullet.bulletSpeed
                DrawRect bullet.x,bullet.y,bullet.bulletSize,bullet.bulletSize
            EndIf
        Next
    End Function
End Type

Local mx:Int, my:Int

Graphics SCREEN_WIDTH,SCREEN_HEIGHT

Repeat
Cls
mx = MouseX()
my = MouseY()

'Hit left mouse to fire a single bullet
If MouseHit(1) Then TBullet.AddBullet(mx,my)
'Keep right mouse down for rapid AF fire! :)
If MouseDown(2) Then TBullet.AddBullet(mx,my)

TBullet.ProcessBullets()

DrawText("Bullets: "+TBullet.bulletList.Count(),0,0)
Flip
Until KeyDown(KEY_ESCAPE)

Not everything has to be a method! ;)

Dabz
Intel Core i5 6400 2.7GHz, NVIDIA GeForce GTX 1070 (8GB), 16Gig DDR4 RAM, 256GB SSD, 1TB HDD, Windows 10 64bit

Midimaster

#2
Less writing ---> less bugs and less typos



Do not create unnecessary GLOBALS.
GraphicsWidth() needs not be stored into a GLOBAL


Do not create "double named" variable names:

TBullet.BulletList         -----> TBullet.List
bullet.X = bullet.x + bulletsize -----> X = X + Size
Tbullet.ProcessBullets           -----> TBullet.Process
' extrem:
DrawRect bullet.x,bullet.y,bullet.bulletSize,bullet.bulletSize -----> DrawRect x, y, size, size


Better often use Methods instead staying inside the function
Type TBullet
    ....
    Function ProcessAll()
        For Local bullet:TBullet = EachIn List
        bullet.process
    Next
   End Function

    Method Process()
        ' do it here
    End Method


    Function DrawAll()
        For Local bullet:TBullet = EachIn List
            bullet.Draw
        Next
    End Function
   
    Method Draw()
        ' do it here
    End Method
End Type


Do not combine Checking and Drawing in one function
Repeat
    ...
    TBullet.ProcessAll()
    TBullet.DrawAll()
Until AppTerminate()

Type TBullet
    ....
    Function ProcessAll()
        For Local bullet:TBullet = EachIn List
            bullet.process
        Next
    End Function

    Function DrawAll()
        For Local bullet:TBullet = EachIn List
            bullet.Draw
        Next
    End Function

End Type
This will be later helpful, if checking needs a different order than drawing (e.g. near to far away objects)


Use Method New() for define the default setting
Repeat
    ...
    If MouseHit(1)  Then New TBullet( x , y )
    ...
Until AppTerminate()


Type TBullet
    ...
    Method New(newX:Int, newY:Int)
        x = newX
        y = newY
        List.addLast Self
    End Method


Here is a runnable code example:

SuperStrict
Graphics 800,600

Repeat
    Cls
    If MouseHit(1)  Or MouseDown(2) Then New TBullet( MouseX() , MouseY() )

    TBullet.ProcessAll()
    TBullet.DrawAll()

    DrawText("Bullets: "+TBullet.List.Count(),0,0)
    Flip
Until AppTerminate()

Type TBullet
    Global List:TList = New TList
    Field x:Int, y:Int, Size:Int = 2, Speed:Int = 4
 
    Method New(newX:Int, newY:Int)
        x = newX
        y = newY
        List.addLast Self
    End Method
 
    Function ProcessAll()
        For Local bullet:TBullet = EachIn List
            bullet.process
        Next
    End Function
   
    Method Process()
        If (x<0) Or (y<0) Or ( x>GraphicsWidth() ) Or ( y > GraphicsHeight() )
            List.Remove Self
        Else
            x:+ Speed
            y:+ Speed
        EndIf
    End Method

    Function DrawAll()
        For Local bullet:TBullet = EachIn List
            bullet.Draw
        Next
    End Function

    Method Draw()
        DrawRect x, y, Size, Size
    End Method
End Type

...back from North Pole.