Примери за интерактивност, реализирани в средата
1. Графичен редактор
Въвеждайки следния код вие ще имате на разположение прост, графичен редактор. С него ще можете да рисувате свободно, с мишката, както и да чертаете, завъртате и увеличавате три фигури - квадрат, триъгълник и окръжност:
GraphicsWindow.Hide()
w = 620
h = 450
GraphicsWindow.CanResize = "False"
GraphicsWindow.Width = w
GraphicsWindow.Height = h
GraphicsWindow.Top = (Desktop.Height-h) / 2
GraphicsWindow.Left = (Desktop.Width-w) / 2
GraphicsWindow.Show()
GraphicsWindow.Title = "Събития и интерактивност"
GUI()
Controls.ButtonClicked = MouseAction
Sub GUI
GraphicsWindow.DrawRectangle(10, 10, 600, 320)
GraphicsWindow.DrawRectangle(10, 340, 200, 100)
GraphicsWindow.DrawRectangle(10, 340, 600, 100)
GraphicsWindow.DrawRectangle(370, 340, 150, 100)
Triangle = Controls.AddButton("Триъгълник", 40, 345)
Controls.SetSize(Triangle, 120, 30)
Rectangle = Controls.AddButton("Правоъгълник",40,375)
Controls.SetSize(Rectangle, 120, 30)
Circle = Controls.AddButton("Окръжност", 40, 405)
Controls.SetSize(Circle, 120, 30)
Rotate = Controls.AddButton("Завърти", 230, 360)
Controls.SetSize(Rotate, 65, 60)
Zoom = Controls.AddButton("Увеличи", 290, 360)
Controls.SetSize(Zoom, 65, 60)
FreeHand = Controls.AddButton("Рисувай", 390, 360)
Controls.SetSize(FreeHand, 65, 60)
Clear = Controls.AddButton("Изчисти", 450, 360)
Controls.SetSize(Clear, 65, 60)
Exit = Controls.AddButton("Изход", 530, 360)
Controls.SetSize(Exit, 65, 60)
EndSub
Sub MouseAction
x = GraphicsWindow.MouseX
y = GraphicsWindow.MouseY
GraphicsWindow.PenWidth = 1
If x > 40 And x < 160 Then
GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
If y > 345 And y < 375 Then
draw = 0
j = 0
z = 1
tri = Shapes.AddTriangle(20, 20, 100, 100, 150, 20)
Shapes.Move(tri, 80, 100)
EndIf
If y > 375 And y < 405 Then
draw = 0
j = 0
z = 2
rect = Shapes.AddRectangle(100, 100)
Shapes.Move(rect, 250, 150)
EndIf
If y > 405 And y < 435 Then
draw = 0
j = 0
z = 3
circ = Shapes.AddEllipse(100, 100)
Shapes.Move(circ, 400, 150)
EndIf
EndIf
If y > 360 And y < 420 Then
If x > 230 And x < 290 Then
draw = 0
If z = 1 Then
Shapes.Rotate(tri, 30 + m)
Else
If z = 2 Then
Shapes.Rotate(rect,30 + m)
Else
If z = 3 Then
Shapes.Rotate(circ, 30 + m)
Endif
Endif
Endif
m = m + 30
EndIf
If x > 290 And x < 390 Then
draw = 0
i = 0.1 + j
If i < 0.4 Then
If z = 1 Then
Shapes.Zoom(tri, 1 + i, 1 + i)
Else
If z = 2 Then
Shapes.Zoom(rect, 1 + i, 1 + i)
Else
If z = 3 Then
Shapes.Zoom(circ, 1 + i, 1 + i)
EndIf
EndIf
EndIf
j = j + 0.1
EndIf
EndIf
If x > 390 And x < 450 Then
draw = 1
Paint()
EndIf
If x > 450 And x < 510 Then
draw = 0
j = 0
GraphicsWindow.Clear()
GraphicsWindow.BrushColor = "Blue"
GUI()
draw = 0
EndIf
If x > 530 And x < 590 Then
draw = 0
Program.End()
EndIf
EndIf
EndSub
Sub Paint
If draw = 1 Then
GraphicsWindow.MouseMove = MouseDrag
Else
If Mouse.IsLeftButtonDown Then
MouseAction()
EndIf
EndIf
EndSub
Sub MouseDrag
If draw = 1 then
x = GraphicsWindow.MouseX
y = GraphicsWindow.MouseY
If x > 10 And x < 600 And y > 10 And y < 320 Then
If Mouse.IsLeftButtonDown Then
GraphicsWindow.DrawLine(OrgX, OrgY, x, y)
EndIf
EndIf
OrgX = x
OrgY = y
EndIf
EndSub
Резултат от работа с редактора:
2. Движение на НЛО
Графичното изображение на НЛО ще определим по следната схема:
Обектът ще се състои от елипса в която има 3 окръжности, разположени на една линия, на равни разстояние една от друга.
Обектът се извежда в графичния прозорец с помощта на процедура.
схема за определяне на координатите на точките на горен ляв връх на фигурните.
Графичния обект се движи по хоризонтала и като стигне най-дясната позиция се появява отново от ляво.
Ето и самия програмен код:
' НЛО"
' Размер обекта
w = 40
h = 20
' Начално положение na обекта
x = -w
y = 100
dx = 2
bColor = "DarkBlue" ' цвят на фона
GraphicsWindow.BrushColor = bColor
GraphicsWindow.FillRectangle(0,0,GraphicsWindow.Width,GraphicsWindow.Height)
' Звезди
GraphicsWindow.PenColor = "White"
For i=1 To 100
sx = Math.GetRandomNumber(GraphicsWindow.Width)
sy = Math.GetRandomNumber(GraphicsWindow.Height)
' Диаметър на звездите : 1 или 2
d = Math.GetRandomNumber(2)
If ( d = 2) Then
GraphicsWindow.DrawEllipse(sx,sy,2,2)
Else
GraphicsWindow.SetPixel(sx,sy,"White")
EndIf
EndFor
GraphicsWindow.PenWidth = 1
' Настройка на таймера
Timer.Interval = 50
Timer.Tick = onTimer '
' Процедура за обработка на сигнала от таймера
Sub onTimer
' Изтриване на пътя на обекта
GraphicsWindow.BrushColor = bColor
GraphicsWindow.FillRectangle(x-1,y-1,w+2,h+2)
'ново положение на обекта
x = x + dx
If (x > GraphicsWindow.Width) Then
x= -w
EndIf
NLO() ' обръщане към процедура NLO, която рисува обекта
EndSub
Sub NLO
' Корпус
GraphicsWindow.BrushColor = "Silver"
GraphicsWindow.PenColor = "Black"
GraphicsWindow.FillEllipse(x,y,w,h)
GraphicsWindow.DrawEllipse(x,y,w,h)
' Прозорчета
GraphicsWindow.BrushColor = "Yellow"
GraphicsWindow.FillEllipse(x+10,y+7,5,5)
GraphicsWindow.DrawEllipse(x+10,y+7,5,5)
GraphicsWindow.FillEllipse(x+17,y+7, 5,5)
GraphicsWindow.DrawEllipse(x+17,y+7,5,5)
GraphicsWindow.FillEllipse(x+24,y+7,5,5)
GraphicsWindow.DrawEllipse(x+24,y+7,5,5)
EndSub
А резултатът от изпълнението: