OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python class attribute with a value of type inherited class

  • Thread starter Thread starter pythoncraft
  • Start date Start date
P

pythoncraft

Guest
I am making a game in Python with Pyglet and currently I am tring to involve some menu screens or end screens. I made a class called Screen and the screens (for example GameScreen) inherit from it. The Screen class has a class attribute, s, which is the current screen to be displayed. How am I supposed to assign some Screen object to it?

This is my code:

Code:
# main.py

import pyglet
from screen import *

w = pyglet.window.Window()

@w.event
def on_draw():
    w.clear()
    Screen.s.on_draw()

pyglet.app.run()

Code:
# screen.py

class Screen:
    s = MainMenuScreen() # of course doesn't work

    def __init__(self): pass
    def on_draw(self): pass


class MainMenuScreen(Screen):
    def __init__(self):
        self.buttons = [...]
        ...

    def on_draw(self):
        for button in self.buttons: button.draw()


class GameScreen(Screen):
    def __init__(self):
        self.player = Player(0, 0)
        ...

    def on_draw(self):
        self.player.draw()
<p>I am making a game in Python with Pyglet and currently I am tring to involve some menu screens or end screens. I made a class called <code>Screen</code> and the screens (for example <code>GameScreen</code>) inherit from it. The <code>Screen</code> class has a class attribute, <code>s</code>, which is the current screen to be displayed. How am I supposed to assign some <code>Screen</code> object to it?</p>
<p>This is my code:</p>
<pre><code># main.py

import pyglet
from screen import *

w = pyglet.window.Window()

@w.event
def on_draw():
w.clear()
Screen.s.on_draw()

pyglet.app.run()
</code></pre>
<pre><code># screen.py

class Screen:
s = MainMenuScreen() # of course doesn't work

def __init__(self): pass
def on_draw(self): pass


class MainMenuScreen(Screen):
def __init__(self):
self.buttons = [...]
...

def on_draw(self):
for button in self.buttons: button.draw()


class GameScreen(Screen):
def __init__(self):
self.player = Player(0, 0)
...

def on_draw(self):
self.player.draw()
</code></pre>
 
Top