[英]Trying to get borders to my python turtle module game
我正在嘗試制作這個游戲,一旦你碰到盒子的邊界,烏龜就會返回到它到達盒子邊界之前的 position。 我很新,所以任何幫助將不勝感激。 到目前為止,如果 y 線大於盒子,它會在擊中之前返回到正確位置,但它似乎不起作用。
import turtle
wn = turtle.Screen()
wn.bgcolor('black')
line = turtle.Turtle()
line.goto(-450,-15)
line.speed(9999)
line.pendown
line.color('white')
line.forward(850)
line.left(90)
line.forward(400)
line.left(90)
line.forward(850)
line.left(90)
line.forward(410)
line.color('black')
fred = turtle.Turtle()
fred.penup()
fred.goto(-430, 0)
fred.shape('square')
fred.color('white')
fred.penup()
fred.delay = 0.1
fred.direction = "Stop"
#Set Up (Controls)
wn.listen()
def ahead():
fred.forward(10)
def behind():
fred.backward(10)
if fred.ycor() < -500:
fred.goto(0, -500)
if fred.ycor() > 500:
fred.goto(0, 500)
wn.onkey(behind,"a")
wn.onkey(ahead,"d")
wn.mainloop()
您應該使用if
內部函數 - 以及if
中的值,您可能必須手動計算。
def ahead():
fred.forward(10)
#print(fred.xcor())
if fred.xcor() > 430 - 50: # minus turtle width
fred.backward(10)
def behind():
fred.backward(10)
#print(fred.xcor())
if fred.xcor() < -430 :
fred.forward(10)
但問題是如果你有更多的元素應該停止烏龜。
turtle
沒有 function 來檢測碰撞,您可能必須為此訪問tkinter
函數,或者您必須創建自己的 function 來檢查xcor
, ycor
與某些列表中的坐標。 列表應該有所有對象的坐標。 而這可能需要手動計算坐標。
turtle
很適合畫人物,但不適合玩游戲。 有更好的模塊 - 比如PyGame , Arcade , PGzero ( PyGame Zero
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.