0

I'm struggling to figure out how to organize things here. I'm building a race game. There are players and race courses. Player have attributes like name, age, etc. Race courses also have a set of attributes, including difficulty. One thing I want to do is, when a player runs a course their energy level drops based on difficulty of the course. What I'm confused about is, because difficulty is an attribute of the course, and energy level is an attribute of the player, how can I affect the two?

Take the code for example...

class Player():
    energy_level = 0
    def __init__(self, name, ability)
        self.name = name
        self.ability = ability

    def run(self):
        #code to run course here, reduces energy level when running course. Energy level is reduced based on player ability

class Course():
    def __init__(self, name, difficulty) 
        self.name = name
        self.difficulty = difficulty

player1 = Player("Bob", 90)
course1 = Course("Advanced Track", 15)

Maybe I am going about this all wrong? Does course need to be a class?

Thanks for any input.

DavidG
  • 24,279
  • 14
  • 89
  • 82
Erich Purpur
  • 1,337
  • 3
  • 14
  • 34
  • 1
    Course can still be a separate class. How about adding a course (or a list of courses) to Player class? – vahdet Mar 07 '18 at 13:17
  • Just an idea. Make another class that inherits the Player class and Course Class. –  Mar 07 '18 at 13:17
  • Once you get things working, consider asking [CodeReview.SE] for a design review. They'd probably be able to give better advice seeing the whole picture, than we can with just this specific questions. Be sure to read the guidelines before posting (i.e., code has to be working.) –  Mar 07 '18 at 13:26
  • Or how about a third Race class, which uses a set of Players and a Course. Move the code to run the course (or race) there. Anyway, I agree with JETM that this should go on Code Review SO. – martin jakubik Mar 07 '18 at 15:48
  • @mikey and Martin Jakubik can you elaborate on that some more? Specifically, what does an object for that 3rd class look like? Let's say I create a 3rd class called Event, which inherits from Player and Course classes. In that Event class, I create a method to "run course" which reduces energy level. What would my object look like for that? – Erich Purpur Mar 07 '18 at 20:16
  • Are you asking how to use [class inheritance / super](https://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance)? –  Mar 07 '18 at 20:23

2 Answers2

3

How can I affect the two?

This you can achieve by passing an object of type Course to the run function like so:

class Player():
    energy_level = 0
    def __init__(self, name, ability)
        self.name = name
        self.ability = ability

    def run(self, course):
        #use course.difficulty to reduce self.energy_level

class Course():
    def __init__(self, name, difficulty) 
        self.name = name
        self.difficulty = difficulty

player1 = Player("Bob", 90)
course1 = Course("Advanced Track", 15)
player1.run(course1) # this will pass course1 to the run function and run will reduce the energy level according to the difficulty of course1
FlyingTeller
  • 17,638
  • 3
  • 38
  • 53
0

You can make course a member of the player class

class Player():
    course = null;
    energy_level = 0
    def __init__(self, name, ability)
        self.name = name
        self.ability = ability
        self.course1 = Course("Advanced Track", 15)

    def run(self):
        #code to run course here, reduces energy level when running course. Energy level is reduced based on player ability
        #change  self.energy based on self.course1.difficulty

Of course this is an oversimplification. You need to add methods to open new courses, and not hardcode this in the constructor.

Aris
  • 4,643
  • 1
  • 41
  • 38