''''''''' FLAPPY TURTLE I avoid the flashing between frames by doing screen.tracer(0) and then, each frame, screen.update(). I'm suprised this is possible with turtle graphics. ''''''''' import random import turtle from time import sleep d = turtle.Turtle() screen = turtle.getscreen() screen.setup(500, 500) screen.clearscreen() screen.bgcolor("cyan") screen.tracer(0) d.width(50) d.pencolor("dark green") d.speed(0) d.shape("turtle") d.fillcolor("yellow") d.up() d.ht() y_spd = 0 y_pos = 50 score = 0 pipe_height = [] pipe_btm = [] pipe_x = [] '''------------------------------------------------------------------------------------------------''' '''------------------------------------------------------------------------------------------------''' def flap(): global y_spd y_spd = 15 screen.listen() screen.onkey(flap, "space") def drawpipe(height, ybtm, xpos): d.goto(xpos, ybtm) d.pendown() d.goto(xpos, ybtm + height) d.penup() def draw(): global y_pos, score d.pencolor("dark green") d.clear() d.fillcolor("yellow") d.goto(-200, y_pos) d.st() d.stamp() d.ht() for i in range(len(pipe_x)): drawpipe(pipe_height[i], pipe_btm[i], pipe_x[i]) d.goto(0, 100) d.fillcolor("red") d.pencolor("red") d.write(round(score), font=("Arial", 30, "bold")) screen.update() def die(): d.goto(-100, 100) d.pencolor("red") d.fillcolor("red") d.clear() d.write("GAME OVER", font=("Arial", 30, "bold")) screen.update() sleep(1) d.clear() d.write("RESTARTING...", font=("Arial", 30, "bold")) screen.update() sleep(1) d.clear() start() def setup(): global y_pos, y_spd, pipe_x, pipe_btm, pipe_height, score y_spd = 0 y_pos = 50 score = 0 pipe_height = [] pipe_btm = [] pipe_x = [] # title screen d.goto(-230, 100) d.fillcolor("red") d.pencolor("red") d.write("FLAPPY TURTLE", font=("Arial", 30, "bold")) d.sety(50) d.write("PRESS SPACE TO JUMP", font=("Arial", 30, "bold")) screen.update() sleep(1) d.goto(-30, -50) d.write("3", font=("Arial", 30, "bold")) screen.update() sleep(1) d.setx(0) d.write("2", font=("Arial", 30, "bold")) screen.update() sleep(1) d.setx(30) d.write("1", font=("Arial", 30, "bold")) screen.update() sleep(1) '''------------------------------------------------------------------------------------------------''' '''------------------------------------------------------------------------------------------------''' def start(): global y_pos, y_spd, pipe_x, pipe_btm, pipe_height, score setup() # main loop tick = 0 while True: sleep(0.1) tick += 1 # draw draw() # add new pipes if tick % 20 == 0: space = 100 pipe_x.append(250) pipe_x.append(250) middle = random.randint(50, 350) pipe_height.append(middle - space) pipe_btm.append(-250) pipe_height.append(400 - middle) pipe_btm.append(-250 + space + middle) # update pipes i = 0 for i in range(len(pipe_x)): pipe_x[i] -= 15 i = 0 while i < len(pipe_x): if pipe_x[i] < -300: pipe_x.pop(i) pipe_btm.pop(i) pipe_height.pop(i) score += 0.5 i += 1 # collision if not (-240 < y_pos < 240): die() for i in range(len(pipe_x)): if pipe_x[i]-25 < -200 < pipe_x[i]+25: if pipe_btm[i]-25 < y_pos < pipe_btm[i] + pipe_height[i]+25: die() # physics y_spd -= 3 y_pos += y_spd '''------------------------------------------------------------------------------------------------''' '''------------------------------------------------------------------------------------------------''' start()