所以我正在测试我在这个网站上找到的这个spritesheet代码,并且从我写的另一个python文件中,我试图将spritesheet传递给它,在那里它将它切成一系列图像,并允许在我的游戏中为我的怪物制作动画。
但是,当我尝试运行它时,我收到此错误:
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