Microsoft Small Basic

Program Listing: LZN025-0
' TRON 0.2 - light cycle game
'
' History:
' 0.2 11/06/2013 Human vs humen version. (LZN025-0)
' 0.1a 03/06/2013 Created as alpha version. (LZN025)
'
title = "TRON 0.2"
GraphicsWindow.Title = title
GraphicsWindow.BackgroundColor = "Black"
Init()
DrawTRON()
ClearTRON()
DrawGrid()
DrawCycles()
GraphicsWindow.KeyDown = OnKeyDown
Timer.Interval = 1000 / 24
Timer.Tick = OnTick
Sub Player1_Move
If dy = "Up" Then
ry = ry - 1
ElseIf dy = "Down" Then
ry = ry + 1
ElseIf dy = "Left" Then
cy = cy - 1
ElseIf dy = "Right" Then
cy = cy + 1
EndIf
EndSub
Sub Player2_Move
If dc = "Up" Then
rc = rc - 1
ElseIf dc = "Down" Then
rc = rc + 1
ElseIf dc = "Left" Then
cc = cc - 1
ElseIf dc = "Right" Then
cc = cc + 1
EndIf
EndSub
Sub OnKeyDown
key = GraphicsWindow.LastKey
If key = "Up" Or key = "Down" Or key = "Left" Or key = "Right" Then
dc = key
ElseIf key = "S" Then
dy = "Up"
ElseIf key = "D" Then
dy = "Down"
ElseIf key = "A" Then
dy = "Left"
ElseIf key = "F" Then
dy = "Right"
EndIf
EndSub
Sub OnTick
Player1_Move()
Player2_Move()
DrawCycles()
EndSub
Sub ClearTRON
For op = 0 To 100
Shapes.SetOpacity(mask, op)
Program.Delay(10)
EndFor
GraphicsWindow.Clear()
Shapes.Remove(mask)
EndSub
Sub DrawArc
GraphicsWindow.PenColor = param["pc"]
GraphicsWindow.PenWidth = param["pw"]
If param["a1"] < param["a2"] Then
da = 0.3
Else
da = -0.3
EndIf
a1 = ""
For a = param["a1"] To param["a2"] Step da
a2 = Math.GetRadians(a)
x2 = param["x"] + param["r"] * Math.Cos(a2)
y2 = param["y"] + param["r"] * Math.Sin(a2)
If a1 <> "" Then
GraphicsWindow.DrawLine(x1, y1, x2, y2)
EndIf
a1 = a2
x1 = x2
y1 = y2
EndFor
EndSub
Sub DrawCircle
GraphicsWindow.PenColor = param["pc"]
GraphicsWindow.PenWidth = param["pw"]
GraphicsWindow.DrawEllipse(param["x"] - param["r"], param["y"] - param["r"], param["r"] * 2, param["r"] * 2)
EndSub
Sub DrawCycles
If (map[cc][rc] = "") Or ((lastcc = cc) And (lastrc = rc)) Then
map[cc][rc] = 1
GraphicsWindow.BrushColor = "Cyan"
GraphicsWindow.FillRectangle(cc * size, rc * size, size, size)
lastcc = cc
lastrc = rc
ElseIf map[cc][rc] = 1 Then
GraphicsWindow.BrushColor = "DarkCyan"
GraphicsWindow.FillRectangle(lastcc * size, lastrc * size, size, size)
scorey = scorey + 1
GraphicsWindow.Title = title + " - score " + scorey + ":" + scorec
dc = ""
Timer.Interval = 3000
If 10 <= scorey Then
Timer.Tick = GameEnd
Else
Timer.Tick = Restart
EndIf
EndIf
If (map[cy][ry] = "") Or ((lastcy = cy) And (lastry = ry)) Then
map[cy][ry] = 1
GraphicsWindow.BrushColor = "Yellow"
GraphicsWindow.FillRectangle(cy * size, ry * size, size, size)
lastcy = cy
lastry = ry
ElseIf map[cy][ry] = 1 Then
GraphicsWindow.BrushColor = "Olive"
GraphicsWindow.FillRectangle(lastcy * size, lastry * size, size, size)
scorec = scorec + 1
GraphicsWindow.Title = title + " - score " + scorey + ":" + scorec
dy = ""
Timer.Interval = 3000
If 10 <= scorec Then
Timer.Tick = GameEnd
Else
Timer.Tick = Restart
EndIf
EndIf
EndSub
Sub GameEnd
GraphicsWindow.Clear()
If scorey = 10 And scorec = 10 Then
resulty = "DRAW"
resultc = "DRAW"
ElseIf scorey = 10 Then
resulty = "WIN"
resultc = "LOSE"
ElseIf scorec = 10 Then
resulty = "LOSE"
resultc = "WIN"
EndIf
param = "x=150;y=100;text=PLAYER 1 "+resulty+" : SCORE "+scorey+";bc=Yellow;fn=Arial;fs=18;"
DrawText()
param = "x=150;y=150;text=PLAYER 2 "+resultc+" : SCORE "+scorec+";bc=Cyan;fn=Arial;fs=18;"
DrawText()
scorec = 0
scorey = 0
GraphicsWindow.Title = title + " - score " + scorey + ":" + scorec
Timer.Interval = 5000
Timer.Tick = Restart
EndSub
Sub DrawGrid
GraphicsWindow.PenWidth = 2
GraphicsWindow.PenColor = "DimGray"
For x = 0 To gw Step size * gap
GraphicsWindow.DrawLine(x, 0, x, gh)
EndFor
For y = 0 To gh Step size * gap
GraphicsWindow.DrawLine(0, y, gw, y)
EndFor
EndSub
Sub DrawLine
GraphicsWindow.PenColor = param["pc"]
GraphicsWindow.PenWidth = param["pw"]
GraphicsWindow.DrawLine(param["x1"], param["y1"], param["x2"], param["y2"])
EndSub
Sub DrawRect
GraphicsWindow.PenColor = param["pc"]
GraphicsWindow.PenWidth = param["pw"]
GraphicsWindow.DrawRectangle(param["x"], param["y"], param["width"], param["height"])
EndSub
Sub DrawText
GraphicsWindow.BrushColor = param["bc"]
GraphicsWindow.FontBold = param["fb"]
GraphicsWindow.FontItalic = param["fi"]
GraphicsWindow.FontName = param["fn"]
GraphicsWindow.FontSize = param["fs"]
GraphicsWindow.DrawText(param["x"], param["y"], param["text"])
EndSub
Sub DrawTRON
GraphicsWindow.BrushColor = "Black"
mask = Shapes.AddRectangle(gw, gh)
param = "x1=124;y1=120;x2=476;y2=120;pc=#009900;pw=2;"
DrawLine() ' horizontal line
param = "x1=340;y1=64;x2=340;y2=176;pc=#009900;pw=2;"
DrawLine() ' vertical line
param = "x=124;y=64;width=352;height=112;pc=#990033;pw=4;"
DrawRect() ' frame
param = "x1=140;y1=82;x2=222;y2=82;pc=#990033;pw=4;"
DrawLine() ' T
param = "x1=142;y1=83;x2=142;y2=100;pc=#990033;pw=4;"
DrawLine() ' T
param = "x1=220;y1=83;x2=220;y2=100;pc=#990033;pw=4;"
DrawLine() ' T
param = "x1=140;y1=100;x2=162;y2=100;pc=#990033;pw=4;"
DrawLine() ' T
param = "x1=196;y1=100;x2=222;y2=100;pc=#990033;pw=4;"
DrawLine() ' T
param = "x1=160;y1=100;x2=160;y2=160;pc=#990033;pw=4;"
DrawLine() ' T
param = "x1=198;y1=100;x2=198;y2=160;pc=#990033;pw=4;"
DrawLine() ' T
param = "x1=160;y1=158;x2=198;y2=158;pc=#990033;pw=4;"
DrawLine() ' T
param = "x=220;y=120;width=18;height=38;pc=#990033;pw=4;"
DrawRect() ' R
param = "x1=232;y1=83;x2=232;y2=100;pc=#990033;pw=4;"
DrawLine() ' R
param = "x1=230;y1=82;x2=260;y2=82;pc=#990033;pw=4;"
DrawLine() ' R
param = "x1=230;y1=100;x2=260;y2=100;pc=#990033;pw=4;"
DrawLine() ' R
param = "x=260;y=120;r=20;a1=0;a2=-90;pc=#990033;pw=4;"
DrawArc() ' R
param = "x=260;y=120;r=38;a1=0;a2=-90;pc=#990033;pw=4;"
DrawArc() ' R
param = "x1=277;y1=120;x2=300;y2=120;pc=#990033;pw=4;"
DrawLine() ' R
param = "x1=240;y1=120;x2=278;y2=158;pc=#990033;pw=4;"
DrawLine() ' R
param = "x1=262;y1=120;x2=300;y2=158;pc=#990033;pw=4;"
DrawLine() ' R
param = "x1=240;y1=120;x2=263;y2=120;pc=#990033;pw=4;"
DrawLine() ' R
param = "x1=276;y1=158;x2=302;y2=158;pc=#990033;pw=4;"
DrawLine() ' R
param = "x=340;y=120;r=20;pc=#990033;pw=4;"
DrawCircle() ' O
param = "x=340;y=120;r=40;pc=#990033;pw=4;"
DrawCircle() ' O
param = "x1=380;y1=82;x2=460;y2=158;pc=#990033;pw=4;"
DrawLine() ' N
param = "x1=396;y1=118;x2=418;y2=118;pc=#990033;pw=4;"
DrawLine() ' N
param = "x1=380;y1=82;x2=380;y2=160;pc=#990033;pw=4;"
DrawLine() ' N
param = "x1=398;y1=120;x2=398;y2=160;pc=#990033;pw=4;"
DrawLine() ' N
param = "x1=378;y1=158;x2=400;y2=158;pc=#990033;pw=4;"
DrawLine() ' N
param = "x1=438;y1=82;x2=460;y2=82;pc=#990033;pw=4;"
DrawLine() ' N
param = "x1=440;y1=82;x2=440;y2=120;pc=#990033;pw=4;"
DrawLine() ' N
param = "x1=458;y1=82;x2=458;y2=158;pc=#990033;pw=4;"
DrawLine() ' N
param = "x1=420;y1=122;x2=442;y2=122;pc=#990033;pw=4;"
DrawLine() ' N
param = "x=150;y=250;text=PLAYER 1 : USE A S D F KEYS;bc=Yellow;fn=Arial;fs=18;"
DrawText()
param = "x=150;y=300;text=PLAYER 2 : USE ← ↑ ↓ → KEYS;bc=Cyan;fn=Arial;fs=18;"
DrawText()
For op = 100 To 0 Step -1
Shapes.SetOpacity(mask, op)
Program.Delay(100)
EndFor
EndSub
Sub Init
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
size = 6
gap = 10
cols = Math.Floor(gw / size / gap) * gap
gw = cols * size
GraphicsWindow.Width = gw
rows = Math.Floor(gh / size / gap) * gap
gh = rows * size
GraphicsWindow.Height = gh
cy = gap * 2 - 1
scorey = 0
scorec = 0
lastcy = cy
ry = Math.Floor(rows / 2)
lastry = ry
cc = cols - gap * 2
lastcc = cc
rc = Math.Floor(rows / 2)
lastrc = rc
map = ""
For c = 0 To cols - 1
map[c][-1] = 1
map[c][rows] = 1
EndFor
For r = 0 To rows - 1
map[-1][r] = 1
map[cols][r] = 1
EndFor
dy = "Right"
dc = "Left"
EndSub
Sub Restart
dy = "Right"
dc = "Left"
cy = gap * 2 - 1
lastcy = cy
ry = Math.Floor(rows / 2)
lastry = ry
cc = cols - gap * 2
lastcc = cc
rc = Math.Floor(rows / 2)
lastrc = rc
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FillRectangle(0, 0, gw, gh)
DrawGrid()
DrawCycles()
map = ""
For c = 0 To cols - 1
map[c][-1] = 1
map[c][rows] = 1
EndFor
For r = 0 To rows - 1
map[-1][r] = 1
map[cols][r] = 1
EndFor
Timer.Interval = 1000 / 24
Timer.Tick = OnTick
EndSub
Sub Dummy
OnTick = ""
EndSub