2 回答

TA貢獻1799條經驗 獲得超6個贊
所以,你得到的代碼有效,唯一的問題是當汽車與終點線相交時,你只在屏幕上顯示文本
因此,每幀都有一個字符串放在屏幕上,當汽車越過這條線時,將字符串更改為那輛車
#put these outside the game loop
finish_text = ""
font2 = pygame.font.SysFont("Papyrus", 45)
players_finished = 0
placings = ["1st", "2nd", "3rd", "4th"]
while running: #game loop
...
# Did anyone cross the line?
if (finish_line_rect.collidepoint(playerX, playerY)):
if finish_text[:9] != "Player 1": #so it doesnt do this every frame the car is intersecting
finish_text = "Player 1 is " + placings[players_finished]
players_finished += 1
print("Player (one) has crossed into finish line!")
elif (finish_line_rect.collidepoint(playerX_two , playerY_two)):
if finish_text[:9] != "Player 2":
print("Player one has crossed into finish line first other car lost!")
finish_text = "Player 2 is " + placings[players_finished]
players_finished += 1
elif (finish_line_rect.collidepoint(playerX , playerY)):
if finish_text[:9] != "Player 3":
print("Player two has crossed into finish line first other car lost!")
finish_text = "Player 3 is " + placings[players_finished]
players_finished += 1
elif (finish_line_rect.collidepoint(playerX_two, playerY_two)):
if finish_text[:9] != "Player 4":
print("Player two has crossed into finish line first other car lost!")
finish_text = "Player 4 is " + placings[players_finished]
players_finished += 1
if finish_text != "": #test to see if there is anything to put on screen
text2 = font2.render(finish_text, 5, (255, 99, 7))
screen.blit(text2, (135 - (text2.get_width() / 5), 95))
pygame.display.flip()
筆記。還沒有測試過

TA貢獻1878條經驗 獲得超4個贊
我從未使用過 pygame,所以不知道是否有內置函數。但是,您可以輕松地制作一個計數器。
#this is all in your while loop
if playerY<50 or playerY_two<50 or playerY_three<50 or playerY_four<50:
count+=1
if count == 1:
if playerY<50:
print('playerY first place')
playerY=10000
if playerY_two<50:
print('playerY_two first place')
playerY_two=100000
if playerY_three<50:
print('playerY_three first place')
playerY_three=100000
if playerY_four<50:
print('playerY_four first place')
playerY_four=1000000
if count == 2:
if playerY<50:
print('playerY second place')
playerY=10000
if playerY_two<50:
print('playerY_two seoncd place')
playerY_two=100000
if playerY_three<50:
print('playerY_three second place')
playerY_three=100000
if playerY_four<50:
print('playerY_four second place')
playerY_four=1000000
if count == 3:
if playerY<50:
print('playerY third place')
playerY=10000
if playerY_two<50:
print('playerY_two third place')
playerY_two=100000
if playerY_three<50:
print('playerY_three third place')
playerY_three=100000
if playerY_four<50:
print('playerY_four third place')
playerY_four=1000000
if count == 4:
if playerY<50:
print('playerY 4th place')
playerY=10000
if playerY_two<50:
print('playerY_two 4th place')
playerY_two=100000
if playerY_three<50:
print('playerY_three 4th place')
playerY_three=100000
if playerY_four<50:
print('playerY_four 4th place')
playerY_four=1000000
break
你可以創建一個類/函數來將上面的全部內容寫成 4 行到 5 行,但一般的想法是你想打印出誰越過終點線,但你只希望打印一次。
添加回答
舉報