簡體   English   中英

Python 海龜碰撞循環

[英]Python Turtle collision loop

我正在嘗試構建一個簡單的游戲。 用戶控制海龜。 當它接觸圓圈時,圓圈會重新定位,玩家將獲得一分。 該程序只運行一次這一位,請參見代碼中的注釋。 我如何使程序始終運行它? 我認為 mainloop() 做到了這一點。

def move_t2():
    t2.ht()
    rc_list = [-300, -250, -200, -150, -100, -50, 0, 50, 100, 150, 200, 250, 300]
    r1 = random.choice(rc_list)
    r2 = random.choice(rc_list)
    t2.setpos(r1, r2)
    t2.st()

def more_points():
    t3_points.clear()
    new_points=points + 1
    if new_points >=0:
        t3_points.write("Points "+str(new_points),font=10)
  
# The program only run this part one time. I want it to loop.
if t.distance(t2) < 50:
    move_t2()
    more_points()


def mr():
    if t.xcor()<301:
        t.setheading(0)
        t.fd(50)
    else:
        t.setheading(180)
def ml():
    if t.xcor() > -301:
        t.setheading(180)
        t.fd(50)
    else:
        t.setheading(0)
def mu():
    if t.ycor() < 301:
        t.setheading(90)
        t.fd(50)
    else:
        t.setheading(270)
def md():
    if t.ycor() > -301:
        t.setheading(270)
        t.fd(50)
    else:
        t.setheading(90)

ts.onkey(mr,"Right")
ts.onkey(ml,"Left")
ts.onkey(mu,"Up")
ts.onkey(md,"Down")

ts.listen()
ts.mainloop()

該程序只運行一次這一位,請參見代碼中的注釋。 我如何使程序始終運行它? 我認為 mainloop() 做到了這一點。

有問題的代碼位位於頂層,僅設置為運行一次。 mainloop()將控制權交給事件處理程序,以便鍵盤按鍵等可以觸發事件。 需要發生的是碰撞檢查代碼需要成為 function 並添加到每個鍵盤事件處理程序中:

from turtle import Screen, Turtle
from random import choice

POSITION_LIST = [-300, -250, -200, -150, -100, -50, 0, 50, 100, 150, 200, 250, 300]

FONT = ('Arial', 18, 'normal')

def move_turtle(t):
    t.hideturtle()
    r1 = choice(POSITION_LIST)
    r2 = choice(POSITION_LIST)
    t.setpos(r1, r2)
    t.showturtle()

points = 0

def more_points():
    global points

    points += 1
    pen.clear()
    pen.write("Points: {}".format(points), align='center', font=FONT)

def mr():
    if t1.xcor() < 301:
        t1.setheading(0)
        t1.fd(25)
    else:
        t1.setheading(180)

    check_collision()

def ml():
    if t1.xcor() > -301:
        t1.setheading(180)
        t1.fd(25)
    else:
        t1.setheading(0)

    check_collision()

def mu():
    if t1.ycor() < 301:
        t1.setheading(90)
        t1.fd(25)
    else:
        t1.setheading(270)

    check_collision()

def md():
    if t1.ycor() > -301:
        t1.setheading(270)
        t1.fd(25)
    else:
        t1.setheading(90)

    check_collision()

def check_collision():
    if t1.distance(t2) < 20:
        move_turtle(t2)
        more_points()

screen = Screen()
screen.setup(700, 700)

t1 = Turtle()
t1.shape('turtle')
t1.penup()
move_turtle(t1)

t2 = Turtle()
t2.shape('circle')
t2.penup()
move_turtle(t2)

pen = Turtle()
pen.hideturtle()
pen.penup()
pen.sety(-325)
pen.write("Points: {}".format(points), align='center', font=FONT)

screen.onkey(mr, "Right")
screen.onkey(ml, "Left")
screen.onkey(mu, "Up")
screen.onkey(md, "Down")

screen.listen()
screen.mainloop()

暫無
暫無

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

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