Site icon Python GUI

How To Create a Memory Game For Android With DelphiFMX Python Package

How To Create a Memory Game For Android With DelphiFMX Python Package

Memory games have long been a popular way to kill time while improving cognitive skills. It’s no surprise that, with the increase of mobile devices, memory games have made their way onto our smartphones and tablets. If you want to create a memory game for Android devices, Delphi is a strong tool to assist you. In this article, we’ll look at how to make a memory game for Android with Delphi, a rapid application creation tool.

Why Create a Memory Game Using Delphi For Python?

Using Delphi for Python to create a memory game can be advantageous for Python developers looking for an efficient way to build Graphical User Interfaces (GUIs) for their game. Delphi for Python gives developers powerful tools and cross-platform support. It allows them to build visually appealing and functional user interfaces that operate across multiple platforms without writing separate code for each. Developers can build sophisticated and flexible GUIs with access to the FireMonkey GUI framework, making the development process faster and more efficient.

Furthermore, Delphi’s UI design tools, such as the Delphi4PythonExporter tool, can help accelerate development while ensuring that the resulting GUIs are functional and aesthetically appealing. Finally, Delphi Community Edition and PythonFMXBuilder are open-source tools ideal for developers who prefer to work with open-source tools whenever feasible. In conclusion, using Delphi for Python to create a memory game can assist Python developers in streamlining their development process and ensuring that their game works smoothly and efficiently across all platforms.

How to Create this Game as a Desktop App?

Delphi offers powerful tools that make developing visually appealing and engaging games easily. So, let’s dive in and start creating your own memory game for Android using Delphi.

What Are the Requirements to Create this App?

To create the memory game desktop app, you must have Delphi installed on your device and the FireMonkey GUI framework. Additionally, you will need a text editor or IDE that supports Python, such as PyScripter and the Delphi4PythonExporter tool.

If you don’t have these tools installed, we recommend checking out the article “Powerful Python GUI Project Setup” to help you get started. Once you have all the required tools, you can proceed with the tutorial to create your memory game desktop app using Delphi

You can grab the code for this tutorial from our GitHub repository using the following link: https://github.com/Embarcadero/PythonBlogExamples/tree/main/Memory_Game

How to Create Forms in Delphi CE?

Begin by opening Delphi CE and creating a blank application. To do this, navigate to File > New > Multi-Device Application > Blank Application > Ok. In this tutorial, we have named our project MemoryGame.

If you are not familiar with the various sections of the Delphi IDE, we recommend referring to our free eBook bundle, which covers the Delphi Python EcoSystem and all Python GUI offerings.

To start, let’s give our form a name. Right-click on the form and select QuickEdit. Here, we will name our form MainForm with the caption MainMenu.

Next, let’s resize our form. Go to the object inspector and search ClientHeight and ClientWidth in the properties tab. We will set them to 500 and 360, respectively.

We will also rename the source file of our main form to MainMenu.pas to make it easier to keep track of.

This form will be the primary screen where users can start the game. To enhance the user experience, let us add a label that will display the title of our app. To do this, navigate to the standard palette and locate the TLabel component. Next, drag and drop this component onto the form.

To modify the text style to reflect the title, we will use TextSettings. Navigate to the Object Inspector and locate TextSettings. Select Font beneath it and click “...” to see options for modifying the label’s font, size, and style based on your preferences. Here we have set our font family as Britannic with a font size of 48.

Rename each label’s text by right-clicking on the label and selecting Quick Edit. Following an intuitive naming convention for the labels is crucial so they can be tracked more easily. Here, we called our TLabel component “Title” with the text “Memory Game“.

Here we also centered the text using the HorzAlign and VertAlign properties to ensure that the text appears symmetrical.

We can adjust the form’s background color by navigating to the Object Inspector and selecting the form’s fill property.

Finally, we need a method for navigating to the next form where the main game would be played. To accomplish this, go to the Palette and add a button using the TButton component.

Let’s give this button a suitable name. Additionally, we changed the font size and style of these button texts (as we did for the TLabels above) to make them more prominent. Here, we used Segoe UI with a font size of 18.

After making these changes, our form will be much more visually appealing:

Now, let’s add some styles to our forms using the power of DelphiFMX styles. The Delphi ebook bundle comes shipped with a variety of styles that you can use for your applications.

So first download the free ebook bundle and extract the styles zip file into a folder of your choosing. Now select the style you want to choose, for this project we will be using Light.style. You can find other styles online, such as delphistyles.com.

Head to the Palette and search of the TStyleBook object. Add this to your form.

Next, double click on the object. This will open up your Style Designer. Now press the click on the open button.

Now head to the directory of your extracted style files, select the style of your choosing, and click on exit. Next, select the form and head over to the Object Inspector. Here we will search for the StyleBook property. Select the drop down menu and select StyleBook1.

This will apply the style to your form. Here is what our final MainForm looks like:

Now that we are done with the main form of our app, let’s create a new form that will display our Memory Game. So right-click on MemoryGame.exe and select Add New > Multi-Device Form.

This will create a new form which we have named as GameForm, and the corresponding source file as Game.pas. We have also renamed the form caption to Memory Game, and changed the form dimensions to match our MainForm.

Next, let’s start by adding labels and buttons to our form. We will first add two labels to the top of our form which we will name ScoreLabel and TriesLabel, with the captions Score: and Tries: respectively. These labels will help us display the score the player has scored as well as the number of tries they have made.

We have also changed their font style to Britannica with a font size of 24.

Next, we will add 8 buttons to our form that will help us play our game.

For each button we added a default caption as ? and named each of them as Button1, Button2, and so on. We also added two extra buttons Back and Reset that will help us navigate our forms. As you can see we have disabled the Reset button by default which will only open when the player has finished the game.

Finally, we added 3 new labels to the form. The Card1 and Card2 label will help the user remember the card he had selected in his try. The additional label, which we have named Status, lies right above the Back and Reset buttons. It will help display the status of the game. Finally add the Light style to our form. Here is what our final form looks like:

Now that our forms are ready, the last step would be to add functionality to each button. So navigate to each form and double-click each button to create an OnClick method for that button in your .pas file corresponding to your .fmx form. This will enable you to add distinct functionality for each button when clicked.

After creating the OnClick method for each button, adding at least one comment (//) to each procedure is recommended to preserve it when you save the Python file. As we do not export the run-time implementation using the Delphi4PythonExporter, there is no need to write any run-time implementation code.

How Can We Export this App as Python Code?

Now that our forms are ready, we can save our project as a Python script by selecting Tools > Export To Python > Export Current Entire Project.

Here rename your project to MemoryGame, and select the main form as MainForm. Finally, select the directory of your choosing and click Export:

Delphi will generate 5 files, 3 Python scripts named MainMenu.py, Game.py, and MemoryGame.py. The Game.py, MainMenu.py python files contain the classes for the individual forms, and the MemoryGame.py is used to launch our Delphi app. Along with each Python file, Delphi also generates a .pyfmx script of the same name as each form that contains the visual information of that form. Now, let’s take a look at the exported Python files.

MemoryGame.py:

[crayon-66354101704f3951385612/]

Game.py:

[crayon-6635410170506008558052/]

MainMenu.py:

[crayon-6635410170508495750671/]

How to Add Functionality to This App Using the Delphi FMX Library?

Now that our forms are ready, we can start adding functionality to our game. Let’s start by opening up MainMenu.py and navigating to the StartButtonClick function. 

The MainForm only serves the purpose of launching the game and the StartButtonClick will help us do that. So start by importing the GameForm, from Game.py:

[crayon-663541017050b387241131/]

Next, in the StartButtonClick function, create a new variable named Game, and create a new instance of the GameForm. We will then use the Show() method to display our GameForm. Here is what the StartButtonClick function looks like:

[crayon-663541017050d083471578/]

Here is what our MainMenu.py looks like:

[crayon-663541017050f657516546/]

Now that our MainForm is ready, let’s now add functionality to our GameForm. Start by opening Game.py and navigating to the __init__ function. In the __init__ function the first thing we will have to do is initialize our score and tries. Along with this we will keep track of the previous attempt by the user and add a flag that will help us later on:

[crayon-6635410170511464538348/]

Next, we will call a function called __RandomizeCards(), which we will define later:

[crayon-6635410170513830312940/]

Here is what our final __init__ function looks like:

[crayon-6635410170516501086977/]

Now the lets move on and define the __RandomizeCards() function. Let’s start by importing random:

[crayon-6635410170518027282699/]

The __RandomizeCards() function uses the Keys from all the buttons that we have defined for our GameForm. It then shuffles a bunch of alphabets that we have defined in our values variable. Finally, it assigns each button a sigle value by the means of a dictionary. This dictionary is stored in a new variable called self.d. Here is what our final __RandomizeCards() function looks like:

[crayon-663541017051b654599401/]

Now let’s define our Back and Reset buttons. Start by going to the BackButtonClick function. Here all we need to do is destroy the current form. So here is what our BackButtonClick function will look like:

[crayon-663541017051d550514117/]

Next, we need to define our Reset button. The Reset button will call the __RandomizeCards function once again, while resetting the tries, score, flag and all the necessary labels of our form. It will then disable the Reset button and reset the text on all the buttons on our form. Here is what our ResetButtonClick function will look like:

[crayon-6635410170520570003668/]

Now let’s define the main functionality of our game. To do this, let’s create a function called __Functionality that takes in two parameters, the button that presses it, as well as the name of the button. The function uses the name parameter in self.d to get the value assigned to it:

[crayon-6635410170522836647322/]

Now that we have our value, we need to check whether the user is one his first or second try. To do this we will use the flag variable. If flag is equal to 1 then the user is on his second try.

Now if the user is on his second try, we first need to update the tries label and reset the flag to 0:

[crayon-6635410170525844246532/]

Next, we need to check whether the value of the previous try was the same as this new try. For this we will use the self.prev variable. Now if the new values match, we increment the score and update the relevant labels. Additionally, we also disable the new selected button, as well as the previous button. 

Finally, once we have made the relevant updates, we need to check if the current score is equal to 5. This would indicate that the user has selected all buttons, and thus has won the game. In this case, we update the Status label and enable the Reset button. Here is what our __Functionality function looks like so far:

[crayon-6635410170527991515497/]

Now, we add a check if the two buttons selected are not the same. In that case, we show the respective cards in the Card1Label and Card2Label, we then hide the previous button and reset the self.prev variable:

[crayon-6635410170529914863126/]

Finally, to complete the function we add an else for when the user is on his first try. The Card1Label and Card2Label will remain the same, but the selected button flips to show the value behind it. We then update the flag to 1 and then update the self.prev variable. Here is what our completed __Functionality function looks like:

[crayon-663541017052b558751064/]

Now all we need to do is call the __Functionality function in each button. So head over to each button and call the function. Here is what this looks like for Button1:

[crayon-663541017052c000816442/]

Here is what our final Game.py file looks like:

[crayon-663541017052e942766092/]

Can I Create this Game for Android Devices?

Let’s proceed to convert our time widget application into an Android application.

As the Delphi4Python Exporter only exports the initialization logic for desktop-based applications, we need PythonFMXBuilder to convert it to an Android app. To begin, open FMXBuilder, click on the toolbar’s Project > New project option, and provide the project’s name. We named ours MemoryApp.

Before adding our Python files, we must modify some code to allow our project to run on Android. First, we need to create a new file called MemoryGame_Android.py and copy the contents of MemoryGame.py. Next, add your Python and other supporting project files used to build your application. We won’t add MemoryGame.py; it will only run our desktop application.

Right-click on the MemoryGame_Android.py file and set it as the main file. Before building the app, we need to make some important code changes.

In Android applications, Delphi applications are automatically initialized. Therefore, we don’t need to initialize or set a title manually. The Application.MainForm variable only needs to be initialized without launching or destroying the program because Android handles both tasks.

After making the necessary code changes, save the file.

Now connect your mobile to your computer device and enable USB tethering. Next, click on the refresh icon and select your device from the drop down menu.

Finally, choose your device and click on the Run Project button. This will install the app on your Android phone and run it.

This will install the app directly on your mobile device.

Is this App Working as Expected?

Now that our app is ready, you can launch it on your Android device and be greeted with the MainForm screen:

Next click on the START button, which will open up the GameForm:

As you can see the Reset button is disabled and will only open when the game has finished. You can then start playing the game. Clicking on each button will reveal the letter behind that button:

If you don’t guess your alphabet in your try, then the Card1 and Card2 labels show your previous attempt:

If you can guess a button correctly, then your score increments and the buttons get disabled:

Finally, you can continue along with the game until you get everything right:

As you can see the Reset button gets enabled once the game is finished. You can click on it to restart the game with new random alphabets.

Are You Ready to Create Other Apps With Delphi?

To summarize, developing a memory game for Android with Delphi is a rewarding experience for developers looking to hone their skills and build engaging applications. Developers can quickly build visually appealing and functional user interfaces that work on multiple devices using Delphi’s powerful tools and cross-platform support. Whether you’re an experienced developer or a beginner, this piece has given you the information you need to create your own memory game for Android using Delphi.

Now that you’ve learned how to make a memorization game in Delphi for Android, it’s time to put your knowledge to use. You can improve your coding skills and make a game that is engaging and enjoyable to play by doing so. Remember to test your game on various devices to ensure it works smoothly and efficiently. 

Why wait? Start making your own memory game with Delphi for Android today to advance your programming skills.

What Are Some FAQs Related to this Tutorial?

What is Delphi?

Delphi is a rapid application development tool for Windows that allows developers to create Graphical User Interfaces (GUIs) quickly and easily.

What is FireMonkey?

FireMonkey is a powerful, flexible GUI framework to create visually appealing and functional user interfaces.

Is Delphi for Python a good choice for developing memory games?

Delphi for Python is a great solution for developers looking to create memory games quickly and efficiently, with cross-platform support and powerful tools.

Do I need previous programming experience to use Delphi for Python?

Some programming experience is recommended, but beginners can still use Delphi for Python to create simple applications and gradually improve their skills.

Can I create a memory game for other platforms besides Android using Delphi for Python?

Yes, Delphi for Python provides cross-platform support, allowing you to create memory games that work on various platforms such as Windows, macOS, iOS, and more.

Are there any open-source tools available for Delphi for Python?

Delphi Community Edition and PythonFMXBuilder are open-source tools that offer an ideal solution for developers who prefer open-source tools whenever possible.

Exit mobile version