Должна быть строка без нулевых байтов, а не строка в pygame

Итак, я тестирую этот код таблицы спрайтов, который я нашел на этом сайте, и из другого файла Python, который я написал, я пытаюсь пойти и передать ему таблицу спрайтов, где он разрежет ее на серию изображений и позволит чтобы оживить моего монстра в моей игре.

Однако, когда я пытаюсь запустить его, я получаю эту ошибку:

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

Мне сказали использовать CStringIO для обработки ввода и вывода изображений, но даже после просмотра документации я все еще немного не понимаю. Что здесь происходит?

Код, который я написал для червя:

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()

И код, который я использую для 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

person user1768884    schedule 20.10.2013    source источник
comment
Почему вы используете cStringIO в первую очередь? Что не так с просто pygame.image.load?   -  person sloth    schedule 20.10.2013
comment
Я пробовал это, но я получил сообщение об ошибке «Не могу искать в этом источнике данных».   -  person user1768884    schedule 21.10.2013
comment
Код, который вы используете с помощью pygame.image.load, имеет смысл. Покажите код, который дает ошибку, используя это.   -  person ninMonkey    schedule 21.10.2013