使用控件时,我的游戏将无法正常工作,该如何解决?

时间:2019-01-28 18:23:16

标签: python python-3.x pygame

我无法使控件正常工作,我尝试按Escape键打开我制作的菜单,但该菜单无法打开,并且我不知道我是否在正确检查事件,是否有办法做它吗?

我尝试使用功能检查不同的键,然后转到显示所有事件名称的电子表格,以便您可以在pygame.org上映射它们,但是当我使用转义符或也称为“ :

elif event.type == pygame.K_ESCAPE:
    Frame.blit('Textures/GUI/loom.png', (0,0))

此处显示完整代码:

import pygame

#Textures/Blocks/loom_side.png

pygame.init()

Screen = "None"

DB = 0

Width = 800

Height = 600

Frame = pygame.display.set_mode((Width,Height))

pygame.display.set_caption("HypoPixel")

FPS = pygame.time.Clock()

def Raycast(TTR, RayXPos, RayYPos, RaySizeX, RaySizeY):
    RaycastThis = pygame.image.load(TTR)
    RaycastThis = pygame.transform.scale(RaycastThis,(RaySizeX,RaySizeY))
    Frame.blit(RaycastThis, (RayXPos, RayYPos))
Loop = True
Raycast('Textures/Screens/Skybox/Earth.png',0,0,800,600)
while Loop == True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        elif event.type == pygame.K_ESCAPE:
            Frame.blit('Textures/GUI/loom.png', (0,0))
    pygame.display.update()

    FPS.tick(60)

我希望得到我制作的织机GUI。一旦我试图按逃脱键,什么也没发生。

1 个答案:

答案 0 :(得分:0)

pygame.K_ESCAPE不是事件类型(请参见pygame.event),但它是pygame.key

通过将事件类型与pygame.KEYDOWN进行比较,首先检查是否按下了键:

event.type == pygame.KEYDOWN

然后检查导致事件的event.key是否是pygame.K_ESCAPE键:

event.key == pygame.K_ESCAPE

此外,Surface.blit()的参数必须是Surface对象,而不是文件名。

首先通过pygame.image.load()将图像加载到Surface,然后将blit加载到Surface

sprite = pygame.image.load('Textures/GUI/loom.png')
Frame.blit(sprite, (0,0)) 

当然可以调用您的Raycast函数:

Raycast('Textures/GUI/loom.png',0,0,800,600)

您的代码应如下所示:

while Loop == True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()

        elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            Raycast('Textures/GUI/loom.png',0,0,800,600)