Microsoft Small Basic

Program Listing: MLK734
'Tank Rampage
'Copyright (c) 2012
'Art by Timo Sö; Programming by Math Man
Initialize()
TankData()
TowerData()
LevelData()
ToolBar()

Sub OnTick
For h = 1 To Amount_Tanks+3
tX[h] = tX[h] + tSpeed[h]
If tX[h]+35 > Width Then
tTank[h] = tTank[Amount_Tanks]
tX[h] = tX[Amount_Tanks]
tY[h] = tY[Amount_Tanks]
tType[h] = tType[Amount_Tanks]
tHP[h] = tHP[Amount_Tanks]
tSpeed[h] = tSpeed[Amount_Tanks]
tMoney[h] = tMoney[Amount_Tanks]
tTank[Amount_Tanks] = ""
tX[Amount_Tanks] = ""
tY[Amount_Tanks] = ""
tType[Amount_Tanks] = ""
tHP[Amount_Tanks] = ""
tSpeed[Amount_Tanks] = ""
tMoney[Amount_Tanks] = ""
Amount_Tanks = Amount_Tanks-1
For o = 1 To index
If aTarget[o] = Target Then
aTarget[o] = ""
notFiring[o] = "True"
Shapes.Remove(aAmmo[o])
EndIf
EndFor
Lives = Lives-1
Hearts = ""
For i = 1 To Lives
Hearts = Hearts+"♥ "
EndFor
Shapes.SetText(txtLives,"Your lives:"+Text.GetCharacter(10)+Hearts)
If Lives <= 0 Then
GraphicsWindow.ShowMessage("You have been shot down by tanks!","GAME OVER!")
Program.End()
EndIf
EndIf
Shapes.Move(tTank[h],tX[h],tY[h])
EndFor
For h = 1 To index
If notFiring[h] Then
notFiring[h] = "False"
DetermineTarget()
DetermineAngle()
If Target <> "" Then
GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
GraphicsWindow.PenColor = GraphicsWindow.BrushColor
aAmmo[h] = Shapes.AddEllipse(8,8)
Shapes.SetOpacity(aAmmo[h],60)
aTarget[h] = Target
aX[h] = twX[h]
aY[h] = twY[h]
aAngle[h] = angle
aDist[h] = 0
EndIf
EndIf
Target = aTarget[h]
DetermineAngle()
aAngle[h] = angle
aDist[h] = aDist[h]+twSpeed[h]
aX[h] = aX[h]+Math.Cos(aAngle[h])*1/5*twSpeed[h]
aY[h] = aY[h]+Math.Sin(aAngle[h])*1/5*twSpeed[h]
Shapes.Move(aAmmo[h],aX[h]-4,aY[h]-4)
If aDist[h] > twRange[h] Then
notFiring[h] = "True"
Shapes.Remove(aAmmo[h])
EndIf
If aY[h] - tY[Target] < 20 And aY[h] - tY[Target] > -20 And aX[h] - tX[Target] < 20 And aX[h] - tX[Target] > -20 Then
tHP[Target] = tHP[Target]-(twDamage[h]-tArmor[Target])
notFiring[h] = "True"
Shapes.Remove(aAmmo[h])
If twType[h] = 3 Then
tSpeed[Target] = tSpeed[Target]/2
EndIf
If tHP[Target] <= 0 Then
aTarget[h] = ""
Shapes.Remove(tTank[Target])
Money = Money+tMoney[Target]
tTank[Target] = tTank[Amount_Tanks]
tX[Target] = tX[Amount_Tanks]
tY[Target] = tY[Amount_Tanks]
tType[Target] = tType[Amount_Tanks]
tHP[Target] = tHP[Amount_Tanks]
tSpeed[Target] = tSpeed[Amount_Tanks]
tMoney[Target] = tMoney[Amount_Tanks]
tTank[Amount_Tanks] = ""
tX[Amount_Tanks] = ""
tY[Amount_Tanks] = ""
tType[Amount_Tanks] = ""
tHP[Amount_Tanks] = ""
tSpeed[Amount_Tanks] = ""
tMoney[Amount_Tanks] = ""
Amount_Tanks = Amount_Tanks-1
Shapes.SetText(txtMoney,"Your money:"+Text.GetCharacter(10)+" $ "+Money)
For o = 1 To index
If aTarget[o] = Target Then
aTarget[o] = ""
notFiring[o] = "True"
Shapes.Remove(aAmmo[o])
EndIf
EndFor
EndIf
EndIf
EndFor
EndSub


Sub Initialize
Amount_Tanks = 0
GraphicsWindow.CanResize = 0
Width = GraphicsWindow.Width
Height = GraphicsWindow.Height
GraphicsWindow.BrushColor = "DarkGray"
GraphicsWindow.PenColor = "DarkGray"
Road = Shapes.AddRectangle(Width,45)
Shapes.Move(Road,0,Height/2-22)
GraphicsWindow.BackgroundColor = "Green"
GraphicsWindow.Title = "Tank Rampage"
GraphicsWindow.MouseMove = MouseMove
GraphicsWindow.MouseUp = MouseUp
Controls.ButtonClicked = ButtonClick
GameInterval = 30
OnTick = ""
Money = 550
GraphicsWindow.ShowMessage("Welcome to Tank Rampage! It is a TD game that is simple."+Text.GetCharacter(10)+"Grab towers from the right side."+Text.GetCharacter(10)+"Press start when you're ready to start the rampage! ","Tank Rampage")
Timer.Interval = GameInterval
Lives = 5
EndSub

Sub TowerData
Dir = Program.Directory+"\Pictures\"
For i = 1 To 3
TowerImage[i] = ImageList.LoadImage(Dir+"Tower "+i+".gif")
EndFor

TowerDamage[1] = 35
TowerRange[1] = 250
TowerSpeed[1] = 60
TowerCost[1] = 250

TowerDamage[2] = 75
TowerRange[2] = 270
TowerSpeed[2] = 45
TowerCost[2] = 300

TowerDamage[3] = 0
TowerRange[3] = 270
TowerSpeed[3] = 40
TowerCost[3] = 350

TowerNum = Array.GetItemCount(TowerImage)
EndSub

Sub TankData
Dir = Program.Directory+"\Pictures\"
For i = 1 To 4
TankImage[i] = ImageList.LoadImage(Dir+"Tank "+i+".gif")
EndFor
TankImage[5] = ImageList.LoadImage(Dir+"Boss.gif")

TankSpeed[1] = 3 'speed he goes per about ?? milliseconds
TankHP[1] = 50 'hit points
TankArmor[1] = 1 'damage reduction
TankMoney[1] = 50 'money for killing
TankProb[1] = 35 'probability of it appearing in percent

TankSpeed[2] = 6
TankHP[2] = 20
TankArmor[2] = 1
TankMoney[2] = 40
TankProb[2] = 30

TankSpeed[3] = 3
TankHP[3] = 70
TankArmor[3] = 2
TankMoney[3] = 80
TankProb[3] = 20

TankSpeed[4] = 5
TankHP[4] = 80
TankArmor[4] = 3
TankMoney[4] = 140
TankProb[4] = 15

TankSpeed[5] = 4
TankHP[5] = 200
TankArmor[5] = 4
TankMoney[5] = 450
TankProb[5] = 0
EndSub

Sub LevelData
For i = 1 To 50
Level_AmountTanks[i] = Math.Power(i*2,2)
EndFor
EndSub

Sub ToolBar
GraphicsWindow.Width = Width+100
GraphicsWindow.BrushColor = "LightGray"
GraphicsWindow.PenColor = "LightGray"
Bar = Shapes.AddRectangle(100,Height)
Shapes.Move(Bar,Width,0)
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.PenColor = "Gray"
txtMoney = Shapes.AddText("Your money:"+Text.GetCharacter(10)+" $ "+Money)
Hearts = ""
For i = 1 To Lives
Hearts = Hearts+"♥ "
EndFor
txtLives = Shapes.AddText("Your lives:"+Text.GetCharacter(10)+Hearts)
Shapes.Move(txtMoney,Width+20,10)
Shapes.Move(txtLives,Width+20,50)
For i = 1 To TowerNum
BackSquare[i] = Shapes.AddRectangle(30,30)
Icon[i] = Shapes.AddImage(TowerImage[i])
CostDisplay[i] = Shapes.AddText("$ "+TowerCost[i])
Shapes.Move(BackSquare[i],Width+50-15,i*80)
Shapes.Move(Icon[i],Width+50-13,i*80+3)
Shapes.Move(CostDisplay[i],Width+50-25,i*80+40)
Shapes.SetOpacity(BackSquare[i],20)
EndFor
btnStart = Controls.AddButton("Start!",Width+50-30,i*80+30)
btnPause = Controls.AddButton("Pause",Width+50-30,i*80+60)
EndSub

Sub StartTanksComing
Lives = Lives+1
Hearts = ""
For i = 1 To Lives
Hearts = Hearts+"♥ "
EndFor
Shapes.SetText(txtLives,"Your lives:"+Text.GetCharacter(10)+Hearts)
LevelNum = LevelNum+1
For g = 1 To Level_AmountTanks[LevelNum]
NumOfTank = Math.GetRandomNumber(100)
If NumOfTank <= TankProb[1] Then
nowTank = 1
ElseIf NumOfTank <= TankProb[2]+TankProb[1] Then
nowTank = 2
ElseIf NumOfTank <= TankProb[3]+TankProb[2]+TankProb[1] Then
nowTank = 3
ElseIf NumOfTank <= TankProb[4]+TankProb[3]+TankProb[2]+TankProb[1] Then
nowTank = 4
EndIf
tTank[g] = Shapes.AddImage(TankImage[nowTank])
tX[g] = -g*80
tY[g] = Height/2-20+Math.GetRandomNumber(20)
tType[g] = nowTank
tHP[g] = TankHP[nowTank]
tArmor[g] = TankArmor[nowTank]
tMoney[g] = TankMoney[nowTank]
tSpeed[g] = TankSpeed[nowTank]
Amount_Tanks = Amount_Tanks+1
EndFor
EndSub

Sub MouseMove
MX = GraphicsWindow.MouseX
MY = GraphicsWindow.MouseY
If waitingToBePlaced Then
Shapes.Move(twTower[index],MX-13,MY-13)
Else
For q = 1 To TowerNum
If MX > Width+50-15 And MX < Width+50+15 And MY > q*80 And MY < q*80+30 Then
If Money >= TowerCost[q] Then
Shapes.SetOpacity(BackSquare[q],90)
Else
Shapes.SetOpacity(BackSquare[q],20)
EndIf
Else
Shapes.SetOpacity(BackSquare[q],20)
EndIf
EndFor
EndIf
EndSub

Sub MouseUp
If waitingToBePlaced And MY < Height/2-32 Or MY > Height/2+32 Then
twX[index] = MX-13
twY[index] = MY-13
notFiring[index] = "True"
waitingToBePlaced = "False"
Else
For q = 1 To TowerNum
If MX > Width+50-15 And MX < Width+50+15 And MY > q*80 And MY < q*80+30 Then
If Money >= TowerCost[q] Then
index = index+1
waitingToBePlaced = "True"
twTower[index] = Shapes.AddImage(TowerImage[q])
twType[index] = q
twDamage[index] = TowerDamage[q]
twRange[index] = TowerRange[q]
twSpeed[index] = TowerSpeed[q]
Shapes.Move(twTower[index],MX-12,MY-12)
Money = Money-TowerCost[q]
Shapes.SetText(txtMoney,"Your money:"+Text.GetCharacter(10)+" $ "+Money)
EndIf
EndIf
EndFor
EndIf
EndSub

Sub ButtonClick
Button = Controls.LastClickedButton
If Button = btnStart Then
Timer.Tick = OnTick
StartTanksComing()
ElseIf Button = btnPause Then
Pause()
ElseIf Button = btnCancel Then
Controls.Remove(btnCancel)
Controls.Remove(btnNewSaveSlot)
For i = 1 To Array.GetItemCount(SavedGames)
Controls.Remove(btnSaveGame[i])
EndFor
Shapes.Remove(PauseScreen)
Pause()
ElseIf Button = btnUnpause Then
Timer.Tick = OnTick
Shapes.Remove(PauseScreen)
Controls.Remove(btnUnpause)
Controls.Remove(btnSaveGame)
Controls.Remove(btnLoadGame)
Controls.Remove(btnQuit)
ElseIf Button = btnSaveGame Then
SaveGame()
ElseIf Array.ContainsValue(btnSaveGame,Button) Then
OverwriteFile()
ElseIf Button = btnNewSaveSlot Then
AskForFileName()
ElseIf Button = btnLoadGame Then
LoadGame()
ElseIf Array.ContainsValue(btnLoadGame,Button) Then
LoadFile()
ElseIf Button = btnQuit Then
Quit()
EndIf
EndSub

Sub Pause
Timer.Tick = Nothing
GraphicsWindow.BrushColor = "DarkGray"
GraphicsWindow.PenColor = "Gray"
temp = GraphicsWindow.FontSize
GraphicsWindow.FontSize = 50
Amount_Buttons = 4
PauseScreen = Shapes.AddRectangle(Width+100,Height)
btnUnpause = Controls.AddButton("Resume",10,10)
Controls.SetSize(btnUnpause,Width+80,(Height-20)/Amount_Buttons)
btnSaveGame = Controls.AddButton("Save Game",10,10+(Height-20)/Amount_Buttons)
Controls.SetSize(btnSaveGame,Width+80,(Height-20)/Amount_Buttons)
btnLoadGame = Controls.AddButton("Load Game",10,10+(Height-20)/Amount_Buttons*2)
Controls.SetSize(btnLoadGame,Width+80,(Height-20)/Amount_Buttons)
btnQuit = Controls.AddButton("Quit",10,10+(Height-20)/Amount_Buttons*3)
Controls.SetSize(btnQuit,Width+80,(Height-20)/Amount_Buttons)
GraphicsWindow.FontSize = temp
EndSub

Sub SaveGame
Controls.Remove(btnUnpause)
Controls.Remove(btnSaveGame)
Controls.Remove(btnLoadGame)
Controls.Remove(btnQuit)
' The following line could be harmful and has been automatically commented.
' SavedGames = File.GetFiles(Program.Directory+"\Saved games\")
btnCancel = Controls.AddButton("Cancel",10,10)
Controls.SetSize(btnCancel,Width+80,(Height-20)/(Array.GetItemCount(SavedGames)+2))
For i = 1 To Array.GetItemCount(SavedGames)
btnSaveGame[i] = Controls.AddButton(Text.GetSubTextToEnd(Text.GetSubText(SavedGames[i],1,Text.GetLength(SavedGames[i])-4),Text.GetLength(Program.Directory+"\Saved games\")+1),10,i*(Height-20)/(Array.GetItemCount(SavedGames)+2)+10)
Controls.SetSize(btnSaveGame[i],Width+80,(Height-20)/(Array.GetItemCount(SavedGames)+2))
EndFor
btnNewSaveSlot = Controls.AddButton("New save slot",10,(i)*(Height-20)/(Array.GetItemCount(SavedGames)+2)+10)
Controls.SetSize(btnNewSaveSlot,Width+80,(Height-20)/(Array.GetItemCount(SavedGames)+2))
EndSub

Sub OverwriteFile
EndSub

Sub AskForFileName
EndSub

Sub LoadGame
Controls.Remove(btnUnpause)
Controls.Remove(btnSaveGame)
Controls.Remove(btnLoadGame)
Controls.Remove(btnQuit)
' The following line could be harmful and has been automatically commented.
' SavedGames = File.GetFiles(Program.Directory+"\Saved games\")
btnCancel = Controls.AddButton("Cancel",10,10)
Controls.SetSize(btnCancel,Width+80,(Height-20)/(Array.GetItemCount(SavedGames)+1))
For i = 1 To Array.GetItemCount(SavedGames)
btnLoadGame[i] = Controls.AddButton(Text.GetSubTextToEnd(Text.GetSubText(SavedGames[i],1,Text.GetLength(SavedGames[i])-4),Text.GetLength(Program.Directory+"\Saved games\")+1),10,i*(Height-20)/(Array.GetItemCount(SavedGames)+1)+10)
Controls.SetSize(btnLoadGame[i],Width+80,(Height-20)/(Array.GetItemCount(SavedGames)+1))
EndFor
EndSub

Sub LoadFile
EndSub

Sub Nothing
EndSub

Sub Quit
Program.End()
EndSub

Sub DetermineTarget
Target = ""
Greatest = ""
For r = 1 To Amount_Tanks
If tX[r] > Greatest And Math.SquareRoot(Math.Power(tX[r]-twX[h],2)+Math.Power(tY[r]-twY[h],2)) < twRange[h] Then
Greatest = tX[r]
Target = r
EndIf
EndFor
If Math.SquareRoot(Math.Power(tX[Target]-twX[h],2)+Math.Power(tY[Target]-twY[h],2)) > twRange[h] Then
notFiring[h] = "True"
EndIf
EndSub

Sub DetermineAngle
dx = twX[h] - tX[Target]
dy = twY[h] - tY[Target]
If (dx = 0) Then
angle = Math.Pi/2
If (dy < 0) Then
angle = -angle
EndIf
Else
angle = Math.ArcTan(dy/dx)
EndIf
If (dx < 0) Then
angle = angle+Math.Pi
EndIf
angle = angle+Math.Pi
EndSub