【问题标题】:Need help detecting collision in python需要帮助检测python中的碰撞
【发布时间】:2021-12-16 13:32:07
【问题描述】:

我尝试了多种方法来检测玩家乌龟和苹果乌龟之间的碰撞,但没有任何反应。我想要它做的是,当海龟离得太近时,玩家海龟会消失。如果苹果低于玩家,它会重新回到顶部。 这是我完成的代码。我是编码新手,所以我不是很好。

import turtle as trtl
import random as rand

ground_height = -200
# lists for the turtle to use
colors = ["blue", "darkorange", "cyan", "green", "black"]
shapes = [ "triangle", "turtle", "classic"]
sizes = [0.5,1,1.25,1.5,2]
#creates the turtles
player = trtl.Turtle(shape = rand.choice(shapes)) #gives  the circle its shape
player.turtlesize(1.5)
counter = trtl.Turtle()
apple = trtl.Turtle(shape = "circle")
apple.color("red")
apple.turtlesize(rand.choice(sizes))
#gives turtles their colors 
player.color(rand.choice(colors))
player.penup()
apple.penup()
apple.setx(rand.randint(0,200))
apple.sety(rand.randint(0,200))
#sets up timer
font_setup = ("Arial", 20, "normal")
timer = 0
counter_interval = 1000
timer_up = False
counter.hideturtle()
counter.penup()
counter.goto(-200,160)
#gives player movement
def move_player():
  player.forward(10)

def player_left():
  player.left(180)

def player_right():
    player.right(180)

apple.right(90)

#lets the timer start and end
def countdown():
  global timer, timer_up
  counter.clear()
  if timer <= -1:
    counter.write("Time's Up", font=font_setup)
    timer_up = True
  else:
    counter.write("Timer: " + str(timer), font=font_setup)
    timer += 1
    apple.forward(10)
    counter.getscreen().ontimer(countdown,     counter_interval)
countdown()
    # lets the player move on key press
wn = trtl.Screen()
wn.listen()
wn.onkeypress(move_player, "w")
wn.onkeypress(player_left,"a")
wn.onkeypress(player_right,"d")
wn.mainloop()

【问题讨论】:

  • 发布的代码与您想要的相比如何?

标签: python python-turtle


【解决方案1】:

碰撞测试可以像在countdown() 函数中的if 语句中添加一个子句一样简单:

elif apple.distance(player) < 15:
    counter.write("Collision!", font=FONT)

以下是我对您的代码的修改,其中包含了此更改并解决了我注意到的各种样式和编码问题:

from turtle import Screen, Turtle
from random import choice, randint

# lists for the turtle to use
COLORS = ['blue', 'dark orange', 'cyan', 'green', 'black']
SHAPES = ['triangle', 'turtle', 'classic']
SIZES = [0.5, 1, 1.25, 1.5, 2]

FONT = ('Arial', 20, 'normal')

COUNTER_INTERVAL = 1000

# give player movement

def move_player():
    player.forward(10)

def player_left():
    player.left(180)

def player_right():
    player.right(180)

# set up timer
timer = 0

def countdown():
    global timer

    counter.clear()

    if timer <= -1:
        counter.write("Time's Up", font=FONT)
    elif apple.distance(player) < 15:
        counter.write("Collision!", font=FONT)
    else:
        counter.write("Timer: " + str(timer), font=FONT)
        timer += 1
        apple.forward(10)

        screen.ontimer(countdown, COUNTER_INTERVAL)

# create turtles
player = Turtle(shape=choice(SHAPES))
player.turtlesize(1.5)
player.color(choice(COLORS))
player.penup()

counter = Turtle()
counter.hideturtle()
counter.penup()
counter.goto(-200, 160)

apple = Turtle(shape='circle')
apple.color('red')
apple.turtlesize(choice(SIZES))

apple.penup()
apple.setposition(randint(0, 200), randint(0, 200))
apple.setheading(270)

screen = Screen()

# let player move on key press
screen.onkeypress(move_player, 'w')
screen.onkeypress(player_left, 'a')
screen.onkeypress(player_right, 'd')
screen.listen()

countdown()

screen.mainloop()

我想要它做的是,当海龟靠得太近时, 玩家乌龟消失。如果苹果低于玩家, 它会重新设置顶部

我上面的修复只是检测并宣布碰撞,您需要对其进行扩充以包含这些额外的操作。

【讨论】:

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