Microsoft Small Basic

Program Listing: KZX293
'Set GraphicsWindow设置图形窗口
GraphicsWindow.Title="Study_hard------Rectangle"
GraphicsWindow.Width=900
GraphicsWindow.Height=760
GraphicsWindow.Top=0
GraphicsWindow.Left=0
GraphicsWindow.CanResize="No"
GraphicsWindow.BackgroundColor="DeepSkyBlue"
GraphicsWindow.KeyUp=key
GraphicsWindow.MouseMove=usemouse
Controls.ButtonClicked=button
color[1]="Red" 'Color颜色
color[2]="Yellow" 'Color颜色
start=0 'If game start游戏是否开始
colornum[1]=1 'The number of color颜色对应数字
colornum[2]=1 'The number of color颜色对应数字
next=0
'------------------------------------------------------------------------------------------------------------------
GraphicsWindow.BrushColor="Cyan"
GraphicsWindow.FontSize=74
GraphicsWindow.FontItalic="Yes"
title=Shapes.AddText("***Rectangle***")
Shapes.Move(title,100,50)
GraphicsWindow.FontSize=24
GraphicsWindow.BrushColor="SteelBlue"
space=Shapes.AddText("Press 'Space' to continue")
Shapes.Move(space,300,500)
While next=0 'boring...消遣。。。
boring=1
EndWhile
Shapes.HideShape(title)
Shapes.HideShape(space)
GraphicsWindow.BrushColor="Purple"
GraphicsWindow.FillRectangle(0,0,500,500)
GraphicsWindow.BackgroundColor="Snow"
'------------------------------------------------------------------------------------------------------------------
GraphicsWindow.FontSize=24 'For some reason and must be put on the top of it因为某些原因必须放在最上
GraphicsWindow.BrushColor="Orange"
GraphicsWindow.DrawText(500,350,"Player 1:")
GraphicsWindow.BrushColor=color[1]
circle[1]=Shapes.AddEllipse(30,30)
Shapes.Move(circle[1],610,350)
GraphicsWindow.BrushColor="Orange"
GraphicsWindow.DrawText(500,410,"Player 2:")
GraphicsWindow.BrushColor=color[2]
circle[2]=Shapes.AddEllipse(30,30)
Shapes.Move(circle[2],610,405)
'--------------------------------------------------------------------------------------------------------------------
GraphicsWindow.BrushColor="Red"
model[1]=Shapes.AddRectangle(150,100)
model[2]=Shapes.AddRectangle(100,150)
Shapes.Move(model[1],750,0)
Shapes.Move(model[2],800,0)
Shapes.HideShape(model[2])
'------------------------------------------------------------------------------------------------------------------------
GraphicsWindow.FontSize=24
GraphicsWindow.DrawText(520,20,"The shape is:")
'-------------------------------------------------------------------------------------------------------------------
GraphicsWindow.BrushColor="Orange"
firstplayer=Math.GetRandomNumber(2)
GraphicsWindow.DrawText(520,200,"Player"+firstplayer+" first.")
turn=Shapes.AddText("It's player"+firstplayer+" turn.")
Shapes.Move(turn,520,230)
'-----------------------------------------------------------------------------------------------------------------------
giveup=Controls.AddButton("Give up放弃",520,260)
Sub button
If Controls.LastClickedButton=giveup Then
GraphicsWindow.ShowMessage("Player"+(3-firstplayer)+" won.","Win")
Program.End()
EndIf
EndSub
'-----------------------------------------------------------------------------------------------------------------------
GraphicsWindow.BrushColor="Blue"
GraphicsWindow.FontSize=100
GraphicsWindow.FontItalic="Yes"
GraphicsWindow.DrawText(20,530,"Small Basic")
'------------------------------------------------------------------------------------------------------------------------

'The help of game帮助
TextWindow.ForegroundColor="Red"
TextWindow.WriteLine("******************************")
TextWindow.WriteLine(" Play game!!")
TextWindow.WriteLine("******************************")
TextWindow.WriteLine(" ")
TextWindow.WriteLine("This a game of rectangle.")
TextWindow.WriteLine("Put 2×3 rectangle on the purple vacant squares in turn.The computer will tell you who is the first.")
TextWindow.WriteLine("Move the mouse and see it what the rectangle like and where.")
TextWindow.WriteLine("Escape----Leave")
TextWindow.WriteLine("Space----Turn the rectangle")
TextWindow.WriteLine("Up----Start the game")
TextWindow.WriteLine("A----Change the color of player1 before the game start.")
TextWindow.WriteLine("L----Change the color of player2 before the game start.")
TextWindow.WriteLine("Who can't put the rectangle in at the end,who lose,and the other won.")
TextWindow.WriteLine(" ")
TextWindow.WriteLine(" ")
TextWindow.WriteLine("这是个关于放置长方形的游戏。")
TextWindow.WriteLine("轮流在空的,紫的方格上放置2×3的长方形。电脑将告诉你谁先。")
TextWindow.WriteLine("移动鼠标看到长方形的样子及位置。")
TextWindow.WriteLine("Esc键----离开")
TextWindow.WriteLine("空格----旋转长方形")
TextWindow.WriteLine("↑键----游戏开始")
TextWindow.WriteLine("A键----在游戏开始前换玩家1的颜色")
TextWindow.WriteLine("L键----在游戏开始前换玩家2的颜色")
TextWindow.WriteLine("谁最后不能放下长方形,谁就输了。")
TextWindow.WriteLine("******************************")
TextWindow.WriteLine(" Play game!!")
TextWindow.WriteLine("******************************")
TextWindow.Title="Help帮助"
TextWindow.Show()
cannot=0 'True or false to enter Sub aroud是否能不执行Sub around
body=1 'Body of the rectangle.长方形形状
couldnot=0 'True or false to draw rectangle最终决定是否能画长方形
boring=0

'-----------------------------------------------------------------------------------------------------------
While start=0
boring=1
EndWhile
'--------------------------------------------------------------------------------------------------------
'Draw 100 squares in the window.在窗口画100个正方形
squarex=1
squarey=1
GraphicsWindow.BrushColor="Purple"
For i=1 To 10
For j=1 To 10
bered[squarey*10+squarex]=0
square[squarey*10+squarex]=Shapes.AddRectangle(50,50) 'Use value principle用到位置原理
GraphicsWindow.BrushColor=color[1]
redsquare[squarey*10+squarex]=Shapes.AddRectangle(50,50)
GraphicsWindow.BrushColor=color[2]
yellowsquare[squarey*10+squarex]=Shapes.AddRectangle(50,50)
GraphicsWindow.BrushColor="Purple"
Shapes.Move(square[squarey*10+squarex],squarex*50-50,squarey*50-50)
Shapes.Move(redsquare[squarey*10+squarex],squarex*50-50,squarey*50-50)
Shapes.Move(yellowsquare[squarey*10+squarex],squarex*50-50,squarey*50-50)
Shapes.HideShape(redsquare[squarey*10+squarex])
Shapes.HideShape(yellowsquare[squarey*10+squarex])
squarex=squarex+1
EndFor
squarex=1
squarey=squarey+1
EndFor
'-----------------------------------------------------------------------------------------------------------
GraphicsWindow.MouseDown=click
Sub click
'Get where the mouse is.鼠标位置查询
cannot=0
GraphicsWindow.Top=0
GraphicsWindow.Left=0
mousex=GraphicsWindow.MouseX
mousey=GraphicsWindow.MouseY
numx=Math.Floor(mousex/50)+1 'Get where the square is求出鼠标所在的正方形
numy=Math.Floor(mousey/50)+1
'If any problems there.是否有错误
If mousex>=500 Or mousey>=500 Then
cannot=1
GraphicsWindow.ShowMessage("Don't click out of the big square!","Wrong")
Goto true
EndIf
If (Math.Remainder(mousex,50)=0) Then
cannot=1
GraphicsWindow.ShowMessage("Don't click on the line!","Wrong")
Goto true
EndIf
If (Math.Remainder(mousey,50)=0) Then
cannot=1
GraphicsWindow.ShowMessage("Don't click on the line!","Wrong")
Goto true
EndIf
If bered[numy*10+numx]=1 Then
cannot=1
GraphicsWindow.ShowMessage("Don't put the rectangle over another.","Wrong")
Goto true
EndIf
'------------------------------------------------------------------------------------------------------------------------
'Above another or not in the square置于其它长方形上或出界
If body=1 Then
If numx=1 Or numx=10 Or numy=1 Then
GraphicsWindow.ShowMessage("Don't let any square out of the big square!","Wrong")
cannot=1
Goto true
EndIf
EndIf
If body=2 Then
If numx=1 Or numy=1 Or numy=10 Then
GraphicsWindow.ShowMessage("Don't let any square out of the big square!","Wrong")
cannot=1
Goto true
EndIf
EndIf
'---------------------------------------------------------------------------------------------------------------------------
true:
If cannot=0 Then 'Pass the conditions.通过条件
If firstplayer=1 Then
Shapes.ShowShape(redsquare[numy*10+numx]) 'Draw first先画好第一个正方形
Else
Shapes.ShowShape(yellowsquare[numy*10+numx])
EndIf
bered[numy*10+numx]=1
around()
EndIf
EndSub
'-------------------------------------------------------------------------------------------------------------
Sub around
couldnot=0 'More conditions更多条件·
If body=1 Then 'Put it over another放置重叠
If bered[numy*10+numx-11]=1 Or bered[numy*10+numx-10]=1 Or bered[numy*10+numx-9]=1 Or bered[numy*10+numx-1]=1 Or bered[numy*10+numx+1]=1 Then
If firstplayer=1 Then
Shapes.HideShape(redsquare[numy*10+numx])
Else
Shapes.HideShape(yellowsquare[numy*10+numx])
EndIf
couldnot=1
bered[numy*10+numx]=0
GraphicsWindow.ShowMessage("Don't put the rectangle over another.","Wrong")
Goto true2
EndIf
'----------------------------------------------------------------------------------------------------------------------
true2:
If couldnot=0 Then 'Pass the conditions 通过所有条件
If firstplayer=1 Then
Shapes.ShowShape(redsquare[numy*10+numx-11])
Shapes.ShowShape(redsquare[numy*10+numx-10])
Shapes.ShowShape(redsquare[numy*10+numx-9])
Shapes.ShowShape(redsquare[numy*10+numx-1])
Shapes.ShowShape(redsquare[numy*10+numx+1])
Else
Shapes.ShowShape(yellowsquare[numy*10+numx-11])
Shapes.ShowShape(yellowsquare[numy*10+numx-10])
Shapes.ShowShape(yellowsquare[numy*10+numx-9])
Shapes.ShowShape(yellowsquare[numy*10+numx-1])
Shapes.ShowShape(yellowsquare[numy*10+numx+1])
EndIf
bered[numy*10+numx-11]=1
bered[numy*10+numx-10]=1
bered[numy*10+numx-9]=1
bered[numy*10+numx-1]=1
bered[numy*10+numx+1]=1
firstplayer=3-firstplayer
Shapes.SetText(turn,"It's player"+firstplayer+" turn.") 'In turn轮流
EndIf
EndIf
'-------------------------------------------------------------------------------------------------------------------
If body=2 Then 'Put it over another放置重叠
If bered[numy*10+numx-11]=1 Or bered[numy*10+numx-10]=1 Or bered[numy*10+numx-1]=1 Or bered[numy*10+numx+9]=1 Or bered[numy*10+numx+10]=1 Then
If firstplayer=1 Then
Shapes.HideShape(redsquare[numy*10+numx])
Else
Shapes.HideShape(yellowsquare[numy*10+numx])
EndIf
couldnot=1
bered[numy*10+numx]=0
GraphicsWindow.ShowMessage("Don't put the rectangle over another.","Wrong")
Goto true3
EndIf
true3:
If couldnot=0 Then 'Pass the conditions 通过所有条件
If firstplayer=1 Then
Shapes.ShowShape(redsquare[numy*10+numx-11])
Shapes.ShowShape(redsquare[numy*10+numx-10])
Shapes.ShowShape(redsquare[numy*10+numx-1])
Shapes.ShowShape(redsquare[numy*10+numx+9])
Shapes.ShowShape(redsquare[numy*10+numx+10])
Else
Shapes.ShowShape(yellowsquare[numy*10+numx-11])
Shapes.ShowShape(yellowsquare[numy*10+numx-10])
Shapes.ShowShape(yellowsquare[numy*10+numx-1])
Shapes.ShowShape(yellowsquare[numy*10+numx+9])
Shapes.ShowShape(yellowsquare[numy*10+numx+10])
EndIf
bered[numy*10+numx-11]=1
bered[numy*10+numx-10]=1
bered[numy*10+numx-1]=1
bered[numy*10+numx+9]=1
bered[numy*10+numx+10]=1
firstplayer=3-firstplayer
Shapes.SetText(turn,"It's player"+firstplayer+" turn.") 'In turn轮流
EndIf
EndIf
EndSub
'--------------------------------------------------------------------------------------------------------------------
Sub key
If GraphicsWindow.LastKey="Space" And start=0 Then
next=1
EndIf
If GraphicsWindow.LastKey="Space" And start=1 Then
body=3-body 'Change body改变形状
Shapes.HideShape(rectangle[3-body])
Shapes.HideShape(model[3-body])
Shapes.ShowShape(rectangle[body])
Shapes.ShowShape(model[body])
GraphicsWindow.Top=0
GraphicsWindow.Left=0
mousex=GraphicsWindow.MouseX
mousey=GraphicsWindow.MouseY
numx=Math.Floor(mousex/50)+1
numy=Math.Floor(mousey/50)+1
Shapes.Move(rectangle[body],numx*50-50,numy*50-100)
EndIf
If GraphicsWindow.LastKey="A" And start=0 Then
If colornum[1]=6 Then
colornum[1]=1
Else
colornum[1]=colornum[1]+1
EndIf
If colornum[1]=1 Then 'Set the color设置颜色
color[1]="Red"
ElseIf colornum[1]=2 Then
color[1]="Yellow"
ElseIf colornum[1]=3 Then
color[1]="Orange"
ElseIf colornum[1]=4 Then
color[1]="Green"
ElseIf colornum[1]=5 Then
color[1]="Blue"
ElseIf colornum[1]=6 Then
color[1]="Violet"
EndIf
Shapes.Remove(circle[1])
GraphicsWindow.BrushColor=color[1]
circle[1]=Shapes.AddEllipse(30,30)
Shapes.Move(circle[1],610,350)
EndIf
If GraphicsWindow.LastKey="L" And start=0 Then
If colornum[2]=6 Then
colornum[2]=1
Else
colornum[2]=colornum[2]+1
EndIf
If colornum[2]=1 Then
color[2]="Red"
ElseIf colornum[2]=2 Then
color[2]="Yellow"
ElseIf colornum[2]=3 Then
color[2]="Orange"
ElseIf colornum[2]=4 Then
color[2]="Green"
ElseIf colornum[2]=5 Then
color[2]="Blue"
ElseIf colornum[2]=6 Then
color[2]="Violet"
EndIf
Shapes.Remove(circle[2])
GraphicsWindow.BrushColor=color[2]
circle[2]=Shapes.AddEllipse(30,30)
Shapes.Move(circle[2],610,405)
EndIf
If GraphicsWindow.LastKey="Up" Then
If color[1]=color[2] Then
GraphicsWindow.ShowMessage("Don't make two color same!","Wrong")
Else
start=1
EndIf
EndIf
If GraphicsWindow.LastKey="Escape" Then
Program.End()
EndIf
EndSub
'-------------------------------------------------------------------------------------------------------------------
'Move shape when mouse move当鼠标移动时移动图形
GraphicsWindow.BrushColor="Blue"
rectangle[1]=Shapes.AddRectangle(150,100)
rectangle[2]=Shapes.AddRectangle(100,150)
Shapes.HideShape(rectangle[2])
Shapes.SetOpacity(rectangle[1],80)
Shapes.SetOpacity(rectangle[2],80)
Sub usemouse
GraphicsWindow.Top=0
GraphicsWindow.Left=0
mousex=GraphicsWindow.MouseX
mousey=GraphicsWindow.MouseY
numx=Math.Floor(mousex/50)+1
numy=Math.Floor(mousey/50)+1
Shapes.Move(rectangle[body],numx*50-100,numy*50-100)
EndSub