Computersight > Programming > Blitz

Structures in Blitz

A tutorial on code structure in Blitz.

Organization, a programmer's nightmare,However, using good structure means that a code can be both easily reparable and easily reusable . Why reinvent the wheel?

In this article I am going to walk you through and article on how to structure blitz code. The game is called retro space shooter, and it won't work unless you create two of your own images. You need a white on black title that says “Retro Space Shooter”, and you need a start background that is 800x600. Let's begin.

First we start with the header, here I usually include five things.

Graphics 800,600,32,1
AutoMidHandle True 
SetBuffer BackBuffer()
SeedRnd MilliSecs()
AppTitle "Retro Space Shooter"

First I declare the Graphics Mode, then I set all image handles to the center, I set the drawing buffer to the BackBuffer(), I seed the random generator and I set the application title, this way, you never forget these very important things.

Next, I set up all the types, in this game, I did this.

Type ship
	Field x,y
End Type

Type ufo 
	Field x,y
End Type

Type bullet
	Field x,y
	Field who ;1 = shot by player, 2 = shot by ufo
End Type

This makes sure that I create all of the characters and objects that I need in the game, it also means that I can't create any of the variables before the type is created, very smart.

Then I do constants, this is good because it allows me to get everything that just needs to be declared written down, so I don't forget anything.

Const shipspeed = 10
Const shippic$ = "|/^|"
Const killscore = 1000
Const levelscore = 5000
Const shootdeduct = 25
Const ufopic$ = "-0-"
Const bulletspeed = 20
Const bulletpic$ = "*"

Now that those are set, I can declare all my Globals.

Global ufospeed = 7
Global count = 0
Global level = 1
Global numofufos = Rand(3,5) + level
Global timer = 0
Global timerspeed = 2
Global score = ReturnScore()
Global totalkills = 0
Global totalshots = 0
Global cont = 1
Global title = LoadImage("GraphicsRetrotitle.bmp")
Global font = LoadFont("blitz",18)
 
Global starfield = LoadImage("Graphicsstarfield.bmp")

Global scrollx = 0

Global s.ship = New ship
sx = 400
sy = 500

It is a good idea to declare your type variables immediately after the fact, so you don't forget them.

Next up the interface

SetFont font
DrawImage(title,400,300)
Text 400,500,"Press Any Key to continue",True  
Flip 
Delay 3000
WaitKey 
Cls
Text 400,100,"Welcome to Retro Space Shooter",True 
Text 400,200,"This is a simple space shooter game",True 
Text 400,300,"You will need to use the arrow keys to move left and right",True 
Text 400,400,"Spacebar fires your weapon",True 
Text 400,500,"Score is based on levels and kills, but too many shots will deduct",True
Flip  
Delay 3000
Cls 
Text 400,300,"Get Ready to Play",True
Flip  
Delay 3000

Declare your interface either into the loop or immediately before.

The loop should be clear and concise, use functions whenever possible, but don't over do it. If your loop is 50 lines, so be it. Try to clump related code into functions.

Cls 
LevelMake()
While cont = 1
Cls
TileImage(starfield,scrollx,300)
scrollx = scrollx + 5

UpdatePlayer()
AI()
If count = numofufos Then
count = 0
timerspeed = timerspeed + 1
numofufos = numofufos + 1
level = level + 1
LevelMake()
EndIf
score = ReturnScore()
Text 650,50,"Score: " + score

Flip
If KeyHit(1) Then
	cont = 0
EndIf 
Wend 

Place your credits right after.

Cls
DrawImage(title,400,150) 
Text 400,300,"Final Score: " + score,True 
Text 400,400,"Credits",True 
Text 400,500,"Tyler Chaney and Michael Cogswell",True 
Flip 
Delay 5000

Then write out your functions, try to do this in order of usage, it helps immensely.

Function LevelMake()
Cls 

For b.bullet = Each bullet
Delete b
Next 
For u.ufo = Each ufo
Delete u
Next  
For e = 1 To numofufos
u.ufo = New ufo
ux = Rand(51,749)
uy = Rand(50,300)
Next  
sx = 400
sy = 500 
Text 400,300,"Level " + level,True,False
Flip 
Delay 3000 
End Function

Function UpdatePlayer()

If KeyDown(205) Then
	sx = sx + shipspeed
Else If KeyDown(203) Then
	sx = sx - shipspeed
EndIf 

If KeyHit(57) Then
	b.bullet = New bullet
	bx = sx
	by = sy
	bwho = 1
	totalshots = totalshots + 1
EndIf 

For b.bullet = Each bullet
	by = by - bulletspeed
	Color 0,0,0
	Rect bx,by,StringWidth(bulletpic$),StringHeight(bulletpic$),0
	Color 255,255,255
	Text bx,by,bulletpic$
	
	For u.ufo = Each ufo
	If RectsOverlap(ux,uy,StringWidth(ufopic$),StringHeight(ufopic$),bx,by,StringWidth(bulletpic$),StringHeight(bulletpic$)) 
	
	count = count + 1
	totalkills = totalkills + 1
	
	Delete u
	EndIf 
	Next 
	
	If by <= 0 Then
		Delete b
	EndIf  
Next 
Color 0,0,0
Rect sx - .5 * StringWidth(shippic$),sy,StringWidth(shippic$),StringHeight(shippic$),0
Color 255,255,255
Text sx,sy,shippic$,True,False 
 
End Function 

Function AI()
For u.ufo = Each ufo

	ux = ux + ufospeed
	If ux >= 750 Then
		ufospeed = -7
	EndIf
	If ux <= 50 Then
		ufospeed = 7
	EndIf 
	If ux >= 800 Or ux <= 0 Then
		Cls 
		Text 400,300,"UFOs have run away. Moving to new battlefield.",True
		Flip  
		ux = Rand(51,749)
		Delay 3000 
	EndIf
	timer = timer + timerspeed 
	If timer >= 600 Then
		uy = uy + 10
		timer = 0
	EndIf  
   Color 0,0,0
	Rect ux,uy,StringWidth(ufopic$),StringHeight(ufopic$),False 
	Color 255,255,255
	Text ux,uy,ufopic$
	
	Next 
End Function 

Function Test()
Text 100,500,"shots" + totalshots
End Function 

Function ReturnScore()
Return  (((level * levelscore) + (totalkills * killscore)) - (totalshots * shootdeduct)) - 5000 
End Function 

Thanks for reading, hope it helps and happy coding.

1
Liked It
I Like It!
Related Articles
Arrays in Blitz  |  Loops in Blitz
Comments (0)
Post Your Comment:
Name:  
Copy the code into this box:  
Post comment with your Triond credentials?
Inside Computersight

Communication & Networks

 /

Computers

 /

Hardware

 /

Operating Systems

 /

Programming

 /

Software


Popular Tags
Popular Writers
Powered by
Computersight
About Us
Terms of Use
Privacy Policy
Services
Submit an Article
Advertise with Us
Contact

© 2007 Copyright Stanza Ltd. All Rights Reserved.