Microsoft Small Basic

Program Listing: GZN668
' Battleship in Small Basic
' written by Nonki Takahashi
' Last update 2014-03-04
'
GraphicsWindow.Title = "Battleship 0.1"
GraphicsWindow.BackgroundColor = "LightGray"
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
bc = "Turquoise"
GraphicsWindow.PenColor = "LightGray"
GraphicsWindow.FontName = "Trebuchet MS"
fc = "White"
fs = 20
GraphicsWindow.FontSize = fs
size = 28
y0 = Math.Floor((gh - size * 11) / 2) + size
x0 = Math.Floor((gw - size * 21) / 2)
DrawBoard()
radar = Shapes.AddImage("http://www.nonkit.com/smallbasic.files/Radar.PNG")
Shapes.SetOpacity(radar, 50)
Shapes.Move(radar, x0, y0)
bc = "RoyalBlue"
x0 = Math.Floor((gw - size * 21) / 2) + size * 11
DrawBoard()
InitShip()
DrawShip()
angleRadar = 0
Timer.Interval = 100
Timer.Tick = OnTick
Sub DrawBoard
GraphicsWindow.BrushColor = fc
y = y0 - fs * 1.2
For i = 1 To 10
If i = 9 Then
x = x0 + (i - 1) * size + 10
Else
x = x0 + (i - 1) * size + 7
EndIf
GraphicsWindow.DrawText(x, y, Text.GetSubText("ABCDEFGHIJ", i, 1))
EndFor
For j = 1 To 10
y = y0 + (j - 1) * size
If gw / 2 < x0 Then
GraphicsWindow.BrushColor = fc
If j = 10 Then
x = x0 - fs * 1.3
Else
x = x0 - fs
EndIF
GraphicsWindow.DrawText(x, y, j)
EndIf
GraphicsWindow.BrushColor = bc
For i = 1 To 10
x = x0 + (i - 1) * size
GraphicsWindow.FillRectangle(x, y, size, size)
GraphicsWindow.DrawRectangle(x, y, size, size)
EndFor
EndFor
EndSub
Sub DrawShip
For iShip = 1 To 5
shi = ship[iShip]
col = Text.GetIndexOf("ABCDEFGHIJ", Text.GetSubText(shi["pos"], 1, 1))
row = Text.GetSubTextToEnd(shi["pos"], 2)
x = x0 + (col - 1) * size
y = y0 + (row - 1) * size
For iShape = shi["iMin"] To shi["iMax"]
sha = shape[iShape]
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "Gray"
If sha["func"] = "rect" Then
sha["obj"] = Shapes.AddRectangle(sha["width"], sha["height"])
ElseIf sha["func"] = "ell" Then
sha["obj"] = Shapes.AddEllipse(sha["width"], sha["height"])
EndIf
Shapes.Move(sha["obj"], x + sha["x"], y + sha["y"])
EndFor
EndFor
EndSub
Sub InitShip
ship[1] = "id=Carrier;length=5;iMin=1;iMax=1;pos=A1;dir=E;"
ship[2] = "id=Battleship;length=4;iMin=2;iMax=4;pos=A2;dir=E;"
ship[3] = "id=Destroyer;length=3;iMin=5;iMax=7;pos=A3;dir=E;"
ship[4] = "id=Submarine;length=3;iMin=8;iMax=10;pos=A4;dir=E;"
ship[5] = "id=Patrol boat;length=2;iMin=11;iMax=13;pos=A5;dir=E;"
shape[1] = "func=rect;x=2;y=2;width=136;height=24;"
shape[2] = "func=ell;x=2;y=2;width=24;height=24;"
shape[3] = "func=rect;x=14;y=2;width=70;height=24;"
shape[4] = "func=ell;x=58;y=2;width=52;height=24;"
shape[5] = "func=ell;x=2;y=2;width=24;height=24;"
shape[6] = "func=rect;x=14;y=2;width=42;height=24;"
shape[7] = "func=ell;x=30;y=2;width=52;height=24;"
shape[8] = "func=ell;x=2;y=4;width=24;height=20;"
shape[9] = "func=rect;x=14;y=4;width=56;height=20;"
shape[10] = "func=ell;x=58;y=4;width=24;height=20;"
shape[11] = "func=ell;x=2;y=2;width=24;height=24;"
shape[12] = "func=rect;x=14;y=2;width=14;height=24;"
shape[13] = "func=ell;x=2;y=2;width=52;height=24;"
EndSub
Sub OnTick
angleRadar = angleRadar + 5
If 360 <= angleRadar Then
angleRadar = 0
EndIf
Shapes.Rotate(radar, angleRadar)
EndSub