【问题标题】:How do I implement collision detection with Python Turtle如何使用 Python Turtle 实现碰撞检测
【发布时间】:2020-12-07 12:48:56
【问题描述】:

下午好 当我的 python 乌龟撞到一个圆圈时,我需要我的程序重置。有53个圆圈。 这是乌龟。

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')

这是所有小行星的代码。它们必须是这样的大小和形状,除非有其他方法可以确保圆圈不能重叠

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)

代码必须是这样的,这样圆圈才不会重叠 提前谢谢你

【问题讨论】:

  • 正如@JLeno46 已经说过的,要么更改您发布的位置,要么添加与您所要求的内容相匹配的适当标题。
  • 阅读“如何提出一个好问题”stackoverflow.com/help/how-to-ask 从一些基本的谷歌搜索“Python 海龟碰撞”开始,并尝试实施您在那里找到的一些建议。您的代码不会尝试解决您所说的问题。
  • 这能回答你的问题吗? Detecting collision in Python turtle game

标签: python python-3.x turtle-graphics


【解决方案1】:
  1. 定义一个函数来重置玩家的位置和所有小行星的位置。

  2. 添加一个while循环来不断检查玩家是否与任何小行星发生碰撞。

  3. 当检测到碰撞时,调用reset函数。

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 2014-04-13
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多