2

So I'm testing out this spritesheet code that I found on this site, and from another python file I wrote, I am trying to go and pass a spritesheet to it, where it will cut it up into a series of images, and allow to animate my monster in my game.

However, when I try to run it, I get this error:

python wormTest.py 
Traceback (most recent call last):
  File "wormTest.py", line 49, in <module>
    worm = Worm()
  File "wormTest.py", line 38, in __init__
    img = pygame.image.load(outfile.getvalue())
TypeError: must be string without null bytes, not str

I was told to use CStringIO to help handle image input and output, but even after looking at the documentation, it is still a bit fuzzy to me. What is going on here?

The code I wrote for the worm:

import pygame 
from sprite_sheet import sprite_sheet #handles sprite sheet stuff
import cStringIO #input output for images


# Define some colors
black    = (   0,   0,   0)
white    = ( 255, 255, 255)
red      = ( 255,   0,   0)

#initialize pygame
pygame.init()

#set the height and width of the screen
width = 800
height = 480

mainScreen = pygame.display.set_mode([width,height])

#A list of all of the sprites in the game
all_sprites_list = pygame.sprite.Group()

with open("big_worm.png", "rb") as infile:
    outfile = cStringIO.StringIO()
    outfile.write(infile.read())



class Worm(pygame.sprite.Sprite):
    '''class that builds up the worm sprite'''
    #constructor function
    def __init__(self):

        #call up the parent's constructor
        pygame.sprite.Sprite.__init__(self)

        images =[]
        img = pygame.image.load(outfile.getvalue())

        img.set_colorkey(white)#sets the color key. any pixel with same color as colorkey will be trasnparent

        images = sprite_sheet(img, 40) #make up a sprite sheet

        self.image = images[0]

        self.rect = self.image.get_rect()

#creates a player object
worm = Worm()
#adds the player object to the all_sprites_list
all_sprites_list.add(worm)

#a conditional for the loop that keeps the game running until the user Xes out
done = False

#clock for the screen updates
clock = pygame.time.Clock()

#
#   Game Logic Code
#

while done==False:
    for event in pygame.event.get(): #user did something
        if event.type == pygame.QUIT: #if the user hit the close button
                done=True
                mainScreen.fill(white)#makes the background white, and thus, the white part of the images will be invisible
    #player.changeImage()

    #limit the game to 20 fps
    clock.tick(20)

    #update the screen on the regular
    pygame.display.flip()

pygame.quit()

And the code I'm using for the sprite_sheet:

#!/usr/bin/python
#
# Sprite Sheet Loader - hammythepig
#
# Edited by Peter Kennedy
#
# License - Attribution - hammythepig
    #http://stackoverflow.com/questions/10560446/how-do-you-select-a-sprite-image-from-a-sprite-sheet-in-python
#
# Version = '2.0'

import pygame,sys
from pygame.locals import *
import cStringIO


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

    #Initial Values
    len_sprt_x = size #sprite size
    len_sprt_y = 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

#VERSION HISTORY

    #1.1 - turned code into useable function
#2.0 - fully functional sprite sheet loader
user1768884
  • 1,079
  • 1
  • 20
  • 34

0 Answers0