Åben Excel, Tryk ALT F11
insæt en model.
Phase denne kode ind.
Option Explicit
Dim birdRow As Integer
Dim birdCol As Integer
Dim birdDirection As Integer
Dim birdHeight As Integer
Dim gameRunning As Boolean
Dim score As Integer
Dim obstacleCols() As Integer
Sub InitializeGame()
Dim i As Integer
' Ensure Sheet2 exists and has correct headers
If Not SheetExists("Sheet2") Then
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Sheet2"
End If
With Sheets("Sheet2")
.Cells(1, 1).Value = "Windows Username"
.Cells(1, 2).Value = "Score"
.Cells(1, 3).Value = "Time and Date"
End With
birdRow = 10
birdCol = 5
birdHeight = 10
birdDirection = 0
gameRunning = True
score = 0
' Initialize obstacles
ReDim obstacleCols(1 To 3)
For i = 1 To 3
obstacleCols(i) = 25 + (i - 1) * 10
Next i
' Set up the game board
Range("A1:Z20").Interior.ColorIndex = 0
MainGameLoop
End Sub
Sub MainGameLoop()
Dim i As Integer
Dim obstacle As Integer
Dim obstacleHeight As Integer
Do While gameRunning
' Clear the board
Range("A1:Z20").Interior.ColorIndex = 0
' Move obstacles
For i = 1 To UBound(obstacleCols)
obstacleCols(i) = obstacleCols(i) - 1
If obstacleCols(i) < 1 Then
obstacleCols(i) = 25
score = score + 1
End If
Next i
' Draw obstacles
For i = 1 To UBound(obstacleCols)
obstacle = obstacleCols(i)
obstacleHeight = WorksheetFunction.RandBetween(1, 15)
Range(Cells(1, obstacle), Cells(obstacleHeight, obstacle)).Interior.ColorIndex = 3
Range(Cells(obstacleHeight + 5, obstacle), Cells(20, obstacle)).Interior.ColorIndex = 3
Next i
' Apply gravity to bird
birdHeight = birdHeight + birdDirection
If birdDirection < 1 Then birdDirection = birdDirection + 1
' Check for collisions
If birdHeight < 1 Or birdHeight > 20 Then
GameOver
Exit Do
End If
For i = 1 To UBound(obstacleCols)
If obstacleCols(i) = birdCol Then
If birdHeight <= obstacleHeight Or birdHeight >= obstacleHeight + 5 Then
GameOver
Exit Do
End If
End If
Next i
' Draw bird
Cells(birdHeight, birdCol).Interior.ColorIndex = 6
' Update the screen
DoEvents
Delay (0.2)
Loop
End Sub
Sub FlapBird()
birdDirection = -2
End Sub
Sub GameOver()
gameRunning = False
' Get the username and current time
Dim userName As String
Dim currentTime As String
userName = Environ("Username")
currentTime = Now()
' Find the next empty row in Sheet2
Dim nextRow As Long
nextRow = Sheets("Sheet2").Cells(Sheets("Sheet2").Rows.Count, "A").End(xlUp).Row + 1
' Write the score to Sheet2
With Sheets("Sheet2")
.Cells(nextRow, 1).Value = userName
.Cells(nextRow, 2).Value = score
.Cells(nextRow, 3).Value = currentTime
End With
MsgBox "Game Over! Your score is: " & score
End Sub
Sub Delay(seconds As Single)
Dim start As Single
start = Timer
Do While Timer < start + seconds
DoEvents
Loop
End Sub
Function SheetExists(sheetName As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = ThisWorkbook.Sheets(sheetName)
SheetExists = Not ws Is Nothing
On Error GoTo 0
End Function
lav en knap der hedder "Flip" og en "Start"
og match Flip med modulet: sub FlapBird
og Start med modulet: SUB InitializeGame
--
"Signatur fjernet af moderator i samarbejde med Dalai Lama, Anders Fogh, Bill Gates, Marilyn Manson og Michael Moore"