[英]Python turtle Game
import turtle
s1 = turtle.Screen()
b = turtle.Turtle()
b.shape("turtle")
b.color("blue")
b.speed(9)
s1.bgpic("grass.gif")
########## turtles
t1 = turtle.Turtle()
t1.shape('circle')
t1.pu()
t1.goto(100,200)
t2 = turtle.Turtle()
t2.shape('circle')
t2.pu()
t2.goto(-100,-200)
t3 = turtle.Turtle()
t3.shape('circle')
t3.pu()
t3.goto(200,-200)
t3 = turtle.Turtle()
t3.shape('circle')
t3.pu()
t3.goto(-300,100)
t4 = turtle.Turtle()
t4.shape('circle')
t4.pu()
t4.goto(0,200)
t5 = turtle.Turtle()
t5.shape('circle')
t5.pu()
t5.goto(400,300)
###################################################3
score = 0
def eat():
if t1.pos() == b.pos():
t1.hideturtle()
print("You found peice of food!")
score
score += 1
return("Your score is", score)
elif t2.pos() == b.pos():
t2.hideturtle()
print("You found peice of food!")
score
score += 1
return("Your score is", score)
elif t3.pos() == b.pos():
t3.hideturtle()
print("You found peice of food!")
score
score += 1
return("Your score is", score)
elif t4.pos() == b.pos():
t4.hideturtle()
print("You found peice of food!")
score
score += 1
return("Your score is", score)
elif t5.pos() == b.pos():
t5.hideturtle()
print("You found peice of food!")
score
score += 1
return("Your score is", score)
我的指導是繪制一個包含幾只海龜的背景,每只海龜的形狀都類似於食物。
當玩家將他的主要海龜移動到這些食用海龜之一,並在空閑提示下輸入eat()時,eat()函數檢查玩家的海龜是否靠近任何食用海龜,如果是,它讓那只食龜消失了。 它還會增加顯示的分數計數器,該計數器會跟蹤玩家的烏龜吃了多少物品。
我如何做到讓我的海龜在靠近其他海龜時使用我的eat() 函數吃掉其他海龜,而不僅僅是在完全相同的位置?
您可以使用 abs 函數,但首先您需要解包元組,因為 t.pos() 是返回元組 (x, y) 的函數。 將此元組的分量分配給新變量后,您可以使用 abs() 函數輕松查看它們的接近程度。
您可以在此處閱讀有關 abs() 的更多信息: https : //www.w3schools.com/python/ref_func_abs.asp
這里解釋了如何解包元組: https : //www.geeksforgeeks.org/unpacking-a-tuple-in-python/
希望對你有所幫助,祝你游戲愉快。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.