Write a small game with Excel

zhaozj2021-02-16  35

The game has to give people a happy entertainment, not a trouble, to satisfy the heart of play, sometimes a leaf is enough. For our game development enthusiasts, ASM, C, C, C , DirectX, OpenGL, Flash, etc. are not our necessary knowledge and tools, sometimes some unexpected achievements are more realistic.

Office software Excel is familiar, but I don't say much. But everyone can think about it in Excel, can you realize a small game? This doesn't mean the Easter Games in Excel 97, but it is real with Excel resources, running at Excel. After several generations of upgrade, the function of the VBA (Visual Basic for Application) language under MS Office has greatly improved, and it is possible to realize a flat game in Excel. Of course, this should also be characterized by the EXCEL workspace. Writing applications in Excel is generally written through VBA, and these small games are also. As for how to start writing macro, how to insert a form and some selection row cells, change cell color, clear cell content, etc., it is assumed that everyone knows, do not understand the reference help. Honestly, I am not familiar with Visual Basic for Application, and I have to help it through the whole process. The following combination of code is introduced to you.

Let's talk about it first. Everyone has played the foreign Huarong Road, that is, there is n * n. There is an empty, other classes are the puzzle or numbers of the order, the purpose of the game is to use this only space to move the plugs. Puzzle (Digital) Restore (Sequence). Here, our little game is to recover 1 to 15 of the 15 order numbers from 1 to 15 order, just like the Rising website (I don't know if it is still not there).

After opening Excel, open the code writer (Alt F11), write the following code to the universal code section of one of the worksheets (Sheet). The program is first initialized. Define the data structure, select 4 * 4 space, adjust the table size, change the cell color (indicate the game area), then let 1 to 15 non-repetition randomly distributed to the top 15 grid:

TYPE POS ROW AS INTEGER COL AS INTEGEREND TYPE

'Blank is the space, the initial value is the last PUBLIC Blank as POS

'init_pos is the top left corner of the game area, control the location of the game area, here is (5, 5) cell public init_pos as POS

'border is a game area border public border as pos

'N is the size of the game area, control the size of the game (n * n), here N = 4PUBLIC N AS INTEGER

'Running is used to determine if the game is in progress, if you want to exit or play a player to replay this variable control public Running as boolean

'This is the total entrance of this game macro, started by Button in the workspace, if you want to let the game macro automatically run Sub Begin () if Running Then Cells (Border.Row, Border.col) when opening the table. ClearContents 'initializes unique spaces in the lower right corner Running = falseEnd ifinitialize' call global initialization function END SUB

'Global initialization function function initialize () if not running then

Dim i As IntegerDim ii As IntegerDim temp As IntegerDim a (14) As Integerrunning = TrueN = 4init_pos.col = 5init_pos.row = 5border.row = init_pos.row N - 1border.col = init_pos.col N - 1blank.col = border.colblank.row = border.row

The 'game area (color) initialization, omitted the adjustment of the cell size, you can join the corresponding statement to make the game out of the game (cells (init_pos.row, init_pos.col), cells (border.row, border.col)) .Interior.colorindex = 5

'Random number array initialization, here is the number 0 to number 14 total 15 for i = 0 to (n * n-2) a (i) = i 1Next i

'Does not repeat random distribution Most for i = n * n-2 to 0 step -1 randomize II = int (RND * i) TEMP = a (i) a (i) = a (ii) cells (init_pos.row I / N, INIT_POS.COL (I mod n)) = a (i) a (ii) = TempNext IEND IEND FUNCTION

Obviously, Excel's cell is the protagonist of the game. After initialization, the game starts, the logic is very simple: every click on a cell (generating the SelectionChange event in Worksheet), the game determines whether this grid is in the game area, if it judges whether there is a space in the four directions of the upper and lower, if there is The space is passed into the space in the original unit to the space, and the original single is empty (space to the original single-single-single-piece), then determine whether the game ends (1 to 15 order?), If not, nothing does not do anything , Wait for the next clicking event.

Private Sub Worksheet_SelectionChange (Byval Target As Range) If Running Ten 'Click on whether it is a space if (target.row <> blank.row or target.column <> blank.col) THEN' clicked unit Game area if ((target.row> = init_pos.row and target.row <= border.row) and (target.column> = init_pos.col and target.column <= border.col)) THEN 'clicked unit Whether there is space if there is a space IF, or (abs (blank.col - target.column) <= 1 and blank.row = target.row) or (ABS (Blank.row - target.row) <= 1 and blank.col = Target.column) The 'cell and space exchange cells (blank.row, blank.col) = cells (target.row, target.column) cells (target.row, target.column) .clearContents blank.col = Target.column blank.row = target.row end if End ifnd If 'determines whether the game ends? Dim i as integeri = 0do while (i

Finally, put a button in the Excel workspace, used to call the game macro boot game. Double-click Button, enter code private sub button_click () Call Beginend Sub

After saving, click Button, the game starts!

This game can also add a key, limited time, calculate the number of mobile steps, and record a location in the table as a game record. There is no anti-pace function, and people can modify the values ​​in the cell, which can be restricted through the Worksheet_change event (but such a game is cheating, I have nothing to say).

转载请注明原文地址:https://www.9cbs.com/read-22084.html

New Post(0)