2012-05-12 12 views
9

मैं एक स्प्राइट शीट आयात करना चाहता हूं और एक स्प्राइट का चयन करना चाहता हूं। मैं पायथन/पायगम में यह कैसे करूं?पाइथन में एक स्प्राइट शीट से आप एक स्प्राइट छवि का चयन कैसे करते हैं?

+2

आपको अधिक जानकारी प्रदान करनी चाहिए, जैसे गेम डेवलपमेंट के लिए आप कौन सी पुस्तकालयों और ढांचे का उपयोग कर रहे हैं। समाधान उस पर निर्भर करेगा। –

+0

ओह। हाँ, मैं उल्लेख करना भूल गया कि मैं पायगम का उपयोग कर रहा हूं। – enrique2334

उत्तर

6

मैं इस बनाया है, यह आपकी रुचि की हों:

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 

आशा मैं

+1

मुझे एहसास है कि यह पुनरुत्थान एक बहुत पुराना उत्तर है, लेकिन मैं सोच रहा था कि आप set_clip और get_clip का उपयोग करने की परेशानी क्यों करते थे जब आप उस रेक्ट को पार कर सकते थे जिसे आप सीधे सब्सफ़ेस करना चाहते थे। – jlund3

+0

ओह वाह, यह लगभग एक साल पहले लॉल था। खैर, फिर भी मैं वास्तव में अभी भी नया था, भले ही मैं सवालों का जवाब दे रहा था, और यही वह था जो मैंने इसे सीखा। यह मेरे लिए थोड़ा सा क्लीनर भी लगता है। लेकिन आप सही हैं, आप बस खुद को आसानी से पारित कर सकते हैं। – hammythepig

6

ऊपर 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 

के लिए, इस पत्रक व्यक्ति स्प्राइट में एक पंक्ति एक समय में कटौती बाएं से दाएं: कि कोड के आधार पर, मैं इस सामान्यीकृत समारोह है कि किसी भी spritesheet लिए काम करता है बनाया है।

0

एक्वाशीप द्वारा पोस्ट किया गया उपरोक्त कोड शानदार है! हालांकि लाइनों में:
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

मैं -len_sprt_y और -len_sprt_x को दूर करने के लिए किया था कि यह सिर्फ पर x और y अक्ष पिछले स्प्राइट लोड नहीं होगा। यह स्प्राइट शीट एक्स, वाई आकार और वास्तविक स्प्राइट आकार के आधार पर भी भिन्न हो सकता है।

0

आपको अन्य उत्तरों में उल्लिखित set_clip और get_clip पर कॉल करने की आवश्यकता नहीं है। बस 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)) 
संबंधित मुद्दे