Skip to content
Home » Kivy Part 52 – Slugrace – Slug Settings

Kivy Part 52 – Slugrace – Slug Settings

Spread the love

Hey guys, in the previous part of the Kivy series we added the set_players method to the on_press event on the Ready Button. Today we’ll continue handling the slug settings. Let’s start by adding another method, set_slugs.

The set_slugs method will take no arguments. Let’s first add it in the settings.kv file and then implement it. Here’s the kv file:

Setting a slug means setting its properties. If you open the slug.py file, you will see that we only defined three properties in the class:

We will need some more, so let’s add them to the class:

And now let’s create the set_slugs method in the settings.py file. We will need the randint function from the random module to randomize the slugs’ odds, so make sure to import it before you use it. And here’s the method:

Just like we did with the players, we’ll now modify the code in all the screens where we need access to slug data. For now we’ll only need it in the Race screen, so we don’t have to access it like before. Instead, we can just use the ids. But later we’ll need slug data also outside the Race screen, so it’s important that it’s available in the Game class.

The Race Screen

We will need access to slug data in several places in the Race screen. The first place is the Slugs’ Stats area, which at this moment looks like so:

As you can see, here the values are hard-coded. Let’s use the wins and win_percent properties instead. As we are in the same rule as the Slug instances, you can just use the ids instead of the properties. Here’s the modified code:

The next place where we need slug data is in the Track area where we have the white labels with the slug info and the labels displaying the odds. Here’s the code as it looks now:

Now, let’s use the y_position property defined in the Slug class. Also, let’s use the wins and odds properties to replace the literal values in the code. We’re still in the same rule, so we can use ids.

Here’s the code with the modifications:

If you now run the app and go to the Race screen, you will see the new values of the properties displayed:

That’s it as far as slug settings are concerned. In the next part of the series we’ll have a look at game settings.


Spread the love

Leave a Reply