Pygame按住键

时间:2015-07-06 20:59:47

标签: python

我正在尝试与一个在新闻空间时张嘴的家伙制作一个有趣的节目。问题是他只打开它0.1秒然后再次关闭。我想做到这样,每当我占据空间时,嘴巴就会张开。

代码:

    import pygame
pygame.init()

display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Open The Mouth")
clock = pygame.time.Clock()

faceImg = pygame.image.load("face_shut.png")
faceOpenImg = pygame.image.load("face_open.png")
def face(x,y):
    gameDisplay.blit(faceImg,(face_x,face_y))
def faceOpen(x,y):
    gameDisplay.blit(faceOpenImg,(faceOpen_x,faceOpen_y))

faceOpen_x = (1)
faceOpen_y = (1)

face_x = (1)
face_y = (1)

programRunning = True

while programRunning:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            programRunning = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                faceOpen(faceOpen_x,faceOpen_y)
                pygame.display.update()
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_SPACE:
                face(face_x,face_y)
                pygame.display.update()


    face(face_x,face_y)
    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit()

1 个答案:

答案 0 :(得分:1)

问题似乎在这里:

while programRunning:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            programRunning = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                faceOpen(faceOpen_x,faceOpen_y)
                pygame.display.update()
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_SPACE:
                face(face_x,face_y)
                pygame.display.update()

    #  Here, always drawn closed at the end of the while
    face(face_x,face_y)
    pygame.display.update()
    clock.tick(60)

无论在什么时候设置了什么,你都会在最后关闭脸部。

相反如何:

# Drawn before the loop starts
face(face_x,face_y)
pygame.display.update()

while programRunning:
    for event in pygame.event.get():
        event_occurred = True
        if event.type == pygame.QUIT:
            programRunning = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                faceOpen(faceOpen_x,faceOpen_y)
                pygame.display.update()
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_SPACE:
                face(face_x,face_y)
                pygame.display.update()

    clock.tick(60)