我希望代码是这样的,如果我按某个键,手电筒颜色
pygame.draw.ellipse(gameDisplay, yellow,[375+lead_x,-43+lead_y,105,led])
变得更亮,如浅黄色。请协助。这是完整的代码。
import pygame
pygame.init()
white = (34,34,34)
black=(0,0,0)
red=(255,0,0)
led=45
silver=(110,108,108)
yellow=(193,206,104)
yellow2=(213,230,100)
gameDisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption('Slither')
gameExit=False
myfont = pygame.font.SysFont("monospace", 15)
lead_x = 300
lead_y = 300
background_color=black
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x -= 10
print("LEFT")
if event.key == pygame.K_RIGHT:
lead_x +=10
print("RIGHT")
if event.key == pygame.K_UP:
lead_y -=10
print("UP")
if event.key == pygame.K_DOWN:
lead_y +=10
print("DOWN")
if event.key == pygame.K_a:
gameDisplay.fill(red)
led +=10
if led == 95:
background_color=red
print("YOU FOUND ME.NOW YOU WILL D I E")
background_color=red
gameDisplay.fill(background_color)
pygame.draw.ellipse(gameDisplay, black,[-295+300,-54+300,75,100])
pygame.draw.ellipse(gameDisplay, red,[-285+300,-35+300,20,34])
pygame.draw.ellipse(gameDisplay, red,[-255+300,-35+300,20,34])
pygame.draw.rect(gameDisplay, silver,[470+lead_x,-35+lead_y,75,30])
pygame.draw.ellipse(gameDisplay, yellow,[375+lead_x,-43+lead_y,105,led])
pygame.display.update()
pygame.quit()
quit()
答案 0 :(得分:2)
问题在于 PyGame支持RGBA color values ( r ed, g reen, b lue和一个一个颜色的lpha值),但不幸的是 PyGames绘制函数没有透明地绘制(根据domcumanetaion)。
要避免此问题,您可以
.set_alpha()
这是更新的程序:
# ... your code
alpha = 0
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
#... your original code
if event.key == pygame.K_SPACE:
#change alpha value
alpha += 10
gameDisplay.fill(background_color)
pygame.draw.ellipse(gameDisplay, black,[-295+300,-54+300,75,100])
pygame.draw.ellipse(gameDisplay, red,[-285+300,-35+300,20,34])
pygame.draw.ellipse(gameDisplay, red,[-255+300,-35+300,20,34])
alphaSurface = pygame.Surface((105,led)) # 1.
alphaSurface.set_alpha(alpha) # 2.
alphaSurface.fill(background_color)
pygame.draw.ellipse(alphaSurface, yellow,[0,0,105,led]) # 3.
gameDisplay.blit(alphaSurface, (375+lead_x,-43+lead_y)) # 4.
pygame.draw.rect(gameDisplay, silver,[470+lead_x,-35+lead_y,75,30])
pygame.display.update()
pygame.quit()
quit()
请注意,我们需要更改主循环的alphaSurface
曲面每次迭代的背景颜色和大小以获得预期结果,这可能会减慢我们的游戏速度。
您还可以考虑使用pygame.HWSURFACE
标记创建硬件加速曲面。有关详细信息,请参阅PyGame documentation about surfaces。