[英]Having issues with making Sprites transparent in pygame! (image included inside)
[英]Making sprites inside/outside of a class
我已經將所有代碼獨立運行。 我需要開始將所有鏈接鏈接到按鈕。
問題:嘗試將多個按鈕設置為精靈,以實現碰撞目的。 在課外不知道該怎么做。
我的按鈕在單獨的類中工作,但由於顯而易見的原因,無法使它們在同一類中工作,第二個按鈕的self.image覆蓋了第一個按鈕。
class Icons(pygame.sprite.Sprite):
def __init__(self, *args):
pygame.sprite.Sprite.__init__(self, *args)
self.image = pygame.image.load("images/airbrushIC.gif").convert()
self.rect = self.image.get_rect()
ic1 = self.image
self.rect.x = 50
self.rect.y = 490
self.image = pygame.image.load("images/fillIC.gif").convert()
self.rect = self.image.get_rect()
ic2 = self.image
self.rect.x = 10
self.rect.y = 540
def update(self):
pygame.mouse.get_pos()
pygame.mouse.get_pressed()
這段代碼不必是一個類。 但是我不知道如何在不進入課堂的情況下使圖像成為精靈。 任何幫助將不勝感激!
而不是圖標,您應該具有通用的Icon類。 然后,您可以為每個按鈕創建一個Icon實例。
class Icon(pygame.sprite.Sprite):
def __init__(self, image_name, pos, cb, cb_data, *args):
pygame.sprite.Sprite.__init__(self, *args)
self.image = pygame.image.load("images/" + image_name).convert()
self.rect = self.image.get_rect()
self.rect.x = pos[0]
self.rect.y = pos[1]
this.cb = cb # function to call when button is pressed
this.cb_data = cb_data # data to pass to the function
def pressed():
this.cb(cb_data)
然后在您的主要功能中創建按鈕:
ic1 = Icon("airbrushIC.gif", (50, 490), button_pressed, "airbrushIC")
ic2 = Icon("fillIC.gif", (10, 540), button_pressed, "fillIC")
buttons = [ic1, ic2]
def button_pressed(data):
print "Button pressed:" + str(data)
最后,對於每個鼠標按下事件,您都會尋找按鈕分類:
for b in buttons:
if b.rect.collidepoint(event.pos):
b.pressed()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.