I'm using Pygame to create a small game with few rectangle shapes. However, I want to learn more about classes, and I'm trying to use them. I'm very new to classes and inheritance, but I want to learn the best habits possible. I'm using Python 3.3.2.
import pygame
class Screen(object):
''' Creates main window and handles surface object. '''
def __init__(self, width, height):
self.WIDTH = width
self.HEIGHT = height
self.SURFACE = pygame.display.set_mode((self.WIDTH, self.HEIGHT), 0, 32)
class GameManager(object):
''' Handles game state datas and updates. '''
def __init__(self, screen):
self.screen = screen
pygame.init()
Window = Screen(800, 500)
Game = GameManager(Window)
I skipped a few functions to make it simpler. The thing that bothers me is that I've been told that I don't need GameManager
to be subclasses of Screen
. I was advised to use this implementation, but I find it very strange to use 'Window' as a parameter to GameManager
.
Do I need to use inheritance? Should I use super()
? Later on, I will have class Player
and I will need some of the attributes from the Screen
class. From the viewpoint of designing OO for a GUI game using Pygame.
-
1\$\begingroup\$ See Prefer composition over inheritance? \$\endgroup\$Janne Karila– Janne Karila2013年10月03日 06:39:08 +00:00Commented Oct 3, 2013 at 6:39
1 Answer 1
First off, you don't need to explicitly inherit from object
when creating classes. In Python 3.x, you can just type class MyClass:
, and leave it as that if you aren't inheriting from any other classes.
Secondly, I think your current design is fine, as long as the Screen
class implements more methods, and the GameManager
as well.
Finally, just a little nitpicky tip, your variables Window
, and Game
should be renamed to window
and game
.
Likewise, in Screen
, the attributes WIDTH
, HEIGHT
, SURFACE
should be renamed to width
, height
, surface
, respectively.
Actually more than being nitpicky,
these recommendations come from PEP8, the Python style guide.
Explore related questions
See similar questions with these tags.