Single shot ?

Blitz3D Forums/Blitz3D Beginners Area/Single shot ?

How can i get just one grenade to fire at a time

Graphics 800,600,16,2

Type player
	Field x#,y#
	Field ang#,speed#
	Field img
	Field hp
End Type

Type grenade
	Field x#,y#
	Field ang#,speed#
	Field img
	Field life#
	Field ammo
End Type

Const Gunsize = 8

Global me.player = New player
Global g.grenade = New grenade

me\x=400
me\y=300
me\img=CreateImage(9,9)
SetBuffer ImageBuffer(me\img)
Color 100,255,100
Oval 0,0,8,8 
MidHandle me\img

SeedRnd MilliSecs()
HidePointer
SetBuffer BackBuffer()

;MAIN LOOP----------------------------------------

While Not KeyDown(1)

	Cls

		Color 150,150,150
		Oval MouseX(),MouseY(),4,4 

		Color 255,255,255
		Text 0,0,"LMB = Grenade "
			
		DrawPlayer()
	
		UpdatePlayer()
			
	Flip

Wend

FreeImage me\img

End

;FUNCTIONS-----------------------------------------

Function DrawPlayer()
	
	DrawImage me\img,me\x,me\y 
	
End Function

;--------------------------------------------------

Function UpdatePlayer()
		
		dX = MouseX() - me\x
		dY = MouseY() - me\y

		Gunang = ATan2(dY,dX)

		Gundx# = Cos(Gunang)
		Gundy# = Sin(Gunang)
	
		GunendX = me\x + gundx*Gunsize
		GunendY = me\y + gundy*Gunsize
	
		Color 100,255,100
		Line(me\x,me\y,gunendx,gunendy)
		
		If MouseHit(1) 
				
		g.grenade = New grenade 
		g\x = gunendx : g\y = gunendy
		g\ang= ATan2(MouseX()-g\x,MouseY()-g\y)
		g\speed=4+me\speed
		g\life=100
		g\ammo=1
			
		End If
		
		For g.grenade = Each grenade
			
			If g\ammo >0
				movegrenade(g)
			End If
			
		Next

End Function

;--------------------------------------------------

Function Movegrenade(g.grenade)

		g\y=g\y+(Cos(g\ang)*g\speed)
		g\x=g\x+(Sin(g\ang)*g\speed)
		
		gw=4
		gh=4
		
		Color 100,100,100		 		
		Oval g\x,g\y,gw,gh,1
		
		g\life=g\life-1
		
		If g\life<90 
			
			g\speed=4
			
		End If
		
		If g\life<70 
			
			g\speed=1
						
		End If

		If g\life<30
		
			g\speed=0
			
		End If
			
		If g\life<1
			g\ammo=0
			Delete g.grenade
		
		End If
	
End Function

;--------------------------------------------------


I need it to wait for the first grenade to delete before i can fire a second grenade


Thanks
destroyer :)

Change your creation code for grenade to this:

		If MouseHit(1) 
			g.grenade = First grenade
			If g = Null Then  		
				g.grenade = New grenade 
				g\x = gunendx : g\y = gunendy
				g\ang= ATan2(MouseX()-g\x,MouseY()-g\y)
				g\speed=4+me\speed
				g\life=100
				g\ammo=1
			EndIf
		End If


(and delete the global g.grenade = new grenade line, it is not needed...)

Basically it just sets g.grenade to the last grenade of the type, and if there is no grenade in that spot, then there are no grenades on screen. Alternatively, you could just use a global number of grenades, and then incriment it when you create a gernade and decrement it when you delete a grenade, and then check to see how many grenades are present before creating.

Thanks mindstorms works spot on :)

I did'nt know about the First type_variable.

Pays to read the manual lol

Thanks
Destroyer :)

Ok a new problem :)

How can i get the enemy to respawn every new level?

I start off with 2 enemy's and start on level 1.
I would like to increase the amount of enemy by 2 every new level.

Graphics 800,600,16,2

Type player
	Field x#,y#
	Field ang#,speed#
	Field img
	Field hp
End Type

Type grenade
	Field x#,y#
	Field ang#,speed#
	Field img
	Field life#
	Field ammo
End Type

Type shrap
    Field x#,y#,dx#,dy#
	Field timer
	Field size#
	Field r,g,b
End Type

Type enemy
	Field enx#,eny#
	Field life
	Field img
	Field hp
	Field state
	Field m_time
	Field dir
End Type

Const Gunsize = 8

Global me.player = New player
Global clickedx = MouseX()
Global clickedy = MouseY()
;Global sndExplode=LoadSound("explode.wav")

Global nme = 2
Global hp = 100
Global gammo=6
Global level=1

me\x=400
me\y=300
me\img=CreateImage(9,9)
SetBuffer ImageBuffer(me\img)
Color 100,255,100
Oval 0,0,8,8 
MidHandle me\img

SeedRnd MilliSecs()

;enemy/gfx-----------

For i=1 To nme
	en.enemy=New enemy
	en\enx=Rand(0,800)
	en\eny=Rand(0,600)
	en\img=CreateImage(9,9)
	en\hp=100
	SetBuffer ImageBuffer(en\img)
	Color 255,100,100
	Oval 0,0,8,8 
	MidHandle en\img	
Next

;--------------------

HidePointer
SetBuffer BackBuffer()

;MAIN LOOP----------------------------------------

While Not KeyDown(1)

	Cls

		Color 150,150,150
		Oval MouseX(),MouseY(),4,4 

		Color 255,255,255
		Text 0,0,"LMB = Move / RMB = Grenade"
		
		Text 100,15,"G-Ammo "+gammo
		
		Text 200,15,"Level "+level
				
		DrawPlayer()
		
		DrawEnemy()
		
		UpdatePlayer()
		
		Moveshrap()
			
	Flip

Wend

FreeImage me\img

End

;FUNCTIONS-----------------------------------------

Function DrawPlayer()
	
	DrawImage me\img,me\x,me\y 
	
End Function

;--------------------------------------------------

Function DrawEnemy()

For en.enemy = Each enemy

		Text 0,15,"Enemy = "+nme
		
		DrawImage en\img,en\enx,en\eny

		
	Next

End Function

;--------------------------------------------------

Function UpdatePlayer()
		
		If MouseHit(1)
	
			clickedx=MouseX()
			clickedy=MouseY()
			me\ang=ATan2(clickedx-me\x,clickedy-me\y)
			me\speed=2
		
		EndIf
	
		If Sqr((me\x-clickedx)^2+(me\y-clickedy)^2)<3
   			me\speed=0
		EndIf
	
		me\y=me\y+(Cos(me\ang)*me\speed)
		me\x=me\x+(Sin(me\ang)*me\speed)

		dX = MouseX() - me\x
		dY = MouseY() - me\y

		Gunang = ATan2(dY,dX)

		Gundx# = Cos(Gunang)
		Gundy# = Sin(Gunang)
	
		GunendX = me\x + gundx*Gunsize
		GunendY = me\y + gundy*Gunsize
	
		Color 100,255,100
		Line(me\x,me\y,gunendx,gunendy)
			
		If MouseHit(2) And gammo > 0
			g.grenade = First grenade
			If g = Null Then  		
				g.grenade = New grenade 
				g\x = gunendx : g\y = gunendy
				g\ang= ATan2(MouseX()-g\x,MouseY()-g\y)
				g\speed=4+me\speed
				g\life=150
				gammo=gammo-1
				
				If gammo <= 0
				gammo=0
				End If
				
			EndIf
		End If
		
		For g.grenade = Each grenade
		
				MoveGrenade(g)
				
		
		Next

End Function

;--------------------------------------------------

Function MoveGrenade(g.grenade)

		g\y=g\y+(Cos(g\ang)*g\speed)
		g\x=g\x+(Sin(g\ang)*g\speed)
		
		gw=4
		gh=4
		
		Color 100,100,100		 		
		Oval g\x,g\y,gw,gh,1
		
		g\life=g\life-1
		
		If g\life<150 
			
			g\speed=3
			
		End If
		
		If g\life<100 
			
			g\speed=1.5
					
		End If

		If g\life<50
		
			g\speed=0
			Color Rnd(48,228),Rnd(48,228),0	 		
			Oval g\x,g\y,gw,gh,1
	
		End If
			
		If g\life<1
			
			PlaySound sndExplode
		
			For i=1 To 20
				s.shrap = New shrap
				s\x = g\x
				s\y = g\y
				s\dx = Rnd(-4,4):s\dy=Rnd(-4,4)
				s\size = Rnd(2,4)
				
				col# = Rnd(48,128)
				s\r = col: s\g = col: s\b = col
			
			If i>10 Then
				
				s\r=255: s\g=300-s\size*40: s\b=0
				s\dx=s\dx/2
				s\dy=s\dy/2
			
			End If
		
			Next
			
			Delete g
		
		End If
	
End Function

;--------------------------------------------------

Function Moveshrap()

	For s.shrap = Each shrap
       	s\x = s\x + s\dx
    	s\y = s\y + s\dy
    	s\timer=s\timer+1
    	If s\timer>30 Then Delete s 
	Next

	For s.shrap = Each shrap
		Color s\r,s\g,s\b
		Oval s\x-s\size/2,s\y-s\size/2,s\size,s\size
	Next
	
	;Collision check
	For s.shrap = Each shrap
		For en.enemy=Each enemy
		
		If Sqr((s\x-en\enx)^2+(s\y-en\eny)^2)<8
			
			en\hp=en\hp-20
			
			If en\hp < 0
				nme = nme-1 : Delete en.enemy
				hp=100
				
				If nme <=0
					gammo=gammo+1
					level=level+1
				End If
				
			End If
			
	
		End If
		
		Next
	Next

End Function



Thanks

Destroyer :)

Just put a function like this:

Function create_enemies(numb)
For i=1 To numb
	en.enemy=New enemy
	en\enx=Rand(0,800)
	en\eny=Rand(0,600)
	en\img=CreateImage(9,9)
	en\hp=100
	SetBuffer ImageBuffer(en\img)
	Color 255,100,100
	Oval 0,0,8,8 
	MidHandle en\img
	SetBuffer BackBuffer()	
Next
End Function

then call it when the number of enemies is 0, with the level times two as the number of enemies to create

Thanks again Mindstorms.

Will add that function.

Thanks
Destroyer :)

On second thought, It would probably be better to create a base image at the beginning of the program, then copy it in the create enemy function

This is what i have so far.

Things seem to be working well apart from the enemy fire, but its late so ill try again tomorrow

 ;*****************
;*   F O D D A   *
;*****************

Graphics 800,600,16,2

Type player
	Field x#,y#
	Field ang#,speed#
	Field img
	Field hp
End Type

Type enemy
	Field enx#,eny#
	Field life
	Field img
	Field hp
	Field state
	Field m_time
	Field dir
End Type

Type bullet
	Field x#,y#
	Field ang#,speed#
	Field img
	Field life#
	Field ammo#
	Field make
End Type

Type grenade
	Field x#,y#
	Field ang#,speed#
	Field img
	Field life#
	Field ammo#
End Type

Type blood
    Field x#,y#,dx#,dy# ;location and speed
	Field timer
	Field size#
	Field r,g,b
End Type

Type shrap
    Field x#,y#,dx#,dy# ;location and speed
	Field timer
	Field size#
	Field r,g,b
End Type

Type particle
	Field x#,y#,dx#,dy#;location and speed
	Field size#
	Field r,g,b
End Type

Const Gunsize = 8

Global me.player = New player
Global b.bullet = New bullet

Global clickedx = MouseX()
Global clickedy = MouseY()
Global mover = 1
Global nme = 2
Global hp = 100
Global gammo=6
Global level=1

Global sndExplode=LoadSound("explode.wav")

Global sndPistol=LoadSound("pistol.wav")

;player gfx----------

me\x=400
me\y=300
me\img=CreateImage(9,9)
SetBuffer ImageBuffer(me\img)
Color 100,255,100
Oval 0,0,8,8 
MidHandle me\img

;--------------------
SeedRnd MilliSecs()

create_enemies(nme)
;--------------------

HidePointer
SetBuffer BackBuffer()

;**MAIN LOOP**--------------------------------------

While Not KeyDown(1)

	Cls

		Color 150,150,150
		Oval MouseX(),MouseY(),4,4 ;mouse gfx
		
					
			DrawPlayer()

			DrawEnemy()
	
			UpdatePlayer()
			
			MoveBlood()
			
			Moveshrap()

	For pt.particle = Each particle
		
		pt\x = pt\x + pt\dx
		pt\y = pt\y + pt\dy
		pt\size = pt\size - .6

	If pt\size=<0 Then Delete pt
	
	Next
		    					
	For pt.particle = Each particle
		
		Color pt\r,pt\g,pt\b
		Oval pt\x-pt\size/2,pt\y-pt\size/2,pt\size,pt\size
	
	Next
	
		Color 255,255,255;t
		Rect 0,0,800,40
		Color 55,55,55;b
		Rect 1,1,800,40
		Color 150,150,150;m
		Rect 1,1,799,39
		
		;On screen info
		Color 55,55,55
		Text 5,5,"LMB = Move / RMB = Shoot / Spacebar = Grenade"
		
		Text 100,20,"G-Ammo "+gammo
		
		Text 190,20,"Level "+level
		
		Text 5,20,"Enemy = "+nme

	Flip

Wend

FreeImage me\img

End

;--------------------------------------------------
;Functions
;--------------------------------------------------

Function DrawPlayer()
	
	DrawImage me\img,me\x,me\y ;draw player
	
End Function

;--------------------------------------------------

Function UpdatePlayer()

		If MouseHit(1)
	
			clickedx=MouseX()
			clickedy=MouseY()
			me\ang=ATan2(clickedx-me\x,clickedy-me\y)
			me\speed=2
		
		EndIf
	
		If Sqr((me\x-clickedx)^2+(me\y-clickedy)^2)<3
   			me\speed=0
		EndIf
	
		me\y=me\y+(Cos(me\ang)*me\speed)
		me\x=me\x+(Sin(me\ang)*me\speed)
		
		dX = MouseX() - me\x
		dY = MouseY() - me\y

		Gunang = ATan2(dY,dX)

		Gundx# = Cos(Gunang)
		Gundy# = Sin(Gunang)
	
		GunendX = me\x + gundx*Gunsize
		GunendY = me\y + gundy*Gunsize
	
		Color 100,255,100
		Line(me\x,me\y,gunendx,gunendy) ;draw gun	
		
		If MouseHit(2)
		
			b.bullet = New bullet ;Create bullet
			b\x = gunendx : b\y = gunendy
			b\ang= ATan2(MouseX()-b\x,MouseY()-b\y)+Rnd(-3,6)
			b\speed=4+me\speed
			b\life=60
			b\make=0 
		
		PlaySound sndPistol
		
		End If
		
	
		For b.bullet = Each bullet
			movebullet(b)
		Next
			
		If MouseHit(3) Or KeyHit(57) And gammo > 0
			g.grenade = First grenade
			If g = Null Then  		
				g.grenade = New grenade 
				g\x = gunendx : g\y = gunendy
				g\ang= ATan2(MouseX()-g\x,MouseY()-g\y)
				g\speed=4+me\speed
				g\life=100
				gammo=gammo-1
				
				If gammo <= 0
				gammo=0
				End If
				
			EndIf
		End If


		
		For g.grenade = Each grenade
		
			movegrenade(g)
		
		Next

End Function

;--------------------------------------------------

Function DrawEnemy()

		If nme <= 0
		
				level=level+1
				gammo=gammo+1
				nme=level*2
				
				If level >= 5
				nme=level*3
				End If
			
				create_enemies(nme)
		End If

For en.enemy = Each enemy
		
		DrawImage en\img,en\enx,en\eny

		Select(en\state)
			
			Case 0
			
            	 If en\m_time = 0 
                        en\dir = Rand(4) 
                        en\m_time = Rand(300)
                 End If

				 If en\dir = Rand(4) And Sqr((en\enx-me\x)^2+(en\eny-me\y)^2)<50
					en\state=2				
				 End If	
				
			Case 1
			
				If en\enx > me\x Then en\dir=1
                If en\enx < me\x Then en\dir=0
                If en\eny > me\y Then en\dir=3
                If en\eny < me\y Then en\dir=2
				en\state=2
		
			Case 2

				dX = me\x - en\enx
				dY = me\y - en\eny
				
				Gunang = ATan2(dy,dx)

				Gundx# = Cos(Gunang)
				Gundy# = Sin(Gunang)
	
				GunendX = en\enx + gundx*Gunsize
				GunendY = en\eny + gundy*Gunsize
				Color 255,100,100
				Line(en\enx,en\eny,gunendx,gunendy)	
				
				If en\enx>me\x Then en\enx=en\enx-.8
				If en\enx<me\x Then en\enx=en\enx+.8
				If en\eny>me\y Then en\eny=en\eny-.8
				If en\eny<me\y Then en\eny=en\eny+.8
				
				b.bullet = First bullet
				If b = Null
				b.bullet = New bullet ;Create bullet
				b\x = gunendx : b\y = gunendy
				b\ang= ATan2(me\x-b\x,me\y-b\y)+Rnd(-3,6)
				b\speed=4
				b\life=60
				b\make=1 
				PlaySound sndPistol
				End If

		End Select
		
		Select (en\dir)
        
        Case 0
                en\enx=en\enx+.5
        Case 1
                en\enx=en\enx-.5
        Case 2
                en\eny=en\eny+.5
        Case 3
                en\eny=en\eny-.5
        Case 4
        
        End Select

		en\m_time=en\m_time-1

		If en\enx > 790 Then en\dir=1
        If en\eny > 590 Then en\dir=3
        If en\enx < 20 Then en\dir=0
        If en\eny < 50 Then en\dir=2

		If ImagesCollide (me\img, me\x, me\y,0, en\img, en\enx, en\eny,0)
			angle# = ATan2(me\y - en\eny,en\enx - me\x)
			en\enx = en\enx + (mover * (Cos (angle)))
			en\eny = en\eny + (mover * (Sin (angle)))
		End If

	Next

End Function

;--------------------------------------------------

Function MoveBullet(b.bullet)

		b\y=b\y+(Cos(b\ang)*b\speed)
		b\x=b\x+(Sin(b\ang)*b\speed)
		
		Color 200,200,100		 		
		Rect b\x,b\y,2,2

		;Collision check
		If b<>Null
		For en.enemy=Each enemy
		
		If Sqr((b\x-en\enx)^2+(b\y-en\eny)^2)<8 And b\make=0
			
			hp=hp-20
			
			If hp < 0
				nme = nme-1 : Delete en.enemy
				hp=100
			End If
			
			b\life=-1
			
			For i=1 To 10
				bl.blood = New blood
				bl\x = b\x
				bl\y = b\y
				bl\dx = Rnd(-2,2):bl\dy=Rnd(-2,2)
				bl\size = Rand(1,2)	
			Next

		End If
		
		Next
		End If
		
		b\life=b\life-1	

		If b\life<0 ;Then Delete b.bullet

			For i=1 To 10
				
				pt.particle = New particle
				pt\x = b\x
				pt\y = b\y
				pt\dx = Rnd(-2,2):pt\dy=Rnd(-2,2)
				pt\size = Rand(2,4)
				col# = Rnd(48,128)
				pt\r = col: pt\g = col: pt\b = col
				
				If i>3 Then
				
				pt\r=255: pt\g=300-pt\size*40: pt\b=0 ;colour it red/yellow
				pt\dx=pt\dx/2 ;slow it down
				pt\dy=pt\dy/2
				
				End If
			Next

		
			Delete b.bullet
			
		End If

End Function

;--------------------------------------------------

Function Movegrenade(g.grenade)
		
		g\y=g\y+(Cos(g\ang)*g\speed)
		g\x=g\x+(Sin(g\ang)*g\speed)
		
		gw=4
		gh=4
		
		Color 100,100,100		 		
		Oval g\x,g\y,gw,gh,1
		
		g\life=g\life-1
			
		If g\life<70
		
			g\speed=1
		
		End If
		
		If g\life<50
			
			g\speed=.5
		
		End If

		If g\life<30
		
			g\speed=0
			Color Rnd(48,228),Rnd(48,228),0	 		
			Oval g\x,g\y,gw,gh,1

		End If
			
		If g\life<1
	
		PlaySound sndExplode
		
			For i=1 To 30
				s.shrap = New shrap
				s\x = g\x
				s\y = g\y
				s\dx = Rnd(-4,4):s\dy=Rnd(-4,4)
				s\size = Rnd(2,4)
				
				col# = Rnd(48,128)
				s\r = col: s\g = col: s\b = col
				
			If i>5 Then
				
				s\r=255: s\g=300-s\size*40: s\b=0
				s\dx=s\dx/2
				s\dy=s\dy/2
			
			End If
			
			Next
	
			Delete g.grenade
		
		End If
		
End Function

;--------------------------------------------------

Function MoveBlood()

	For bl.blood = Each blood
       	bl\x = bl\x + bl\dx
    	bl\y = bl\y + bl\dy
    	bl\timer=bl\timer+1
    	If bl\timer>15 Then Delete bl ;15 frames until destroy
	Next

	For bl.blood = Each blood
		Color 255,0,0
		Oval bl\x-bl\size/2,bl\y-bl\size/2,bl\size,bl\size
	Next

End Function

;--------------------------------------------------

Function Moveshrap()

	For s.shrap = Each shrap
       	s\x = s\x + s\dx
    	s\y = s\y + s\dy
    	s\timer=s\timer+1
    	If s\timer>30 Then Delete s ;15 frames until destroy
	Next

	For s.shrap = Each shrap
		Color s\r,s\g,s\b
		Oval s\x-s\size/2,s\y-s\size/2,s\size,s\size
	Next

	;Collision check
	For s.shrap = Each shrap
		For en.enemy=Each enemy
		
		If Sqr((s\x-en\enx)^2+(s\y-en\eny)^2)<8
			
			en\hp=en\hp-50
			
			If en\hp < 0
				nme = nme-1 : Delete en.enemy
				hp=100
			End If
			
			
			For i=1 To 2
				bl.blood = New blood
				bl\x = s\x
				bl\y = s\y
				bl\dx = Rnd(-2,2):bl\dy=Rnd(-2,2)
				bl\size = 2	
			Next
	
		End If
		
		Next
	Next
End Function

;--------------------------------------------------

Function create_enemies(nme)
For i=1 To nme
	en.enemy=New enemy
	en\enx=Rand(0,800)
	en\eny=Rand(0,600)
	en\img=CreateImage(9,9)
	en\hp=100
	SetBuffer ImageBuffer(en\img)
	Color 255,100,100
	Oval 0,0,8,8 
	MidHandle en\img
	SetBuffer BackBuffer()	
Next
End Function


Thanks
Destroyer

If I were you I would go through your code and make it more efficient and easy to read (like using just the function to create enemies instead of three different times, or not drawing the enemy image to the new enemy everytime, but copy the image from a base image created at the beginning.)
In other words, delete duplicated code and try to make it run more efficiently...

[codebox]

Thanks Octothorpe and Mindstorms for the tips

Thanks
Destroyer :)