创建一个透明的非活动按钮 - pygame
Creating a transparent inactive button - pygame
我目前正在尝试为游戏菜单创建一个按钮,在所述按钮的顶部使用了一个图像,我希望它是透明的,因为背景应该可以从图像的背面看到( png 具有透明背景)。无论如何,我是否可以在 pygame 内创建具有透明非活动状态的按钮?
编辑:例如透明色的颜色代码
我发现删除处于非活动状态的按钮(矩形)的 draw
行可以解决此问题。
原代码:
def button(x, y, width, height, active_color, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x,y,width,height))
if click[0] == 1 and action != None:
print("reached")
if action == "quit":
pygame.quit()
quit()
elif action == "instructions":
print("instructions")
elif action == "credits":
print("credits")
elif action == "warrior":
print("warrior")
elif action == "archer":
print("archer")
elif action == "mage":
print("mage")
else:
pygame.draw.rect(gameDisplay, white, (x,y,width,height))
正确代码:
def button(x, y, width, height, active_color, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x,y,width,height))
if click[0] == 1 and action != None:
print("reached")
if action == "quit":
pygame.quit()
quit()
elif action == "instructions":
print("instructions")
elif action == "credits":
print("credits")
elif action == "warrior":
print("warrior")
elif action == "archer":
print("archer")
elif action == "mage":
print("mage")
else:
pass
请注意,else
最后甚至不是必需的,可以删除。我只是留下它来显示我更改了代码的位置。
我目前正在尝试为游戏菜单创建一个按钮,在所述按钮的顶部使用了一个图像,我希望它是透明的,因为背景应该可以从图像的背面看到( png 具有透明背景)。无论如何,我是否可以在 pygame 内创建具有透明非活动状态的按钮?
编辑:例如透明色的颜色代码
我发现删除处于非活动状态的按钮(矩形)的 draw
行可以解决此问题。
原代码:
def button(x, y, width, height, active_color, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x,y,width,height))
if click[0] == 1 and action != None:
print("reached")
if action == "quit":
pygame.quit()
quit()
elif action == "instructions":
print("instructions")
elif action == "credits":
print("credits")
elif action == "warrior":
print("warrior")
elif action == "archer":
print("archer")
elif action == "mage":
print("mage")
else:
pygame.draw.rect(gameDisplay, white, (x,y,width,height))
正确代码:
def button(x, y, width, height, active_color, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x,y,width,height))
if click[0] == 1 and action != None:
print("reached")
if action == "quit":
pygame.quit()
quit()
elif action == "instructions":
print("instructions")
elif action == "credits":
print("credits")
elif action == "warrior":
print("warrior")
elif action == "archer":
print("archer")
elif action == "mage":
print("mage")
else:
pass
请注意,else
最后甚至不是必需的,可以删除。我只是留下它来显示我更改了代码的位置。