Slot Machine Python Tkinter

  

Simpsons slot machine in Python

5. July 2018

C Framework for slot machine games GambleKit is a free open-source C object oriented library for quick and easy building of slot machine and gambling games with reels and GUI management.

Raspberry Pi is a FANTASTIC device and super fun toy. One of the most popular coding languages for the Pi is Python. I messed around with Python a few years back (I made an exam project in Python when I was in high school) but haven’t touched it in the last three years or so. My exam project in high school was a CLI (Command Line Interface)-styled slot machine. It was extremely simple but also a lot of fun to make, so I figured I would try to make a proper slot machine game this time around.

Python

I have some experience with the TKinter module for Python but I knew that it wasn’t really that great of handling animations or screen refreshes. As a result I choose a module called Pygame. This module is well documented, has a large userbase and a great community supporting it. Learning it isn’t that difficult if you already have some experience with TKinter or some other GUI library. What’s great about this module is that you can specify what area of the screen you want to be refreshed instead of having the entire display refresh each time some movement happens. This cuts down massively on the CPU power required – which is a BIG plus on hardware like the Raspberry Pi with limited resources.

As previously mentioned my first slot machine game from high school was CLI-based and I really wanted some GUI experience in Python so one of the first things I needed to figure out was what kind of theme the game needed to have. After some thinking, I settled with a Simpsons theme.

Slot Machine Python Tkinter

Since the code for this project is on GitHub and is well commented within the code itself I won’t go over how it works. The game automatically gives you 1000 credits to begin with. Using the 1, 2, and 3 number keys you can bet 10, 50, and 100 credits respectively. With the 4, 5, and 6 number keys you can change how many playlines you want to bet on. To activate the game you press the arrow down key.

The code is available on my GitHub: github.com/SuneKuntz/Simpsons-Slot-Machine/ . Feel free to download the code and make all the changes you want! Make a different theme, add new functions, change the workings of the code: open-source rocks!

In this tutorial, we will try to understand slots in Python with a simple example.

In Python, we use __dict__ function to store the object attributes. This allows setting new attributes at runtime.
The function __dict__ acts as a dictionary. It doesn’t have a fixed number of attributes stored. One can add attributes to the dictionary after defining them but dynamic adding of attributes to a class is not possible in built-in classes like ‘int’ or ‘list’ etc.

However the above function takes up lesser spaces but also, this leads to the function taking up a lot of space in RAM(if thousands and millions of objects are there).
To solve this limitation, we have __slots__ in Python. It provides a static structure not allowing adding attributes after the creation of an instance. __slots__ saved about 9GB RAM.
To define __slots__, one has to define a list with the name ___slots__ which would contain all the attributes to be in use.

Let’s learn slots in Python with a simple piece code.

__slots__ in Python

NOTE: ONE SHOULD DECLARE A PARTICULAR SLOT ONE TIME IN A CLASS. AN ERROR WON’T OCCUR IF THIS IS NOT FOLLOWED BUT OBJECTS WILL TAKE UP MORE SPACE THAN THEY SHOULD

(36,44)


In the above program, __slots__ is used in the class Right only once declaring a particular slot one time in the class. While the opposite happened in class Left thus declaring three slots at one time. Thus the size of the class Right is 36 which is lesser than 44 (size of class Left).

In the above programs, the line “__slots__= ……..” is to define the slots and “def __init__” we define or instantiate the values.

Python Tkinter Tutorial

Also lastly, __slots___ was actually created for faster attribute access.

Leave a Reply

Python Tkinter Examples

You must be logged in to post a comment.