Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualRedLynx

Posted 05 March 2013 - 09:50 PM

Hey guys i have been making a game (trying to make a game) in PyGame how ever i'm afraid that my code is pretty messy and that the way i have coded things i might not be able to use sprite sheets to animate characters and whatnot i have tried various functions and modules on the internet to try to get my code use sprite sheets  i'm hoping you guys can help me see where i can clean up my code and hopefully help me implement a system to handle sprite sheets.

here's my source tongue.png

 

import sys
import pygame
import math
import random
from pygame.locals import *

# Define colours
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
magenta =(255, 45, 255)

# Variables
player_x = 0
player_y = 0
player_xspeed = 0
player_yspeed = 0
show_inventory = False

# Initialize Pygame
pygame.init()

# Set size of screen and stuff :P
size = (800, 480)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Engine")

# Loop until user exits the application
done = False

# Manage the screen update speed
clock = pygame.time.Clock()

# Mouse visibility
pygame.mouse.set_visible(0)
# Declare classes
class Character():
    level = 0
    experience = 0
    attributes = {"strength":0,"dexterity":0,"intelligence":0,"wisdom":0,"constitution":0,"charisma":0,"agility":0}
    items = {"h_potion":0,"m_potion":0,"antidote":0}
    equiped = {"w_primary":"none", "w_secondary":"none", "armor_helm":"none", "armor_gauntlets":"none", "armor_torso":"none", "armor_greaves":"none", "armor_boots":"none"}
    inventory = []

class Fighter(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] += 16
    Character.attributes["dexterity"] = 13
    Character.attributes["intelligence"] = 11
    Character.attributes["wisdom"] = 11
    Character.attributes["constitution"] = 12
    Character.attributes["charisma"] = 15
    Character.attributes["agility"] = 7
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "s_sword"
    Character.equiped["w_secondary"] = "leather_shield"
    Character.equiped["armor_torso"] = "leather cuircass"

class Rogue(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] = 13
    Character.attributes["dexterity"] = 16
    Character.attributes["intelligence"] = 13
    Character.attributes["wisdom"] = 11
    Character.attributes["constitution"] = 10
    Character.attributes["charisma"] = 16
    Character.attributes["agility"] = 8
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "knife"
    Character.equiped["armor_torso"] = "cloth_armor"

class Mage(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] = 12
    Character.attributes["dexterity"] = 13
    Character.attributes["intelligence"] = 13
    Character.attributes["wisdom"] = 16
    Character.attributes["constitution"] = 10
    Character.attributes["charisma"] = 9
    Character.attributes["agility"] = 5
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "q_staff"
    Character.equiped["armor_torso"] = "mage_robes"

class Sorcerer(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] = 14
    Character.attributes["dexterity"] = 15
    Character.attributes["intelligence"] = 16
    Character.attributes["wisdom"] = 13
    Character.attributes["constitution"] = 10
    Character.attributes["charisma"] = 10
    Character.attributes["agility"] = 5
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "q_staff"
    Character.equiped["armor_torso"] = "sorcerers_robes"

class Demigod(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] = 17
    Character.attributes["dexterity"]  = 16
    Character.attributes["intelligence"] = 16
    Character.attributes["wisdom"] = 15
    Character.attributes["constitution"] = 14
    Character.attributes["charisma"] = 15
    Character.attributes["agility"] = 8
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "l_sword"
    Character.equiped["w_secondary"] = "g_axe"
    Character.equiped["armor_torso"] = "iron_chainmail"
    Character.equiped["armor_helm"] = "iron_helm"
    Character.equiped["armor_gauntlets"] = "iron_gauntlets"
    Character.equiped["armor_greaves"] = "iron_greaves"
    Character.equiped["armor_boots"] = "iron_boots"

    def print_stats(self):
        print("Level: ", Character.level,"\nExperience: ", Character.experience)

class Player_sprite(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("player.png")
        self.image.convert()
        self.image.set_colorkey(black)
        self.rect = self.image.get_rect()

class Inventory_window(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("inventory_menu.png")
        self.image.convert()
        self.image.set_colorkey(black)
        self.rect = self.image.get_rect()

class Cursor(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("cursor.png")
        self.image.convert()
        self.rect = self.image.get_rect()

invent_w = Inventory_window()
invent_w.rect.x = 50
invent_w.rect.y = 50

m_cursor = Cursor()
non_collision_sprites = pygame.sprite.Group()
non_collision_sprites.add(m_cursor)

inventory_sprites = pygame.sprite.Group()
inventory_sprites.add(invent_w)

player = Player_sprite()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)

def inventory():
        inventory_sprites.draw(screen)
    

#---------- MAIN LOOP ----------
while done == False:
    # Event Processing BELOW
    # When the use clicks close, exit the main loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                player_yspeed = -8
            if event.key == pygame.K_DOWN:
                player_yspeed = 8
            if event.key == pygame.K_LEFT:
                player_xspeed = -8
            if event.key == pygame.K_RIGHT:
                player_xspeed = 8
            # Inventory
            if event.key == pygame.K_TAB:
                if show_inventory == True:
                    show_inventory = False
                else:
                    show_inventory = True

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                player_yspeed = 0
            if event.key == pygame.K_DOWN:
                player_yspeed = 0
            if event.key == pygame.K_LEFT:
                player_xspeed = 0
            if event.key == pygame.K_RIGHT:
                player_xspeed = 0

                
    """player_x = player_x + player_xspeed
    player_y = player_y + player_yspeed
    player.rect.x = player_x
    player.rect.y = player_y"""

    # Event Processing ABOVE


    # Game Logic BELOW
    #Get mouse Position
    mouse_pos = pygame.mouse.get_pos()
    mouse_x = mouse_pos[0]
    mouse_y = mouse_pos[1]
    m_cursor.rect.x = mouse_x
    m_cursor.rect.y = mouse_y

    # Game Logic ABOVE


    # Draw Code BELOW
    # Clear Screen
    screen.fill(black)

    # Draw sprites
    all_sprites.draw(screen)
    non_collision_sprites.draw(screen)
    if show_inventory == True:
        inventory()
        
    # Draw Code ABOVE


    # Update screen
    pygame.display.flip()

    # Limit Framerate
    clock.tick(20)

# Exit pygame
pygame.quit() 

 

Thanks Guys


#3RedLynx

Posted 05 March 2013 - 09:50 PM

Hey guys i have been making a game (trying to make a game) in PyGame how ever i'm afraid that my code is pretty messy and that the way i have coded things i might not be able to use sprite sheets to animate characters and whatnot i have tried various functions and modules on the internet to try to get my code use sprite sheets  i'm hoping you guys can help me see where i can clean up my code and hopefully help me implement a system to handle sprite sheets.

here's my source tongue.png

 

import sys
import pygame
import math
import random
from pygame.locals import *

# Define colours
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
magenta =(255, 45, 255)

# Variables
player_x = 0
player_y = 0
player_xspeed = 0
player_yspeed = 0
show_inventory = False

# Initialize Pygame
pygame.init()

# Set size of screen and stuff :P
size = (800, 480)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Engine")

# Loop until user exits the application
done = False

# Manage the screen update speed
clock = pygame.time.Clock()

# Mouse visibility
pygame.mouse.set_visible(0)
# Declare classes
class Character():
    level = 0
    experience = 0
    attributes = {"strength":0,"dexterity":0,"intelligence":0,"wisdom":0,"constitution":0,"charisma":0,"agility":0}
    items = {"h_potion":0,"m_potion":0,"antidote":0}
    equiped = {"w_primary":"none", "w_secondary":"none", "armor_helm":"none", "armor_gauntlets":"none", "armor_torso":"none", "armor_greaves":"none", "armor_boots":"none"}
    inventory = []

class Fighter(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] += 16
    Character.attributes["dexterity"] = 13
    Character.attributes["intelligence"] = 11
    Character.attributes["wisdom"] = 11
    Character.attributes["constitution"] = 12
    Character.attributes["charisma"] = 15
    Character.attributes["agility"] = 7
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "s_sword"
    Character.equiped["w_secondary"] = "leather_shield"
    Character.equiped["armor_torso"] = "leather cuircass"

class Rogue(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] = 13
    Character.attributes["dexterity"] = 16
    Character.attributes["intelligence"] = 13
    Character.attributes["wisdom"] = 11
    Character.attributes["constitution"] = 10
    Character.attributes["charisma"] = 16
    Character.attributes["agility"] = 8
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "knife"
    Character.equiped["armor_torso"] = "cloth_armor"

class Mage(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] = 12
    Character.attributes["dexterity"] = 13
    Character.attributes["intelligence"] = 13
    Character.attributes["wisdom"] = 16
    Character.attributes["constitution"] = 10
    Character.attributes["charisma"] = 9
    Character.attributes["agility"] = 5
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "q_staff"
    Character.equiped["armor_torso"] = "mage_robes"

class Sorcerer(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] = 14
    Character.attributes["dexterity"] = 15
    Character.attributes["intelligence"] = 16
    Character.attributes["wisdom"] = 13
    Character.attributes["constitution"] = 10
    Character.attributes["charisma"] = 10
    Character.attributes["agility"] = 5
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "q_staff"
    Character.equiped["armor_torso"] = "sorcerers_robes"

class Demigod(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] = 17
    Character.attributes["dexterity"]  = 16
    Character.attributes["intelligence"] = 16
    Character.attributes["wisdom"] = 15
    Character.attributes["constitution"] = 14
    Character.attributes["charisma"] = 15
    Character.attributes["agility"] = 8
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "l_sword"
    Character.equiped["w_secondary"] = "g_axe"
    Character.equiped["armor_torso"] = "iron_chainmail"
    Character.equiped["armor_helm"] = "iron_helm"
    Character.equiped["armor_gauntlets"] = "iron_gauntlets"
    Character.equiped["armor_greaves"] = "iron_greaves"
    Character.equiped["armor_boots"] = "iron_boots"

    def print_stats(self):
        print("Level: ", Character.level,"\nExperience: ", Character.experience)

class Player_sprite(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("player.png")
        self.image.convert()
        self.image.set_colorkey(black)
        self.rect = self.image.get_rect()

class Inventory_window(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("inventory_menu.png")
        self.image.convert()
        self.image.set_colorkey(black)
        self.rect = self.image.get_rect()

class Cursor(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("cursor.png")
        self.image.convert()
        self.rect = self.image.get_rect()

invent_w = Inventory_window()
invent_w.rect.x = 50
invent_w.rect.y = 50

m_cursor = Cursor()
non_collision_sprites = pygame.sprite.Group()
non_collision_sprites.add(m_cursor)

inventory_sprites = pygame.sprite.Group()
inventory_sprites.add(invent_w)

player = Player_sprite()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)

def inventory():
        inventory_sprites.draw(screen)
    

#---------- MAIN LOOP ----------
while done == False:
    # Event Processing BELOW
    # When the use clicks close, exit the main loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                player_yspeed = -8
            if event.key == pygame.K_DOWN:
                player_yspeed = 8
            if event.key == pygame.K_LEFT:
                player_xspeed = -8
            if event.key == pygame.K_RIGHT:
                player_xspeed = 8
            # Inventory
            if event.key == pygame.K_TAB:
                if show_inventory == True:
                    show_inventory = False
                else:
                    show_inventory = True

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                player_yspeed = 0
            if event.key == pygame.K_DOWN:
                player_yspeed = 0
            if event.key == pygame.K_LEFT:
                player_xspeed = 0
            if event.key == pygame.K_RIGHT:
                player_xspeed = 0

                
    """player_x = player_x + player_xspeed
    player_y = player_y + player_yspeed
    player.rect.x = player_x
    player.rect.y = player_y"""

    # Event Processing ABOVE


    # Game Logic BELOW
    #Get mouse Position
    mouse_pos = pygame.mouse.get_pos()
    mouse_x = mouse_pos[0]
    mouse_y = mouse_pos[1]
    m_cursor.rect.x = mouse_x
    m_cursor.rect.y = mouse_y

    # Game Logic ABOVE


    # Draw Code BELOW
    # Clear Screen
    screen.fill(black)

    # Draw sprites
    all_sprites.draw(screen)
    non_collision_sprites.draw(screen)
    if show_inventory == True:
        inventory()
        
    # Draw Code ABOVE


    # Update screen
    pygame.display.flip()

    # Limit Framerate
    clock.tick(20)

# Exit pygame
pygame.quit() 

 

Thanks Guys


#2RedLynx

Posted 05 March 2013 - 09:50 PM

Hey guys i have been making a game (trying to make a game) in PyGame how ever i'm afraid that my code is pretty messy and that the way i have coded things i might not be able to use sprite sheets to animate characters and whatnot i have tried various functions and modules on the internet to try to get my code use sprite sheets  i'm hoping you guys can help me see where i can clean up my code and hopefully help me implement a system to handle sprite sheets.

here's my source tongue.png

 

import sys
import pygame
import math
import random
from pygame.locals import *

# Define colours
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
magenta =(255, 45, 255)

# Variables
player_sprt_rect_x = 5
player_sprt_rect_y = 0
length_plr_x = 16
length_plr_y = 32
player_x = 0
player_y = 0
player_xspeed = 0
player_yspeed = 0
show_inventory = False

# Initialize Pygame
pygame.init()

# Set size of screen and stuff :P
size = (800, 480)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Engine")

# Loop until user exits the application
done = False

# Manage the screen update speed
clock = pygame.time.Clock()

# Mouse visibility
pygame.mouse.set_visible(0)
# Declare classes
class Character():
    level = 0
    experience = 0
    attributes = {"strength":0,"dexterity":0,"intelligence":0,"wisdom":0,"constitution":0,"charisma":0,"agility":0}
    items = {"h_potion":0,"m_potion":0,"antidote":0}
    equiped = {"w_primary":"none", "w_secondary":"none", "armor_helm":"none", "armor_gauntlets":"none", "armor_torso":"none", "armor_greaves":"none", "armor_boots":"none"}
    inventory = []

class Fighter(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] += 16
    Character.attributes["dexterity"] = 13
    Character.attributes["intelligence"] = 11
    Character.attributes["wisdom"] = 11
    Character.attributes["constitution"] = 12
    Character.attributes["charisma"] = 15
    Character.attributes["agility"] = 7
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "s_sword"
    Character.equiped["w_secondary"] = "leather_shield"
    Character.equiped["armor_torso"] = "leather cuircass"

class Rogue(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] = 13
    Character.attributes["dexterity"] = 16
    Character.attributes["intelligence"] = 13
    Character.attributes["wisdom"] = 11
    Character.attributes["constitution"] = 10
    Character.attributes["charisma"] = 16
    Character.attributes["agility"] = 8
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "knife"
    Character.equiped["armor_torso"] = "cloth_armor"

class Mage(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] = 12
    Character.attributes["dexterity"] = 13
    Character.attributes["intelligence"] = 13
    Character.attributes["wisdom"] = 16
    Character.attributes["constitution"] = 10
    Character.attributes["charisma"] = 9
    Character.attributes["agility"] = 5
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "q_staff"
    Character.equiped["armor_torso"] = "mage_robes"

class Sorcerer(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] = 14
    Character.attributes["dexterity"] = 15
    Character.attributes["intelligence"] = 16
    Character.attributes["wisdom"] = 13
    Character.attributes["constitution"] = 10
    Character.attributes["charisma"] = 10
    Character.attributes["agility"] = 5
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "q_staff"
    Character.equiped["armor_torso"] = "sorcerers_robes"

class Demigod(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] = 17
    Character.attributes["dexterity"]  = 16
    Character.attributes["intelligence"] = 16
    Character.attributes["wisdom"] = 15
    Character.attributes["constitution"] = 14
    Character.attributes["charisma"] = 15
    Character.attributes["agility"] = 8
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "l_sword"
    Character.equiped["w_secondary"] = "g_axe"
    Character.equiped["armor_torso"] = "iron_chainmail"
    Character.equiped["armor_helm"] = "iron_helm"
    Character.equiped["armor_gauntlets"] = "iron_gauntlets"
    Character.equiped["armor_greaves"] = "iron_greaves"
    Character.equiped["armor_boots"] = "iron_boots"

    def print_stats(self):
        print("Level: ", Character.level,"\nExperience: ", Character.experience)

class Player_sprite(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("player.png")
        self.image.convert()
        self.image.set_colorkey(black)
        self.rect = self.image.get_rect()

class Inventory_window(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("inventory_menu.png")
        self.image.convert()
        self.image.set_colorkey(black)
        self.rect = self.image.get_rect()

class Cursor(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("cursor.png")
        self.image.convert()
        self.rect = self.image.get_rect()

invent_w = Inventory_window()
invent_w.rect.x = 50
invent_w.rect.y = 50

m_cursor = Cursor()
non_collision_sprites = pygame.sprite.Group()
non_collision_sprites.add(m_cursor)

inventory_sprites = pygame.sprite.Group()
inventory_sprites.add(invent_w)

player = Player_sprite()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)

def inventory():
        inventory_sprites.draw(screen)
    

#---------- MAIN LOOP ----------
while done == False:
    # Event Processing BELOW
    # When the use clicks close, exit the main loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                player_yspeed = -8
            if event.key == pygame.K_DOWN:
                player_yspeed = 8
            if event.key == pygame.K_LEFT:
                player_xspeed = -8
            if event.key == pygame.K_RIGHT:
                player_xspeed = 8
            # Inventory
            if event.key == pygame.K_TAB:
                if show_inventory == True:
                    show_inventory = False
                else:
                    show_inventory = True

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                player_yspeed = 0
            if event.key == pygame.K_DOWN:
                player_yspeed = 0
            if event.key == pygame.K_LEFT:
                player_xspeed = 0
            if event.key == pygame.K_RIGHT:
                player_xspeed = 0

                
    """player_x = player_x + player_xspeed
    player_y = player_y + player_yspeed
    player.rect.x = player_x
    player.rect.y = player_y"""

    # Event Processing ABOVE


    # Game Logic BELOW
    #Get mouse Position
    mouse_pos = pygame.mouse.get_pos()
    mouse_x = mouse_pos[0]
    mouse_y = mouse_pos[1]
    m_cursor.rect.x = mouse_x
    m_cursor.rect.y = mouse_y

    # Game Logic ABOVE


    # Draw Code BELOW
    # Clear Screen
    screen.fill(black)

    # Draw sprites
    all_sprites.draw(screen)
    non_collision_sprites.draw(screen)
    if show_inventory == True:
        inventory()
        
    # Draw Code ABOVE


    # Update screen
    pygame.display.flip()

    # Limit Framerate
    clock.tick(20)

# Exit pygame
pygame.quit() 

 

Thanks Guys


#1RedLynx

Posted 05 March 2013 - 09:49 PM

Hey guys i have been making a game (trying to make a game) in PyGame how ever i'm afraid that my code is pretty messy and that the way i have coded things i might not be able to use sprite sheets to animate characters and whatnot i have tried various functions and modules on the internet to try to get my code use sprite sheets  i'm hoping you guys can help me see where i can clean up my code and hopefully help me implement a system to handle sprite sheets.

here's my source :P 

 

import sys
import pygame
import math
import random
from pygame.locals import *

# Define colours
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
magenta =(255, 45, 255)

# Variables
player_sprt_rect_x = 5
player_sprt_rect_y = 0
length_plr_x = 16
length_plr_y = 32
player_x = 0
player_y = 0
player_xspeed = 0
player_yspeed = 0
show_inventory = False

# Initialize Pygame
pygame.init()

# Set size of screen and stuff :P
size = (800, 480)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Engine")

# Loop until user exits the application
done = False

# Manage the screen update speed
clock = pygame.time.Clock()

def sprite_sheet(size,file,pos=(0,0)):

    #Initial Values
    len_sprt_x,len_sprt_y = size #sprite size
    sprt_rect_x,sprt_rect_y = pos #where to find first sprite on sheet

    sheet = pygame.image.load(file).convert_alpha() #Load the sheet
    sheet_rect = sheet.get_rect()
    sprites = []
    print(sheet_rect.height, sheet_rect.width)
    for i in range(0,sheet_rect.height-len_sprt_y,size[1]):#rows
        print("row")
        for i in range(0,sheet_rect.width-len_sprt_x,size[0]):#columns
            print("column")
            sheet.set_clip(pygame.Rect(sprt_rect_x, sprt_rect_y, len_sprt_x, len_sprt_y)) #find sprite you want
            sprite = sheet.subsurface(sheet.get_clip()) #grab the sprite you want
            sprites.append(sprite)
            sprt_rect_x += len_sprt_x

        sprt_rect_y += len_sprt_y
        sprt_rect_x = 0
    print(sprites)
    return sprites

# Mouse visibility
pygame.mouse.set_visible(0)
# Declare classes
class Character():
    level = 0
    experience = 0
    attributes = {"strength":0,"dexterity":0,"intelligence":0,"wisdom":0,"constitution":0,"charisma":0,"agility":0}
    items = {"h_potion":0,"m_potion":0,"antidote":0}
    equiped = {"w_primary":"none", "w_secondary":"none", "armor_helm":"none", "armor_gauntlets":"none", "armor_torso":"none", "armor_greaves":"none", "armor_boots":"none"}
    inventory = []

class Fighter(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] += 16
    Character.attributes["dexterity"] = 13
    Character.attributes["intelligence"] = 11
    Character.attributes["wisdom"] = 11
    Character.attributes["constitution"] = 12
    Character.attributes["charisma"] = 15
    Character.attributes["agility"] = 7
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "s_sword"
    Character.equiped["w_secondary"] = "leather_shield"
    Character.equiped["armor_torso"] = "leather cuircass"

class Rogue(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] = 13
    Character.attributes["dexterity"] = 16
    Character.attributes["intelligence"] = 13
    Character.attributes["wisdom"] = 11
    Character.attributes["constitution"] = 10
    Character.attributes["charisma"] = 16
    Character.attributes["agility"] = 8
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "knife"
    Character.equiped["armor_torso"] = "cloth_armor"

class Mage(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] = 12
    Character.attributes["dexterity"] = 13
    Character.attributes["intelligence"] = 13
    Character.attributes["wisdom"] = 16
    Character.attributes["constitution"] = 10
    Character.attributes["charisma"] = 9
    Character.attributes["agility"] = 5
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "q_staff"
    Character.equiped["armor_torso"] = "mage_robes"

class Sorcerer(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] = 14
    Character.attributes["dexterity"] = 15
    Character.attributes["intelligence"] = 16
    Character.attributes["wisdom"] = 13
    Character.attributes["constitution"] = 10
    Character.attributes["charisma"] = 10
    Character.attributes["agility"] = 5
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "q_staff"
    Character.equiped["armor_torso"] = "sorcerers_robes"

class Demigod(Character):
    Character.level = 1
    Character.experience = 0
    Character.attributes["strength"] = 17
    Character.attributes["dexterity"]  = 16
    Character.attributes["intelligence"] = 16
    Character.attributes["wisdom"] = 15
    Character.attributes["constitution"] = 14
    Character.attributes["charisma"] = 15
    Character.attributes["agility"] = 8
    Character.items["h_potion"] = 2
    Character.items["m_potion"] = 2
    Character.items["antidote"] = 1
    Character.equiped["w_primary"] = "l_sword"
    Character.equiped["w_secondary"] = "g_axe"
    Character.equiped["armor_torso"] = "iron_chainmail"
    Character.equiped["armor_helm"] = "iron_helm"
    Character.equiped["armor_gauntlets"] = "iron_gauntlets"
    Character.equiped["armor_greaves"] = "iron_greaves"
    Character.equiped["armor_boots"] = "iron_boots"

    def print_stats(self):
        print("Level: ", Character.level,"\nExperience: ", Character.experience)

class Player_sprite(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("player.png")
        self.image.convert()
        self.image.set_colorkey(black)
        self.rect = self.image.get_rect()

class Inventory_window(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("inventory_menu.png")
        self.image.convert()
        self.image.set_colorkey(black)
        self.rect = self.image.get_rect()

class Cursor(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("cursor.png")
        self.image.convert()
        self.rect = self.image.get_rect()

invent_w = Inventory_window()
invent_w.rect.x = 50
invent_w.rect.y = 50

m_cursor = Cursor()
non_collision_sprites = pygame.sprite.Group()
non_collision_sprites.add(m_cursor)

inventory_sprites = pygame.sprite.Group()
inventory_sprites.add(invent_w)

player = Player_sprite()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)

def inventory():
        inventory_sprites.draw(screen)
    

#---------- MAIN LOOP ----------
while done == False:
    # Event Processing BELOW
    # When the use clicks close, exit the main loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                player_yspeed = -8
            if event.key == pygame.K_DOWN:
                player_yspeed = 8
            if event.key == pygame.K_LEFT:
                player_xspeed = -8
            if event.key == pygame.K_RIGHT:
                player_xspeed = 8
            # Inventory
            if event.key == pygame.K_TAB:
                if show_inventory == True:
                    show_inventory = False
                else:
                    show_inventory = True

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                player_yspeed = 0
            if event.key == pygame.K_DOWN:
                player_yspeed = 0
            if event.key == pygame.K_LEFT:
                player_xspeed = 0
            if event.key == pygame.K_RIGHT:
                player_xspeed = 0

                
    """player_x = player_x + player_xspeed
    player_y = player_y + player_yspeed
    player.rect.x = player_x
    player.rect.y = player_y"""

    # Event Processing ABOVE


    # Game Logic BELOW
    #Get mouse Position
    mouse_pos = pygame.mouse.get_pos()
    mouse_x = mouse_pos[0]
    mouse_y = mouse_pos[1]
    m_cursor.rect.x = mouse_x
    m_cursor.rect.y = mouse_y

    # Game Logic ABOVE


    # Draw Code BELOW
    # Clear Screen
    screen.fill(black)

    # Draw sprites
    all_sprites.draw(screen)
    non_collision_sprites.draw(screen)
    if show_inventory == True:
        inventory()
        
    # Draw Code ABOVE


    # Update screen
    pygame.display.flip()

    # Limit Framerate
    clock.tick(20)

# Exit pygame
pygame.quit() 

 

Thanks Guys


PARTNERS