Voglio importare un foglio sprite e selezionare uno sprite. Come lo farei in Python/pygame?Come si seleziona un'immagine sprite da un foglio sprite in Python?
risposta
ho fatto questo, potrebbe interessarvi:
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
Spero potrebbe aiutare
Mi rendo conto che questa risurrezione è una risposta piuttosto vecchia, ma mi chiedevo perché ti sei preso la briga di usare set_clip e get_clip quando avresti potuto passare il Rect che volevi sottosuperare direttamente. – jlund3
Oh wow, questo è stato da quasi un anno fa lol. Bene, all'epoca ero ancora nuovo in Pygame anche se stavo rispondendo alle domande, ed è stato proprio così che l'ho imparato. Mi sembra anche un po 'più pulito così com'è. Ma tu hai ragione, potresti altrettanto facilmente passare il Rect stesso. – hammythepig
È possibile che questo spritesheet caricatore è un buon metodo. Sulla base di tale codice, feci questa funzione generalizzata che funziona per qualsiasi spritesheet:
#!/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
Per riferimento, questo riduce i fogli in singoli sprites una riga alla volta da sinistra a destra.
Il codice sopra postato da aquasheep è geniale! Sebbene in linea:
for i in range(0,sheet_rect.height-len_sprt_y,size[1]):#rows
for i in range(0,sheet_rect.width-len_sprt_x,size[0]):#columns
ho dovuto togliere -len_sprt_y
e -len_sprt_x
semplicemente non sarebbe caricare gli ultimi sprite sul xe asse y. Può variare a seconda delle dimensioni x, y della sprite sheet e della dimensione effettiva dello sprite.
Non è necessario chiamare set_clip
e get_clip
come indicato nelle altre risposte. Basta passare un rect, una tupla o un elenco a Surface.subsurface
.
# 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))
È necessario fornire ulteriori informazioni, ad esempio le librerie e i framework che si utilizzano per lo sviluppo del gioco. La soluzione dipenderà probabilmente da questo. –
oh. Sì, ho dimenticato di menzionare che sto usando Pygame. – enrique2334