Making a game with animation frames in Excel

Submitted by MisterBeck on Mon, 05/18/2020 - 14:24

Why might you make a game Excel? For learning and for fun. So let's get to it.

The Beginnings of a Game.

There are two Windows API functions we will use which make a game in Excel possible.

#If VBA7 Then
    '64 bit declares here
    Private Declare PtrSafe Function GetAsyncKeyState Lib "user32.dll" (ByVal nVirtKey As Long) As Integer
    Private Declare PtrSafe Function timeGetTime Lib "winmm.dll" () As Long
#Else
    '32 bit declares here
    Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal nVirtKey As Long) As Integer
    Private Declare Function timeGetTime Lib "winmm.dll" () As Long
#End If

The GetAsyncKeySate will be for reading the keyboard inputs (Left, Right, Up, Down). timeGetTime will be used for the game counter to iterate frames. That's all you really need. Everything else can be down in VBA pure. Next we come to the game loop. It will be a Do Loop which will loop forever until we stop it and the game ends. Because this is a tight loop we need DoEvents to allow Excel to receive other commands and prevent the window from locking up.
 

Do
    DoEvents

    'Game code goes here.
Loop

 

Now we need a way to control and time updating the game state. timeGetTime is the answer. It returns the current time in milliseconds since boot time. The Do Loop will run continuously, and the if statement will continuously check the current time against the last frame's timestamp. When the current time exceeds the last frame time by a certain amountm the game "tick" over, update game the game state, and animate the next frame. I chose 50 milliseconds arbitrarily. Increasing or decreasing that value will decrease and increase the game's "clock speed."

'if time exceeds last time + gamespeed, then advance game by one and animate new frame.
If timeGetTime - lastFrameTime > 50 Then        
    'Game code goes here
End if


Now the game code itself. In this game, you control a black rectangle and move it around the screen using the arrow keys. Nice, right? Less of a game than Pong.

The game logic is very simple.

1) check if an arrow key is pressed.
2) if so, move a colored cell in that direction
3) repeat

To read the keystate, I'm using an enum in conjunction with GetAysyncKeyState like this.

Private Enum Direction
    None = 0
    Up = 1
    Down
    Left
    Right
End Enum

Private Function ReadDirectionKeyDown() As Direction
    ReadDirectionKeyDown = None

    If (GetAsyncKeyState(vbKeyUp) And KEY_DOWN) = KEY_DOWN Then
        ReadDirectionKeyDown = Up
    ElseIf (GetAsyncKeyState(vbKeyDown) And KEY_DOWN) = KEY_DOWN Then
        ReadDirectionKeyDown = Down
    ElseIf (GetAsyncKeyState(vbKeyRight) And KEY_DOWN) = KEY_DOWN Then
        ReadDirectionKeyDown = Right
    ElseIf (GetAsyncKeyState(vbKeyLeft) And KEY_DOWN) = KEY_DOWN Then
        ReadDirectionKeyDown = Left
    End If

End Function


Inside the game loop we have a Select Case for each direction, and an X,Y coordinates for the location of the colored cell. Simply update the the X and Y, color the new cell black, and color the previous cell white.

Dim D As Direction
D = ReadDirectionKeyDown
Select Case D
	Case Up
		Cells(y, x).Interior.ColorIndex = -4142
		y = y - 1
		Cells(y, x).Interior.ColorIndex = 1
	Case Down
		Cells(y, x).Interior.ColorIndex = -4142
		y = y + 1
		Cells(y, x).Interior.ColorIndex = 1
	Case Left
		Cells(y, x).Interior.ColorIndex = -4142
		x = x - 1
		Cells(y, x).Interior.ColorIndex = 1
	Case Right
		Cells(y, x).Interior.ColorIndex = -4142
		x = x + 1
		Cells(y, x).Interior.ColorIndex = 1
End Select

The End. You have a "game." Ok, not a full game, but you have a controllable character in a space. Here's the entire module.

Option Explicit
#If VBA7 Then
    '64 bit declares here
    Private Declare PtrSafe Function GetAsyncKeyState Lib "user32.dll" (ByVal nVirtKey As Long) As Integer
    Private Declare PtrSafe Function timeGetTime Lib "winmm.dll" () As Long
#Else
    '32 bit declares here
    Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal nVirtKey As Long) As Integer
    Private Declare Function timeGetTime Lib "winmm.dll" () As Long
#End If


Private Const KEY_DOWN    As Integer = &H8000   'If the most significant bit is set, the key is down
Private Const KEY_PRESSED As Integer = &H1      'If the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState

Private Enum Direction
    None = 0
    Up = 1
    Down
    Left
    Right
End Enum

Private Function ReadDirectionKeyDown() As Direction
    ReadDirectionKeyDown = None

    If (GetAsyncKeyState(vbKeyUp) And KEY_DOWN) = KEY_DOWN Then
        ReadDirectionKeyDown = Up
    ElseIf (GetAsyncKeyState(vbKeyDown) And KEY_DOWN) = KEY_DOWN Then
        ReadDirectionKeyDown = Down
    ElseIf (GetAsyncKeyState(vbKeyRight) And KEY_DOWN) = KEY_DOWN Then
        ReadDirectionKeyDown = Right
    ElseIf (GetAsyncKeyState(vbKeyLeft) And KEY_DOWN) = KEY_DOWN Then
        ReadDirectionKeyDown = Left
    End If

End Function


Sub Game()

    Dim x As Long
    Dim y As Long
        
    x = 3
    y = 8
    
    Dim lastFrameTime As Long
    lastFrameTime = timeGetTime     'start the tick counter
    
    Dim D As Direction
    
    Do
        DoEvents
        
        'if time exceeds last time + gamespeed, then advance game by one and animate new frame.
        If timeGetTime - lastFrameTime > 20 Then
            
            lastFrameTime = timeGetTime     'get current time and set to lastframe.
            
            'All game code goes here.
            '*********************************
            
            D = ReadDirectionKeyDown
            
            Select Case D
            
                Case Up
                    Cells(y, x).Interior.ColorIndex = -4142
                    y = y - 1
                    Cells(y, x).Interior.ColorIndex = 1
                Case Down
                    Cells(y, x).Interior.ColorIndex = -4142
                    y = y + 1
                    Cells(y, x).Interior.ColorIndex = 1
                Case Left
                    Cells(y, x).Interior.ColorIndex = -4142
                    x = x - 1
                    Cells(y, x).Interior.ColorIndex = 1
                Case Right
                    Cells(y, x).Interior.ColorIndex = -4142
                    x = x + 1
                    Cells(y, x).Interior.ColorIndex = 1
                
            End Select
            
            '*********************************
        End If
        
    Loop
    
End Sub

 

Comments

<a href="http://www.biketw.com/nike-air-max-97-womens-uk-billig-runsc">nike air max 97 womens uk billig</a> <a href="http://www.saunasmith.com/adidas-equipment-eqt-support-boost-up-billig-… equipment eqt support boost up billig</a> <a href="http://www.cironajle.com/black-chicago-bulls-snapback-for-sale-capb">bl… chicago bulls snapback for sale</a> <a href="http://www.stephenemay.com/air-jordan-6-30th-anniversary-booto">air jordan 6 30th anniversary</a> <a href="http://www.qubacafe.com/mitchell-and-ness-philadelphia-eagles-reggie-wh… and ness philadelphia eagles reggie white 92 green authentic jersey sale</a> <a href="http://www.sdadhd.com/create-your-own-soccer-uniform-nfld">create your own soccer uniform</a>
[url=http://www.symbioo.net/]symbioo[/url]

<a href="http://www.grasslandip.com/nike-air-huarache-utility-premium-green-obuv… air huarache utility premium green</a> <a href="http://www.kabatecnico.com/badyfur-moncler0">badyfur</a&gt; <a href="http://www.techieva.com/adidas-superstar-nero-very-booto">adidas superstar nero very</a> <a href="http://www.segurosblog.com/nike-patriots-10-jimmy-garoppolo-red-alterna… patriots 10 jimmy garoppolo red alternate super bowl li 51 mens stitched nfl elite jersey</a> <a href="http://www.gestdom.com/nike-philadelphia-eagles-65-lane-johnson-midnigh… philadelphia eagles 65 lane johnson midnight green team color mens stitched nfl elite jersey</a> <a href="http://www.envirofilt.com/new-balance-1500-all-white-runso">new balance 1500 all white</a>
[url=http://www.gmcoftulsa.com/]gmcoftulsa[/url]

<a href="http://www.rehema.net/authentic-franco-harris-mens-throwback-jersey-pit… franco harris mens throwback jersey pittsburgh steelers 32 road white mitchell and ness nfl</a> <a href="http://www.knowmorris.com/moncler-daos-monclerc">moncler daos</a> <a href="http://www.oncopdl.net/best-places-for-oversized-graphic-tees-poloe">be… places for oversized graphic tees</a> <a href="http://www.sitool.net/adidas-superstar-slip-on-core-black-white-obuvuk"… superstar slip on core black white</a> <a href="http://www.docinsider.net/harga-nike-flyknit-air-max-premium-quality-bi… nike flyknit air max premium quality billig</a> <a href="http://www.fotowerft.net/nike-knit-hat-sale-discount-hath">nike knit hat sale discount</a>
[url=http://www.loveliteuk.com/]loveliteuk[/url]

<a href="http://www.bjgruppen.com/palm-print-maxi-dress-dressq">palm print maxi dress</a> <a href="http://www.bucumarket.com/adidas-nmd-todas-negro-todas-negro-booty">adi… nmd todas negro todas negro</a> <a href="http://www.baltiktur.com/where-can-i-order-the-jordan-retro-12-boot0">w… can i order the jordan retro 12</a> <a href="http://www.skwpartners.com/women-nike-washington-redskins-98-brian-orak… nike washington redskins 98 brian orakpo game burgundy red number alternate 80th anniversary nfl jersey sale</a> <a href="http://www.asecondmind.com/nike-free-run-maschio-a-buon-mercato-schuhew… free run maschio a buon mercato</a> <a href="http://www.ascarov.com/nike-hommes-noir-air-endurance-fonctionnement-ch… hommes noir air endurance fonctionnement chaussures</a>
[url=http://www.calmission.com/]calmission[/url]

<a href="http://www.rehema.net/authentic-franco-harris-mens-throwback-jersey-pit… franco harris mens throwback jersey pittsburgh steelers 32 road white mitchell and ness nfl</a> <a href="http://www.knowmorris.com/moncler-daos-monclerc">moncler daos</a> <a href="http://www.oncopdl.net/best-places-for-oversized-graphic-tees-poloe">be… places for oversized graphic tees</a> <a href="http://www.sitool.net/adidas-superstar-slip-on-core-black-white-obuvuk"… superstar slip on core black white</a> <a href="http://www.docinsider.net/harga-nike-flyknit-air-max-premium-quality-bi… nike flyknit air max premium quality billig</a> <a href="http://www.fotowerft.net/nike-knit-hat-sale-discount-hath">nike knit hat sale discount</a>
[url=http://www.chapacms.com/]chapacms[/url]

<a href="http://www.aptarismrm.com/jordan-13-svart-and-grey-zapatost">jordan 13 svart and grey</a> <a href="http://www.azoils.com/jordan-mid-blu-runsw">jordan mid blu</a> <a href="http://www.wunderboys.com/nhl-jerseys-buffalo-sabres-jerseys-30-ryan-mi… jerseys buffalo sabres jerseys 30 ryan miller away white jersey</a> <a href="http://www.requeil.com/nike-browns-34-isaiah-crowell-black-womens-nfl-f… browns 34 isaiah crowell black womens nfl fashion game jersey</a> <a href="http://www.energistech.com/knit-baby-hat-with-bunny-ears-pattern-capu">… baby hat with bunny ears pattern</a> <a href="http://www.earcandyllc.com/willie-stargell-8-black-authentic-jersey-sal… stargell 8 black authentic jersey sale</a>
[url=http://www.jbtbrains.com/]jbtbrains[/url]

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.