• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
RedLynx

PyGame Sprite Sheet Help

2 posts in this topic

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

Edited by RedLynx
0

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0