Python 键盘输入辅助

Python 键盘输入辅助,python,pygame,Python,Pygame,我希望代码是这样的,如果我按下某个键,手电筒的颜色 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) yell

我希望代码是这样的,如果我按下某个键,手电筒的颜色

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()

问题是,PyGame支持(颜色的红色、绿色、蓝色和alpha值),但不幸的是,PyGames绘图函数不能透明地绘制(根据标准)

为了避免这个问题,你可以

  • 创建新曲面
  • 使用
  • 在新曲面上绘制“手电筒”椭圆
  • 通过调用支持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
    surface的背景颜色和大小,以获得预期的结果,这可能会降低游戏速度

    您还可以考虑使用<代码> PyPrase.HWSurth标志> <强>创建硬件加速表面< /强>。参阅更多信息。