4
\$\begingroup\$

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.

Sᴀᴍ Onᴇᴌᴀ
29.5k16 gold badges45 silver badges201 bronze badges
asked Oct 2, 2013 at 11:33
\$\endgroup\$
1

1 Answer 1

3
\$\begingroup\$

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.

janos
113k15 gold badges154 silver badges396 bronze badges
answered Jul 3, 2015 at 2:47
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.