Location>code7788 >text

Remake of Tetris game based on MonoGame

Popularity:893 ℃/2024-08-15 21:01:33

Two years ago, I wrote a Tetris game based on MonoGame using C#, an introduction to which can be found at [this article] Recently, using my spare time to refactor my previous MonoGame-based game development framework, I took the opportunity to remake my previous Tetris game as well, adding many of the features I've been intending to add, even including providing a cross-platform version.

First of all, let's talk about this game development framework, I named it Mfx (MonoGame Fx, Fx is short for Framework, but don't want to rename it with MonoGame Framework), the storage address is:/daxnet/mfx. This refactored framework has a better overall structural design than its predecessor version, Ovow (/daxnet/ovow) is much more reasonable, removing some unnecessary features and adding some new ones. My spare time is limited, so I don't intend to really develop it into a complete game engine, just to be able to support some of the small games I've written myself, so I don't intend to introduce it too much.

Back to the remake of the Tetris game, open source first: [/daxnet/tetris-sharp-revisedIn the Releases list on this page, you can directly click on the latest release version, and then select the corresponding operating system to download the ZIP package to play with, currentlyThe game is compiled and exported for Windows, Linux and MacOS.(Linux I tested it works under Ubuntu 24.04 LTS Desktop, macOS didn't try it). Of course, if you don't want to play the game, you can just watch the video:

Compared with the old version, the new version of the file size is smaller, packaged size of 62MB, compared with the previous 107MB, a reduction of 45MB, in fact, the new version comes with a richer soundtrack, the size of the resource has become larger, but the total size is down, but also because of the .NET upgraded from 6 to 8, the optimization itself has been done a lot. For such a simple game why would it have such a large size? There are two main reasons: various resources need to take up some space, and the other is that the compiled program is packaged using the--self-containedNET 8 is packaged in one package, and players don't need to install any .NET-related runtime libraries. Think about it the other way around, if you develop a large game using the same set of technologies, although the resources will take up more space, but even if it's hundreds of MB or even GB, maybe you won't care about its size.

Once downloaded and unzipped, double-click(Linux first)chmod +x ./TetrisSharp(and then run it directly) to start the game, click [NEW GAME】The menu will take you to the main screen to start the game, [CONTINUEThe] menu is currently unavailable, during the game, press the ESC key on the keyboard to return to the title screen, at this time the CONTINUE menu is available, click to continue the game. [___LOADThe [ ] menu is also unavailable, but if you quit the game at any point during the game, the game state will be saved, and the next time you re-run the game, the LOAD menu will become available, and you can click on it to load the last saved game state. Clicking on the [INPUT SETTINGSThe] menu allows you to access the key setting interface:

The red font suggests the key that is currently being set, at this time, just press the corresponding key on the keyboard or gamepad, and the red selected item will jump to the next item. If there is any key setting error in the middle of the process, you can click [RESET] button to reset the buttons from scratch. After setting, click the [SAVEbutton to save, or click the [CANCEL] button to cancel the setting. For the gamepad, it seems that not all joysticks are supported at the moment (I myself use 8BitDo M30), so I suggest that you can connect it and try it out, switching the mode of the joystick (I guess you need the XBOX mode), and if it doesn't work, it's better to experience it with the keyboard first.

As shown above, the first time you run the game, the keyboard is used as input by default, and the keys are defined as follows:

  • Square down: S
  • Square left: A
  • Move the square right: D
  • Cube rotation: J
  • Cube to the end: K
  • Pause the game: Spacebar
  • (Hide: Back to start screen: ESC)

Compared with the old version, the new version has been optimized in terms of interface (the top picture below shows the old version and the bottom picture shows the new version):

The main areas of improvement are:

  1. The board has gotten bigger: 15x28 instead of 12x24
  2. Cube materials have been adjusted
  3. The falling blocks leave a projection on the board for easy positioning.
  4. New separate button for fast falling blocks, when pressed it drops the block directly to the bottom
  5. The interface color scheme and fonts have been adjusted
  6. Displays the number of cubes that have been used
  7. Counts the highest score and updates the highest score when it is exceeded

In addition, the game control has been optimized, and the button control is more flexible. Due to the fixing of the background music loading problem in the old version, the memory usage of the new version has been drastically reduced to about 50% of the original one, from more than 400MB to more than 200MB. all in all, the new version is closer to a complete game.

Like the old version, the new version offers the ability to customize the cube, except that the new version uses a more concise text file instead of the previous XML file. In the game directory, find thefile, double-click to open it for editing. The file format is as follows:

  • # Beginning line comment line
  • Each square is defined in terms ofblock "<name>"at the beginning of the command.end blockThe end of the command, which contains one to several lines of definitions for the cube variant (aka Rotation).
  • Each cube variant occupies one row, for example, the L-shaped cube, which has four rotational forms: 𠃊, 𠃎, 𠃍, and factory, then there are four rows of 0s and 1s that define its form
  • For each rotational form, use the numbers 0 and 1 to define its shape, and there can be multiple lines, each separated by spaces

For example, if we wish to add the following square to the game:

If it is rotated, it will have the following four rotation patterns, and if in each rotation pattern, the squares that need to show the material are represented by 1, and the squares that don't need to be shown are represented by 0, you get:

Thus, for this square, it is possible to define it with the following text:

block "custom"
	111 101
	11 10 11
	101 111
	11 01 11
end block

After adding this definition to the file, restart the game and this customized square will appear, for example:

In terms of program design, because of the use of C#, so follow the basic principles of object-oriented undoubtedly, in the Mfx framework and the game itself, but also applied some design patterns (such as Builder, Adapter, Mediator, etc.) in order to make the overall structure of the game is more elegant, I have time to introduce in another blog post. For using the source code, if you wish to debug it yourself, first make sure that the .NET 8 SDK is installed, and then open it with Visual Studio 2022.srcdirectory of thewillTetrisSharpThe project is set to start the project, and you can debug it directly by F5. If you wish to compile the source code directly, you can set thesrcdirectory, use the following command to generate the compilation, the generated result will be output to thebin\Release\net8.0Catalog:

REM generatingWindowscompiling
dotnet publish -c Release --sc -r win-x64

REM generatingLinux Desktopcompiling
dotnet publish -c Release --sc -r linux-x64

REM generatingMacOScompiling
dotnet publish -c Release --sc -r osx-x64

OK, that's all for today, interested readers are welcome to try out the troll.