簡體   English   中英

在海龜比賽中,如何讓海龜停在某個坐標?

[英]How do you make a turtle stop at certain coordinates in a turtle race?

我正在創建一個烏龜比賽,我想讓烏龜在到達終點線時停下來。 在此處輸入圖片說明

圓圈部分是終點線,位於 x 坐標 220。這是我目前的代碼:

import turtle as trtl

#add the background
wn = trtl.Screen()
wn.setup(width = 1.0 , height = 1.0)
wn.bgpic("image.png")

# Make the turtle racers
BlueTurtle = trtl.Turtle()
BlueTurtle.shape("turtle")
BlueTurtle.color("blue")
BlueTurtle.speed(4)
BlueTurtle.penup()
BlueTurtle.goto(-220,-0)
BlueTurtle.pendown()

RedTurtle = trtl.Turtle()
RedTurtle.color("red")
RedTurtle.speed(4)
RedTurtle.shape("turtle")
RedTurtle.penup()
RedTurtle.goto(-220,-75)
RedTurtle.pendown()

# Game configuration variables
def move_blue():
    BlueTurtle.forward(10)

def move_red():
    RedTurtle.forward(10)

# Create numbers and the title
pen = trtl.Turtle()
pen.pensize(10)
pen.speed(10)
pen.speed(20)
pen.penup()
pen.goto(-100, 70)
pen.pendown()
pen.write("Turtle Race", font=("Times New Roman", 40, "normal"))
numbers = ["1","2"]
coordinates = -200,50
for i in range(2):
  pen.penup()
  pen.goto(coordinates)
  pen.pendown()
  pen.write(numbers[i])
  coordinates = (coordinates[0], coordinates[1] + -100)

# Make the lines that divide the racers and the finish line
pen.penup()
pen.goto(-180, -40)
pen.pendown()
pen.forward(400)
pen.penup()
pen.goto(-180, 40)
pen.pendown()
pen.forward(400)
pen.penup()
pen.goto(-180, -110)
pen.pendown()
pen.forward(400)
pen.pensize(0)
pen.penup()
pen.goto(230, 60)
pen.pendown()
pen.right(90)
pen.forward(175)
pen.hideturtle()

# Personalizes it so that the player can move their turtle with any key they like
key1 = input("Type a letter to use to move your turtle - Blue Turtle")
key2 = input("Type a letter to use to move your turtle - Red Turtle")

# Listen for events
wn = trtl.Screen()
wn.onkeypress(move_blue, key1)
wn.onkeypress(move_red, key2)
wn.listen()

# The function for the game to stop if one of the turtles touches the finish line
keepPlaying = True
while keepPlaying:
    if BlueTurtle.pos() == (220, 0):
        pen.penup()
        pen.goto(-220, -220)
        pen.pendown()
        pen.write("The Blue Turtle hit the finish line - the blue turtle wins!" , font = 
("Arial", 20, "normal"))
        keepPlaying = False
    if RedTurtle.pos() == (220, 0):
        pen.penup()
        pen.goto(-220, -220)
        pen.pendown()
        pen.write("The Red Turtle hit the finsh line = the red turtle wins!" , font =("Arial", 
20, "normal"))
        keepPlaying = False
wn.mainloop()

最后一個區域寫着“如果其中一只海龜觸及終點線,游戲就會停止的功能。” 是我到目前為止嘗試過的。 它在終端中沒有顯示任何錯誤,但是當它被執行時,它似乎總是說“vs 代碼沒有響應”,如果沒有該部分它可以正常工作。

紅色從(-220,-75)開始,所以if RedTurtle.pos() == (220, 0):永遠不會為真。 但是,除此之外,您還混合了事件驅動編程和游戲循環編程。

wn.onkeypress(move_blue, key1)
wn.onkeypress(move_red, key2)
wn.listen()

您設置了 2 個事件並告訴應用程序開始偵聽這些事件。 當一個發生時,相應的函數將被調用。

但在這里:

keepPlaying = True
while keepPlaying:
   ...

您有一個繁忙的循環,不斷檢查海龜的位置。 但這會阻止到達最后一行wn.mainloop()

您可以將該循環更改為檢查海龜是否在終點線的函數。 使用全局變量來標記游戲何時結束:

game_over = False
def move_blue():
    if not game_over:
        BlueTurtle.forward(10)
        check_for_win()

def move_red():
    if not game_over:
        RedTurtle.forward(10)
        check_for_win()

然后稍后,將循環更改為函數

def check_for_win():
    global game_over
    if BlueTurtle.pos() == (220, 0):
        pen.penup()
        pen.goto(-220, -220)
        pen.pendown()
        pen.write("The Blue Turtle hit the finish line - the blue turtle wins!" , font = 
("Arial", 20, "normal"))
        game_over = True
    if RedTurtle.pos() == (220, -75):
        pen.penup()
        pen.goto(-220, -220)
        pen.pendown()
        pen.write("The Red Turtle hit the finsh line = the red turtle wins!" , font =("Arial", 
20, "normal"))
        game_over = True

還有其他改進,但我認為這是你的問題的核心。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM