This is a game that I was working on a while ago but haven't really had the motivation to put the final touches to it. Its fun watching the FPS drop as the amount of spiders increases
. To stay alive longer move in reverse facing your enemy to shoot.
Sync On
Hide Mouse
Set display mode 1280,1024,32
``````Load Media`````````````
Load Image "images/beetle1.bmp",1
Load Image "images/enemy1.bmp",2
Load Image "images/Bullet1.bmp",3
Load Image "images/Rock.bmp",4
Load Image "images/grass3.bmp",5
Load Image "images/Blood.bmp",7
Type Vec
X as Integer
Y as Integer
EndType
`Our ViewPort
Global VPPos as Vec
`````````Define the Objects```````````
Type Player
X as Float
Y as Float
Health as Integer
Angle as Float
Speed as Float
Radius as Float
SpriteNum as Integer
BulletNum as Integer
TotalBullets as Integer
ShootTime as Float
EndType
Type Spider
X as Float
Y as Float
Angle as Float
Speed as Float
Radius as Float
Active as Boolean
SpriteNum as Integer
EndType
Type Bullet
X as Float
Y as Float
Angle as Float
Speed as Float
Radius as Float
LifeTime as Float
SpriteNum as Integer
EndType
Type Rock
X as Float
Y as Float
Radius as Float
SpriteNum as Integer
EndType
Type Effect
X as Float
Y as Float
Category as String
CreationTime as Float
DestructionTime as Float
SpriteNum as Integer
EndType
Type Destructor
Object as Integer
SpriteNum as Integer
EndType
`Enums for Our Destructor Function
#Constant SpiderType 1
#Constant BulletType 2
#Constant EffectType 3
```````Game Object Arrays`````````
Dim Spiders(0) as Spider
Dim Bullets(0) as Bullet
Dim Rocks(0) as Rock
Dim Effect(0) as Effect
```````GameManager Arrays`````````
Dim SpriteNumber(0) as Integer
Dim Destructor(0) as Destructor
For aRock=1 to 10
CreateRock()
Next aRock
CreatePlayer()
`Create A Timer
Global StartTime as Integer :StartTime =Timer()
Global BaseTime as Float : BaseTime =Timer()
Global Update=0
Global WaveTime as Float
`___________________________________________________________________________________________________________
` MAIN LOOP
`___________________________________________________________________________________________________________
Do
`Update Our Timer
If Timer()=>BaseTime+10 then BaseTime=Timer() : Update=1
`Update These Functions Continuously
DeleteObjects()
Render()
`Update These Functions Every 100th of a secound
If Update=1
PlayerAction()
SpiderAction()
BulletAction()
EffectAction()
Update=0
Endif
Ink RGB(255,255,255),1 : Set Cursor 0,0 : Print "FPS:", Screen FPS()
Print "Health:", Player.Health
Print "Spiders:", Array Count(Spiders(0))
Print "Playing Time Sec:", (Timer()-StartTime)/1000
Sync
Cls
Loop
`___________________________________________________________________________________________________________
` PLAYER ACTION
`___________________________________________________________________________________________________________
Function PlayerAction()
`Stop Our Player from continusly Moving
Player.Speed=0
`Player Controls
If Upkey() =1 then Player.Speed=1.2
If Downkey()=1 then Player.Speed=-1.2
If Leftkey() =1 then Player.Angle=Wrapvalue(Player.Angle-2.4)
If Rightkey()=1 then Player.Angle=Wrapvalue(Player.Angle+2.4)
`Fire Player's Gun and reset The time for Next Fire
If Controlkey()=1 and Timer()>(Player.ShootTime+1000) : Player.ShootTime=timer()
`Create a Bullet then set it a direction
CreateBullet()
Bullets(Array Count(Bullets(0))).Angle = Player.Angle
Bullets(Array Count(Bullets(0))).X = Player.X
Bullets(Array Count(Bullets(0))).Y = Player.Y
Bullets(Array Count(Bullets(0))).LifeTime = Timer()
EndIf
Local MoveX as Float
Local MoveY as Float
`Calculate where to move sprite based on rotation angle and distance to move
MoveX = Cos(Player.Angle)*Player.Speed
MoveY = Sin(Player.Angle)*Player.Speed
`Move the player in the Calculated Direction
Player.x = Player.x+MoveX
Player.y = Player.y+MoveY
`Check to make sure Player is still alive
If Player.Health <=0
Player.Health=0
Do
cls
Center Text Screen Width()/2,Screen Height()/2 ,"YOU DIED - Press Escape to exit"
sync
Loop
end
endif
`Player Collide with Spiders
For aSpider=1 to Array Count(Spiders(0))
Collision(Player.X,Player.Y,Player.Radius,Spiders(aSpider).X,Spiders(aSpider).Y,Spiders(aSpider).Radius)
Player.X = ReturnValueX
Player.Y = ReturnValueY
If ReturnValueCollided = 1 then Player.Health = Player.Health-1
Next aSpider
`Player Collide with Rocks
For aRock=1 to Array Count(Rocks(0))
Collision(Player.X,Player.Y,Player.Radius,Rocks(aRock).X,Rocks(aRock).Y,Rocks(aRock).Radius)
Player.X = ReturnValueX
Player.Y = ReturnValueY
Next aRock
EndFunction
`___________________________________________________________________________________________________________
` SPIDER ACTION
`___________________________________________________________________________________________________________
Function SpiderAction()
local MoveX as Float
local MoveY as Float
local DistX as Float
local DistY as Float
For aSpider=1 to Array Count(Spiders(0))
`Calculate the X and Y distance between the Spiders and Player
DistX=Player.x-Spiders(aSpider).x
DistY=Player.y-Spiders(aSpider).y
`Calculate the angle the Spider has to face to point to Player
Spiders(aSpider).Angle=(ATANFULL(DistY,DistX))
`Calculate the distance to move the Spider
MoveX = Cos(Spiders(aSpider).Angle) * Spiders(aSpider).Speed
MoveY = Sin(Spiders(aSpider).Angle) * Spiders(aSpider).Speed
`Move the Spider towards Player
Spiders(aSpider).x = Spiders(aSpider).x +MoveX
Spiders(aSpider).y = Spiders(aSpider).y + MoveY
`Circle Spiders(aSpider).x,Spiders(aSpider).y,Spiders(aSpider).Radius
Next aSpider
Local SpiderWave as Integer : SpiderWave=20
`Create a New Wave of Spiders Every 30 secounds
If Timer()=> WaveTime+20000
For aSpider=1 to SpiderWave
CreateSpider()
Next aSpider
WaveTime=Timer()
EndIf
`Spiders Collision with Player
For aSpider=1 to Array Count(Spiders(0))
Collision(Spiders(aSpider).X,Spiders(aSpider).Y,Spiders(aSpider).Radius,Player.X,Player.Y,Player.Radius)
Spiders(aSpider).x = ReturnValueX
Spiders(aSpider).y = ReturnValueY
Next aSpider
`Spiders Collision with Spiders
For CurrentSpider=1 to Array Count(Spiders(0))
For aSpider=1 to Array Count(Spiders(0))
If CurrentSpider<>aSpider
Collision(Spiders(CurrentSpider).X,Spiders(CurrentSpider).Y,Spiders(CurrentSpider).Radius,Spiders(aSpider).X,Spiders(aSpider).Y,Spiders(aSpider).Radius)
Spiders(CurrentSpider).x = ReturnValueX
Spiders(CurrentSpider).y = ReturnValueY
EndIf
Next aSpider
Next CurrentSpider
`Spider Collision with Rocks
For aSpider=1 to Array Count(Spiders(0))
For aRock=1 to Array Count(Rocks(0))
Collision(Spiders(aSpider).X,Spiders(aSpider).Y,Spiders(aSpider).Radius,Rocks(aRock).X,Rocks(aRock).Y,Rocks(aRock).Radius)
Spiders(aSpider).X = ReturnValueX
Spiders(aSpider).Y = ReturnValueY
Next aRock
Next aSpider
EndFunction
`___________________________________________________________________________________________________________
` BULLET ACTION
`___________________________________________________________________________________________________________
Function BulletAction()
local MoveX as Float
local MoveY as Float
For aBullet=1 to Array Count(Bullets(0))
`Calculate the Current Bullets Direction
MoveX = Cos(Bullets(aBullet).Angle) * Bullets(aBullet).Speed
MoveY = Sin(Bullets(aBullet).Angle) * Bullets(aBullet).Speed
`Move the bullet in the calculated direction
Bullets(aBullet).x = Bullets(aBullet).x+MoveX
Bullets(aBullet).y = Bullets(aBullet).y+MoveY
`Kill the Bullets if their LifeTime has exceeded 15 secounds
If Timer()>(Bullets(aBullet).LifeTime+15000) then AddtoDeletionList(BulletType , Bullets(aBullet).SpriteNum)
`Bullet Collision With Spiders
For aSpider=1 to Array Count(Spiders(0))
Collision(Bullets(aBullet).X,Bullets(aBullet).Y,Bullets(aBullet).Radius,Spiders(aSpider).X,Spiders(aSpider).Y,Spiders(aSpider).Radius)
If ReturnValueCollided = 1
CreateEffect(Spiders(aSpider).X,Spiders(aSpider).Y, "Blood")
AddtoDeletionList(BulletType , Bullets(aBullet).SpriteNum)
AddtoDeletionList(SpiderType , Spiders(aSpider).SpriteNum)
EndIf
Next aSpider
`Bullet Collision With Rocks
For aRock=1 to Array Count(Rocks(0))
Collision(Bullets(aBullet).X,Bullets(aBullet).Y,Bullets(aBullet).Radius,Rocks(aRock).X,Rocks(aRock).Y,Rocks(aRock).Radius)
If ReturnValueCollided = 1 then AddtoDeletionList(BulletType, Bullets(aBullet).SpriteNum)
Next aRock
Next aBullet
EndFunction
Function EffectAction()
For aEffect=1 to Array Count(Effect(0))
If Effect(aEffect).Category="Blood"
If Timer()=>Effect(aEffect).CreationTime+((Effect(aEffect).DestructionTime/5)*1)
Set Sprite Alpha Effect(aEffect).SpriteNum,200
EndIf
If Timer()=>Effect(aEffect).CreationTime+((Effect(aEffect).DestructionTime/5)*2)
Set Sprite Alpha Effect(aEffect).SpriteNum,130
EndIf
If Timer()=>Effect(aEffect).CreationTime+((Effect(aEffect).DestructionTime/5)*3)
Set Sprite Alpha Effect(aEffect).SpriteNum,80
EndIf
If Timer()=>Effect(aEffect).CreationTime+((Effect(aEffect).DestructionTime/5)*4)
Set Sprite Alpha Effect(aEffect).SpriteNum,40
EndIf
EndIf
`Delete the effects after there LifeTime has expired
If Timer()=>Effect(aEffect).CreationTime+Effect(aEffect).DestructionTime
AddtoDeletionList(EffectType, Effect(aEffect).SpriteNum)
EndIf
Next aEffect
EndFunction
`___________________________________________________________________________________________________________
` RENDER
`___________________________________________________________________________________________________________
Function Render()
`Render BackGround
Paste Image 5,0,0
`Render HealthBar
Ink RGB(255,255,255),1
Set Cursor (Screen Width()/2)-100,29 : Print "Health"
If Player.Health > 666 then Ink RGB(0,255,0),1
If Player.Health > 333 and Player.Health < 666 then Ink RGB(255,255,0),1
If Player.Health < 333 then Ink RGB(255,0,0),1
Box (Screen Width()/2)-50,30,((Screen Width()/2)-50)+(Player.Health/10),40
`Render Rocks
For aRock=1 to Array Count(Rocks(0))
Paste Sprite Rocks(aRock).SpriteNum , Rocks(aRock).X , Rocks(aRock).Y
Next aRock
`Render Blood
For aEffect=1 to Array Count(Effect(0))
Paste Sprite Effect(aEffect).SpriteNum , Effect(aEffect).X , Effect(aEffect).Y
Next aEffect
`Render Player
Paste Sprite Player.SpriteNum , Player.X , Player.Y
Rotate Sprite Player.SpriteNum , Player.Angle
`Render Spiders
For aSpider=1 to Array Count(Spiders(0))
Paste Sprite Spiders(aSpider).SpriteNum , Spiders(aSpider).X , Spiders(aSpider).Y
Rotate Sprite Spiders(aSpider).SpriteNum , Spiders(aSpider).Angle
Next aSpider
`Render Bulets
For aBullet=1 to Array Count(Bullets(0))
Paste Sprite Bullets(aBullet).SpriteNum , Bullets(aBullet).X , Bullets(aBullet).Y
Rotate Sprite Bullets(aBullet).SpriteNum , Bullets(aBullet).Angle
Next aBullet
EndFunction
`___________________________________________________________________________________________________________
` COLISION
`___________________________________________________________________________________________________________
Function Collision(Object1X as Float, Object1Y as Float , Object1Radius as Float, Object2X as Float, Object2Y as Float, Object2Radius as Float)
Local DistX as Float : Local RadCord1X as Float : Local RadCord2X as Float : Local MoveX as Float
Local DistY as Float : Local RadCord1Y as Float : Local RadCord2Y as Float : Local MoveY as Float
Local Distance as Float : ColAngle as Float
`Values that we send Back
Global ReturnValueX as Float
Global ReturnValueY as Float
Global ReturnValueCollided as Integer
`Set the return Value to equal the Objects Position
ReturnValueX = Object1X
ReturnValueY = Object1Y
ReturnValueCollided = 0
`Calculate the distance between the two units
Distance = sqrt((Object1X - Object2X)^2 + (Object1Y - Object2Y)^2)
`If the distance is less than their radius, ie If they have collided
If Distance < Object1Radius + Object2Radius
ReturnValueCollided = 1
`Calculate the X and Y distance between Each Unit
DistX=(Object2X)-(Object1X)
DistY=(Object2Y)-(Object1Y)
`Calculate the Angle between the Two Units
`Note: Use atan2() in c++
ColAngle=ATANFULL(DistY,DistX)
`Get the cords of the First Units radius in the direction of the collsion
DistX=Cos(ColAngle)*(Object1Radius)
DistY=Sin(ColAngle)*(Object1Radius)
`Add the distance to the first units cords
RadCord1X=(Object1X + DistX)
RadCord1Y=(Object1Y + DistY)
`Get the cords of the Second Units radius in the direction of the collsion
DistX=Cos(ColAngle)*(Object2Radius)
DistY=Sin(ColAngle)*(Object2Radius)
`Minus the distance to the second units cords
RadCord2X=(Object2X - DistX)
RadCord2Y=(Object2Y - DistY)
`Get the distance between the two radius cords
DistX=(RadCord1X)-(RadCord2X)
DistY=(RadCord1Y)-(RadCord2Y)
`Get the Distance to move the Unit away From the Other Unit
MoveX=cos(ColAngle)*(DistX)
MoveY=sin(ColAngle)*(DistY)
`Set the return value to equal our new objects position
If DistX<0 then ReturnValueX = Object1X + MoveX
If DistY<0 then ReturnValueY = Object1Y + MoveY
If DistX>0 then ReturnValueX = Object1X - MoveX
If DistY>0 then ReturnValueY = Object1Y - MoveY
EndIf
EndFunction `(ReturnValueX,ReturnValueY, ReturnValueCollided)
Function CreatePlayer()
Global Player as Player
Player.X=(Screen Width()/2)
Player.Y=(Screen Height()/2)
Player.Health=1000
Player.Angle=0
Player.Radius=17
Player.SpriteNum=CreateSpriteNumber()
Sprite Player.SpriteNum,0,0,1
Offset Sprite Player.SpriteNum,20,17
Hide Sprite Player.SpriteNum
EndFunction
Function CreateBullet()
Array Insert at Bottom Bullets(0)
Local aBullet as Integer : aBullet=Array Count(Bullets(0))
Bullets(aBullet).SpriteNum=CreateSpriteNumber()
Sprite Bullets(aBullet).SpriteNum,0,0,3
Offset Sprite Bullets(aBullet).SpriteNum,7,4
Hide Sprite Bullets(aBullet).SpriteNum
Bullets(aBullet).Speed=4
Bullets(aBullet).Radius=5
EndFunction
Function CreateSpider()
Local NewSpider as Integer
Array Insert at Bottom Spiders(0)
NewSpider=Array Count(Spiders(0))
`Spawn the Spiders outside the view of the player
Local Buffer as Integer : Buffer = 50
Local Direction as Integer : Direction=1+rnd(3)
`Top of Player
If Direction=1
Spiders(NewSpider).X = (rnd(Screen Width()))
Spiders(NewSpider).Y = Player.Y-(Screen Height())-Buffer
EndIf
`Bottom of Player
If Direction=2
Spiders(NewSpider).X = (rnd(Screen Width()))
Spiders(NewSpider).Y = Player.Y+(Screen Height())+Buffer
EndIf
`Left of Player
If Direction=3
Spiders(NewSpider).X = Player.x-(Screen Width())-Buffer
Spiders(NewSpider).Y = (rnd(Screen Height()))
EndIf
`Left of Player
If Direction=4
Spiders(NewSpider).X = Player.x+(Screen Width())+Buffer
Spiders(NewSpider).Y = (rnd(Screen Height()))
EndIf
`Set the Spiders other Atributes
Spiders(NewSpider).Angle = rnd(360)
Spiders(NewSpider).Radius = 4 + rnd(25)
Spiders(NewSpider).Speed = 0.05 * Spiders(NewSpider).Radius
Spiders(NewSpider).SpriteNum = CreateSpriteNumber()
Sprite Spiders(NewSpider).SpriteNum,0,0,2
If Spiders(NewSpider).Radius >8 then STRETCH SPRITE Spiders(NewSpider).SpriteNum, 30 * (Spiders(NewSpider).Radius-4) , 30 * (Spiders(NewSpider).Radius-4)
Offset Sprite Spiders(NewSpider).SpriteNum,5,4
Hide Sprite Spiders(NewSpider).SpriteNum
EndFunction
Function CreateRock()
Array Insert at Bottom Rocks(0)
Local aRock as Integer : aRock=Array Count(Rocks(0))
Rocks(aRock).x=Rnd(Screen Width())
Rocks(aRock).y=Rnd(Screen Height())
Rocks(aRock).Radius=4
Rocks(aRock).SpriteNum=CreateSpriteNumber()
Sprite Rocks(aRock).SpriteNum,0,0,4
Offset Sprite Rocks(aRock).SpriteNum,10,10
Hide Sprite Rocks(aRock).SpriteNum
EndFunction
Function CreateEffect(X as Integer,Y as Integer , Category as String)
Array Insert at Bottom Effect(0)
Local aEffect as Integer : aEffect=Array Count(Effect(0))
Effect(aEffect).X=X
Effect(aEffect).Y=Y
Effect(aEffect).Category=Category
Effect(aEffect).CreationTime=Timer()
Effect(aEffect).DestructionTime=5000
Effect(aEffect).SpriteNum=CreateSpriteNumber()
If Effect(aEffect).Category="Blood" then Sprite Effect(aEffect).SpriteNum,0,0,7
Offset Sprite Effect(aEffect).SpriteNum,13,13
Hide Sprite Effect(aEffect).SpriteNum
EndFunction
Function AddtoDeletionList(Object as Integer, SpriteNum as Integer)
`Do a Safety Check to make sure the Object hasn't already been added to the list
For aObject=1 to Array Count(Destructor(0))
If Destructor(aObject).Object=Object and Destructor(aObject).SpriteNum=SpriteNum then ExitFunction
Next aObject
`Add the Object to the deletion list
Array Insert at Bottom Destructor(0)
Destructor(Array Count(Destructor(0))).Object = Object
Destructor(Array Count(Destructor(0))).SpriteNum = SpriteNum
EndFunction
Function DeleteObjects()
Local aObject as Integer : aObject = 1
While aObject <= Array Count(Destructor(0))
Local DeleteDest as Integer
Local ArrayNumber as Integer
If Destructor(aObject).Object=SpiderType
`Get the Array Number of our Object by Using its SpriteNumber
ArrayNumber = GetArrayNum(Destructor(aObject).Object,Destructor(aObject).SpriteNum)
`Delete the Spider
Delete Sprite Spiders(ArrayNumber).SpriteNum
FreeSpriteNumber(Spiders(ArrayNumber).SpriteNum)
Array Delete Element Spiders(0),ArrayNumber
EndIf
If Destructor(aObject).Object=BulletType
`Get the Array Number of our Object by Using its SpriteNumber
ArrayNumber = GetArrayNum(Destructor(aObject).Object,Destructor(aObject).SpriteNum)
Delete Sprite Bullets(ArrayNumber).SpriteNum
FreeSpriteNumber(Bullets(ArrayNumber).SpriteNum)
Array Delete Element Bullets(0),ArrayNumber
EndIf
If Destructor(aObject).Object=EffectType
`Get the Array Number of our Object by Using its SpriteNumber
ArrayNumber = GetArrayNum(Destructor(aObject).Object,Destructor(aObject).SpriteNum)
Delete Sprite Effect(ArrayNumber).SpriteNum
FreeSpriteNumber(Effect(ArrayNumber).SpriteNum)
Array Delete Element Effect(0),ArrayNumber
EndIf
Inc aObject
EndWhile
`Safely Empty Destructor Array
Empty Array Destructor(0) : Array Insert at Bottom Destructor(0)
EndFunction
Function CreateSpriteNumber()
If Array Count(SpriteNumber(0))>10000
Exit Prompt "You have reached The maximum amount of sprites allowed", "CreateSpriteNumber Error" : End
EndIf
`Pick a Sprite Number
Local aSpriteNum : aSpriteNum=(Rnd(10000)+1)
Local Unique as Integer : Unique=0
`Check if the Sprite number is being used if so pick another new number
Repeat
If Unique=0
Unique=1
For aSprite=1 to Array Count(SpriteNumber(0))
If SpriteNumber(aSprite) = aSpriteNum then aSpriteNum=(Rnd(10000)+1) : Unique=0
Next aSprite
EndIf
Until Unique=1
`If the Sprite Number is free to use then assign it to the SpriteNumber Array
Array Insert at Bottom SpriteNumber(0)
SpriteNumber(Array Count(SpriteNumber(0))) = aSpriteNum
EndFunction aSpriteNum
Function FreeSpriteNumber(aSpriteNum as Integer)
Local aSprite as Integer : aSprite = 1
While aSprite<= Array Count(SpriteNumber(0))
If aSpriteNum=SpriteNumber(aSprite) then Array Delete Element SpriteNumber(0),aSprite : ExitFunction
Inc aSprite
EndWhile
Exit Prompt "Sprite Number " + str$(aSpriteNum)+" that you are trying To delete Does not Exist", "FreeSpriteNumber Error"
End
EndFunction
Function GetArrayNum(ObjectType as Integer SpriteNum as Integer)
If ObjectType=SpiderType
For aSpider=1 to Array Count(Spiders(0))
If SpriteNum = Spiders(aSpider).SpriteNum then ExitFunction aSpider
Next aSpider
EndIf
If ObjectType=BulletType
For aBullet=1 to Array Count(Bullets(0))
If SpriteNum = Bullets(aBullet).SpriteNum then ExitFunction aBullet
Next aBullet
EndIf
If ObjectType=EffectType
For aEffect=1 to Array Count(Effect(0))
If SpriteNum = Effect(aEffect).SpriteNum then ExitFunction aEffect
Next aEffect
EndIf
Exit Prompt "Object " +str$(ObjectType) + " Does not have a Sprite of that Number", "GetArrayNum Error " + str$(SpriteNum)
End
EndFunction 0
Better to be dead, than to live your life afraid.