Code: Select all
type EngineType
MaxX as integer
MaxY as integer
ScreenWidth as integer
ScreenHeight as integer
Action as integer 'Action tracker
MaxNPCs as integer 'Maximum amount of NPCs
PlayerGFX as integer
end type
type MapType
tile as integer
Collision as integer
end type
type TankType
Sprites(1 to 4) as any ptr
Direction as integer
x as integer
y as integer
end type
declare sub DrawMap () 'Draws our map
declare sub MoveUp (byref Tank as TankType) 'These next subs will handle moving up, down, left, and right
declare sub MoveDown (byref Tank as TankType)
declare sub MoveLeft (byref Tank as TankType)
declare sub MoveRight (byref Tank as TankType)
declare sub LoadTiles (TileFile as string)
declare sub LoadMap (MapFile as string)
declare function CheckFileLines (File as string) as integer
declare function IsCollision(X1 as integer, Y1 as integer, X2 as integer, Y2 as integer) as integer
#define KeyLeft &h4B
#define KeyRight &h4D
#define KeyUp &h48
#define KeyDown &h50
#define KeyEsc &h01
#define CloseApp chr(255) + "k"
const South = 1, North = 2, West = 3, East = 4 'North, South, East, and West
const False = 0, True = NOT False 'True and False
const Wall = 1, Grass = 0
dim shared as EngineType Engine 'The game's engine data
dim shared as MapType Map(19, 19) 'The map!
dim shared as any ptr Tiles() 'The tileset array
dim shared as any ptr Explosion1, Explosion2, Explosion3
dim shared as TankType BlueTank
dim shared as TankType GreenTank
dim as integer xCount, yCount
screenres 640, 480, 32, 2 '512 by 384 at 32 bits
screenset 1, 0 'Sets screen 1to the working screen and screen 0 to display
screeninfo Engine.ScreenWidth, Engine.ScreenHeight 'Stores 512 and 384 into the Engine type
Engine.MaxX = 32 * 20 'Sets the max X value in our Engine type
Engine.MaxY = 32 * 20 'Sets the max Y value in our Engine type
LoadTiles("sprites/tiles.dat") 'Loads the tiles into the Tiles() array
LoadMap("easymap.map") 'Loads the 20 by 20 map into the Map array
do
if multikey(KeyUp) then
MoveUp(GreenTank)
Engine.Action = True
elseif multikey(KeyDown) then
MoveDown(GreenTank)
Engine.Action = True
elseif multikey(KeyLeft) then
MoveLeft(GreenTank)
Engine.Action = True
elseif multikey(KeyRight) then
MoveRight(GreenTank)
Engine.Action = True
end if
if Engine.Action = True then
DrawMap()
Engine.Action = False
end if
loop until inkey = chr(27)
'###############################################################
'S U B P R O G R A M S
'###############################################################
sub DrawMap()
dim as integer CameraX, CameraY, xTile, yTile
dim as integer yCount, xCount, Tile, xOffset, yOffset
cls
'GreenTank Half
CameraX = GreenTank.X - Engine.ScreenWidth \ 2
CameraY = GreenTank.Y - Engine.ScreenHeight \ 2
if CameraX < 0 then CameraX = 0
if CameraY < 0 then CameraY = 0
if CameraX > Engine.MaxX - Engine.ScreenWidth then CameraX = Engine.MaxX - Engine.ScreenWidth
if CameraY > Engine.MaxY - Engine.ScreenHeight then CameraY = Engine.MaxY - Engine.ScreenHeight
xTile = int(CameraX \ 32)
yTile = int(CameraY \ 32)
xOffset = CameraX mod 32
yOffset = CameraY mod 32
for xCount = 0 to Engine.ScreenWidth \ 32 - 1
for yCount = 0 to Engine.ScreenHeight \ 32
Tile = Map( xCount + xTile, yCount + ytile ).Tile
put (xCount * 32 - xOffset, yCount * 32 - yOffset), Tiles(Tile)
next
next
put (GreenTank.X - CameraX, GreenTank.Y - CameraY), GreenTank.Sprites(GreenTank.Direction), trans
sleep 4
screencopy 1, 0 'Copies the working page to the page that is displayed
end sub
sub LoadTiles(TileFile as string)
dim as integer FIleLines, i, FileNum
dim as string FileToLoad
FileLines = CheckFileLines(TileFile)
FileNum = FreeFile
redim Tiles(FileLines - 1) as any ptr
for i = 0 to FileLines - 1
Tiles(i) = ImageCreate(32, 32)
next
open TileFile for input as #FileNum
for i = 0 to FileLines - 1
input #FileNum, FileToLoad
bload "sprites/" & FileToLoad, Tiles(i)
next
close #FileNum
bload "explode1.bmp", Explosion1
bload "explode2.bmp", Explosion2
bload "explode3.bmp", Explosion3
with GreenTank
for i = 1 to 4
.Sprites(i) = ImageCreate(32, 32)
next
bload "sprites/greentankright.bmp", .Sprites(East)
bload "sprites/greentankleft.bmp", .Sprites(West)
bload "sprites/greentankup.bmp", .Sprites(North)
bload "sprites/greentankdown.bmp", .Sprites(South)
end with
with BlueTank
for i = 1 to 4
.Sprites(i) = ImageCreate(32, 32)
next
bload "sprites/bluetankright.bmp", .Sprites(East)
bload "sprites/bluetankleft.bmp", .Sprites(West)
bload "sprites/bluetankup.bmp", .Sprites(North)
bload "sprites/bluetankdown.bmp", .Sprites(South)
end with
end sub
sub LoadMap (MapFile as string)
dim as integer FileInteger, FileNum, yCount, xCount
FileNum = FreeFile
open MapFile for input as #FileNum
for yCount = 0 to 19
for xCount = 0 to 19
input #FileNum, FileInteger
Map(xCount, yCount).Tile = FileInteger
next
next
close #1
for yCount = 0 to 19
for xCount = 0 to 19
if Map(xCount, yCount).Tile = Wall then
Map(xCount, yCount).Collision = True
elseif Map(xCount, yCount).Tile = Grass then
Map(xCount, yCount).Collision = False
end if
next
next
end sub
function CheckFileLines(File as String) as integer
dim as integer FileNum, FileLines
dim as string GarbageString
FileNum = FreeFile
open File for input as #FileNum
do
input #1, GarbageString
FileLines = FileLines + 1
loop until eof(FileNum)
close #FileNum
return FileLines
end function
sub MoveUp(byref Tank as TankType)
dim as integer Collision, xTile, yTile
yTile = int(Tank.Y/ 32)
xTile = int(Tank.X / 32)
Tank.Direction = North
Collision = Map(xTile, YTile ).Collision
if Collision = False and Tank.Y - 1 > 0 then
if Map(int((Tank.X + 30) / 32), yTile).Collision = False then
Tank.Y = Tank.Y - 1
end if
end if
end sub
sub MoveDown(byref Tank as TankType)
dim as integer Collision, xTile, yTile
Tank.Direction = South
yTile = int(Tank.Y / 32)
xTile = int(Tank.X / 32)
Collision = Map(xTile, yTile + 1).Collision
if IsCollision(Tank.X, Tank.Y, xTile * 32, (yTile + 1) * 32) <> True and Collision <> True then
Tank.Y = Tank.Y + 1
end if
end sub
sub MoveLeft(byref Tank as TankType)
dim as integer Collision, xTile, yTile
yTile = int(Tank.Y/ 32)
xTile = int(Tank.X / 32)
Tank.Direction = West
Collision = Map(xTile, YTile ).Collision
if Collision = False and Tank.X - 1 > 0 then
Tank.X = Tank.X - 1
end if
end sub
sub MoveRight(byref Tank as TankType)
dim as integer Collision, xTile, yTile
yTile = int(Tank.Y/ 32)
xTile = int(Tank.X / 32)
Tank.Direction = East
Collision = Map(xTile, YTile ).Collision
if Collision = False and Tank.X + 1 < Engine.MaxX - 32 then
Tank.X = Tank.X + 1
end if
end sub
function IsCollision(X1 as integer, Y1 as integer, X2 as integer, Y2 as integer) as integer
IsCollision = -1
if (((X1 + 32) < X2) OR (X1 > (X2 + 32)) or _
((Y1 + 32) < Y2) OR (Y1 > (Y2 + 32))) then
IsCollision = 0
end if
end function