Gun shooting?

Blitz3D Forums/Blitz3D Beginners Area/Gun shooting?

po(Posted 2003) [#1]
I am making a very simple version of commander keen, kind of, and I am having trouble making a laser shoot out of the gun(2D game). I have an image as the laser, and the gun is just part of the image of the character. I don't know how to make the laser somehow follow the character and when you hit ctrl for a laser too shoot out of it.

I only have this:
If KeyDown(29) :DrawImage laser,laser_x,laser_y:laser_x=laser_x+1*laser_speed:EndIf


Oh, and laser_x=player_x+35 and laser_y=player_y+24

-Thanks


Brandon(Posted 2003) [#2]
You can have a variable that is true if the laser is fired and otherwise false. Then in your main loop add a function that handles the laser if the variable is true. Like this:

LaserFired = false
LaserDist = 0
Const MAXDIST = 100
Const laser_speed = 5

While keydown(1) = false

If KeyDown(29) and LaserFired=false then
laser_x=player_x+35
laser_y=player_y+24
LaserFired = true
End If

UpdateLaser()
Flip

Wend


Function UpdateLaser()

If LaserFired = true then

If LaserDist >= MAXDIST then
LaserFired = false
LaserDist = 0
EndIf

laser_x=laser_x+1*laser_speed
LaserDist = LaserDist + 1
DrawImage laser,laser_x,laser_y

EndIf

End Function


Sorry for the sloppyness, other than that, I hope that helps, Cya. =)


injeevious(Posted 2003) [#3]
Sorry this has nothing to do with the question, but if a commander keen game is made like you said, and you don't make money off it, is it legal to distribute?


po(Posted 2003) [#4]
Hmmmmm... I don't know anything about copyright stuff so I wouldn't know. But the only resemblance between my game and Cammander Keen is the view.


po(Posted 2004) [#5]
A little late but... Epyon, the code you gave doesn't seem to work. When I press ctrl, the laser just stays right by the player and does nothing. I have tried fixing it somehow, but nothing really works.


cermit(Posted 2004) [#6]
Laser types whould be okey to use here
if you dont want only one laser exist at
the time..



; Title

AppTitle "Commander Keen Laser"



; Graphics Mode

Graphics 640, 480, 16, 2

SetBuffer BackBuffer()



; Player

player_x = 310
player_y = 230
player_dir = 1 ; 1 = Left , 2 = Right


; Laser Type

Type laser

Field l.laser
Field laser_x, laser_y
Field laser_spd

End Type




; Program Loop

While KeyHit(1) = False



; Fire Laser

If MilliSecs() > time + 220 Then

If KeyDown(29) Then Firelaser( player_x + 5, player_y + 10, player_dir )

time = MilliSecs()

EndIf


; Move Player

If Not KeyDown(29) Then

If KeyDown(203) Then player_x = player_x - 1 : player_dir = 1

If KeyDown(205) Then player_x = player_x + 1 : player_dir = 2

EndIf


; Clear Screen

Cls



; Draw Player

Color 0, 255, 0

Oval player_x, player_y, 20, 20, 1



; Paint Laser :)

For l.laser = Each laser

Next


; Update Laser

Updatelaser()



; Flip Buffer

Flip





; End Program

Wend


End



; Fire Laser

Function Firelaser( ls_x, ls_y, dir )


; Create Laser

l.laser = New laser

l\laser_x = ls_x
l\laser_y = ls_y


; Assign Laser Direction

If dir = 1 Then

l\laser_spd = -2

Else

l\laser_spd = 2

EndIf


End Function



; Update Laser

Function Updatelaser()

For l.laser = Each laser


; Move Laser

l\laser_x = l\laser_x + l\laser_spd


; Paint Laser

Color 255, 0, 0

Line l\laser_x, l\laser_y, l\laser_x + 16, l\laser_y

Line l\laser_x, l\laser_y + 2, l\laser_x + 16, l\laser_y + 2

Color 255, 255, 255

Line l\laser_x, l\laser_y + 1, l\laser_x + 16, l\laser_y + 1

Next

End Function




Hope its what you looking for :]


CodeD(Posted 2004) [#7]
Yes, indeed my friend. Welcome to the world of types. Back in the days of dos I feared types, because I did not understand them. So, it's all coming back to bite me in the but now darmnit. That's why I'm waiting on Krylar's book to come in the mail. You can buy just the book by itself from blitzcoder.com and you get 3 free games with it too from kool dog. check it out.


po(Posted 2004) [#8]
Cermit64: Yes! Thanks! It's very cool. :)
CodeD: Is it one of those big fat books on programming you find in book stores? :)


cermit(Posted 2004) [#9]
The types of the dos days must have been
horrible to use, i dont know much about
it though.

The book and three free games for $27,
that does not sound very bad although
i already got my book of "The Way Of Blitz" :]


CodeD(Posted 2004) [#10]
It's about a 300 page book, and I don't know if you can get it in book stores, though. Where did you get 'the way of blitz'?


cermit(Posted 2004) [#11]
I meant the B3D book, would be cool with a "The way of Blitz" too :) to bad there isnt any.


Hansie(Posted 2004) [#12]
Idigicon is the official distributor of the book mentioned above - which is written by Krylar, former Webmaster of www.blitzcoder.com. He is currently writing a sequal to the first book due to be released later this year including a full game, if I have understood the forums on BlitzCoder correctly.


Gauge(Posted 2004) [#13]
Heres a little test of me playing around with a quicke 3d shooter, I want to work on the server code for something like this, and not much more complicated to start. Well take a look if ya like, here ya go: 3dRocks, 2d is for kids!

Graphics3D 400,300,16,1 ;change to 1 for faster
SetBuffer BackBuffer()
;Set Collision Types as Constants
Const Type_player=1
Const Type_wall=2

light=CreateLight()
RotateEntity light,90,0,0

;Create Base Bullet and resize
bullx=CreateSphere()
ScaleEntity bullx,.5,.5,.5
HideEntity bullx

;create player fighter
model=CreateCube()
EntityType model,1
EntityRadius model,1.25
PositionEntity model,40,1,5

cam=CreateCamera()
PositionEntity cam,40,3,1

;attach the camera pivot
pivot=CreatePivot()
EntityParent pivot,model
EntityParent cam,model

;create a generic checkboard texture used on walls and floor both=need some color
tex=CreateTexture( 256,256 )
;set the texturebuffer to draw on such texture
SetBuffer TextureBuffer( tex ) 
Color 255,0,0
For x=0 To 256 Step 32
For y=0 To 256 Step 32
Rect x,y,16,16
Next
Next
;reset buffer
SetBuffer BackBuffer()
Cls
;create the floor
Arena=CreateMesh()
Afloor=CreateSurface(Arena)
v1=AddVertex (afloor,0,0,100,0,1)
v2=AddVertex (afloor,100,0,100,1,1)
v3=AddVertex (afloor,100,0,0,1,0)
v4=AddVertex (afloor,0,0,0,0,0)
tri=AddTriangle(afloor,v1,v2,v3)
tri2=AddTriangle(afloor,v1,v3,v4)
EntityTexture  arena,tex
EntityRadius arena,1
EntityType arena,Type_floor
;and the four sequential walls
nwall=CreateMesh()
nFloor=CreateSurface(nwall)
v1=AddVertex (nFloor,0,10,0,0,1)
v2=AddVertex (nFloor,0,10,100,1,1)
v3=AddVertex (nFloor,0,0,100,1,0)
v4=AddVertex (nFloor,0,0,0,0,0)
tri=AddTriangle(nFloor,v1,v2,v3)
tri2=AddTriangle(nFloor,v1,v3,v4)
afloor=LoadTexture("c:\blitz\checker2.bmp")
EntityTexture nwall,tex
EntityRadius nwall,1
EntityType nwall,2
UpdateNormals(nwall)

swall=CreateMesh()
sFloor=CreateSurface(swall)
v1=AddVertex (sfloor,100,10,100,0,1)
v2=AddVertex (sfloor,100,10,0,1,1)
v3=AddVertex (sfloor,100,0,0,1,0)
v4=AddVertex (sfloor,100,0,100,0,0)
tri=AddTriangle(sfloor,v1,v2,v3)
tri2=AddTriangle(sfloor,v1,v3,v4)
EntityTexture swall,tex
EntityRadius  swall,1
EntityType swall,2
UpdateNormals(swall)

wwall=CreateMesh()
wfloor=CreateSurface(wwall)
v1=AddVertex (wfloor,0,10,100,0,1)
v2=AddVertex (wfloor,100,10,100,1,1)
v3=AddVertex (wfloor,100,0,100,1,0)
v4=AddVertex (wfloor,0,0,100,0,0)
tri=AddTriangle(wfloor,v1,v2,v3)
tri2=AddTriangle(wfloor,v1,v3,v4)
EntityTexture wwall,tex
EntityRadius wwall,1
EntityType wwall,2
UpdateNormals(wwall)

ewall=CreateMesh()
efloor=CreateSurface(ewall)
v1=AddVertex (efloor,100,10,0,0,1)
v2=AddVertex (efloor,0,10,0,1,1)
v3=AddVertex (efloor,0,0,0,1,0)
v4=AddVertex (efloor,100,0,0,0,0)
tri=AddTriangle(efloor,v1,v2,v3)
tri2=AddTriangle(efloor,v1,v3,v4)
EntityTexture ewall,tex
EntityRadius ewall,1
EntityType ewall,2
;update the rest of the normals for light,etc
UpdateNormals(ewall)
UpdateNormals(arena)
UpdateNormals(model)

;enable collisions between player and wall
;note since the player model is moving, if you did collisions 2,1,2,2 it wouldn't work

Collisions 1,2,2,2

While Not KeyHit(1)
;Check NewFire
If KeyDown(57)=True Then ;change If keydown(57) to If keyhit(57) for noncontinous firing

;if fired Create a new bullet and position it at players location
b.bullet=New bullet
b\model=CopyEntity(bullx)
ax#=EntityX(model)
ay#=EntityY(model)
az#=EntityZ(model)
PositionEntity  b\model,ax#,ay#,az# 
TurnEntity b\model,0,rot#,0
MoveEntity b\model,0,0,3
b\dur=50
;after dur is over, bullet disappears
EndIf

;Read KeyCommands
If KeyDown( 205 )=True Then 
rot#=rot#-5
TurnEntity model,0,-5,0 ;next line stores rotation of player model
If rot#<0 Then rot#=Rot#+360
EndIf
If KeyDown( 203 )=True Then
rot#=rot#+5
TurnEntity model,0,5,0
If rot#>360 Then rot#=rot#-360
EndIf
If KeyDown( 208 )=True Then MoveEntity model,0,0,-1
If KeyDown( 200 )=True Then MoveEntity model,0,0,1
;Update Each Bullet
For b.bullet=Each bullet
MoveEntity b\model,0,0,3
b\dur=b\dur-1
If b\dur<0 Then
FreeEntity b\model
Delete b
EndIf
Next
;check collisions
UpdateWorld
RenderWorld
;I never Did Add life!
Text 10,10,"Life: "+life
Flip
Wend

ClearWorld()
End

Type bullet
Field model
Field dur
End Type