PyGame Sprite Sheet Help

Started by
1 comment, last by RedLynx 11 years, 1 month ago

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

Advertisement

Before working on how to handle sprite sheets I would recommend some reading on Python classes and attributes. http://docs.python.org/2/tutorial/classes.html#class-objects will specifically point out some flaws I saw from a quick glance with how you are handling your Character base and sub classes, specifically in regards to class objects and attribute references.

Once you read about class objects (I would recommend you read the entire tutorial on Python classes) take a look at how you are handling characters. You will be able to see which items should be attribute references, and which should be data attributes.

Learning more about Python classes will help you towards cleaning up your code. As for handling sprite sheets a simple google search for "pygame sprite sheet" will give you very useful results. There are actually several code snippets that people wrote for handling just that in PyGame.

Happy coding!

Thanks for your help dmreichard. when i made those classes i knew in the back of my mind that there was probably a better way for me to do it i just didnt know how. also eventually found a good sprite sheet snippet that needed a bit of tweaking for use with python 3.3 and to fit nicely with my code.

Thanks :D

This topic is closed to new replies.

Advertisement