How do you get bullets to shoot?

BlitzPlus Forums/BlitzPlus Programming/How do you get bullets to shoot?

ok(Posted 2013) [#1]
I literally just started BlitzPlus programming and after watching a couple videos I thought I would make my own REALLY simple game but I can't figure out how to get the bullet to move and hit the bad guy?


;setup

AppTitle("BangSheBang Diggy Diggy")

Graphics 1080,720,0,2

SetBuffer BackBuffer()

Flip


;initialization

Global img_man = LoadImage("man.bmp")           ; Width:175   Height:200
Global img_bullet = LoadImage("bullet.bmp")		; Width:10    Height:10
Global img_evil = LoadImage("evil.bmp")			; Width:175   Height:200


Global background = LoadImage("background.bmp")
Global background_x = 0

Global player_x = 100
Global player_y = 360

random = Rnd(100,540)

Global evil_x = 1080
Global evil_y = random

Global bullet_x = player_x
Global bullet_y = player_y
Global bulletspeed_x = 10

Global score = 0

; Main Loop

While Not KeyHit(1)

	Cls
		FDrawBackground()
		FDisplayScore()
		FDrawPlayer()
		FDrawEvil()
		FShootGun()
		
	Flip
Wend 
	; 											Draw Background
	
	Function FDrawBackground()
	
		TileImage(background,background_x,0)
		background_x = background_x - 3
	
	End Function 
	
	
	
	; 											Display Score
	
	Function FDisplayScore()
	
		Text 500,100, "Score: " + score
	
	End Function 
	
	
	
	; 											draw/move player
	
	Function FDrawPlayer()
	
		DrawImage img_man,player_x,player_y
	
		If KeyDown(200) 
			player_y = player_y - 6
			If player_y < 100 Then player_y = 100
		EndIf

	
		If KeyDown(208)
			player_y = player_y + 6
			If player_y > 720 - 200 Then player_y = 720 - 200
		EndIf
	
	
		If KeyDown(203)
			player_x = player_x - 6
			If player_x < 0 Then player_x = 0	
		EndIf
	
		If KeyDown(205)
			player_x = player_x + 6
			If player_x > 300 Then player_x = 300
		EndIf
	
	End Function 
	

	
	
	;											draw/move villan
	
	Function FDrawEvil()
	
		DrawImage img_evil,evil_x,evil_y 
	
		evil_x = evil_x - 4
		
	End Function 
	
	
	
	
	;											shoot gun
	
	Function FShootGun()

	
		DrawImage img_bullet,bullet_x,bullet_y
	
		If KeyHit(57)
			bullet_x = bullet_x + bulletspeed_x
		EndIf 
	End Function 
	
	
	
	;end
	
	If ImagesCollide(img_man,player_x,player_y,0,img_evil,evil_x,evil_y,0) 
		evil_x = 1080
		evil_y = random	
	EndIf 
		




I know it's bad. I also set it so that the bad guy appears randomly on the y-axis but it keeps appearing in the same spot. If I can get help for any of this it would be VERY much appreciated.


Midimaster(Posted 2013) [#2]
I give you the anser to your first question:


the bug is in the function FShootGun(). Here you add bulletspeed_x to the bullets coordinate bullet_x.

But you do this only one time, when the KeyHit(57) is pressed.

You should add the speed all the time. But if you do that the bullet flys away at the beginning of the game before you started the shoot.

The solution is to keep the speed to zero until the KeyHit() is pressed. Then set the speed to 10:

Global bulletspeed_x = 0
....

Function FShootGun()
	DrawImage img_bullet,bullet_x,bullet_y
	bullet_x = bullet_x + bulletspeed_x

	If KeyHit(57)
		bulletspeed_x =10
	EndIf 
End Function 



The hitting is another new problem:

It is the same procedure as the collision between the player and the evil. I see, you already found the solution for those both actors.

In the case of bullets you have to create a similar function, but this time with "bullets" and "evil".


RemiD(Posted 2013) [#3]
Or you could add another function :



I suggest to learn good habits now, and try to separate the logic and the render (or "drawing") in your functions, for example :
UpdateEnvironment()
UpdatePlayer()
UpdateEnemy()
UpdateBullets()
DrawEnvironment()
DrawPlayer()
DrawEnemy()
DrawBullets()
DrawScore()


ok(Posted 2013) [#4]
Thanks both of you, it works great!