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:
# File name: settings.kv
#:import settings settings
...
### READY BUTTON ###
RedButton:
text: 'Ready'
on_press:
root.set_players(root.players)
root.set_slugs()
root.manager.current = 'racescreen'
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:
# File name: slug.py
...
class Slug(RelativeLayout):
body_image = StringProperty('')
eye_image = StringProperty('')
y_position = NumericProperty(0)
We will need some more, so let’s add them to the class:
# File name: slug.py
from kivy.uix.relativelayout import RelativeLayout
from kivy.properties import StringProperty, NumericProperty
class Slug(RelativeLayout):
body_image = StringProperty('')
eye_image = StringProperty('')
y_position = NumericProperty(0)
# how many times the slug has won so far
wins = NumericProperty(0)
# percent of races won by this particular slug
win_percent = NumericProperty(0)
# the current odds for the slug - they will change after each race
odds = NumericProperty(0)
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:
# File name: settings.py
...
class SettingsScreen(Screen):
def set_players(self, players):
...
# Here's the method that sets the default values of the slug properties.
# For each slug the initial wins and win_percent values are zero.
# The initial odds are randomized and rounded to two decimal places.
def set_slugs(self):
self.game.speedster.wins = 0
self.game.speedster.win_percent = 0.00
self.game.speedster.odds = round(1.33 + randint(0, 10) / 100, 2)
self.game.trusty.wins = 0
self.game.trusty.win_percent = 0.00
self.game.trusty.odds = round(1.59 + randint(0, 10) / 100, 2)
self.game.iffy.wins = 0
self.game.iffy.win_percent = 0.00
self.game.iffy.odds = round(2.5 + randint(0, 10) / 100, 2)
self.game.slowpoke.wins = 0
self.game.slowpoke.win_percent = 0.00
self.game.slowpoke.odds = round(2.89 + randint(0, 10) / 100, 2)
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:
# File name: race.kv
#:import race race
...
<RaceScreen>:
...
# Slugs' Stats
...
BoldLabel:
text: "Slugs' Stats"
SlugStats:
name: 'Speedster'
wins: 7
win_percent: 70
SlugStats:
name: 'Trusty'
wins: 1
win_percent: 10
SlugStats:
name: 'Iffy'
wins: 0
win_percent: 0
SlugStats:
name: 'Slowpoke'
wins: 2
win_percent: 20
# Players' Stats
...
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:
# File name: race.kv
#:import race race
...
<RaceScreen>:
...
# Slugs' Stats
...
BoldLabel:
text: "Slugs' Stats"
SlugStats:
name: 'Speedster'
wins: _speedster.wins
win_percent: _speedster.win_percent
SlugStats:
name: 'Trusty'
wins: _trusty.wins
win_percent: _trusty.win_percent
SlugStats:
name: 'Iffy'
wins: _iffy.wins
win_percent: _iffy.win_percent
SlugStats:
name: 'Slowpoke'
wins: _slowpoke.wins
win_percent: _slowpoke.win_percent
# Players' Stats
...
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:
# File name: race.kv
#:import race race
...
<RaceScreen>:
...
### THE TRACK ###
...
# labels with slug info
SlugInfo:
y_position: .875
name: 'Speedster'
wins: 7
SlugInfo:
y_position: .625
name: 'Trusty'
wins: 1
SlugInfo:
y_position: .375
name: 'Iffy'
wins: 0
SlugInfo:
y_position: .125
name: 'Slowpoke'
wins: 2
# the odds labels
WhiteOddsLabel:
text: '1.42'
pos_hint: {'x': .77, 'center_y': .875}
WhiteOddsLabel:
text: '1.61'
pos_hint: {'x': .77, 'center_y': .625}
WhiteOddsLabel:
text: '2.53'
pos_hint: {'x': .77, 'center_y': .375}
WhiteOddsLabel:
text: '2.89'
pos_hint: {'x': .77, 'center_y': .125}
# the slugs
...
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:
# File name: race.kv
#:import race race
...
<RaceScreen>:
...
### THE TRACK ###
...
# labels with slug info
SlugInfo:
y_position: _speedster.y_position
name: 'Speedster'
wins: _speedster.wins
SlugInfo:
y_position: _trusty.y_position
name: 'Trusty'
wins: _trusty.wins
SlugInfo:
y_position: _iffy.y_position
name: 'Iffy'
wins: _iffy.wins
SlugInfo:
y_position: _slowpoke.y_position
name: 'Slowpoke'
wins: _slowpoke.wins
# the odds labels
WhiteOddsLabel:
text: str(_speedster.odds)
pos_hint: {'x': .77, 'center_y': _speedster.y_position}
WhiteOddsLabel:
text: str(_trusty.odds)
pos_hint: {'x': .77, 'center_y': _trusty.y_position}
WhiteOddsLabel:
text: str(_iffy.odds)
pos_hint: {'x': .77, 'center_y': _iffy.y_position}
WhiteOddsLabel:
text: str(_slowpoke.odds)
pos_hint: {'x': .77, 'center_y': _slowpoke.y_position}
# the slugs
...
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.