我正在尝试在游戏中编写一个简单的对话框。我在整个游戏循环中使用了while循环。这个循环中的while循环由玩家按下空格激活。我希望播放器按空格(speech()函数)进入第二个while循环并按A(speech_end())退出此循环。但是,speech_end在第二个循环中不起作用,除非我已经持有A.我已在此循环之外测试它并且它工作正常。任何帮助表示赞赏。
#Classes
class Bertha(object):
image = pygame.image.load("bertha.png")
def __init__(self, berthax, berthay, screen):
self.screen = screen
self.screen_rect = screen.get_rect()
self.rect = self.image.get_rect()
self.rect.x = berthax
self.rect.y = berthay
def draw(self, screen):
screen.blit(self.image, self.rect)
class Player(object):
# one image for all instances
image = pygame.image.load('player.png')
def __init__(self, playerx, playery, screen):
self.screen = screen
self.screen_rect = screen.get_rect()
# every instance can have own image
#self.image = pygame.image.load('test.jpg')
self.rect = self.image.get_rect()
self.rect.x = playerx
self.rect.y = playery
#moving with Arrow keys
def movement(self):
for event in pygame.event.get():
self.dist_x = 0
self.dist_y = 0
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
self.dist_x += 5
if keys[pygame.K_UP]:
self.dist_y -= 5
elif keys[pygame.K_DOWN]:
self.dist_y += 5
elif keys[pygame.K_LEFT]:
self.dist_x -= 5
if keys[pygame.K_UP]:
self.dist_y -= 5
elif keys[pygame.K_DOWN]:
self.dist_y += 5
elif keys[pygame.K_UP]:
self.dist_y -= 5
if keys[pygame.K_RIGHT]:
self.dist_x += 5
elif keys[pygame.K_LEFT]:
self.dist_x -= 5
elif keys[pygame.K_DOWN]:
self.dist_y += 5
if keys[pygame.K_RIGHT]:
self.dist_x += 5
elif keys[pygame.K_LEFT]:
self.dist_x -= 5
#------------Confirming player position--------------
self.rect.x += self.dist_x
self.rect.y += self.dist_y
def draw(self, screen):
screen.blit(self.image, self.rect)
# --------Talking to another character----------------
def speech():
keys1 = pygame.key.get_pressed()
global talking
if keys1[pygame.K_SPACE]:
talking = 1
def speech_end():
keys2 = pygame.key.get_pressed()
global talking
if keys2[pygame.K_a]:
talking = 2
print("speech_end")
# ----- Bertha Messages
def bertha_text(screen, font):
text = font.render("spacebar worked!", True, BLACK)
text_rect = text.get_rect()
text_rect.center = (400, 20)
screen.blit(text, text_rect)
pygame.display.update()
# - other -
font = pygame.font.SysFont("moon_get-Heavy.ttf", 40)
clock = pygame.time.Clock()
playerx = 100
playery = 100
berthax = 400
berthay = 400
player_one = (Player(playerx, playery, screen))
bertha_npc = (Bertha(berthax, berthay, screen))
talking = 0
# - mainloop -
running = True
while running:
talking = 0
screen.blit(map_img, (0, 0))
#Player
player_one.movement()
player_one.draw(screen)
#Bertha
bertha_npc.draw(screen)
#------Show the text------
speech()
while talking == 1:
bertha_text(screen, font)
speech_end()
print(talking)
if talking == 2:
print("talking == False")
talking -= 2
break
#UPDATE
pygame.display.update()
# - FPS -
clock.tick(60)
# - end -
pygame.quit()
quit()
答案 0 :(得分:1)
您必须处理内部while循环中的事件。可以调用pygame.event.pump()
或使用其他事件函数之一,例如for event in pygame.event.get():
。
此外,也可以在内部循环中调用clock.tick(60)
。
while talking == 1:
pygame.event.pump()
bertha_text(screen, font)
speech_end()
print(talking)
if talking == 2:
print("talking == False")
talking -= 2
break
clock.tick(60)