[英]How do I implement collision detection with Python Turtle
Good afternoon I need my program to reset when my python turtle hits a circle.下午好,当我的 python 乌龟撞圈时,我需要重置我的程序。 There are 53 circles.
有53个圆圈。 Here is the turtle.
这里是乌龟。
move = turtle.Turtle()
move.penup()
showturtle()
turtle.hideturtle()
move.setposition(-500,0)
move.pencolor('cyan')
move.fillcolor("blue")
move.pos()
move.speed()
move.shapesize(3,3,3)
turtle.fillcolor("blue")
turtle.shapesize(3,3,3)
outline = ['white', 'green', 'red', 'blue', 'purple', 'yellow', 'orange','black','gray']
colors = ['red', 'blue', 'green', 'purple', 'yellow', 'orange', 'black','gray']
size = ['4,4,4', '2,2,2']
bg = ['blue', 'green', 'purple', 'yellow', 'orange', 'black', 'gray']
def up():
move.forward(25)
def down():
move.backward(15)
def left():
move.left(30)
def right():
move.right(30)
def b():
turtle.bgcolor(random.choice(bg))
def clickleft(x,y):
move.fillcolor(random.choice(colors))
def clickright(x,y):
move.pencolor(random.choice(outline))
turtle.listen()
turtle.onscreenclick(clickleft, 1)
turtle.onscreenclick(clickright, 3)
turtle.onkey(up, 'Up')
turtle.onkey(down, 'Down')
turtle.onkey(left, 'Left')
turtle.onkey(right, 'Right')
turtle.onkey(b, 'b')
And here is the code for all the asteroids.这是所有小行星的代码。 They have to be this size and shape unless there's another way to make sure the circles can't overlap
它们必须是这样的大小和形状,除非有另一种方法来确保圆圈不能重叠
WIDTH, HEIGHT = 400, 400
ASTEROID_RADIUS = 53
NUMBER_ASTEROIDS = 53
CURSOR_SIZE = 20
asteroid = Turtle()
if move.distance(asteroid)<5:
move.goto(0,0)
asteroid_prototype = Turtle()
asteroid_prototype.hideturtle()
asteroid_prototype.color('grey')
asteroid_prototype.shape('circle')
asteroid_prototype.shapesize(ASTEROID_RADIUS / CURSOR_SIZE)
asteroid_prototype.speed('fastest') # because 15 isn't a valid argument
asteroid_prototype.penup()
asteroids = []
for _ in range(NUMBER_ASTEROIDS):
asteroid = asteroid_prototype.clone()
asteroid.setposition( \
randint(ASTEROID_RADIUS - WIDTH, WIDTH - ASTEROID_RADIUS), \
randint(ASTEROID_RADIUS - HEIGHT, HEIGHT - ASTEROID_RADIUS) \
)
while any(map((lambda a: lambda b: a.distance(b) < ASTEROID_RADIUS)(asteroid), asteroids)):
asteroid.setposition( \
randint(ASTEROID_RADIUS - WIDTH, WIDTH - ASTEROID_RADIUS), \
randint(ASTEROID_RADIUS - HEIGHT, HEIGHT - ASTEROID_RADIUS) \
)
asteroid.showturtle()
asteroids.append(asteroid)
The code has to be like this so the circles don't overlap Thank you in advance代码必须是这样的,所以圆圈不会重叠提前谢谢你
Define a function that will reset the player's position and the positions of all the asteroids.定义一个 function 将重置玩家的 position 和所有小行星的位置。
Add a while loop to constantly check to see if the player collides with any of the asteroids.添加一个while循环来不断检查玩家是否与任何小行星发生碰撞。
When detected a collision, call the reset
function.当检测到碰撞时,调用
reset
function。
def reset(asteroids):
move.setposition(-500,0)
for asteroid in asteroids:
asteroid.goto(1000, 1000)
asteroids2 = []
for asteroid in asteroids:
asteroid.setposition( \
randint(ASTEROID_RADIUS - WIDTH, WIDTH - ASTEROID_RADIUS), \
randint(ASTEROID_RADIUS - HEIGHT, HEIGHT - ASTEROID_RADIUS) \
)
while any(map((lambda a: lambda b: a.distance(b) < ASTEROID_RADIUS)(asteroid), asteroids2)):
asteroid.setposition( \
randint(ASTEROID_RADIUS - WIDTH, WIDTH - ASTEROID_RADIUS), \
randint(ASTEROID_RADIUS - HEIGHT, HEIGHT - ASTEROID_RADIUS) \
)
asteroid.showturtle()
asteroids2.append(asteroid)
return asteroids2
turtle.listen()
turtle.onscreenclick(clickleft, 1)
turtle.onscreenclick(clickright, 3)
turtle.onkey(up, 'Up')
turtle.onkey(down, 'Down')
turtle.onkey(left, 'Left')
turtle.onkey(right, 'Right')
turtle.onkey(b, 'b')
while True:
if any(move.distance(asteroid) < 37 for asteroid in asteroids):
asteroids = reset(asteroids)
update()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.