Excel Snake Game VBA Macro (Advanced) Part 2/2
Summary
TLDRIn this tutorial, the process of creating a simple Snake game using VBA macros in Excel is explained. The game involves adding a randomized apple to the grid, detecting when the snake eats the apple, and dynamically growing the snake. The movement of the snake is controlled through a timer, with user input (arrow keys) adjusting its direction. The game ends when the snake collides with itself or the border. The tutorial provides an easy-to-follow approach to building the game, with a focus on VBA programming within Excel to create interactive gameplay.
Takeaways
- đ The script explains how to add an apple to the Snake game in Excel using VBA by defining random coordinates and setting a yellow color for the apple.
- đ The macro `add apple` uses randomization to place the apple on the board based on randomized row and column values.
- đ The script introduces a logic to increase the size of the snake when it eats the apple, including redimensioning the snake array to accommodate the new body segment.
- đ The snakeâs movement is triggered by a timer that calls the `move snake` function at a set interval (200 milliseconds), which can be adjusted for speed.
- đ The game includes a check to see if the snake's head collides with the apple. If so, it increases the apple count or score and adds a new apple to the board.
- đ If the snake collides with the border or its own body, the game ends, and a message is displayed with a 'Game Over' notification.
- đ The `move snake` macro checks if the snakeâs head has eaten an apple by verifying if the cellâs color matches yellow.
- đ A redimensioning operation is used to extend the snake's array after eating an apple, which adds a new segment to the snakeâs body.
- đ The game provides start and stop functionalities using a `start timer` and `stop timer` macro, allowing the user to control the game flow.
- đ The script demonstrates how to use VBA's `set timer` function to control the snake's movement at regular intervals, ensuring continuous game action.
Q & A
How is the apple placed in the game?
-The apple is placed randomly using a macro that generates random row and column values. The row is calculated as 'r and d times 24 plus 2', and the column is calculated as 'r and d times 14 plus 2'. The cell corresponding to this position is then filled with a yellow color to represent the apple.
What happens when the snake's head touches the apple?
-When the snake's head touches the apple, the game checks if the cell color is yellow. If it is, the score (apples or points) is incremented. Additionally, the snake's body array is resized to accommodate the new segment, and a new apple is placed on the grid.
How is the snake's growth implemented after eating the apple?
-The snake's growth is implemented by resizing the array that tracks the snake's body. Specifically, the array is redimensioned with the 'Preserve' option to maintain previous values while adding one more element to the array, representing the snakeâs new tail.
What is the purpose of the set timer function in the game?
-The set timer function is used to automatically trigger the 'move snake' function at regular intervals (every 200 milliseconds by default). This allows the snake to move continuously and the game to progress without requiring manual input.
How can the speed of the snake's movement be adjusted?
-The speed of the snake's movement can be adjusted by modifying the interval value in the set timer function. The default is 200 milliseconds, but it can be changed to 100 milliseconds or 50 milliseconds to make the game faster.
What happens if the snake collides with the body or the border?
-If the snake's head collides with its body or the border (i.e., the cell color is not black), the game ends. The timer is stopped, and a 'Game Over' message is displayed to the player.
What role does the 'move snake' function play in the game?
-The 'move snake' function is responsible for updating the snake's position on the grid. It moves the snake by adjusting the row and column values based on the direction indicated by the arrow keys, and it is called repeatedly by the timer function.
How are the arrow keys used to control the snake's direction?
-The arrow keys are used to adjust the direction of the snake by updating the row or column increment. When an arrow key is pressed, the corresponding row or column value is incremented or decremented, causing the snake to move in that direction.
How does the game detect when the snake eats an apple?
-The game detects when the snake eats an apple by checking if the cell at the snake's head position (row 0, column 0) is yellow. If it is, it means the snake has eaten the apple, and the score is updated, along with the resizing of the snake's body array.
What happens when the game ends?
-When the game ends, the timer is stopped to prevent further movement of the snake. A 'Game Over' message is displayed, and the game exits. This occurs when the snake collides with its body or the border of the game area.
Outlines
Dieser Bereich ist nur fĂŒr Premium-Benutzer verfĂŒgbar. Bitte fĂŒhren Sie ein Upgrade durch, um auf diesen Abschnitt zuzugreifen.
Upgrade durchfĂŒhrenMindmap
Dieser Bereich ist nur fĂŒr Premium-Benutzer verfĂŒgbar. Bitte fĂŒhren Sie ein Upgrade durch, um auf diesen Abschnitt zuzugreifen.
Upgrade durchfĂŒhrenKeywords
Dieser Bereich ist nur fĂŒr Premium-Benutzer verfĂŒgbar. Bitte fĂŒhren Sie ein Upgrade durch, um auf diesen Abschnitt zuzugreifen.
Upgrade durchfĂŒhrenHighlights
Dieser Bereich ist nur fĂŒr Premium-Benutzer verfĂŒgbar. Bitte fĂŒhren Sie ein Upgrade durch, um auf diesen Abschnitt zuzugreifen.
Upgrade durchfĂŒhrenTranscripts
Dieser Bereich ist nur fĂŒr Premium-Benutzer verfĂŒgbar. Bitte fĂŒhren Sie ein Upgrade durch, um auf diesen Abschnitt zuzugreifen.
Upgrade durchfĂŒhrenWeitere Ă€hnliche Videos ansehen
Code Snake Game in Java
2. State and State Manager | 2D Snake Game - C++ & SFML #DevKage
Project 4 - Multi-level Maze Game In Scratch | maze | scratch | knowledgequotient | scratchgame |
How to Make a Maze Game in Scratch | Tutorial
SI GRINGSING DAN SI KASUR KISAH LEGENDA DARI TEGAL #ewinghd #mistery #tegal
Fizemos o JOGO da COBRINHA #SagaDosComputadores Ep.10
5.0 / 5 (0 votes)