patternpythonMinor
Creating a game with rectangular shapes
Viewed 0 times
creatingwithshapesgamerectangular
Problem
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.
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
Do I need to use inheritance? Should I use
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.Solution
First off, you don't need to explicitly inherit from
Secondly, I think your current design is fine, as long as the
Finally, just a little nitpicky tip, your variables
Likewise, in
Actually more than being nitpicky,
these recommendations come from PEP8, the Python style guide.
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.
Context
StackExchange Code Review Q#32129, answer score: 3
Revisions (0)
No revisions yet.