Hi all,

I am fairly confident with Python, and am now turning my focus from learning how to program the language to learning to program it well. I am wondering what the best way to approch classes which have too many variables in them.

I an writing a platformer game, and, though it works fine, I am trying to clean up the code. My Player class is especially messy.

Here is an example:

class Player(obj.PhySprite):

    warp = None #this stores the function to change levels. It should take the
                #same arguments as the Level class 
    shards = 0 #shards collected
    points = 0
    lives = 5

    IS_PLAYER = True
    FALLS = True
    MOVES = True
    IS_ITEM = True

    entrance_coords = None #the normal coords when the player entered the
                           #portal to the level. When he dies/wins he is
                           #returned to this location.

    CLIMB_VEL = 1.25 #how fast he climbs up/down/left/right
    WALK_ACCEL = .1 #how fast he normally accelerates
    RUN_ACCEL = .02 #how fast he accelerates when running or dashing

    DECREST_ACCEL = .4 #when the jump key is releaced this is added to
                       #gravity until he stops going up and starts

    DEACCEL = .4 #when slowing down or turning this is additional accel

    MAX_WALK = 1  #max walking speed
    MAX_RUN = 2.95 #max running speed
    MAX_DASH = 3  #max dashing speed
    MAX_VEL = 5   #max overall speed
    MAX_FALL = MAX_VEL #max falling speed
    MAX_TWIRL = 25
    JUMP_VEL = 5.7 #the up velocity given when jump key is pressed
    SPIN_JUMP_VEL = 5 #the up velocity given when jump key is pressed
    TAP_JUMP_VEL = 4 #the up velocity given when spin jump onto something
                   #tappable when not holding spin key.
    RUN_JUMPVEL_RATIO = 1/2.8 #this times abs(horz speed) is added to
                              #initial jump velocity

    is_small = True #Powered up and tall or small?
    is_caped = False #Has a cape?

    run_button = False #is run button being held? Used to find when it
                       #it is first pressed
    grounded = False #player's feet touching ground.
    standing_on = None #the object the player's feet is touching
    climbing = False #on ladder and holding on
    fell_off = False #fell off the ladder.
    can_jump = False #is able to jump (on ground or ladder and not holding
                     #jump key from previouse jump)
    skidding = False #turning one direction with momentum in another
    turning = False #facing the screen after skidding.
    ascending = False
    ascending_counter = 0
    glide_dashed = False
    gliding = False
    twirling = False
    twirling_counter = 0

    direction = 1 #direction of motion -1 is left 1 is right

    frame = 0 #framecounter for animations

    spin_jump = False #spin jumping

    fainting = False
    faint_timer = 0

    blinking = False
    blink_timer = 0

    holding = None #item player is holding.

    kicking = 0 #timer for holding out player's foot when kicking.

    destroyed = 0 #keeps track of combo kills

    pocket = None #contents of player's pocket
    falling_item = None

    won = False #Got the shard
    winning_number = None #The shard player got
    won_frame = 0
    won_timer = -15
    final_explosion = None

    holding_up = False #has the player been holding the up key.
                       #Used to detect if he should go though a portal or just
                       #walked by one while holding the up key.
def __init__(self):
    code, code, code

As you can see, most of these are just counters, timers, constants, and flags. How is the most pythonic way to sort them? The way it is now is rather confusing. I have thought about breaking off chunks of variables into classes just meant to store them, but I don't think that is the best way.

What is generally good practise for this?

4 Years
Discussion Span
Last Post by Sudo Bash

That configparser looks useful, I have been useing configuration files, but I was just wondering what to do with the clutter. Thanks for the link.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.