私のミニ メイジ プロジェクトでは、チェス盤上のユニットが負傷または死亡するたびに、ユニットが描かれている四角形がそれぞれ黄色または赤色で輪郭が描かれます。
これが発生すると、アウトラインを非表示にするために、次のプレーヤーのクリックを待つ必要があります (これは、チェス盤と現在の位置にあるすべてのユニットを再描画することを意味します)。
私が探しているのは、次のプレイヤーがクリックする前にすべてを再描画する手段です (現時点では、黄色のアウトライン/負傷したユニットのみ)。
私が思いついた最も簡単な方法は、ゲームを 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()
