如何从Python中的精灵工作表中选择精灵图像?
我想导入一个精灵工作表并选择一个精灵。我将如何在Python/pygame中实现这一点?我制作了这个,您可能会感兴趣:如何从Python中的精灵工作表中选择精灵图像?,python,pygame,Python,Pygame,我想导入一个精灵工作表并选择一个精灵。我将如何在Python/pygame中实现这一点?我制作了这个,您可能会感兴趣: import pygame, sys from pygame.locals import * SCREEN_X=400 SCREEN_Y=400 #Screen size SPRT_RECT_X=0 SPRT_RECT_Y=0 #This is where the sprite is found on the sheet LEN_SPRT_X=100 LEN_SPRT
import pygame, sys
from pygame.locals import *
SCREEN_X=400
SCREEN_Y=400
#Screen size
SPRT_RECT_X=0
SPRT_RECT_Y=0
#This is where the sprite is found on the sheet
LEN_SPRT_X=100
LEN_SPRT_Y=100
#This is the length of the sprite
screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y)) #Create the screen
sheet = pygame.image.load('C:\YOURFILE') #Load the sheet
sheet.set_clip(pygame.Rect(SPRT_RECT_X, SPRT_RECT_Y, LEN_SPRT_X, LEN_SPRT_Y)) #Locate the sprite you want
draw_me = sheet.subsurface(sheet.get_clip()) #Extract the sprite you want
backdrop = pygame.Rect(0, 0, SCREEN_X, SCREEN_Y) #Create the whole screen so you can draw on it
screen.blit(draw_me,backdrop) #'Blit' on the backdrop
pygame.display.flip()
#Draw the sprite on the screen
希望我能帮忙上面的spritesheet loader是一个很好的方法。基于该代码,我创建了适用于任何精灵表的通用函数:
#!/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 *
def sprite_sheet(size,file,pos=(0,0)):
#Initial Values
len_sprt_x,len_sprt_y = size #sprite 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
作为参考,这将从左到右一次将纸张切割成一行单独的精灵。以上由aquasheep发布的代码非常出色!尽管在线路上: 对于范围为0的i,图纸垂直高度长度,大小[1]:行 对于范围为0的i,活页长度宽度为x,大小为[0]:列 我必须删除-len_sprt_y和-len_sprt_x,因为它无法加载x轴和y轴上的最后一个精灵。它可能会根据精灵图纸的x、y尺寸和实际精灵尺寸而有所不同 您不需要像其他答案中提到的那样调用set_clip和get_clip。只需将rect、tuple或list传递给
你应该提供更多的信息,比如你在游戏开发中使用的库和框架。解决方案很可能取决于此。是的,我忘了提到我正在使用Pygame。我意识到这是一个非常古老的答案,但我想知道为什么你会不厌其烦地使用set_clip和get_clip,而你本可以直接通过你想要的Rect。哦,哇,这是一年前的lol。嗯,那时候,尽管我在回答问题,但实际上我对pygame还是很陌生,这就是我学习pygame的方式。对我来说,它看起来也有点干净。但是你是对的,你可以很容易地通过Rect本身。
# The size of a sprite in this example is 64x64 pixels.
x_coord = 64 * 2 # This would be the third column.
y_coord = 64 * 3 # Fourth row.
width = 64
height = 64
sheet = pygame.image.load('your_sprite_sheet.png').convert_alpha()
single_image = sheet.subsurface((x_coord, y_coord, width, height))