[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Good code



Organisation is good, so try to organise things into functions, and
name them something.  For example, my basecode for most of my programs
looks like:
#imports
#variables
#functions
def get_input():
    #get input
def update_game_stuff():
    #Move objects, update fps, etc.
def draw():
    draw()
def main():
    while True:
        get_input()
        update_game_stuff()
        draw()

Next, name your variables coherently.  Work up a system.  Will you use
capital letters like 'FrameRateCounter' or with underscores:
'frame_rate_counter', or both?  Will you put suffixes on your
variables?  Like what I do for Surface pointers?:
SpaceShip_Surface
Planet_Surface
Bullet_Surface

Whatever you do, make sure it's consistent.  If your program gets big,
especially, similar sounding variables start getting used
interchangeably.  That's bad.  Make sure that each is different, and
that you know the difference.

Will you use classes for individual items?  Will you use lists?  I
like classes because they are organised, but lists because they are
fast.  If you have something simple, like a tennis ball you could make
a list like:
tennisballs = [[xpos,ypos,speedx,speedy],[xpos,ypos,speedx,speedy],[xpos,ypos,speedx,speedy],[xpos,ypos,speedx,speedy]]
OR, you could do:
class tennisball:
    def __init__(self):
        self.x = xpos
        self.y = ypos
        self.speedx = speedx
        self.speedy = speedy
tennisballs = []
tennisballs.append(tennisball())
tennisballs.append(tennisball())
tennisballs.append(tennisball())
tennisballs.append(tennisball())

Will you specify positions like: 'pos = [3,4]' or like 'posx = 3; posy
= 4'?  Separate variables or not?

There's a lot of stuff to think about, and it might seem obvious, but
until I thought about stuff like this, I had problems.

Ian