`2D DEATH STAR ATTACK 
`BY: SEGAN
 
Remstart
Well, in light of Mike S's article in the newletter, and this competition, I
have decided to make a 2D sidescroller of the deathstar attack. Lets hope I
get it done!
 
Notes: 
1: Not everything in here is mine. I took these from other people's code:
--> Check_Free_Image_Number() from Ric (I worded it differently though)
Remend
 
`Fun stuff to start off the program
Initialise_Screen()
Gosub _Globals
 
 
`--------------------------------------------
`Main Loop
`--------------------------------------------
Do
	`User Input
	If Leftkey() = 1 Then HVelocity = HVelocity - 0.1
	If Rightkey() = 1 Then HVelocity = HVelocity + 0.1
	If Upkey() = 1 Then HY = HY - 1.0
	If Downkey() = 1 Then HY = HY + 1.0
 
	`The calculations for all of this
	TileOffset = Tileoffset - HVelocity
	Repeat
		If TileOffset < -20.0 Then TileOffset = TileOffset + 20.0: FirstTile = FirstTile + 1
	until TileOffset => -20.0
 
	`Write the values for each tile in the array
	For tx = 1 To 33
		For ty = 1 TO 20
			Screen(tx,ty) = Map(tx+FirstTile,ty)
		next ty
	next tx
 
 
 
	`Draw everything on screen
	cls 0
	Sprite XWingSprite, HX, HY, XWingImage
	For tx = 1 To 33
		For ty = 1 To 20
			If Screen(tx,ty) = 1 
				Sprite TileSpriteNumber(tx,ty), ((tx-1)*20)+TileOffset, (ty-1)*20, TileImage
				Show Sprite TileSpriteNumber(tx,ty)
			Else
				Hide Sprite TileSPriteNumber(tx,ty)
			Endif
		next ty 
	Next tx
 
	Sync
 
 
loop
 
_Globals:
 
`Terrain Variables
Global TileImage As Integer
Dim TileSpriteNumber(33,20)
Dim Screen(33,20)
Global MapLength As Integer: Maplength = 1000
Dim Map(Maplength,20)
Dim TempMap(Maplength,20)
Global TileOffset As Float
Global FirstTile As Integer
 
PositionTiles()
 
`Human character variables
Global HVelocity as float
Global HX as float: HX = screen width()/3
Global HY as float: HY = screen height()/2
 
`X-Wing Variables
Global XWingImage As Integer
Global XWingSprite As Integer
 
`Now lets get all of our sprites 
Draw_Tiles()
Draw_XWing()
 
Return
 
Function Initialise_Screen()
	flush video memory
	sync on: sync rate 0
	color backdrop RGB(0,0,0)
	backdrop off
Endfunction
 
 
Function GetFreeImageNumber()
	`Scavenging through some past entries, I found this function (not worded the
	`same but still) in Ric's game. Good enough for a try.
	repeat
	inc image
	until image exist(image)=0
 
endfunction image
 
Function GetFreeSpriteNumber()
	repeat
		inc spritenumber
	until  Sprite Exist(spritenumber) = 0
endfunction spritenumber
 
Function Draw_Tiles()	
	cls 0
	`Draw a box
	box 0,0,20,20,RGB(100,100,100),RGB(125,125,125),RGB(100,100,100),RGB(125,125,125)
 
	`Add a bunch of gray dots (noise if you will)
	For dots = 1 To 400
		tx = RND(19)
		ty = RND(19)
		tcolor = RND(100)+50
		dot tx,ty, RGB(tcolor,tcolor,tcolor)		
	next dots
 
	`Assign all of the sprites to this
	TileImage = GetfreeImageNumber()
	Get Image TileImage, 0,0,20,20	
	For tx = 1 To 33
		For ty = 1 To 20
			TileSpriteNumber(tx,ty) = GetFreeSpriteNumber()
			Sprite TileSpriteNumber(tx,ty), (tx-1)*20, (ty-1)*20, TileImage
		next ty
	Next tx
endfunction
 
Function Draw_Xwing()
	cls 0
	Set Text Size 40
	Ink Rgb(0,0,255), 0	
	Text 0,0,"X"
	XWingImage = GetFreeImageNumber()
	XWingSprite = GetFreeSpriteNumber()
	Get Image XWingImage, 0,0,40,40
	Sprite XWingSprite, HX, HY, XWingImage
endfunction
 
Function PositionTiles()
	quarter = Maplength/4
 
	For times = 1 To 4 Step 3
		For tx = 1 To Maplength
			For ty = 1 To 20
				If maplength - quarter > tx
					If RND(15) = 1 THen Map(tx,ty) = 1
				Endif
				If ty = 20 Then Map(tx,ty) = 1
				If tx = Maplength Then Map(tx,ty) = 1
				next ty			
		next tx
 
 
 
		For tx = 2 TO (Maplength-quarter)
			For ty = 2 To 19
				If Map(tx-1,ty-1)+Map(tx-1,ty)+Map(tx-1,ty+1)+Map(tx,ty-1)+Map(tx,ty+1)+Map(tx+1,ty-1)+Map(tx+1,ty)+Map(tx+1,ty+1) > times
					TempMap(tx,ty) = 1
				Else
					TempMap(tx,ty) = 0
				Endif			
			next ty
		next tx
	 	For tx = 2 TO (Maplength-quarter)
			For ty = 2 To 19
				Map(tx,ty) = TempMap(tx,ty)
			next ty
		Next tx
 
	next times
 
endfunction