前回作画したクラシック F-1 レーシングカーを動かしてみました。 GraphicsWindow.DrawEllipse、GraphicsWindow.FillEllipse など描画のための操作を DrawEllipse、FillEllipse などのサブルーチンに置き換え、その中で Shapes.AddEllipse のように Shapes オブジェクトの操作で作図し直し、それを Timer オブジェクトのイベントで動かしています。
Small Basic オンラインで実行したスクリーンショットを示します。
前回からの変更点について説明します。下記のプログラムを Small Basic オンライン (SBO) のエディタ画面にコピー&ペーストすれば実行できます。なお、 Small Basic デスクトップ (SBD) のエディタ画面にコピー&ペーストしても実行できますが、 DrawEllipse 呼び出される Shapes.AddEllipse の結果がちょっと小さめに なります。また、SBD のほうがスムーズに動きます。
' Classic F-1
' Version 0.2.0
' Copyright © 2020 Nonki Takahshi. The MIT License.
' Last update 2020-08-26
carLength = 530
DrawGrid()
' ground
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.FillRectangle(0, 400, gw, gh - 400)
' driver
xo = 380
yo = 280
size = 50
DrawDriver()
' cockpit
GraphicsWindow.PenColor = "Silver"
GraphicsWindow.PenWidth = 20
x1 = 490
y1 = 320
x2 = 580
y2 = 310
DrawLine()
GraphicsWindow.BrushColor = "DarkGreen"
x1 = 290
y1 = 310
x2 = 330
y2 = 290
x3 = 340
y3 = 310
FillTriangle()
GraphicsWindow.BrushColor = "Black"
x = 410
y = 260
width = 20
height = 40
FillEllipse()
y = y + height
FillEllipse()
GraphicsWindow.BrushColor = "Silver"
x = 420
y = 255
width = 10
height = 10
FillEllipse()
y = 260
height = 60
FillRectangle()
GraphicsWindow.BrushColor = "DarkGreen"
x = 440
y = 270
width = 60
height = 100
FillEllipse()
x1 = 430
y1 = 260
x2 = 430
y2 = 350
x3 = 470
y3 = 270
FillTriangle()
' body
GraphicsWindow.BrushColor = "DarkGreen"
x = 50
y = 310
width = 480
height = 80
FillEllipse()
x = x + width / 2
width = 150
FillRectangle()
x = 340
y = 310
width = 200
FillEllipse()
' tire
xo = 200
yo = 350
size = 100
DrawTire()
xo = 500
DrawTire()
' number
xo = 370
yo = 345
size = 70
DrawNumber()
Timer.Interval = 200
Timer.Tick = MoveCar
Sub DrawDriver
' helmet
GraphicsWindow.BrushColor = "Gold"
x = xo - size / 2
y = yo - size / 2
width = size
height = size
FillEllipse()
GraphicsWindow.BrushColor = "DarkGreen"
x = xo - size * 0.4
y = yo + size / 2
width = size * 0.8
FillEllipse()
y = yo + size
height = size / 2
FillRectangle()
GraphicsWindow.BrushColor = "Peru"
' nose
x = xo - size * 0.5
y = yo
width = size * 0.2
height = size * 0.2
FillRectangle()
x1 = x
y1 = y
x2 = xo - size * 0.6
y2 = yo + size * 0.25
x3 = xo - size * 0.4
y3 = y2
FillTriangle()
' face
x = xo - size * 0.5
y = yo
width = size * 0.2
height = size * 0.5
FillEllipse()
x = xo - size * 0.4
width = size * 0.4
FillRectangle()
' helmet (side)
GraphicsWindow.BrushColor = "Gold"
x = xo - size * 0.2
y = yo - size / 2
width = size * 0.4
height = size
FillEllipse()
' helmet peak
GraphicsWindow.BrushColor = "Yellow"
x1 = xo - size * 0.45
y1 = yo - size * 0.2
x2 = xo - size * 0.7
y2 = yo
x3 = xo - size * 0.1
y3 = yo
FillTriangle()
EndSub
Sub DrawEllipse
' GraphicsWindow.DrawEllipse(x, y, width, height)
GraphicsWindow.BrushColor = "Transparent"
nShp = nShp + 1
shp[nShp] = Shapes.AddEllipse(width, height)
Shapes.Move(shp[nShp], x, y)
EndSub
Sub DrawGrid
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
fn = GraphicsWindow.FontName
If (fn = "Tahoma") Or (fn = "Segoe UI") Then
c10 = "#33000000"
c100 = "#66000000"
bc = "#CC000000"
Else ' for SBO
c10 = "#00000033"
c100 = "#00000066"
bc = "#000000CC"
EndIf
GraphicsWindow.FontName = "Courier New"
GraphicsWindow.FontSize = 12
GraphicsWindow.BrushColor = bc
For x = 0 To gw Step 10
If Math.Remainder(x, 100) = 0 Then
GraphicsWindow.PenColor = c100
GraphicsWindow.DrawText(x + 2, 0, x)
Else
GraphicsWindow.PenColor = c10
EndIf
GraphicsWindow.DrawLine(x, 0, x, gh)
EndFor
For y = 0 To gh Step 10
If Math.Remainder(y, 100) = 0 Then
GraphicsWindow.PenColor = c100
GraphicsWindow.DrawText(2, y, y)
Else
GraphicsWindow.PenColor = c10
EndIf
GraphicsWindow.DrawLine(0, y, gw, y)
EndFor
EndSub
Sub DrawLine
' GraphicsWindow.DrawLine(x1, y1, x2, y2)
nShp = nShp + 1
shp[nShp] = Shapes.AddLine(x1, y1, x2, y2)
EndSub
Sub DrawNumber
GraphicsWindow.BrushColor = "White"
x = xo - size / 2
y = yo - size / 2
width = size
height = size
FillEllipse()
_size = size * 0.8
GraphicsWindow.FontName = "Arial"
GraphicsWindow.FontSize = _size
GraphicsWindow.BrushColor = "Black"
x = xo - _size * 0.27
y = yo - _size * 0.65
txt = "8"
DrawText()
EndSub
Sub DrawText
' GraphicsWindow.DrawText(x, y, txt)
nShp = nShp + 1
shp[nShp] = Shapes.AddText(txt)
Shapes.Move(shp[nShp], x, y)
EndSub
Sub DrawTire
GraphicsWindow.BrushColor = "#222222"
x = xo - size / 2
y = yo - size / 2
width = size
height = size
FillEllipse()
_size = size * 0.5
GraphicsWindow.BrushColor = "Gold"
x = xo - _size / 2
y = yo - _size / 2
width = _size
height = _size
FillEllipse()
GraphicsWindow.PenWidth = size * 0.1
GraphicsWindow.PenColor = "Silver"
DrawEllipse()
EndSub
Sub FillEllipse
' GraphicsWindow.FillEllipse(x, y, width, height)
GraphicsWindow.PenWidth = 0
nShp = nShp + 1
shp[nShp] = Shapes.AddEllipse(width, height)
Shapes.Move(shp[nShp], x, y)
EndSub
Sub FillRectangle
' GraphicsWindow.FillRectangle(x, y, width, height)
GraphicsWindow.PenWidth = 0
nShp = nShp + 1
shp[nShp] = Shapes.AddRectangle(width, height)
Shapes.Move(shp[nShp], x, y)
EndSub
Sub FillTriangle
' GraphicsWindow.FillTriangle(x1, y1, x2, y2, x3, y3)
GraphicsWindow.PenWidth = 0
nShp = nShp + 1
shp[nShp] = Shapes.AddTriangle(x1, y1, x2, y2, x3, y3)
EndSub
Sub MoveCar
If Shapes.GetLeft(shp[18]) < -carLength Then
dx = gw + carLength
Else
dx = -50
EndIf
For i = 1 To nShp
x = Shapes.GetLeft(shp[i]) + dx
y = Shapes.GetTop(shp[i])
Shapes.Move(shp[i], x, y)
EndFor
EndSub