键盘输入辅助
Keyboard Input Assistance
我希望代码是这样的,如果我按下某个键,手电筒颜色
pygame.draw.ellipse(gameDisplay, yellow,[375+lead_x,-43+lead_y,105,led])
变亮,如光yellow.Please assist.Here是完整代码
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支持RGBA color values (red, green, blue and alpha value of a Color), but unfortunately PyGames draw functions don´t draw transparently (according to the domcumanetaion).
要避免此问题,您可以
- 创建新表面
- 使用
.set_alpha()
更改其 alpha 值
- 将 "flashlight" 椭圆 绘制到新表面
- 通过调用支持 alpha 透明度的 .blit() 方法将此表面 Blit 到主屏幕 表面
这是更新后的程序:
# ... 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。
我希望代码是这样的,如果我按下某个键,手电筒颜色
pygame.draw.ellipse(gameDisplay, yellow,[375+lead_x,-43+lead_y,105,led])
变亮,如光yellow.Please assist.Here是完整代码
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支持RGBA color values (red, green, blue and alpha value of a Color), but unfortunately PyGames draw functions don´t draw transparently (according to the domcumanetaion).
要避免此问题,您可以
- 创建新表面
- 使用
.set_alpha()
更改其 alpha 值
- 将 "flashlight" 椭圆 绘制到新表面
- 通过调用支持 alpha 透明度的 .blit() 方法将此表面 Blit 到主屏幕 表面
这是更新后的程序:
# ... 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。