0

I am trying to call a method in another object of a class to invoke a gamer restart.

At the moment in the ball class; which will call its own restart class if the ball goes out of bounds

class Ball(Entity): 
    def restart(self):
        self.ball.x = self.ball_starting_spot[0]
        self.ball.y = self.ball_starting_spot[1]
        # This is where I want to call the player class restart method

The player class has its own restart method that will have to be called to reset itself. Both classes have the same Entity base class.

How would I get about this efficiently?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Kyle Harte
  • 33
  • 3
  • already answered on here https://stackoverflow.com/questions/3856413/call-class-method-from-another-class – stefan_aus_hannover Oct 06 '21 at 20:50
  • @stefan_mit_hannover Answered there, but I don't think that is a good dupe target. – MegaIng Oct 06 '21 at 20:51
  • 1
    Does the `Ball` have a reference to the player? If so, call `self.player.reset()` – Barmar Oct 06 '21 at 20:52
  • Ball does not have a reference to player in any way, unfortunately. However I think I have found a way to do it using pygame custom event, will update whenever I try it out; see https://stackoverflow.com/questions/24475718/pygame-custom-event – Kyle Harte Oct 06 '21 at 20:55
  • 2
    The fact that they are both derived from the same base class doesn't matter per se — they're just two different classes as far as doing what you want goes. Using a custom event might be a way to provide the needed information. – martineau Oct 06 '21 at 21:01
  • Since all classes now derive from object, all classes are in some sense sibling classes. Unfortunately only hierarchy or reference gets you access – 2e0byo Oct 06 '21 at 21:51
  • I eventually achieved this using pygame custom events; please read [stackoverflow.com/questions/24475718/pygame-custom-event](http://stackoverflow.com/questions/24475718/pygame-custom-event) – Kyle Harte Oct 06 '21 at 22:19
  • That's good to hear — btw you can answer your own questions here (and accept them). – martineau Oct 07 '21 at 00:14

0 Answers0