So I am making a clone for chrome's dino game in python turtle . I have done the randomisation for the cactus (which is very hackish , and yes if something is better than my aaproach , please help !)and the jumping is working.But as the heading says , I am unable to detect collisions properly. I have tried this much :
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()
Normally, I'd say use the
turtle.distance()
function. But since your two turtles are different shapes, you need a custom collision function that takes into account the shape of both. I've reworked your code below incorporating such as well as fixing it to use turtle timer events instead ofwhile True
andsleep()
and other changes:I believe it is bascially playable now but you might want to tweak some of the constants to taste.