1

在我的迷你法师项目中,每当棋盘上的一个单位受伤或被杀时,该单位所在的方格就会分别用黄色或红色勾勒出来。

一旦发生这种情况,我必须等待下一次玩家点击才能使轮廓消失(这显然意味着重新绘制棋盘和当前位置的所有单位)。

我要寻找的是在下一个玩家点击之前重新绘制所有内容的方法(目前仅针对黄色轮廓/受伤的单位)。

我想出的最简单的方法是让游戏等待 time.sleep() 然后重绘所有内容(在同一块代码中),所以我只需在 highlight_yellow(square_on) 之后插入 time.sleep(3)函数,在改变游戏状态的语句之前。

在此处输入图像描述

好吧,我不知道为什么,但由于某种原因它可以工作(等待 3 秒并重绘所有内容),除了显示黄色轮廓。

谁能告诉我我做错了什么?

我的主循环如下:

# main loop
while 1:

    # event loop
    for event in pygame.event.get():
        if event.type is pygame.QUIT:
            sys.exit()

        # mouse click handling
        if event.type == pygame.MOUSEBUTTONDOWN:
            new_position = pygame.mouse.get_pos()

            # unit not selected
            if game_state is 'no unit selected':
                if cancel_yellow_square:
                    draw_chessboard()
                    for white_unit in white_army:
                        white_unit.draw_unit()
                    for black_unit in black_army:
                        black_unit.draw_unit()
                    cancel_yellow_square = False
                if cancel_red_square:
                    selected_unit.set_unit_position(selected_unit.new_position[0], selected_unit.new_position[1])
                    selected_unit = None
                    draw_chessboard()
                    for white_unit in white_army:
                        white_unit.draw_unit()
                    for black_unit in black_army:
                        black_unit.draw_unit()
                    cancel_red_square = False

                if turn is 'white army':
                    for white_unit in white_army:
                        if white_unit.square_on.collidepoint(new_position[0], new_position[1]):
                            selected_unit = white_unit
                            game_state = 'unit selected'
                            print(str(game_state) + ', this unit is a white ' + str(selected_unit.get_condition()) +
                                  ' ' + str(selected_unit.kind))

                elif turn is 'black army':
                    for black_unit in black_army:
                        if black_unit.square_on.collidepoint(new_position[0], new_position[1]):
                            selected_unit = black_unit
                            game_state = 'unit selected'
                            print(str(game_state) + ', this unit is a black ' + str(selected_unit.get_condition()) +
                                  ' ' + str(selected_unit.kind))

            elif game_state == 'unit selected':
                if turn is 'white army':
                    current_position = selected_unit.square_on
                    if not current_position.collidepoint(new_position[0], new_position[1]):
                        if is_legal_move(selected_unit) and white_unit_collision():
                            print('this square is occupied by a friendly white unit, therefore the move is not allowed.'
                                  ' choose another square')
                        elif is_legal_move(selected_unit) and black_unit_collision():
                            for black_unit in black_army:
                                if black_unit.get_unit_position() == selected_unit.new_position:
                                    if black_unit.get_condition() == 'fresh':
                                        game_state = 'enemy unit wounded'
                                        print('black unit wounded')
                                    elif black_unit.get_condition() == 'wounded':
                                        game_state = 'enemy unit killed'
                                        print('black unit killed')
                        elif not is_legal_move(selected_unit):
                            print('move not allowed for this unit')
                        elif is_legal_move(selected_unit):
                            game_state = 'unit movement allowed'
                            turn = 'black army'
                    else:
                        print('this unit has been deselected, select a unit')
                        game_state = 'no unit selected'
                        current_position = selected_unit.get_unit_position()
                        new_position = selected_unit.get_unit_position()
                        selected_unit.set_unit_position(new_position[0], new_position[1])

                elif turn is 'black army':
                    current_position = selected_unit.square_on
                    if not current_position.collidepoint(new_position[0], new_position[1]):
                        if is_legal_move(selected_unit) and black_unit_collision():
                            print('this square is occupied by a friendly black unit, therefore the move is not allowed.'
                                  ' choose another square')
                        elif is_legal_move(selected_unit) and white_unit_collision():
                            for white_unit in white_army:
                                if white_unit.get_unit_position() == selected_unit.new_position:
                                    if white_unit.get_condition() == 'fresh':
                                        game_state = 'enemy unit wounded'
                                        print('black unit wounded')
                                    elif white_unit.get_condition() == 'wounded':
                                        game_state = 'enemy unit killed'
                                        print('white unit killed')
                        elif not is_legal_move(selected_unit):
                            print('move not allowed for this unit')
                        elif is_legal_move(selected_unit):
                            game_state = 'unit movement allowed'
                            turn = 'white army'

                    else:
                        print('this unit has been deselected, select a unit')
                        game_state = 'no unit selected'
                        current_position = selected_unit.get_unit_position()
                        new_position = selected_unit.get_unit_position()
                        selected_unit.set_unit_position(new_position[0], new_position[1])

    # application loop

    # highlight square
    if game_state == 'unit selected':
        highlight_green(selected_unit.square_on)
        highlight_possible_moves()

    # move unit
    if game_state == 'unit movement allowed':
        selected_unit.set_unit_position(selected_unit.new_position[0], selected_unit.new_position[1])
        game_state = 'no unit selected'
        print('---' + str(turn).upper() + ', IS YOUR TURN---')
        selected_unit = None

    # enemy wounded
    if game_state == 'enemy unit wounded':
        print(selected_unit.new_position)
        if turn == 'white army':
            for black_unit in black_army:
                if black_unit.get_unit_position() == selected_unit.new_position:
                    black_unit.set_condition('wounded')
                    print('this unit has been ' + str(black_unit.get_condition()))
        elif turn == 'black army':
            for white_unit in white_army:
                if white_unit.get_unit_position() == selected_unit.new_position:
                    white_unit.set_condition('wounded')
                    print('this unit has been ' + str(white_unit.get_condition()))
        square_on = selected_unit.new_position[0], selected_unit.new_position[1], 100, 100
        highlight_yellow(square_on)
        time.sleep(3)
        draw_chessboard()
        for white_unit in white_army:
            white_unit.draw_unit()
        for black_unit in black_army:
            black_unit.draw_unit()
        if turn == 'white army':
            turn = 'black army'
        elif turn == 'black army':
            turn = 'white army'
        game_state = 'no unit selected'
        cancel_yellow_square = True
        print('---' + str(turn).upper() + ', IS YOUR TURN---')
        selected_unit = None

    # enemy killed
    if game_state == 'enemy unit killed':
        print(selected_unit.new_position)
        if turn == 'white army':
            for black_unit in black_army:
                if black_unit.get_unit_position() == selected_unit.new_position:
                    black_unit.set_condition('killed')
                    black_army.remove(black_unit)
                    print('this unit has been ' + str(black_unit.get_condition()))
        elif turn == 'black army':
            for white_unit in white_army:
                if white_unit.get_unit_position() == selected_unit.new_position:
                    white_unit.set_condition('killed')
                    white_army.remove(white_unit)
                    print('this unit has been ' + str(white_unit.get_condition()))
        square_on = selected_unit.new_position[0], selected_unit.new_position[1], 100, 100
        highlight_red(square_on)
        if turn == 'white army':
            turn = 'black army'
        elif turn == 'black army':
            turn = 'white army'
        game_state = 'no unit selected'
        cancel_red_square = True
        print('---' + str(turn).upper() + ', IS YOUR TURN---')

    pygame.display.update()
4

1 回答 1

0

如果你只是等待一段时间,你可以使用pygame.time.waitor pygame.time.delay。但是,如果要显示一条消息然后等待一段时间,则需要事先更新显示。仅当 调用pygame.display.update()或时才更新显示。此外,在显示更新在窗口中可见之前,pygame.display.flip()您必须通过 处理事件:pygame.event.pump()

highlight_yellow(square_on)
pygame.display.flip()
pygame.event.pump()
pygame.time.delay(3000) # 3 second == 3000 milliseconds

在任何情况下,这都不是典型应用程序中等待或延迟某事的方式。等待时游戏没有响应。用来pygame.time.get_ticks()测量时间。
有关更多信息,请参阅问题的答案:

于 2020-12-20T08:45:14.533 回答