python - 如何让我的乌龟感觉到与广场的碰撞?
问题描述
我应该为班级制作游戏,但我对如何进行碰撞感到困惑。我是编码新手,完全不知道如何让我的海龟碰撞并产生结果。我想做的是,当经典形状的乌龟“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() 函数。
解决方案
推荐阅读
- .net - 使用带有 Saxon.Api(.NET 版本)的 XSD 断言的 XML 日期验证
- python-3.x - PyPDF2 写入损坏的文件
- sql - Postgres SQL 按最近日期选择两个字段,一个字段必须是唯一的
- python - 如何从 Mysql 数据库中删除列表框 Python Tkinter 中选择的项目
- c# - 如何在不使用 foreach 循环的情况下从列表中打印 X 个名称?
- node.js - 无法将变量从 app.js 传递到节点中的另一个文件
- python - 使用 bs4 和 python 删除 html 代码中的隐藏行
- linux - 如何链接“mimetype -b”和“find”命令以在同一 csv 中获取文件名和文件类型?
- loadrunner - web_reg_save_param_regexp loadrunner- 未提取值
- java - 使用 snakeyaml 动态生成 YAML 文件