TGC Codebase Backup



Shoot to Kill by Richard Davey

25th Sep 2003 19:46
Summary

I wanted to do a complete game that didn't use any external BMP files. Here is the end result! Use the cursor keys or joystick and blow away the on-coming alien hordes. Features ma



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `	-------------------------------------------------------------------------
`	Shoot to Kill                            DarkForge Mini-Game (18/10/2000)
`	-------------------------------------------------------------------------
`	I wanted to do a complete game that didn't use any external BMP files.
`	Here is the end result! Use the cursor keys or joystick and blow away the
`	on-coming alien hordes. Features math based collision, smooth controls, 
`	5 levels, score and great graphics in only 17k :-)

hide mouse
sync rate 0
sync on

`	- DF --------------------------------------------------------------------
`			The GenImage function re-generates our sprites from the data
`	-------------------------------------------------------------------- DF -

for a=1 to 5
	GenImage(a,a)
next a

`	- DF --------------------------------------------------------------------
`			Make a little star for use in the background
`	-------------------------------------------------------------------- DF -

cls 0 : ink rgb(200,200,200),0 : dot 1,1
ink rgb(50,50,250),0 : dot 1,0 : dot 1,2 : dot 0,1 : dot 2,1
get image 7,0,0,3,3 : cls 0

`	- DF --------------------------------------------------------------------
`			Draw a small game logo
`	-------------------------------------------------------------------- DF -

set text font "Terminal"
set text size 12
i=6
for a=50 to 255 step 50
	ink rgb(a,a,a),0
	text i,0,"SHOOT TO KILL !"
	dec i
next a
get image 8,0,0,80,16 : cls 0

set text font "Courier"
set text size 10

`	- DF --------------------------------------------------------------------
`			Set-up all the arrays and values used within the game
`	-------------------------------------------------------------------- DF -

life=4
stars=75
score=0
level=1
px=320
py=420
speed=8
bulletspeed=12
bullethalt=timer()
alienspeed#=4

dim stars(stars,3)

for i=1 to stars
	stars(i,1)=rnd(639)+1
	stars(i,2)=rnd(479)+1
	stars(i,3)=rnd(9)+1
next i

dim bullets(8,2)

for a=0 to 8

	bullets(a,1)=0
	bullets(a,2)=0

next a

dim aliens#(50,4)

for a=0 to level*10

	aliens#(a,1)=rnd(620)
	aliens#(a,2)=-700+rnd(400)
	aliens#(a,3)=rnd(1)
	aliens#(a,4)=1

next a

create bitmap 1,640,480

`	- DF Main Loop Starts Here ----------------------------------------------

do

	cls 0

`	- DF --------------------------------------------------------------------
`			Check to see if we've been hit by an alien
`	-------------------------------------------------------------------- DF -

	for alien=0 to level*10

		if aliens#(alien,4)=1

			alienx=int(aliens#(alien,1))+7
			alieny=int(aliens#(alien,2))+7
			shipx1=px-4
			shipx2=px+30
	
`			Uncomment this to show where the collision zone is on the ship
`			ink rgb(255,0,0),0
`			dot shipx1,py : dot shipx2,py : dot shipx1,py+22 : dot shipx2,py+22
	
			if alienx>=shipx1 and alienx<=shipx2

				if alieny>=py and alieny<=py+22
	
`					We've hit one so let's reset everything
	
					for a=0 to level*10
				
						aliens#(a,1)=rnd(620)
						aliens#(a,2)=-700+rnd(400)
						aliens#(a,3)=rnd(1)
						aliens#(a,4)=1
					
					next a
	
					for a=0 to 8
					
						bullets(a,1)=0
						bullets(a,2)=0
					
					next a
					
					dec life

					repeat

						set current bitmap 0
						paste image 5,(px-20)+rnd(40),py+rnd(30)
						sync
						inc ex

					until ex=40

					set current bitmap 1

					px=320
					py=420
					ex=0

					if life=0

						set text size 24
						set current bitmap 0

						ink rgb(100,100,100),0
						center text 320,240,"GAME OVER"

						ink rgb(200,200,200),0
						center text 320,239,"GAME OVER"

						ink rgb(255,255,255),0
						center text 320,238,"GAME OVER"

						wait key
						end

					endif
	
				endif

			endif

		endif

	next alien

`	- DF --------------------------------------------------------------------
`			Test for collision between bullet and aliens
`			First check the bullets, then aliens, then X and Y values
`	-------------------------------------------------------------------- DF -

	for bullet=0 to 8

		if bullets(bullet,2)!0

			for alien=0 to level*10
	
				if aliens#(alien,4)=1

					bullx=bullets(bullet,1)+2
					bully=bullets(bullet,2)+3
					alienx=int(aliens#(alien,1))+7
					alieny=int(aliens#(alien,2))+7
		
					if bullx>=alienx-10 and bullx<=alienx+10
		
						if bully>=alieny-8 and bully<=alieny+8
		
`							reset the offending bullet
							bullets(bullet,1)=0
							bullets(bullet,2)=0
	
`							explode the dead alien
							aliens#(alien,4)=2

							inc score,25

							if score=250 then inc level
							if score=900 then inc level : inc alienspeed#,1
							if score=1500 then inc level
							if score=3500 then inc alienspeed#,1
							if score=4000 then inc level
							if score>5000 then inc alienspeed#,0.1
		
						endif
	
					endif
	
				endif

			next alien

		endif

	next bullet

`	- DF --------------------------------------------------------------------
`			Draw the starfield
`	-------------------------------------------------------------------- DF -

	for i=1 to stars

		stars(i,2)=stars(i,2)+stars(i,3)
		
		if stars(i,2)>=480
			stars(i,2)=0
			stars(i,1)=rnd(640)+1
		endif

		paste image 7,stars(i,1),stars(i,2)
	
	next i

`	- DF --------------------------------------------------------------------
`			Draw your ship
`	-------------------------------------------------------------------- DF -

	paste image 1,px,py

`	- DF --------------------------------------------------------------------
`			Draw the aliens! Up to 50 of them depending on the level
`	-------------------------------------------------------------------- DF -

	for a=0 to level*10

		alienx#=aliens#(a,1)
		alieny#=aliens#(a,2)

		if aliens#(a,4)=1
			paste image 2,alienx#,alieny#
			inc alieny#,alienspeed#
			if alieny#>=480
				alieny#=-700+rnd(400)
				alienx#=rnd(620)
				aliens#(a,1)=alienx#
				aliens#(a,2)=alieny#
				aliens#(a,3)=rnd(1)
				aliens#(a,4)=1
			else
				aliens#(a,1)=alienx#
				aliens#(a,2)=alieny#
			endif
		else
			paste image 5,alienx#,alieny#
			dec alieny#,alienspeed#*2
			if alieny#<=1
				alieny#=-700+rnd(400)
				alienx#=rnd(620)
				aliens#(a,1)=alienx#
				aliens#(a,2)=alieny#
				aliens#(a,3)=rnd(1)
				aliens#(a,4)=1
			else
				aliens#(a,1)=alienx#
				aliens#(a,2)=alieny#
			endif
		endif


	next a

`	- DF --------------------------------------------------------------------
`			Check to see if there are any bullets on the move
`	-------------------------------------------------------------------- DF -

	for a=0 to 8
		if bullets(a,2)!0
			paste image 3,bullets(a,1),bullets(a,2)
		endif
	next a

`	- DF --------------------------------------------------------------------
`			Check the controls left or right
`	-------------------------------------------------------------------- DF -

	if leftkey() or joystick left() then direction=1
	if rightkey() or joystick right() then direction=2

	if direction=1
		dec px,speed
		if px<=0 then px=0
		direction=1
	endif

	if direction=2
		inc px,speed
		if px>=615 then px=615
		direction=2
	endif

	direction=0

`	- DF --------------------------------------------------------------------
`			Check the fire button with timer delay to stop all bullets
`			being launched in one shot!
`	-------------------------------------------------------------------- DF -

	if timer()>=bullethalt+150

		if spacekey() or joystick fire a()
	
	`		Find a free bullet if there is one
	
			nextbullet=99

			for a=0 to 8
	
				tempb=bullets(a,1)
				if tempb=0 then nextbullet=a
	
			next a

			if nextbullet!99
	
				bullets(nextbullet,1)=px+11
				bullets(nextbullet,2)=py

			endif
	
			bullethalt=timer()
	
		endif

	endif

`	- DF --------------------------------------------------------------------
`			Move any live bullets up the screen
`	-------------------------------------------------------------------- DF -

	for a=0 to 8

		if bullets(a,2)!0

			by=bullets(a,2)
			dec by,bulletspeed

			if by<=0
				bullets(a,1)=0
				bullets(a,2)=0
			endif

			bullets(a,2)=by

		endif

	next a

`	- DF --------------------------------------------------------------------
`			Draw the lives remaining counter
`	-------------------------------------------------------------------- DF -

	for a=1 to life
		paste image 4,18*a,450
	next a

`	- DF --------------------------------------------------------------------
`			Draw the little game logo
`	-------------------------------------------------------------------- DF -

	paste image 8,640-90,0

`	- DF --------------------------------------------------------------------
`			Draw the score
`	-------------------------------------------------------------------- DF -

	ink rgb(155,155,155),0
	text 550,449,"SCORE: "+str$(score)
	ink rgb(255,255,255),0
	text 550,448,"SCORE: "+str$(score)

`	- DF --------------------------------------------------------------------
`			Copy the bitmap and sync the screen - nice and fast
`	-------------------------------------------------------------------- DF -

	copy bitmap 1,0
	sync

loop


`	- DF Function --------------------------------------------------------
`	Converts the data statements into images/sprites
`	-------------------------------------------------------- DF Function -

function GenImage(n,i)

	cls 0

	if n=1 then restore ship1
	if n=2 then restore alien1
	if n=3 then restore bullet
	if n=4 then restore lifecounter
	if n=5 then restore explode

	read x
	read y

	for a=0 to y
		for b=0 to x
			read c
			ink c,0
			dot b,a
		next b
	next a

	get image i,0,0,x,y

`	- DF Data ------------------------------------------------------------
`	Sprite data starts here
`	------------------------------------------------------------ DF Data -

	ship1:
	data 26
	data 22
	data 0,0,0,0,0,0,0,0,0,0,0,0,2113634,2113634,0,0,0,0,0,0,0,0,0,0,0,0,0
	data 0,0,0,0,0,0,0,0,0,0,0,2113634,10798822,8627653,2113634,0,0,0,0,0,0,0,0,0,0,0,0
	data 0,0,0,0,0,0,0,0,0,0,0,8627653,10798822,8627653,6456740,0,0,0,0,0,0,0,0,0,0,0,0
	data 0,0,0,0,0,0,0,0,0,0,2113634,8627653,10798822,8627653,6456740,2113634,0,0,0,0,0,0,0,0,0,0,0
	data 0,0,0,0,0,0,0,0,0,0,6456740,8627653,10798822,8627653,24832,24832,0,0,0,0,0,0,0,0,0,0,0
	data 0,0,0,0,0,0,0,0,0,0,6456740,8627653,58880,42240,24832,24832,0,0,0,0,0,0,0,0,0,0,0
	data 0,0,0,0,0,0,0,0,0,0,24832,42240,58880,42240,24832,24832,0,0,0,0,0,0,0,0,0,0,0
	data 0,0,0,0,0,0,0,0,0,0,24832,42240,58880,42240,6456740,4284803,0,0,0,0,0,0,0,0,0,0,0
	data 0,0,0,0,0,0,0,0,0,0,24832,42240,10798822,8627653,6456740,4284803,0,0,0,0,0,0,0,0,0,0,0
	data 0,0,0,0,0,0,0,0,0,12969702,8627653,8627653,10798822,8627653,6456740,4284803,2113634,0,0,0,0,0,0,0,0,0,0
	data 0,0,0,0,0,0,0,6456740,12969702,10798822,2113634,2113634,2113634,0,0,0,2113634,2113634,0,0,0,0,0,0,0,0,0
	data 0,0,0,0,0,2113634,6456740,10798822,10798822,2113634,2113634,50918,50918,34278,34278,0,2113634,2113634,2113634,2113634,0,0,0,0,0,0,0
	data 0,0,0,0,2113634,12969702,10798822,10798822,6456740,2113634,50918,50918,34278,34278,34278,16614,0,2113634,2113634,4284803,15132160,2113634,0,0,0,0,0
	data 0,0,0,4284803,15132160,15132160,10798822,10798822,6456740,2113634,34278,34278,12969702,10798822,16614,16614,0,2113634,4284803,4284803,15132160,15132160,2113634,0,0,0,0
	data 0,0,4284803,10798822,15132160,15132160,10798822,10798822,6456740,2113634,34278,16614,12969702,8627653,16614,16614,0,2113634,4284803,4284803,15132160,15132160,6456740,2113634,0,0,0
	data 0,4284803,8627653,10798822,15132160,15115520,10798822,10798822,6456740,2113634,16614,2113634,10798822,8627653,0,16614,0,2113634,4284803,6456740,15132160,15115520,6456740,6456740,2113634,0,0
	data 0,10798822,10798822,10798822,15115520,15115520,10798822,10798822,6456740,2113634,2113634,2113634,10798822,8627653,0,0,0,2113634,4284803,6456740,15115520,15115520,6456740,6456740,6456740,0,0
	data 4284803,10798822,10798822,10798822,15115520,15081472,10798822,10798822,6456740,4284803,8627653,6456740,10798822,8627653,6456740,4284803,2113634,2113634,4284803,6456740,15115520,15081472,6456740,6456740,6456740,2113634,0
	data 10798822,10798822,10798822,10798822,15081472,15081472,4284803,0,0,0,0,4284803,4284803,2113634,2113634,0,0,0,0,2113634,15081472,15081472,6456740,6456740,6456740,6456740,0
	data 6456740,10798822,10798822,4284803,0,0,0,0,0,0,2113634,15081472,15132160,15132160,15081472,2113634,0,0,0,0,0,0,2113634,6456740,6456740,4284803,0
	data 0,0,0,0,0,0,0,0,0,0,0,15081472,15115520,15115520,15081472,0,0,0,0,0,0,0,0,0,0,0,0
	data 0,0,0,0,0,0,0,0,0,0,0,0,15081472,15081472,0,0,0,0,0,0,0,0,0,0,0,0,0
	
	alien1:
	data 14
	data 15
	data 0,4284803,2113634,2113634,0,0,0,0,0,0,2113634,2113634,2113634,0,0
	data 4284803,6456740,4284803,4284803,2113634,2113634,0,0,2113634,2113634,2113634,2113634,4284803,4284803,0
	data 4284803,10798822,6456740,4284803,2113634,2113634,2113634,2113634,2113634,2113634,2113634,4284803,6456740,4284803,0
	data 4284803,12969702,10798822,4284803,4284803,2113634,4284803,4284803,2113634,2113634,2113634,6456740,8627653,4284803,0
	data 2113634,4284803,12969702,4284803,6456740,2113634,6456740,6456740,2113634,4284803,2113634,8627653,4284803,2113634,0
	data 0,4284803,12969702,4284803,6456740,2113634,2113634,2113634,2113634,6456740,2113634,8627653,4284803,0,0
	data 0,2113634,4284803,4284803,8627653,2113634,15081472,15081472,2113634,8627653,2113634,4284803,2113634,0,0
	data 0,0,4284803,4284803,10798822,2113634,15115520,15081472,2113634,8627653,2113634,4284803,0,0,0
	data 0,0,2113634,4284803,12969702,2113634,15115520,15115520,2113634,10798822,4284803,2113634,0,0,0
	data 0,0,0,4284803,12969702,2113634,15132160,15115520,2113634,10798822,4284803,0,0,0,0
	data 0,0,0,2113634,4284803,2113634,2113634,2113634,2113634,4284803,2113634,0,0,0,0
	data 0,0,0,0,4284803,8627653,6456740,4284803,4284803,4284803,0,0,0,0,0
	data 0,0,0,0,2113634,4284803,8627653,6456740,4284803,2113634,0,0,0,0,0
	data 0,0,0,0,0,4284803,12969702,8627653,4284803,0,0,0,0,0,0
	data 0,0,0,0,0,0,4284803,4284803,0,0,0,0,0,0,0
	
	bullet:
	data 4
	data 6
	data 0,10798822,6456740,0,0
	data 6456740,10798822,6456740,4284803,0
	data 6456740,10798822,6456740,4284803,0
	data 34278,50918,16614,16614,0
	data 15115520,15132160,15081472,15081472,0
	data 6456740,10798822,8627653,6456740,0
	
	lifecounter:
	data 15
	data 13
	data 0,0,0,0,0,0,0,4284803,0,0,0,0,0,0,0,0
	data 0,0,0,0,0,0,4284803,10798822,4284803,0,0,0,0,0,0,0
	data 0,0,0,0,0,0,6456740,10798822,4284803,0,0,0,0,0,0,0
	data 0,0,0,0,0,0,6456740,58880,24832,0,0,0,0,0,0,0
	data 0,0,0,0,0,4284803,42240,58880,6456740,2113634,0,0,0,0,0,0
	data 0,0,0,0,4284803,8627653,6456740,10798822,6456740,4284803,0,0,0,0,0,0
	data 0,0,0,2113634,10798822,10798822,4284803,4284803,4284803,6456740,4284803,2113634,0,0,0,0
	data 0,0,2113634,15132160,10798822,4284803,50918,34278,34278,2113634,6456740,15132160,0,0,0,0
	data 0,2113634,10798822,15115520,10798822,4284803,34278,12969702,16614,2113634,6456740,15115520,6456740,2113634,0,0
	data 2113634,10798822,10798822,15115520,10798822,4284803,16614,10798822,16614,2113634,6456740,15115520,8627653,8627653,0,0
	data 6456740,10798822,10798822,15081472,10798822,4284803,4284803,6456740,4284803,4284803,6456740,15081472,8627653,8627653,4284803,0
	data 6456740,6456740,0,2113634,4284803,0,15115520,15132160,15115520,0,4284803,0,0,4284803,2113634,0
	data 0,0,0,0,0,0,0,15081472,0,0,0,0,0,0,0,0

	explode:
	data 13
	data 13
	data 0,0,0,0,0,0,0,0,0,0,0,15081472,15115520,0
	data 0,15115520,15081472,0,0,0,0,0,0,15081472,15115520,15132160,15081472,0
	data 0,15081472,15132160,15115520,0,15081472,15081472,15081472,15115520,15132160,15132160,15115520,0,0
	data 0,0,15115520,15132160,15132160,15115520,15132160,15132160,15132160,15132160,15132160,15081472,0,0
	data 0,0,0,15132160,15132160,15132160,15132160,15132160,15132160,15132160,15115520,0,0,0
	data 0,0,15081472,15115520,15132160,15132160,12969702,15132160,15132160,15132160,15081472,0,0,0
	data 0,0,15081472,15132160,15132160,12969702,12969702,12969702,15132160,15132160,15081472,0,0,0
	data 0,0,15081472,15115520,15132160,15132160,12969702,15132160,15132160,15115520,15081472,0,0,0
	data 0,0,15115520,15132160,15132160,15132160,15132160,15132160,15132160,15132160,0,0,0,0
	data 0,15081472,15132160,15132160,15132160,15115520,15132160,15115520,15132160,15132160,15115520,0,0,0
	data 0,15115520,15132160,15132160,15115520,15081472,15081472,15081472,0,15115520,15132160,15081472,0,0
	data 15081472,15132160,15115520,15081472,15081472,15081472,0,0,0,0,15081472,15115520,0,0
	data 15115520,15081472,0,0,0,0,0,0,0,0,0,0,0,0

endfunction