Microsoft Small Basic

Program Listing: TFP475
' ふらふらUFO
init()
While "true"
start()
gameloop()
gameover()
EndWhile

' 初期設定
Sub init
GraphicsWindow.Width = 640
GraphicsWindow.Height = 480
GraphicsWindow.CanResize = "false"
GraphicsWindow.BackgroundColor = "#8b4513" ' 背景色
dir = Program.Directory + "\"
' 谷
vw = 300 ' 幅
vh = 20 ' 高さ
GraphicsWindow.BrushColor = "#ffebcd"
GraphicsWindow.PenColor = "#ffebcd"
For i=0 To 23
valley[i] = Shapes.AddRectangle(vw,vh)
EndFor
' 自機(UFO)
pw = 100 ' 幅
ph = 55 ' 高さ
player = Shapes.AddImage(dir+"p_ufo.png")

' スコア
GraphicsWindow.FontSize = 30
GraphicsWindow.BrushColor = "#2E9AFE"
sctext = Shapes.AddText("SCORE:0")
Shapes.Move(sctext, 20,8)

' ゲームオーバー
GraphicsWindow.FontSize = 72
GraphicsWindow.BrushColor = "#000000"
msg2 = Shapes.AddText("GAME OVER")
Shapes.Move(msg2, 115,175)
Shapes.HideShape(msg2)
GraphicsWindow.BrushColor = "#01DF3A"
msg1 = Shapes.AddText("GAME OVER")
Shapes.Move(msg1, 110,170)
Shapes.HideShape(msg1)

' PUSH SPACE KEY
GraphicsWindow.FontSize = 30
GraphicsWindow.BrushColor = "#FF8000"
pushmsg = Shapes.AddText("PUSH SPACE KEY")
Shapes.Move(pushmsg, 200,330)
Shapes.HideShape(pushmsg)
EndSub

' プレイ前の準備
Sub start
score = 0
movekey = "" ' 入力情報の初期化
' 自機の初期配置
px = 300
py = 100
pycnt = py *100 ' y座標計算用
Shapes.Rotate(player,0) ' 絵の角度を初期化
' 谷の初期配置
For i=0 To 23
vx[i] = 170
vy[i] = i*20
EndFor
state = "play"
EndSub

Sub gameloop
vcnt = 0 ' 谷を左右に動かすタイミング用
While state = "play"
score = score +1
Shapes.SetText(sctext,"SCORE:"+score)
valleymove() ' 谷を動かす

inkey = GraphicsWindow.LastKey
' 左右キーのみ受け付ける
If inkey = "Right" Or inkey = "Left" Then
movekey = inkey
EndIf
' 自機を少しずつ下げていく
pycnt = pycnt +10
py = pycnt /100
If py > 350 Then ' 下げすぎないように制限する
py = 350
EndIf
' 自機の移動
If movekey = "Right" Then
px = px +10
EndIf
If movekey = "Left" Then
px = px -10
EndIf
Shapes.Move(player,px,py)
' 谷と自機の当たり判定の対象を選ぶ
For i=0 To 23
If py < vy[i] And (py+ph) > vy[i] Then
hitcheck()
ElseIf py < (vy[i]+20) And (py+ph) > (vy[i]+20) Then
hitcheck()
EndIf
EndFor

' 30 fps で動かす
Program.Delay(1000/30)
EndWhile
EndSub

' 谷と自機の当たり判定
Sub hitcheck
If px < vx[i] Or (px+pw) > (vx[i]+vw) Then
state = "gameover"
EndIf
EndSub

' 谷を動かす
Sub valleymove
vcnt = vcnt +1
' 一番下の四角を左右どちらかに動かす
If vcnt > 4 Then
rnd = (Math.GetRandomNumber(3) -2)*10
vcnt = 0
EndIf
vx[23] = vx[23] +rnd
If vx[23] < 10 Then
vx[23] = 10
rnd = -rnd
vcnt = 0
EndIf
If vx[23] > 330 Then
vx[23] = 330
rnd = -rnd
vcnt = 0
EndIf

' 谷の移動
For i=0 To 23
If i < 23 Then
vx[i] = vx[i+1]
EndIf
Shapes.Move(valley[i],vx[i],vy[i])
EndFor
EndSub

Sub gameover
mcnt = 0
vec = 1 ' 移動の向き 1:右へ -1:左へ
' 自機が画面の右側にいるときは左向きをセット
If px > (GraphicsWindow.Width /2) Then
vec = -1
EndIf

While state = "gameover"
mcnt = mcnt +10
' 自機のやられ演出
If mcnt < 720 Then
psin = Math.Sin(Math.Pi*mcnt/180)*100
Shapes.Move(player,px+(mcnt*vec), py-psin)
Shapes.Rotate(player,mcnt)
EndIf
If mcnt = 600 Then ' GAME OVER 表示
Shapes.ShowShape(msg1)
Shapes.ShowShape(msg2)
EndIf
If mcnt > 1200 Then ' PUSH SPACE KEY の表示
If Math.Remainder(mcnt,600) < 400 Then
Shapes.ShowShape(pushmsg)
Else
Shapes.HideShape(pushmsg)
EndIf
If GraphicsWindow.LastKey = "Space" Then
state = "start"
EndIf
EndIf

Program.Delay(1000/30)
EndWhile
' 表示したメッセージを隠す
Shapes.HideShape(pushmsg)
Shapes.HideShape(msg1)
Shapes.HideShape(msg2)
EndSub