在我的微型法术项目中,无论棋盘上的一个单元受伤或被杀死,该单元所在的正方形都会分别用黄色或红色绘制轮廓。
一旦发生这种情况,我必须等待下一次播放器单击才能使轮廓消失(这显然意味着要重新绘制棋盘和当前位置的所有单位)。
我要寻找的是在下一位玩家单击之前重绘所有内容的一种方法(目前仅用于黄色轮廓/受伤的单元)。
我想到的最简单的方法是让游戏等待time.sleep()然后重绘所有内容(在同一代码段中),因此我只在highlight_yellow之后插入time.sleep(3) (square_on)函数,并在更改游戏状态的语句之前。
好吧,我不知道为什么,但由于某种原因,除了显示黄色轮廓外,它仍然有效(等待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()
答案 0 :(得分:0)
如果只是等待一段时间,可以使用pygame.time.wait
或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()
测量时间。
有关更多信息,请参阅问题的答案: