How to create a Google Dino Run game in Python
“Using turtle”
I am trying to create one, but I have got only half success.
I am not able to make it jump properly.
You can notice that whenever the dinosaur jumps, the plant stops moving.
(Believe me, it’s turtle not the actual game. Though I have used their images in .gif format. XD)
Though, I will work on the project in future (maybe after a month) but as my exams are coming so I will not make any upgrade in the codes.
Still if you want to learn, here’s the source code:
- import turtle
- import time
- import keyboard
- count = 0
- dx = 0.3
- img = r"C:\Users...dinoo.gif"
- img1 = r"C:\Users...dino1.gif"
- img2 = r"C:\Users...dino2.gif"
- obj1 = r"C:\Users...obs1.gif"
- bgpic = r"C:\Users...bgpic.png"
- imglst = [img1,img2]
- win = turtle.Screen()
- win.bgpic(bgpic)
- win.addshape(img)
- win.addshape(img1)
- win.addshape(img2)
- win.addshape(obj1)
- win.tracer(0)
- obs1 = turtle.Turtle()
- obs1.shape(obj1)
- obs1.ht()
- obs1.pu()
- obs1.goto(420,142)
- dino = turtle
- dino.lt(90)
- dino.shape(img)
- dino.pu()
- dino.goto(-255,142)
- win.update()
- def jump():
- for i in range(128):
- dino.sety(142+i)
- win.update()
- time.sleep(0.1)
- dino.sety(170)
- win.update()
- dino.sety(142)
- def obs1move():
- obs1x = obs1.xcor()
- obs1.setx(obs1x-dx)
- if obs1x < 300:
- obs1.st()
- if obs1x < -300:
- obs1.ht()
- obs1.setx(420)
- while True:
- win.update()
- count = count+1
- if count > 1:
- count = 0
- img = imglst[count]
- dino.shape(img)
- if keyboard.is_pressed("up") or keyboard.is_pressed("space"):
- jump()
- obs1move()
You need to download these images (keep the format same, i.e, if it’s .png then save it as .png):
- bgpic.png
- obs1.gif
- dino2.gif
- dino1.gif
- dinoo.gif
I will try to complete the game soon, after my exams.
Thanks for reading.
Have a great day ahead.
RajMa