首页 > 解决方案 > 如何让我的乌龟感觉到与广场的碰撞?

问题描述

我应该为班级制作游戏,但我对如何进行碰撞感到困惑。我是编码新手,完全不知道如何让我的海龟碰撞并产生结果。我想做的是,当经典形状的乌龟“tori”非常靠近我的名为“goal”的乌龟时,它会发生碰撞,然后调用 update_score() 函数。请告诉我应该怎么做才能发生碰撞。

#1.2.5

w = 500
h = 500

#Set up
import turtle as trtl
import turtle
import random as rand
score = 0


wn = trtl.Screen()
tori = trtl.Turtle()
scorer = trtl.Turtle()
goal = trtl.Turtle()
font_setup = ("Arial", 20, "normal")


tori.shape("classic")
tori.color("white")
tori.speed(0)
tori.pu()
tori.goto(-100, -200)
tori.left(90)


#Functions do what?

wn.bgcolor("pink")
wn.setup(500, 500)


# This function is supposed to make my turtle named scorer write the score and update it when         called on
def update_score():
    scorer.ht()
    scorer.goto(0, 0)
    scorer.color("white")
    global score
    score += 1
    scorer.clear()
    scorer.write(score, font=font_setup)


#mover1-4 makes tori move across the screen 
def mover1():
    tori.forward(10)
wn.onkeypress(mover1, "w")

def mover2():
  tori.backward(10)
wn.onkeypress(mover2, "s")
def mover3():
    tori.left(90)
    tori.forward(10)
    tori.right(90)
wn.onkeypress(mover3, "a")
def mover4():
    tori.right(90)
    tori.forward(10)
    tori.left(90)
wn.onkeypress(mover4, "d")


#goal_location is where my turtle, goal is at. When tori collides with goal, I want to call     the update_score function
def goal_location():
    goal.speed("fastest")
    goal.pu()
    goal.shape("square")
    goal.turtlesize(20)
    goal.color("white")
    goal.goto(0, 400)

#After update_score is called this function is to reset tori to her starting position so she can reach the goal again and again
def reset_tori():
    tori.goto(-100, -200)

goal_location()

wn.listen()
wn.mainloop()

我应该为班级制作游戏,但我对如何进行碰撞感到困惑。我是编码新手,完全不知道如何让我的海龟碰撞并产生结果。我想做的是,当经典形状的乌龟“tori”非常靠近我的名为“goal”的乌龟时,它会发生碰撞,然后调用 update_score() 函数。

标签: pythonvisual-studio

解决方案


推荐阅读