需要帮助检测python中的冲突



我尝试了多种方法来检测玩家乌龟和苹果乌龟之间的碰撞,但都没有发生。我想让它做的是,当乌龟离得太近时,玩家乌龟就会消失。如果苹果在玩家下方变低,它会重置为顶部。这是我完成的代码。我是新来的代码,所以我不是很好。

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

冲突测试可以像在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()

我想让它做的是,当海龟离得太近时玩家乌龟消失了。如果苹果低于玩家,它将重置回顶部

我上面的修复程序只是检测并宣布碰撞,您需要增强它以包括这些额外的操作。

最新更新