所以我正在为chrome的恐龙游戏python turtle制作一个克隆。我已经为仙人掌做了随机化(这是非常粗糙的,是的,如果有什么比我的方法更好,请帮助!),跳跃是有效的。但是正如标题所说,我无法正确地检测碰撞。我已经尝试了很多:
import turtle
import time
import random
print("dino game in turtle")
wn = turtle.Screen()
wn.bgcolor("white")
wn.setup(width=650, height=400)
wn.tracer(0)
delay = 0.1
#scoring system
score = 0
w = 1
h = 1
#dino
dino = turtle.Turtle()
dino.shape("square")
dino.shapesize(w, h)
dino.color("black")
dino.penup()
dino.goto(-200, -50)
#ground
g = turtle.Turtle()
g.penup()
g.goto(0, -60)
g.pendown()
g.lt(180)
g.fd(500)
g.rt(180)
g.fd(1000)
g.hideturtle()
#cactus
#y = random.randint(2, 4)
cactus = turtle.Turtle()
cactus.penup()
cactus.shape("square")
cactus.color("green")
cactus.shapesize(3, 0.5)
cactus.goto(-50, -30)
cactus2 = turtle.Turtle()
cactus2.penup()
cactus2.shape("square")
cactus2.color("green")
cactus2.shapesize(3, 0.5)
cactus2.goto(600, -30)
cactus3 = turtle.Turtle()
cactus3.penup()
cactus3.shape("square")
cactus3.color("green")
cactus3.shapesize(3, 0.5)
cactus3.goto(600, -30)
#score_pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score: 0 High Score: 0", align="center", font=("Courier", 24, "normal"))
max_height = 150
def steady():
y = dino.ycor()
if y == -50:
return True
else:
return False
def jump():
y = dino.ycor()
if y != -50:
return True
else:
return False
def jumping():
y = dino.ycor()
y += 200
dino.sety(y)
def cactus_move():
x = cactus.xcor()
x -= 20
cactus.setx(x)
def cactus_move2():
x = cactus2.xcor()
x -= 20
cactus2.setx(x)
def cactus_move3():
x = cactus3.xcor()
x -= 20
cactus3.setx(x)
x = dino.xcor()
y = dino.ycor()
def check_rect_collision(dino, x, y, w, h):
if p.x >= x and p.x <= x+w and p.y >= y and p.y <= y+h:
# collision between p and rectangle
return True
return False
wn.listen()
wn.onkeypress(jumping, "space")
while True:
score += 1
check_rect_collision()
#print(score)
#pen.clear()
#pen.clear()
#pen.write("Score: {} ".format(score), align="center", font=("Courier", 24, "normal"))
x = random.randint(200, 300)
xm = random.randint(300, 500)
xms = random.randint(500, 550)
cactus_move()
cactus_move2()
cactus_move3()
y = dino.ycor()
if steady() == True and dino.distance(cactus) < 25:
print("Hello")
break
if steady() == True and dino.distance(cactus2) < 25:
print("Hello")
break
if steady() == True and dino.distance(cactus3) < 25:
print("Hello")
break
if jump() == True and dino.ycor() <= cactus.ycor():
print("Ycor")
break
if jump() == True and dino.ycor() <= cactus2.ycor():
print("Ycor")
break
if jump() == True and dino.ycor() <= cactus3.ycor():
print("Ycor")
break
if steady() == False:
y -= 25
dino.sety(y)
if y >= max_height:
y -= 200
dino.sety(y)
if cactus.xcor() < -400:
cactus.goto(x, -30)
if cactus2.xcor() < -400:
cactus2.goto(xm, -30)
if cactus3.xcor() < -400:
cactus3.goto(xms, -30)
time.sleep(delay)
wn.update()
wn.mainloop()
通常,我会说使用
turtle.distance()
函数。但是由于你的两只海龟的形状不同,你需要一个自定义的碰撞函数来考虑它们的形状。我在下面重新编写了您的代码,包括这样的代码,并将其修复为使用turtle计时器事件,而不是while True
和sleep()
以及其他更改:我相信它现在基本上是可以玩的,但是你可能想要调整一些常数来体验
你可以像游戏引擎一样,把形状放在物体上,应用碰撞检测算法,有一些算法非常简单,比如球体对球体,这就是它的工作原理
在想要检测colision的对象上画一个圆圈,这可以通过创建一个类CircleCollider来实现,例如,将它作为海龟的属性。这个对撞机有一个ID、半径和位置,和海龟一样
创建另一个类来管理碰撞器和碰撞,将碰撞保存在数组或列表中
每次迭代(我假设您的游戏代码在一个循环中),应用sphere vs sphere算法来检测冲突。您必须在所有对象之间执行此操作。(也不要忘记更新碰撞器位置)
球体vs球体算法(两个球体之间):只需计算两个碰撞器之间的距离,它必须小于半径,您可以使用公式d=sqrt((x1-x2)^2+(y1-y2)^2)执行此操作,如果没有,则存在碰撞
我知道这个解决方案可能有点长,但它值得,而且你必须考虑如果圆圈是完美的形状为你的性格,你也可以使用盒子和胶囊,但算法有点复杂。最后,如果您只有两种类型的对象(角色和障碍物),而不是使用id,则可以使用标记(角色或障碍物),而不是每个对象的不同id相关问题 更多 >
编程相关推荐