表示調整
閉じる
挿絵表示切替ボタン
▼配色
▼行間
▼文字サイズ
▼メニューバー
×閉じる

ブックマークに追加しました

設定
設定を保存しました
エラーが発生しました
※文字以内
ブックマークを解除しました。

エラーが発生しました。

エラーの原因がわからない場合はヘルプセンターをご確認ください。

ブックマーク機能を使うにはログインしてください。
<R15>15歳未満の方は移動してください。

ExcelVBA;GAME Fall Down Panic 〜 落下物を避けて生き延びよ!

作者: 安永祐二
掲載日:2025/10/18


懐かしのレトロゲーム風の単純サバイバルGAMEのコード・プログラムです。




【コード・プログラム】



' ExcelVBA 落下物を避けて生き延びよ!

Dim posX As Integer

Dim posY As Integer

Dim charSymbol As String

Dim gameOver As Boolean

Dim fallObjects As Collection

Dim gameRange As Range

Dim nextTick As Double


Sub StartGame()

Dim ws As Worksheet

Set ws = ActiveSheet

ws.Cells.Clear

Set gameRange = ws.Range("B2:K11") ' 10列×10行

With gameRange

.Interior.Color = vbBlack

.Font.Color = vbGreen

.Font.Bold = True

.Font.Size = 14

.HorizontalAlignment = xlCenter

.VerticalAlignment = xlCenter

End With


posX = 5: posY = 10 ' 中央下

charSymbol = "大"

gameOver = False

Set fallObjects = New Collection


Application.OnKey "{LEFT}", "MoveLeft"

Application.OnKey "{RIGHT}", "MoveRight"


DrawScene

ScheduleNextTick

End Sub


Sub ScheduleNextTick()

If Not gameOver Then

nextTick = Now + TimeSerial(0, 0, 0.6)

Application.OnTime nextTick, "GameTick"

End If

End Sub


Sub GameTick()

MoveObjectsDown

If fallObjects.Count < 3 Then SpawnObject

CheckCollision

DrawScene

ScheduleNextTick

End Sub


Sub DrawScene()

gameRange.ClearContents


' 落下物描画

Dim i As Integer

For i = 1 To fallObjects.Count

Dim coords As Variant: coords = fallObjects(i)

If IsArray(coords) And UBound(coords) = 1 Then

Dim fx As Integer: fx = coords(0)

Dim fy As Integer: fy = coords(1)

If fy >= 1 And fy <= gameRange.Rows.Count Then

gameRange.Cells(fy, fx).Value = "■"

End If

End If

Next


' キャラ描画

gameRange.Cells(posY, posX).Value = charSymbol

End Sub


Sub MoveLeft()

If posX > 1 Then

posX = posX - 1

ToggleCharSymbol

DrawScene

End If

End Sub


Sub MoveRight()

If posX < gameRange.Columns.Count Then

posX = posX + 1

ToggleCharSymbol

DrawScene

End If

End Sub


Sub ToggleCharSymbol()

If charSymbol = "大" Then

charSymbol = "火"

Else

charSymbol = "大"

End If

End Sub


Sub SpawnObject()

Dim col As Integer: col = Int(Rnd() * gameRange.Columns.Count) + 1

fallObjects.Add Array(col, 1) ' X, Y

End Sub


Sub MoveObjectsDown()

Dim newFall As New Collection

Dim i As Integer

For i = 1 To fallObjects.Count

Dim coords As Variant: coords = fallObjects(i)

If IsArray(coords) And UBound(coords) = 1 Then

Dim fx As Integer: fx = coords(0)

Dim fy As Integer: fy = coords(1)

If fy < gameRange.Rows.Count Then

newFall.Add Array(fx, fy + 1)

End If

End If

Next

Set fallObjects = newFall

End Sub


Sub CheckCollision()

Dim i As Integer

For i = 1 To fallObjects.Count

Dim coords As Variant: coords = fallObjects(i)

If IsArray(coords) And UBound(coords) = 1 Then

If coords(0) = posX And coords(1) = posY Then

gameOver = True

MsgBox "GAME OVER", vbCritical

Application.OnKey "{LEFT}"

Application.OnKey "{RIGHT}"

Exit For

End If

End If

Next

End Sub



挿絵(By みてみん)





【実際に動作しているところの動画】


https://youtube.com/shorts/-k7jbHN8j-c?si=WutFfXk1BDGtupG4



評価をするにはログインしてください。
ブックマークに追加
ブックマーク機能を使うにはログインしてください。
― 新着の感想 ―
感想はまだ書かれていません。
感想一覧
+注意+

特に記載なき場合、掲載されている作品はすべてフィクションであり実在の人物・団体等とは一切関係ありません。
特に記載なき場合、掲載されている作品の著作権は作者にあります(一部作品除く)。
作者以外の方による作品の引用を超える無断転載は禁止しており、行った場合、著作権法の違反となります。

↑ページトップへ