2016-01-07 49 views
0

我想用卡片製作遊戲,像Heartstone,但很簡單(因爲我不是親程序員)。這只是程序Pygame將表面轉換爲精靈

import pygame 
class Card: 
def AdCard(self, AdCard): 
    self.AdCard = AdCard 
def HpCard(self, HpCard): 
    self.HpCard = HpCard 
def Picture(self, Picture): 
    self.Picture = Picture 
def Special(self, Special): 
    if Special == "Heal": 
     pass 

pygame.init() 
display = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) 


swordsman = Card() 
swordsman_picture = pygame.image.load("Swordsman.png").convert() 
swordsman.Picture(swordsman_picture) 
print(type(swordsman.Picture)) 

的一部分,現在的問題是,它打印該類型的圖片是類的pygame.Surface',但我希望這幅畫是精靈。 如何做到這一點。 Tnx。

+0

'Sprite'是使用'Surface'保持圖像和'Rect'保持現在的位置和大小的類。 – furas

回答

0

Sprite是使用Surface來保持圖像和Rect保持位置和大小的一類。

class Card(pygame.sprite.Sprite): 

    def __init__(self, surface): 
     pygame.sprite.Sprite.__init__(self) 

     self.image = surface 

     self.rect = self.image.get_rect() # size and position 

# and then 

one_card = Card(swordsman_picture) 

(見pygame的文檔:pygame.sprite.Sprite

或可能,但餘did't之前

one_card = pygame.sprite.Sprite() 
one_card.image = swordsman_picture 
one_card.rect = one_card.image.get_rect() # size and position 

看到這個BTW:使用 「駝峯」 只有名字班名稱 - 使代碼更具可讀性 - 即使StackOveflor編輯器將Picture,AdCard等作爲類名稱並使用b顏色。對於函數和變量,使用lower_case名稱。


這似乎沒用

def Picture(self, Picture): 
    self.Picture = Picture 

swordsman.Picture(swordsman_picture) 

你可以做同樣的一條線 - 並使其更具可讀性。

swordsman.Picture = swordsman_picture