Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
while循环中的按键识别。python_Python_Loops_While Loop_Pygame - Fatal编程技术网

while循环中的按键识别。python

while循环中的按键识别。python,python,loops,while-loop,pygame,Python,Loops,While Loop,Pygame,我试图在游戏中编写一个简单的对话框。我在整个游戏循环中使用while循环。在这个循环中有一个while循环,由玩家按空格键激活。我想让播放器按空格键(speech()函数)进入第二个while循环,然后按A键(speech_end())退出这个循环。但是,speech_end在第二个循环中不起作用,除非我已经持有A。我已经在这个循环之外测试了它,它工作得很好。感谢您的帮助 #Classes class Bertha(object): image = pygame.image.load(

我试图在游戏中编写一个简单的对话框。我在整个游戏循环中使用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()

您必须处理内部while循环中的事件。调用或使用其他事件函数之一,例如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)

请仔细阅读。我们喜欢简洁且可执行的示例。顺便说一句,最好从您的player类中删除事件循环(
for event in pygame.event.get():
),并将其插入主while循环。当你的代码正确运行时,你可以发布它(一个完整的、可运行的版本)来获得一些重构技巧。为冗长的文章道歉我认为多细节总比少细节好。下次我会牢记在心。你仍然可以(或应该)尝试为其他评论员或有相同问题的人改进这个问题。欢迎来到Stack Overflow!明白了,明天就可以了。谢谢