Microsoft Small Basic

Program Listing: PJBJ480.000
' Program "Nature". Version 0.15. 19.04.2024 (translated by Google translator)
'
' This program is the engine of the World of the game "AI Snake" and the interface for communication with human.
' The LitDev extension v1.2.26 is used.
'

'----------------------------------------
' Variables of the "Nature" system
'----------------------------------------

'--- various variables
x = 0 ' free variable
y = 0 ' free variable

'--- GUI parameters
cell_size = 15 ' cell size of the playing field in pixels
number_of_cells = 30 ' number of cells in the length and width of the playing field
n_startingPoint_X = cell_size * number_of_cells + 20
n_startingPoint_Y = 18
n_redSwitchDefaultPosX = n_startingPoint_X + 71
n_redSwitchDefaultPosY = n_startingPoint_Y + 3
n_rabBrushText_X = n_startingPoint_X + 10 'Rabbit Brush Text
n_rabBrushText_Y = n_startingPoint_Y + 67
n_snakeBrushText_X = n_startingPoint_X + 10
n_snakeBrushText_Y = n_startingPoint_Y + 127

'--- variables of objects of the program "Nature"
t_switchStatus = "OFF" ' Server switch status
t_serverStatus = "OFF" ' Server status
shp_LastEventShape = ""
evtp_LastEventType = ""
chbx_LastClickedCheckBox = ""
b_checkBoxChanged = "False"
b_rabbitMindConnected = "False"
b_snakeMindConnected = "False"
'---

Initialization()
GraphicsWindow.Show()

'--- MAIN LOOP ---
While "True"
Program.Delay(50)

'--- checking the click on the Server switch
If evtp_LastEventType = "MouseUp" Then
If shp_LastEventShape = sh_RedSwitch_OFF Then
OnRedSwitchOFF_Click()
shp_LastEventShape = ""
ElseIf shp_LastEventShape = sh_RedSwitch_ON Then
OnRedSwitchON_Click()
shp_LastEventShape = ""
ElseIf shp_LastEventShape = sh_GreenSwitch_ON Then
OnGreenSwitchON_Click()
shp_LastEventShape = ""
EndIf
evtp_LastEventType = ""
EndIf

'--- checking connection flags
If b_checkBoxChanged = "True" Then
If chbx_LastClickedCheckBox = CheckBox1 Then
If LDControls.CheckBoxGetState(CheckBox1) = "False" Then
b_rabbitMindConnected = "False"
OnRabbitDisconnected()
Else
b_rabbitMindConnected = "True"
OnRabbitConnected()
EndIf
Else
If LDControls.CheckBoxGetState(CheckBox2) = "False" Then
b_snakeMindConnected = "False"
OnSnakeDisconnected()
Else
b_snakeMindConnected = "True"
OnSnakeConnected()
EndIf
EndIf

b_checkBoxChanged = "False"
chbx_LastClickedCheckBox = ""
EndIf

'--- setting the color of the Server switch
If t_switchStatus = "ON" Then
If t_serverStatus = "OFF" Then
If (b_rabbitMindConnected = "True") And (b_snakeMindConnected = "True") Then
t_serverStatus = "ON"
SetSvitchColor_Green()
EndIf
Else
If (b_rabbitMindConnected = "False") Or (b_snakeMindConnected = "False") Then
t_serverStatus = "OFF"
SetSvitchColor_Red()
EndIf
EndIf
Else
b_rabbitMindConnected = "False"
OnRabbitDisconnected()
LDControls.CheckBoxState(CheckBox1, "False")
b_snakeMindConnected = "False"
OnSnakeDisconnected()
LDControls.CheckBoxState(CheckBox2, "False")
EndIf
EndWhile
'*****************************

'======================
' SUBROUTINES
'======================
Sub Initialization
'--- Creating a startup graphical interface
x = cell_size * number_of_cells

'--- graphics window
GraphicsWindow.Width = x + 190
GraphicsWindow.Height = x + 20
LDGraphicsWindow.Resize = 0
GraphicsWindow.Title = "AI Snake"
LDGraphicsWindow.PauseUpdates()

'--- playing field
GraphicsWindow.BrushColor = "#FFBBDB9D"
GraphicsWindow.PenColor = "#FFC27452"
Field = Shapes.AddRectangle(x, x)
Shapes.Move(Field,10,10)

'--- text "Server"
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontSize = 18
GraphicsWindow.FontBold = "False"
txt_Server = Shapes.AddText("Server")
Controls.SetSize(txt_Server,56,24)
Shapes.Move(txt_Server,n_startingPoint_X+5,n_startingPoint_Y)

'--- Server switch
GraphicsWindow.BrushColor = "#FFE8E8DB"
GraphicsWindow.PenColor = "Black"
GraphicsWindow.PenWidth = 1
rect_SwitchBG = Shapes.AddRectangle(50, 20)
Shapes.Move(rect_SwitchBG, n_startingPoint_X+70, n_startingPoint_Y+2)

img_txt_OFF = LDImage.NewImage(30, 18, "Red")
GraphicsWindow.FontSize = 10
GraphicsWindow.FontBold = "True"
LDImage.AddText(img_txt_OFF, "OFF", 3, 1, "White")
sh_RedSwitch_OFF = Shapes.AddImage(img_txt_OFF)
Shapes.Move(sh_RedSwitch_OFF, n_redSwitchDefaultPosX, n_redSwitchDefaultPosY)
LDShapes.SetShapeEvent(sh_RedSwitch_OFF)

img_txt_ON_red = LDImage.NewImage(30, 18, "Red")
LDImage.AddText(img_txt_ON_red, "ON", 4, 1, "White")
sh_RedSwitch_ON = Shapes.AddImage(img_txt_ON_red)
Shapes.Move(sh_RedSwitch_ON, n_redSwitchDefaultPosX+18, n_redSwitchDefaultPosY)
LDShapes.SetShapeEvent(sh_RedSwitch_ON)
Shapes.HideShape(sh_RedSwitch_ON)

img_txt_ON_green = LDImage.NewImage(30, 18, "LimeGreen")
LDImage.AddText(img_txt_ON_green, "ON", 4, 1, "White")
sh_GreenSwitch_ON = Shapes.AddImage(img_txt_ON_green)
Shapes.Move(sh_GreenSwitch_ON, n_redSwitchDefaultPosX+18, n_redSwitchDefaultPosY)
LDShapes.SetShapeEvent(sh_GreenSwitch_ON)
Shapes.HideShape(sh_GreenSwitch_ON)

'--- text "Rabbit Mind"
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontSize = 18
GraphicsWindow.FontBold = "False"

txt_RabMind = Shapes.AddText("Rabbit Mind")
Controls.SetSize(txt_RabMind,97,25)
Shapes.Move(txt_RabMind, n_startingPoint_X+5, n_startingPoint_Y+42)

'--- Rabbit connection indicator "disconnected/connected"
brt_RabBrushText = LDShapes.BrushText(" disconnected ", "Red", "White")
LDShapes.BrushRectangle(brt_RabBrushText, n_rabBrushText_X, n_rabBrushText_Y, 140, 25)

'--- text "Snake Mind"
txt_SnakeMind = Shapes.AddText("Snake Mind")
Controls.SetSize(txt_SnakeMind,97,25)
Shapes.Move(txt_SnakeMind, n_startingPoint_X+5, n_startingPoint_Y+102)

'--- Snake connection indicator "disconnected/connected"
brt_SnakeBrushText = LDShapes.BrushText(" disconnected ", "Red", "White")
LDShapes.BrushRectangle(brt_SnakeBrushText, n_snakeBrushText_X, n_snakeBrushText_Y, 140, 25)

GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontSize = 12
CheckBox1 = LDControls.AddCheckBox("Rabbit connected")
Shapes.Move(CheckBox1,490,220)

CheckBox2 = LDControls.AddCheckBox("Snake connected")
Shapes.Move(CheckBox2,490,250)
'----

LDGraphicsWindow.ResumeUpdates()
LDShapes.ShapeEvent = OnLDShapeEvent
LDControls.CheckBoxChanged = OnCheckBoxChanged
EndSub

Sub SetSvitchColor_Green
Shapes.HideShape(sh_RedSwitch_ON)
Shapes.ShowShape(sh_GreenSwitch_ON)
EndSub

Sub SetSvitchColor_Red
Shapes.HideShape(sh_GreenSwitch_ON)
Shapes.ShowShape(sh_RedSwitch_ON)
EndSub

Sub OnLDShapeEvent
shp_LastEventShape = LDShapes.LastEventShape
evtp_LastEventType = LDShapes.LastEventType
EndSub

Sub OnCheckBoxChanged
chbx_LastClickedCheckBox = LDControls.LastCheckBox
b_checkBoxChanged = "True"
EndSub

Sub OnRedSwitchOFF_Click
If t_switchStatus = "OFF" Then
LDGraphicsWindow.PauseUpdates()
Shapes.HideShape(sh_RedSwitch_OFF)
Shapes.ShowShape(sh_RedSwitch_ON)
LDGraphicsWindow.ResumeUpdates()
t_switchStatus = "ON"
Else
GraphicsWindow.ShowMessage("A click OnRedSwitch_OFF was detected at switchStatus != OFF.", "Error!")
EndIf
EndSub

Sub OnRedSwitchON_Click
If t_switchStatus = "ON" Then
LDGraphicsWindow.PauseUpdates()
Shapes.HideShape(sh_RedSwitch_ON)
Shapes.ShowShape(sh_RedSwitch_OFF)
LDGraphicsWindow.ResumeUpdates()
t_switchStatus = "OFF"
Else
GraphicsWindow.ShowMessage("A click OnRedSwitch_ON was detected at switchStatus != ON.", "Error!")
EndIf
EndSub

Sub OnGreenSwitchON_Click
If t_switchStatus = "ON" Then
LDGraphicsWindow.PauseUpdates()
Shapes.HideShape(sh_GreenSwitch_ON)
Shapes.ShowShape(sh_RedSwitch_OFF)
LDGraphicsWindow.ResumeUpdates()
t_switchStatus = "OFF"
Else
GraphicsWindow.ShowMessage("A click OnGreenSwitchON was detected at switchStatus != ON.", "Error!")
EndIf
EndSub

Sub OnRabbitConnected
GraphicsWindow.FontSize = 18
brt_RabBrushText = LDShapes.BrushText(" connected ", "LimeGreen", "White")
LDShapes.BrushRectangle(brt_RabBrushText, n_rabBrushText_X, n_rabBrushText_Y, 140, 25)
EndSub

Sub OnRabbitDisconnected
GraphicsWindow.FontSize = 18
brt_RabBrushText = LDShapes.BrushText(" disconnected ", "Red", "White")
LDShapes.BrushRectangle(brt_RabBrushText, n_rabBrushText_X, n_rabBrushText_Y, 140, 25)
EndSub

Sub OnSnakeConnected
GraphicsWindow.FontSize = 18
brt_SnakeBrushText = LDShapes.BrushText(" connected ", "LimeGreen", "White")
LDShapes.BrushRectangle(brt_SnakeBrushText, n_snakeBrushText_X, n_snakeBrushText_Y, 140, 25)
EndSub

Sub OnSnakeDisconnected
GraphicsWindow.FontSize = 18
brt_SnakeBrushText = LDShapes.BrushText(" disconnected ", "Red", "White")
LDShapes.BrushRectangle(brt_SnakeBrushText, n_snakeBrushText_X, n_snakeBrushText_Y, 140, 25)
EndSub
'***************************