我正在尝试与一个在每次新闻发布时张开嘴的家伙制作一个有趣的程序。问题是他只打开了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()
最佳答案
问题似乎在这里:
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)
关于python - pygame按住键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31255505/