How to make one character shoot bullets

Monkey Forums/Monkey Beginners/How to make one character shoot bullets

En929(Posted 2014) [#1]
I was wondering do any of you have a simple formula that just involves one character image shooting an array of bullets? How would you go about making that? Yes, I know it's weird I made that entire game a week ago but can't seem to get this right. Thanks anyway.


therevills(Posted 2014) [#2]
Something like this:
If player shoot then
  create bullet
  add bullet to bullet list
EndIf

Loop on bullets list
  Move bullets
EndLoop



En929(Posted 2014) [#3]
Here's what I tried to do so far, but I can't get it right. I just have 2 characters here. The robot and the bullet, but it shoots all 4 bullets at once instead of shooting them individually.


Strict

Import mojo
Global CharacterImage:Image
Global CharacterStartingPointX: Int = 200
Global CharacterStartingPointY: Int = 190
Global CharacterW: Int = 120
Global CharacterH: Int = 270
Global BulletImage:Image
Global BulletAmount: Int = 4
Global bullets: Bullet[BulletAmount]
Global BulletW: Float = 32.0
Global BulletH: Float = 32.0


Global Shoot: Bool = false



Class MyApp Extends App



		Method OnCreate:Int()
			SetUpdateRate(60)
	
					CharacterImage = LoadImage("Robo.png", 1, Image.MidHandle)
			
				For Local r: Int = 0 Until BulletAmount
				
					Local blt: Bullet = New Bullet(CharacterStartingPointX+100+ r*BulletW, CharacterStartingPointY-80)
					bullets[r]  = blt
					BulletImage =LoadImage("Bullet.png")
				End
	
			
			
			
				Return 0
		End
	
	
		Method OnUpdate:Int()

				If KeyDown(KEY_A) Then
					Shoot = true
				
				End
				Return True
		End
		
		
		
		Method OnRender:Int()
			Cls
			
			
					DrawImage(CharacterImage, CharacterStartingPointX, CharacterStartingPointY)
				

				If Shoot Then
					For Local blt: Bullet = Eachin bullets
				 
						If  blt Then 
						    blt.Draw()
						End
					End
				End

			Return 0
		End
End




Class Bullet
	Public
	

	Field BulletPosX: Float
	Field BulletPosY: Float
	
			Method New(x1: Float, y1: Float)
			
			BulletPosX = x1
			BulletPosY = y1
	
			
			End
		
		
		
			Method Draw: Void()
				
				
					DrawImage(BulletImage, BulletPosX, BulletPosY)
					
					
			
				
				BulletPosX +=1
			End 

End


Function Main:Int()
	New MyApp()
	Return True
End



therevills(Posted 2014) [#4]
Try something like this, (I've removed the images and replaced them with Rects):
Strict

Import mojo

Global CharacterStartingPointX: Int = 200
Global CharacterStartingPointY:Int = 190
Global CharacterW: Int = 120
Global CharacterH: Int = 270
Global BulletAmount:Int = 4
Global bullets:Bullet[BulletAmount]
Global BulletW: Float = 32.0
Global BulletH: Float = 32.0

Class MyApp Extends App
	Method OnCreate:Int()
		SetUpdateRate(60)
		For Local r:Int = 0 Until BulletAmount
			Local blt:Bullet = New Bullet(CharacterStartingPointX + 100, CharacterStartingPointY)
			blt.shoot = False
			bullets[r] = blt
		End
		Return True
	End

	Method OnUpdate:Int()
		If KeyHit(KEY_A) Then
			For Local blt:Bullet = EachIn bullets
				If Not blt.shoot Then
					blt.shoot = True
					Exit
				End
			Next
		End
		For Local blt:Bullet = EachIn bullets
			If blt.shoot Then
				blt.Update()
			End
		Next
		Return True
	End
	
	Method OnRender:Int()
		Cls
		DrawRect CharacterStartingPointX, CharacterStartingPointY, CharacterW, CharacterH
		For Local blt:Bullet = EachIn bullets
			If blt.shoot Then blt.Draw()
		End
		Return True
	End
End

Class Bullet
	Field BulletPosX:Float
	Field BulletPosY:Float
	Field shoot:bool

	Method New(x1: Float, y1: Float)
		BulletPosX = x1
		BulletPosY = y1
	End

	Method Update:Void()
		BulletPosX += 5
		If BulletPosX > DeviceWidth()
			BulletPosX = CharacterStartingPointX + 100
			shoot = False
		End
	End
	
	Method Draw:Void()
		DrawRect(BulletPosX, BulletPosY, 10, 10)
	End
End

Function Main:Int()
	New MyApp()
	Return True
End



Goodlookinguy(Posted 2014) [#5]
I haven't looked at any of the code posted here, but this thread might have some similarities that may apply with regards to the topic: http://www.monkey-x.com/Community/posts.php?topic=7551


En929(Posted 2014) [#6]
Thanks therevills and Goodlucking, those two examples was perfectly what i was looking for.