RE: Bullet firing

BlitzMax Forums/BlitzMax Beginners Area/RE: Bullet firing

Joe90bigrat90(Posted 2011) [#1]
Hmmm Ive written a small game. (well actually ive had a lot of help from people on here). Its pretty crap really but im just trying to learn stuff as i go along.

I have 2 players on the screen with buildings.

Player1_xpos 'x of player1
Player1_ypos 'y of player1
Player2_xpos
Player2_ypos



Now the players can move in any direction. N,S,E,W,NE,NW,SE,SW
Player1_Direction
and Player2_Direction
to specify the direction they are moving.

How can i fire a bullet(well actually i want like machine gun shots. just full stops really) in the direction that the player is facing so if player 1 presses g it fires a shot and the shot continues on the screen and if you press fire again another shot is drawn so there will be more than 1 bullet on the screen. I dont want it so you fire and then have to wait until the bullet leaves the screen before firing again.

I dont know whether it is best to have an image for the bullet or to draw a line of full stops for the bullet.

If i was to draw a bullet image i would need 8 seperate bullet images for each direction. Well actually forward and back would be the same I guess and left and right and diagonals. So maybe 4 images.

If someone could write a little code to show me what i need to do that would be great.

Many thanks!!!

Kind Regards

Joe


Jesse(Posted 2011) [#2]
I believe that is a bit more advanced than what you are familiar with. I think you should get familiar with Tlist and Type.
Tlist lets you store multiple instances of objects in this case bullets in a list.
type allow you to create multiple objects(bullets) with out having to code variables for each object(bullet) created.
example of a type
Type Tbullet
   Field x:float
   Field y:float
End Type


example of a list:
global bulletList:Tlist = CreateList()


how to create a bullet:
Local bullet:Tbullet = new Tbullet
bullet.x = 10
bullet.y = 15


how to add a bullet to a list

ListAddLast(bulletList,bullet)


how to add 10 bullets to a list:

for local i:int = 1 to 10
   Local bullet:Tbullet = New Tbullet
   bullet.x = Rand(10)
   bullet.y = Rand(20)
  ListAddLast(bulletList,bullet)
next


how to display the 10 bullets that are on the list:

for local bullet:Tbullet = eachin bulletList
   DrawImage bulletImg,bullet.x,bullet.y
next

how to delete bullets from the list:
for local bullet:Tbullet = eachin bulletList
    ListRemove(bulletList,bullet)
next


edit:

how to manipulate bullets in the list:


for local bullet:Tbullet = eachin bulletList
    if bullet.x > 800  'bullet exited the field
        ListRemove(bulletList,bullet)
    else
        bullet.x = bullet.x + 10 ' move bullet 10 pixels
    endif
next


Last edited 2011


Joe90bigrat90(Posted 2011) [#3]
cool thankyou. I am starting to learn about types. They seem to be quite efficient. So looking at moving the bullet 10 pixels would I need to have some sort of image like a bullet image.
bullet.x=bullet.x +10

how does it know what is the image of the bullet????

I guess I need an image. Can i create full stops for bullets like its a machine gun firing bullets.

This evening I will try and code a man shooting a bullet and hope it works lol

Thanks Jesse for your help.


Jesse(Posted 2011) [#4]
you can add an image to the bullet Type

global bulletImg:Timage = LoadImage("myImg.png")

Type Tbullet
   Field x:Float
   Field y:Float
   Field image:Timage
End Type

local bullet:Tbullet = new Tbullet
bullet.x = Rand(100)
bullet.y = Rand(100)
bullet.image = bulletImg




and it can be displayed like so:

DrawImage bullet.image,bullet.x,bullet.y


you don't have to include it in the bullet type but makes it easier to keep track what image is used for what if you include it.


Joe90bigrat90(Posted 2011) [#5]
cool nice one. Going out tonight cos its friday wooohooooo but tommorrow after a hangover ill try coding in some bullet code.
Thanks a lot for your help mate. Much appreciated. If i get stuck or am having any problems ill post back here.
Thanks again

Kind Regards
Joe


Joe90bigrat90(Posted 2011) [#6]
ok im trying at the moment to mess around with types just to get a bullet on the screen. Im not worried about x and y at the moment.
Just if you press 'G' then i just want a bullet to be drawn on the screen. Im not worried about direction at the moment. I just want a bullet to be drawn.
Heres what i coded
Global bulletImg:TImage=LoadImage("d:\sprites\bullet.png")
Type Tbullet
Field x:Float
Field y:Float
Field image:TImage
Field Speed:Float
End Type

Global bulletList:TList = CreateList()

Local bullet:Tbullet=New Tbullet
   bullet.x = Player1_Xpos
   bullet.y = Player1_Ypos
bullet.image=bulletImg

Global Up_Pressed = False
Global Down_Pressed = False
Global Right_Pressed = False
Global Left_Pressed = False
Global Fire_Pressed =False

If KeyDown(KEY_G) Then Fire_Pressed = True

If Fire_Pressed = True Then
		FireBullet_Player1()

Function FireBullet_Player1()
For bullet:Tbullet = EachIn bulletList
  SetBlend(maskblend)
	 DrawImage bullet.image,bullet.x,bullet.y
SetBlend(solidblend)
Next
End Function



Obviously im not listing the whole program here. Just my bullet code.
I run the program and its not bringing up any errors.
I just cant see the bullet being drawn.
I think im doing something wrong. I have had a read of types in teh tutorials. So im learning slowly. hehe
Im just wondering if its drawing it so fast that i cant see it.
Please ignore the lack of code. My program does work so ignore the lack of flip wend and end etc. This is just the bullet code. I think im doing something wrong. Will I need a frametimer to slow down the bullet.
I dont think its even creating a bullet at the moment.

Last edited 2011

Last edited 2011

Last edited 2011

Last edited 2011


Joe90bigrat90(Posted 2011) [#7]
Ok ive added this line of code
ListAddLast(bulletList,bullet)

Now bulletlist,bullet=1
I now press fire button and bullet is appearing on the screen.
Its not moving but at least I know its working correctly.
Now i need to move the bullet depending on the direction of player.


Jesse(Posted 2011) [#8]
Ok. at least you know how to create objects now.

the next think is learning when to create them and when to delete them
I am going to show you an example of a object and when it's created and deleted:
Strict ' important for this code to work

'Global bulletImg:TImage=LoadImage("d:\sprites\bullet.png")
Type Tbullet
   Field x:Float
   Field y:Float
   Field dx:Float
   Field dy:Float
   Field speed:Float
'   Field image:TImage
End Type

Global PlayerBulletList:TList = New TList
Graphics  600,600
Repeat
    Cls()
    If KeyHit(key_space) ' if player hits space bar  create a bullet
         Local bullet:Tbullet = New Tbullet
         'the position at which the player's bullet is to be created. it is usually the the player's position
         bullet.x = 200 ' start position of bullet x 
         bullet.y = 200 ' start position of bullet y
         
         ' set the direction of the bullet with integers you get 8 possible directions
         ' it's not the best way to do it but it's good enough for learning the basics.
         bullet.dx = 1 'direction of bullet.x 1 to the right -1 to the left
         bullet.dy = 0 'direction of bullet.y 1 down -1 up

         ' here you can set the speed of the bullet from a fraction of a unit to any number desired.
         bullet.speed = 2.5
         'bullet.image = bulletImg
        ListAddLast(PlayerBulletList,bullet) 
   End If

   For Local bullet:Tbullet = EachIn playerBulletList
         'move bullet
          bullet.x = bullet.x+bullet.dx*bullet.speed
          bullet.y = bullet.y+bullet.dy*bullet.speed 
          ' check if bullet exited the field area
          If bullet.x < 0 Or bullet.x>600 Or bullet.y < 0 Or bullet.y > 600
              ListRemove(PlayerBulletList,bullet)  'delete the bullet from the list
          Else
             'display the bullet if not deleting it. I am using a rectangle but 
             'uncomment all of the image variables that are commented out tho use the image
             ' and replace the drawrect with the proper drawimage to display the bullet image.
             DrawRect bullet.x,bullet.y,4,4
          EndIf
   Next
   DrawText "Press Space bar to shoot.",10,10
   DrawText "bullets active "+PlayerBulletList.count(),10,40
   Flip()
Until KeyDown(key_escape) 


note that every bullet is an object and is created on the fly when ever you hit the space bar, and is deleted when it exits the play area.

note that everything in your game can be created as an object: the player, the enemies, and even the particle effects.

a little bonus(advanced) see if you can figure it out.
types(objects) can be initialized within themselves:
type Tbullet
   field x:float
   field y:float
   field dx:float
   field dy:float
   field speed:float
   field image:TImage

   function create:Tbullet(x1:float,y1:float,dx1:float,dy1:float,speed1:float,image1:Timage)
          local b:Tbullet = new Tbullet
          b.x = x1
          b.y = y1
          b.dx = dx1
          b.dy = dy1
          b.speed = speed1
          b.image = image1
          return b
    end function
end type 

local bullet:Tbullet = Tbullet.create(200,200,1,0,2.5,bulletImg)



note how in this way the actual bullet is created in the function and then passed to the bullet variable "Local bullet:Tbullet". See how the parameters are the same as in the previous post.
One advantage of putting a function inside the Type is that you can have different obect types such as enemy Type, player Type etc. and each with a function called "create" with its own parameters. if it was outside of the type a different name would have to be give to each function designed to initialize a specific Type.

so now whenever you want to process a bullet all you have to do is this:

    If KeyHit(key_space) ' if player hits space bar  create a bullet
         Local bullet:Tbullet = Tbullet.create(200, 200, 1, 0, 2.5, bulletImg) ' the same parameters as the previous post code.
        ListAddLast(PlayerBulletList,bullet) 
   End If

everything else is the same.

if you figure this last part out and you want to learn about methods(its the principle of encapsulating objects to be self contained). let me know and I will try to explain them to you as best as I can.

Last edited 2011

Last edited 2011

Last edited 2011


Zacho(Posted 2011) [#9]
Thanks again Jesse!! You're explanation has helped me in my program as well