简体   繁体   English

如何在python龟图形中更改龟的Hitbox大小?

[英]How do I change the Hitbox size of a turtle in python turtle graphics?

I dont know how to change the size of a turtle hitbox in python turtle graphics 我不知道如何在python龟图形中改变乌龟hitbox的大小

I haven't tried anything yet because I'm new, and know very little about this. 我还没有尝试任何东西,因为我是新手,对此知之甚少。 I've tried googling it, though, but nothing popped up. 不过我试过谷歌搜索,但没有弹出任何东西。

from turtle import *
import turtle
from random import randint
import time

screen = turtle.Screen()
screen.setup(1920, 1080)
player = turtle.Turtle()

I want to add a button that you have to click to start right here 我想添加一个你必须点击的按钮才能在这里开始

The game starts right here : 游戏从这里开始:

wn = turtle.Screen()

last_pressed = 'up'

def setup(col, x, y, w, s, shape):

  player.penup()
  player.up()
  player.goto(x,y)
  player.width(w)
  player.turtlesize(s)
  player.color(col)
  player.lt(90)
  player.down()
  wn.onkey(up, "s")
  wn.onkey(left, "d")
  wn.onkey(right, "a")
  wn.onkey(back, "w")
  wn.onkey(quitTurtles, "Escape")
  wn.listen()
  wn.mainloop()

This may not be exactly what you are looking for, but this might work in your situation. 这可能不是您正在寻找的,但这可能适用于您的情况。 Detecting collision in Python turtle game This is a thread on collision detection between objects and with some tweaking of numbers you could increase the hitbox of the turtle using the abs() function 在Python龟游戏中检测碰撞这是一个关于对象之间碰撞检测的线程,通过一些数字调整你可以使用abs()函数增加龟的命中箱

I dont know how to change the size of a turtle hitbox in python turtle graphics

What do you mean by "hitbox"? 你是什​​么意思“hitbox”? I'm not sure what you mean by that (and neither does Google, apparently). 我不确定你的意思(显然谷歌也没有)。

Do you mean that you want a rectangular button to click on? 你的意思是你想要一个矩形按钮点击? If that's the case, you could use the tkinter module together with the turtle module to create a button to click. 如果是这种情况,您可以将tkinter模块与turtle模块一起使用来创建一个单击按钮。 (But be aware that it's not always easy to get the tkinter and turtle modules to work together to do what you want.) (但请注意,让tkinterturtle模块一起工作以实现您想要的tkinter并不总是那么容易。)

If you want a button to click on, but don't need a Tkinter button, you could just try creating a new turtle in the shape of a rectangle that intercepts mouse clicks with onclick() . 如果你想要一个按钮点击,但不需要Tkinter按钮,你可以尝试创建一个矩形形状的新龟,用onclick()拦截鼠标点击。 You can see an example of this if you run: 如果你运行,你可以看到一个这样的例子:

 python3 -m turtledemo

and select Examples >> colormixer from the main menubar. 并从主菜单栏中选择Examples >> colormixer

Or, if by "hitbox" you mean how to detect when one turtle has intercepted another turtle (as in, one has come close enough to the other to be considered a "hit"), I suggest querying each turtle's location, then using the Pythagorean Theorem to calculate the distance from each other. 或者,如果通过“hitbox”你的意思是如何检测一只乌龟何时拦截了另一只海龟(如同一只海龟足够接近另一只被认为是“击中”),我建议查询每只乌龟的位置,然后使用毕达哥拉斯定理来计算彼此的距离。 If this distance is within a predetermined threshold, consider the hitbox as being "hit." 如果此距离在预定阈值内,请将hitbox视为“命中”。

You can see an example of this by typing: 您可以通过键入以下内容来查看此示例:

python3 -m turtle

(Pay attention to the yellow turtle as he tries to catch up to the other turtle.) (当他试图赶上另一只乌龟时,注意黄龟。)

I apologize if this answer isn't quite what you're looking for, but I'm just not sure what you mean by "hitbox." 如果这个答案不是你想要的,我很抱歉,但我不确定你对“hitbox”的意思。 Maybe you could clarify? 也许你可以澄清一下?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM