Turret game

Miscellaneous Forums/Blitz Showcase/Turret game

I think ill kick start my arrival at this forums with some free source code of a little turret thing I made not too long ago.

Note: Sorry this isnt very well commented, It started off as a test and I dont usually comment when suddenly feel like programming something
I also forgot to delete the particle types if they were dead so after a while it starts to get a little laggy :)

Update: I quickly added the delete type function for particles and bullets that are no longer needed.

;===========================
;==========================
;====Turret test ==========
;==By John Hannagan========
;====AKA Weetbix===========
;==========================
;===========================


Graphics3D 800,600,32
SetBuffer BackBuffer()

SeedRnd MilliSecs()

fntArialB=LoadFont("Arial",12,True,False,False) 
SetFont fntarialB

Global gravity# = .05
Global friction# = 0.98
Global spacebar = 57
Global arrow_right = 205
Global arrow_left = 203
Global arrow_up = 200
Global arrow_down = 208


Type bullet
	Field x#,y#
	Field xs#,ys#
	Field state
End Type

Type particle
	Field x#,y#
	Field xs#,ys#
	Field size#
End Type

Type cannon
	Field angle#
	Field xspeed#
	Field yspeed#
	Field power#
End Type


cannon.cannon = New cannon
cannon\angle = 0
cannon\power = 1



;;;;;;;MAAIN LOOP



While Not KeyHit(1)
	Cls
	updateparticles()
	drawparticles()
	updatecannon()
	drawcannon()
	checkkeys()
	updatebullet()
	drawbullet()
	drawhud()
	Flip True
Wend



; END MAIN LOOP



Function drawparticles()
	For particle.particle = Each particle 
	If particle\xs And particle\ys > 0.01
	Rect particle\x,particle\y-particle\size,particle\size,particle\size,0
	EndIf
	If particle\xs And particle\ys < 0.01 Then Delete particle
	Next
End Function

Function updateparticles()
	For particle.particle = Each particle
	If particle\xs And particle\ys > 0.009
	particle\x = particle\x + particle\xs
	particle\y = particle\y - particle\ys
	EndIf
	particle\xs = particle\xs*0.9
	particle\ys = particle\ys*0.9
	Next
End Function

Function createparticles(x,y,xs,ys,size)
	particle.particle = New particle
	particle\x = x
	particle\y = y
	particle\xs = xs
	particle\ys = ys
	particle\size = size
End Function

Function drawcannon()
Oval 30,480,20,20,0
Oval 35,485,10,10,0
;Rect 40,490,20,10,0
Line 60,490,50,490
Line 60,490,60,500
Line 60,494,64,494
Line 64,494,66,500
Line 40,495,40,500
Line 30,490,35,490
End Function

Function updatecannon()
	For cannon.cannon = Each cannon 
	cannon\xspeed#=Cos#(cannon\angle#)
	cannon\yspeed#=Sin#(cannon\angle#)
	Line 40,480,40+(cannon\xspeed#*20),480+(cannon\yspeed#*20)
	Next
End Function

Function drawbullet()
	For bullet1.bullet = Each bullet
	;If bullet1\state = 0
	Rect bullet1\x,bullet1\y-4,5,5,0
	;EndIf
	If bullet1\state = 1 Then Delete bullet1
	Next
	
	For i = 0 To 100
	Line 0,500+i,GraphicsWidth(),500+i
	i = i + 2 
	Next
End Function

Function updatebullet()
	For bullet1.bullet = Each bullet
	If bullet1\state = 0
	bullet1\ys = bullet1\ys + gravity 
	bullet1\x = bullet1\x + bullet1\xs
	bullet1\y = bullet1\y + bullet1\ys
	If bullet1\y > 500
		For i = 1 To 5
		createparticles(bullet1\x-((Rnd(5,10)+(bullet1\ys*bullet1\ys))/2),bullet1\y,Rnd(0.3,4)*(bullet1\ys/2),Rnd(0.3,4)*(bullet1\ys/2),Rnd(5,10)+(bullet1\ys*bullet1\ys))
		Next
		bullet1\state = 1
		bullet1\xs = bullet1\xs*friction#
		bullet1\y = 499
	EndIf
	EndIf
	Next
	
End Function

Function drawhud()
	For bullet1.bullet = Each bullet
	Next
	For cannon.cannon = Each cannon
	Rect 20,20,40,15,0
	Rect 59,20,100,15,0
	Rect 20,34,40,15,0
	Rect 59,34,100,15,0
	Text 23,21,"Angle:        "+cannon\angle
	Text 23,36,"Power:        "+cannon\power*100
	;Text 20,20,cannon\angle
	;Text 20,40,cannon\power*100
	Next
	
End Function

Function firebullet()

	For cannon.cannon = Each cannon
	bullet1.bullet = New bullet
	bullet1\state = 0
	bullet1\x = 40+(cannon\xspeed#*20)
	bullet1\y = 480+(cannon\yspeed#*20)
	;bullet1\xs = 5
	;bullet1\ys = -1
	bullet1\xs = (cannon\xspeed#*cannon\power)
	bullet1\ys = (cannon\yspeed#*cannon\power)
	For i = 1 To 10
	createparticles((40+(cannon\xspeed#*20)),(480+(cannon\yspeed#*20)),Rnd(-1,3),Rnd(0.3,3),Rand(1,5))
	Next
	Next
	
End Function

Function checkkeys()
	If Keyhit(57)
		firebullet()
	EndIf
	
	If KeyDown(arrow_right)
	For cannon.cannon = Each cannon
	If cannon\angle < 21 Then cannon\angle = cannon\angle + 0.5
	Next
	EndIf
	
	If KeyDown(arrow_left)
	For cannon.cannon = Each cannon
	If cannon\angle > -195 Then cannon\angle = cannon\angle - 0.5
	Next
	EndIf
	
	If KeyDown(arrow_up)
	For cannon.cannon = Each cannon
	If cannon\power <= 10 Then cannon\power = cannon\power + 0.01
	Next
	EndIf
	
	If KeyDown(arrow_down)
	For cannon.cannon = Each cannon
	If cannon\power > 1 Then cannon\power = cannon\power - 0.01
	Next
	EndIf
	
End Function


Thanks for sharing the code. Pretty clean code - probably it wasn't neccessary to label the main loop :)

Where my mainloops are true continents... I started using label names like .mainloop_____________________
(well only for the most important labels)

So I see them quickly in the labels list.

Thanks man , and what? I labled the main loop so its easy to see when i scroll up and down the code.

What it looks like your saying is that you use goto and lables to loop through your mainloop? (Sorry I dont really understand :P )

Uh, I was only trying to be nice :) But it's also true, my mainloops (I know WHILE btw) are huge. I often use Labels only to be able to click them in the labels list window. Imagine a big app initialisation part (types etc). In this case it's very useful.

For the mainloop thing I maybe should create a function named DoThings() and move the entire mainloop content to it... well I hate to declare globals, so as long as the machine doesn't explode, I leave it the way it is (it actually works!).

Uh, I was only trying to be nice :)


Sorry did I come over hostile ? But yeah I get what you mean now :) thanks .