Snake Game Turtle Graphics in Python - python

So I have a project where I have to create a game in TURTLE graphics only without using any other modules except turtle and the random module. I am creating the traditional snake game except when the snake "eats" the object it doesn't get bigger. The problem with my game is that when the "snake" gets to the object, the object does not disappear and I want it to disappear and reappear in another position. Please Help. Here is the code:
from turtle import *
import random
setup(700, 700)
title("Snake Game!")
bgcolor("blue")
# Boundary
bPen = Pen()
bPen.color("Orange")
bPen.up()
bPen.goto(-300, -300)
bPen.down()
for i in range(4):
bPen.ht()
bPen.fd(600)
bPen.lt(90)
# Player
playerPen = Pen()
playerPen.color("Orange")
playerPen.up()
playerPen.width(4)
# Create Circle
circle = Pen()
circle.shape("circle")
circle.color("Red")
circle.up()
circle.speed(0)
circle.goto(-100, 100)
# Speed
speed = 2
# Movement functions
def left():
playerPen.lt(90)
def right():
playerPen.rt(90)
def speedBoost():
global speed
speed += 1
# Key Prompts
onkey(left, "Left")
onkey(right, "Right")
onkey(speedBoost, "Up")
listen()
# Infinity loop and player knockback
while True:
playerPen.fd(speed)
if playerPen.xcor() < -300 or playerPen.xcor() > 300:
playerPen.rt(180)
elif playerPen.ycor() < - 300 or playerPen.ycor() > 300:
playerPen.rt(180)
# Collision with circle
if playerPen.xcor() == circle.xcor() and playerPen.ycor() == circle.ycor():
circle.goto(random.randint(-300, 300), random.randint(-300, 300))
done()

The code works, but hitting the snake exactly on the center of the circle is difficult. To fix this, at the top of your code add a constant:
RADIUS = 10
That is roughly the radius of the circle. Then, change this line:
if playerPen.xcor() == circle.xcor() and playerPen.ycor() == circle.ycor():
to instead be:
if abs(playerPen.xcor() - circle.xcor()) < RADIUS and abs(playerPen.ycor() - circle.ycor()) < RADIUS:
And enjoy the game!
Or if you can't enjoy the game due not being able to use abs(), here's another way to write that same line without it:
if -RADIUS < playerPen.xcor() - circle.xcor() < RADIUS and -RADIUS < playerPen.ycor() - circle.ycor() < RADIUS:

Related

Is there a command for touching an object or a colour in turtle python?

I'm trying to make my own ping pong game so when the ball touches the bar it bounces but i don't know the command for touching a colour or an object. This is my code:
#imports
import random
import turtle
import time
import winsound
#turtle
x = 0
ball = turtle.Turtle()
ball.shape("circle")
ball.color("red")
ball.goto(0, 0)
ball.penup()
background = turtle.Screen()
background.bgcolor("turquoise")
bar = turtle.Turtle()
bar.shape("square")
bar.shapesize(1, 5, 1)
bar.penup()
bar.speed(100000000000000000000000000000000000000000)
bar.sety(-70)
ball.left(1)
ball.backward(1)
def sp():
while True:
if ball.distance(bar) < 2:
ball.left(1)
ball.backward(1)
else:
ball.right(1)
ball.forward(1)
if bar.xcor == -300 or bar.xcor == 300:
time.sleep(1)
def l():
bar.backward(25)
def r():
bar.forward(25)
background.onkey(l, "Left")
background.onkey(r, "Right")
background.onkey(sp, "Up")
turtle.listen()
background.mainloop()
The bar is moved with the arrows and the ball moves. What's the command for an object touching another one?
If you mean how far the turtle is from an object, use the distance() method.
Exmaple:
if ball.distance(Your Object) < 28:
#do something
You can put for the distance to the object a number between 25 and 30, if you like this.
If you're talking about collision you do this
if ball.pos() == (name of paddle).pos():
## run your command

How to stop turtle movement when two turtles are in proximity of each other?

I'm making a game when the user has to move the car to dodge obstacles, Everything working except I can't get the game to and end. My goal is to end the game when the block hits the turtle.
The issue is I can't get the game to detect the turtles to detect their proximity and the exit or quit command doesn't work. Also I don't think the code pasted properly so adjust the indents if it doesn't work.
import turtle as trtl
import random as rand
import sys
#initialize turtles
turtle = trtl.Turtle(shape = "turtle")
block = trtl.Turtle(shape = "square")
drawer = trtl.Turtle()
blockList = []
wn = trtl.Screen()
#game configuration
turtle.pu()
turtle.goto(0,-150)
drawer.pu()
drawer.goto(0,-160)
drawer.pd()
drawer.pensize(10)
drawer.pencolor("blue")
drawer.forward(180)
drawer.left(90)
drawer.forward(300)
drawer.left(90)
drawer.forward(350)
drawer.left(90)
drawer.forward(300)
drawer.left(90)
drawer.forward(180)
drawer.hideturtle()
def turtleRight():
turtle.setheading(0)
turtle.pu()
turtle.forward(15)
def turtleLeft():
turtle.setheading(180)
turtle.pu()
turtle.forward(15)
#actions
wn.onkeypress(turtleRight, 'Right')
wn.onkeypress(turtleLeft, 'Left')
wn.listen()
for i in range(5):
app = block
blockList.append(app)
#functions
def draw_blocks(index):
blockList[index].penup()
blockList[index].shape("square")
wn.tracer(False)
blockList[index].setx(rand.randint(-150,150))
blockList[index].sety(rand.randint(0,125))
blockList[index].showturtle()
wn.tracer(True)
wn.update()
def drop_block(index):
blockList[index].penup()
blockList[index].clear()
blockList[index].speed(2)
blockList[index].sety(-150)
blockList[index].hideturtle()
draw_blocks(index)
xDistance = abs(turtle.xcor()-block.xcor())
yDistance = abs(turtle.ycor()-block.ycor())
if (xDistance < 20 and yDistance < 20):
sys.exit()
else:
drop_block(i)
for i in range(5):
drop_block(i)
wn.mainloop()
trtl.done()
Can anyone help..!?
The problem is that you're re-drawing the block and thus resetting its position before you're testing the distance.
Alter the function like so:
def drop_block(index):
blockList[index].penup()
blockList[index].clear()
blockList[index].speed(2)
blockList[index].sety(-150)
blockList[index].hideturtle()
xDistance = abs(turtle.xcor()-block.xcor())
yDistance = abs(turtle.ycor()-block.ycor())
print(xDistance, yDistance)
if (xDistance < 20 and yDistance < 20):
sys.exit()
else: # redraw blocks _after_ calculating distance
draw_blocks(index)
drop_block(i)

Turtle stops when I shoot

I have gotten an issue where an enemy turtle stops when I shoot. I am relatively new to python so I know that my code is pretty bad. I cant seem to spot why this happens but I'm assuming its got something to do with the while loop.
here is the code:
(i have added notes so it is easy to skip to important parts of it)
import turtle
import os
#wn is window
#bp = border
bullet = 'ready'
#screen setup
wn = turtle.Screen()
wn.bgcolor('black')
wn.title('SPACE.INVADERS')
#border
bp = turtle.Turtle()
bp.speed(0)
bp.color('green')
bp.penup()
bp.setposition(-300,-300)
bp.pendown()
count=0
while count != 5:
count= (count+1)
bp.fd(600)
bp.lt(90)
bp.hideturtle()
#player
p = turtle.Turtle()
p.color('red')
p.shape('triangle')
p.penup()
p.speed(0)
p.setposition(0,-250)
p.setheading(90)
#enemy
e = turtle.Turtle()
e.penup()
e.speed(0)
e.shape('square')
e.shapesize(1.25,1.25)
e.color('orange')
e.setpos(-250,250)
e.speed(1)
#p = player
#ps = player speed
ps = 15
#moving left and right
def left_mov():
x = p.xcor()
x -= ps
p.setx(x)
def right_mov():
x = p.xcor()
x += ps
p.setx(x)
#shooting
def shoot():
global bullet
if bullet == 'ready':
bullet = 'fire'
shot= turtle.Turtle()
shot.penup()
shot.speed(0)
shot.goto(p.pos())
shot.color('white')
shot.shape('triangle')
shot.shapesize(0.5)
shot.lt(90)
shot.speed(1)
shot.fd(550)
bullet = 'ready'
#bindings
turtle.listen()
turtle.onkey(left_mov, 'Left')
turtle.onkey(right_mov, 'Right')
turtle.onkey(shoot, 'space')
#enemy movement
while True:
e.fd(500)
e.rt(90)
e.fd(25)
e.rt(90)
e.fd(500)
e.lt(90)
e.fd(25)
e.lt(90)
I'm assuming its got something to do with the while loop
Yes, it shouldn't be there! In an event-driven world like turtle, there should never be a while True: loop. Instead, we need a timer event that fires off on regular intervals that does incremental updates of the objects in motion (enemy and bullet) so they appear to move at the same time. Below is my rework of your code using a timer event:
from turtle import Screen, Turtle
# moving left and right
def left_move():
player.setx(player.xcor() - player_speed)
def right_move():
player.setx(player.xcor() + player_speed)
# screen setup
screen = Screen()
screen.bgcolor('black')
screen.title('SPACE.INVADERS')
# border
border = Turtle(visible=False)
border.speed('fastest')
border.color('green')
border.penup()
border.setposition(-300, -300)
border.pendown()
for _ in range(4):
border.forward(600)
border.left(90)
# player
player = Turtle('triangle')
player.speed('fastest')
player.color('red')
player.penup()
player.setposition(0, -250)
player.setheading(90)
player_speed = 15
# enemy
enemy = Turtle('square')
enemy.speed('fast')
enemy.shapesize(1.25)
enemy.color('orange')
enemy.penup()
enemy.setpos(-250, 250)
enemy_speed = 6 # enemy speed
enemy_direction = 1 # enemy direction
# bullet
bullet = Turtle('triangle', visible=False)
bullet.speed('fastest')
bullet.color('white')
bullet.shapesize(0.5)
bullet.penup()
bullet.left(90)
bullet_speed = 9 # bullet speed
# shooting
def shoot():
if not bullet.isvisible():
bullet.goto(player.position())
bullet.showturtle()
def move():
global enemy_direction
if bullet.isvisible():
if bullet.ycor() > 275:
bullet.hideturtle()
else:
bullet.forward(bullet_speed)
# enemy movement
enemy.forward(enemy_direction * enemy_speed)
enemy.settiltangle(enemy.tiltangle() + 3) # just for fun
x, y = enemy.position()
if x > 250 or x < -250:
enemy_direction *= -1
enemy.sety(y - 25)
screen.ontimer(move, 50)
# bindings
screen.onkey(left_move, 'Left')
screen.onkey(right_move, 'Right')
screen.onkey(shoot, 'space')
screen.listen()
move()
screen.mainloop()
This should give you the basic functionality you need to move forward and add collision detection (bullet to enemy, enemy to player, player to wall) and scoring.
Also note that turtles are global entities so creating a new bullet every time you need one is a bad idea. Even if you evenually allow overlapping bullets, keep a reusable set of bullets for that purpose.

Make the snake in game move piece by piece instead of as a whole

My game is a snake game where you have to get the fruit. This is the code:
#Importing random and turtle module
import random
import turtle as t #Shortening the turtle module to t
#Adding a orange background
t.bgcolor("orange")
#Creating a turtle to become the snake
snake = t.Turtle() # Creates a new turtle for the snake
snake.shape("square") # Making the snake square shaped
snake.color("green") # Making the snake green
snake.speed(0) # Making the snake not move before the game starts
snake.penup() # Disables the turtles pen allowing the turtle to move around the screen without drawing a line along the way
snake.hideturtle() #This command hides the turtle
#Creating a turtle to draw the leaves
fruit = t.Turtle() # Creates a new turtle for the fruit
fruit.shape("circle") # Making the fruit circular
fruit.color("red") # Making the fruit red
fruit.penup() # Disables the turtles pen allowing the turtle to move around the screen without drawing a line along the way
fruit.hideturtle() #This command hides the turtle
fruit.speed(0) # Making the fruit not move
#Adding more turtles to display the text for the game and the score
gamestarted = False #To know if the game has started
writeturtle = t.Turtle()# Creates a new turtle for the text
writeturtle.write("Press SPACE to play!", align="center", font=("Calibri", 16, "bold")) #This line draws text on the screen
writeturtle.hideturtle() # Hides the turtle but not the text
#Adding a score turtle
scoreturtledude = t.Turtle()# Creates a new turtle for the score
scoreturtledude.hideturtle()#This command hides the turtle
scoreturtledude.speed(0)#Make sure the turtle stays where it is so it can update the score
def outsidewindow(): #Defining the outside window function
leftwall = -t.window_width() / 2 #Calculates the left wall
rightwall = t.window_width() / 2 #Calculates the right wall
topwall = t.window_height() / 2 #Calculates the top wall
bottomwall = -t.window_height() / 2 #Calculates the bottom wall
(x, y) = snake.pos() #Then it asks the snake for its current position
outside = x< leftwall or x> rightwall or y< bottomwall or y> topwall #Comparing the snakes coordinates with the wall position
return outside #If any of the four conditions above is true then outside is true
def gameover(): #Defining gameover
snake.color("orange")#CHANGES THE COLOR OF THE SNAKE TO ORANGE(the same color as the background) so it can't be seen
fruit.color("orange")#CHANGES THE COLOR OF THE SNAKE TO ORANGE(the same color as the background) so it can't be seen
t.penup() # this disables the turtles pen
t.hideturtle() #This hides the turtle
t.write("YOUR SNAKE CRAWLED INTO THE GARDEN. GAME OVER!", align="center", font=("Calibri", 20, "normal"))#This line writes a text for when the game is over
def showscore(currentscore):
scoreturtledude.clear()# This clears the score
scoreturtledude.penup()# Disables turtle pen
x = (t.window_width() / 2) -75 # 75 pixels from the right
y = (t.window_height() / 2) -75 # And 75 pixels from the top
scoreturtledude.setpos(x, y)#Sets the turtles position
scoreturtledude.write(str(currentscore) , align="right", font=("Calibri", 40, "bold"))# WRITES THE SCOre in calibri font
def placefruit():
fruit.hideturtle()
fruit.setposition(random.randint(-200, 200), random.randint(-200, 200))
fruit.showturtle()
def startgame():
global gamestarted
if gamestarted: #If the game has already started, the return command makes the function quit so it doesn't run a second time
return
gamestarted = True
score = 0 #Sets the score to 0
writeturtle.clear() # Clears the text from the screen
snakespeed = 2
snakelength = 3
snake.shapesize(1, snakelength, 1)#This makes the snake turtle stretch from a square shape into a snake shape
snake.showturtle() #This shows the snake
showscore(score) #This uses the showscore function we defined earlier
placefruit()
while True: #This puts it in a forever loop
snake.forward(snakespeed)
if snake.distance(fruit) < 20:# Makes the snake eat the leaf when it is less than 20 pixels away
placefruit() #This line places another fruit on the screen
snakelength = snakelength + 1 #This line line and the next line together will make the snake grow longer
snake.shapesize(1, snakelength, 1)
snakespeed = snakespeed + 1 #This increases the speed of the snake
score = score + 100 #This increases the score
showscore(score) #This shows the score
if outsidewindow():
gameover()
break
def up():
if snake.heading() == 0 or snake.heading() == 180: #checks if the snake is heading left or right
snake.setheading(90)#Heads the snake straight up
def down():
if snake.heading() == 0 or snake.heading() == 180:#checks if the snake is heading left or right
snake.setheading(270)#Heads the snake straight down
def left():
if snake.heading() == 90 or snake.heading() == 270:#checks if the snake is heading up or down
snake.setheading(180)#Heads the snake straight right
def right():
if snake.heading() == 90 or snake.heading() == 270:#checks if the snake is heading up or down
snake.setheading(0)#Heads the snake straight left
t.onkey(startgame, "space") # The onkey() function binds yhe space bar to the startgame () so the game will not start until the player presses space
t.onkey(up, "Up") #The onkey() function binds the up key to the startgame
t.onkey(down, "Down")#The onkey() function binds the down key to the startgame
t.onkey(right, "Right")#The onkey() function binds the right key to the startgame
t.onkey(left, "Left")#The onkey() function binds the left key to the startgame
t.listen() # The listen function allows the program to recieve signals from the key board
t.mainloop()
This code makes a game where the snake turns altogether, but I want the snake to turn piece by piece. Could you help me?
Below I've merged your game with this short example of how to move a segmented snake around the screen that I wrote earlier -- I'm surprised your search of SO didn't turn this up.
I've also made some other changes to accomodate this motion. Your snake changes from a turtle to a list of turtles, with the final turtle in the list being the head of the snake. The keyboard arrow handlers no longer change the snake's direction directly, they merely set a new direction variable for when the next turtle movement occurs -- this prevents conflicts in adjusting the turtle.
Your while True: has no place in an event-driven world like turtle so it's been replaced with an ontimer() event -- otherwise you potentially lock out events:
from random import randint
from turtle import Turtle, Screen # force object-oriented turtle
SCORE_FONT = ("Calibri", 40, "bold")
MESSAGE_FONT = ("Calibri", 24, "bold")
SEGMENT_SIZE = 20
def outsidewindow():
(x, y) = snake[-1].pos() # Ask the snake head for its current position
return not (leftwall < x < rightwall and bottomwall < y < topwall)
def gameover():
for segment in snake:
segment.hideturtle()
fruit.hideturtle()
writeturtle.write("Your snake crawled into the garden! Game over!", align="center", font=MESSAGE_FONT)
def showscore(currentscore):
scoreturtledude.undo() # Clear the previous score
scoreturtledude.write(currentscore, align="right", font=SCORE_FONT)
def placefruit():
fruit.hideturtle()
fruit.setposition(randint(-200, 200), randint(-200, 200))
fruit.showturtle()
def startgame():
screen.onkey(None, "space") # So this doesn't run a second time
writeturtle.clear() # Clear the text from the screen
showscore(score)
for segment in snake: # Show the snake
segment.showturtle()
placefruit()
rungame()
def rungame():
global score, snakelength
segment = snake[-1].clone() if len(snake) < snakelength else snake.pop(0)
screen.tracer(False) # segment.hideturtle()
segment.setposition(snake[-1].position())
segment.setheading(snake[-1].heading())
segment.setheading(direction)
segment.forward(SEGMENT_SIZE)
if outsidewindow():
gameover()
return
screen.tracer(True) # segment.showturtle()
snake.append(segment)
if snake[-1].distance(fruit) < SEGMENT_SIZE: # Make the snake eat the fruit when it is nearby
placefruit() # Place another fruit on the screen
snakelength += 1
score += 100
showscore(score)
screen.ontimer(rungame, max(50, 150 - 10 * len(snake))) # Speed up slightly as snake grows
def up():
global direction
current = snake[-1].heading()
if current == 0 or current == 180: # Check if the snake is heading left or right
direction = 90 # Head the snake up
def down():
global direction
current = snake[-1].heading()
if current == 0 or current == 180: # Check if the snake is heading left or right
direction = 270 # Head the snake down
def left():
global direction
current = snake[-1].heading()
if current == 90 or current == 270: # Check if the snake is heading up or down
direction = 180 # Head the snake left
def right():
global direction
current = snake[-1].heading()
if current == 90 or current == 270: # Check if the snake is heading up or down
direction = 0 # Head the snake right
screen = Screen()
screen.bgcolor("orange")
leftwall = -screen.window_width() / 2 # Calculate the left wall
rightwall = screen.window_width() / 2 # Calculate the right wall
topwall = screen.window_height() / 2 # Calculate the top wall
bottomwall = -screen.window_height() / 2 # Calculate the bottom wall
# Creating a turtle to become the snake
head = Turtle("square", visible=False) # Create a new turtle for the snake
head.color("green") # Make the snake green
head.penup() # Disable the turtle's pen, allowing the turtle to move around the screen without leaving a trail
snake = [head]
snakelength = 3
score = 0
direction = 0
# Creating a turtle to draw the leaves
fruit = Turtle("circle", visible=False) # Create a turtle for the fruit
fruit.penup() # Raise the turtle's pen, allowing turtle to move around screen without leaving a trail
fruit.color("red")
# Add more turtles to display the text for the game and the score
writeturtle = Turtle(visible=False) # Create a new turtle for the text
writeturtle.write("Press SPACE to play!", align="center", font=MESSAGE_FONT)
# Add a score turtle
scoreturtledude = Turtle(visible=False) # Create a new turtle for the score
scoreturtledude.penup() # Disable turtle pen
x = screen.window_width() / 2 - 75 # 75 pixels from the right
y = screen.window_height() / 2 - 75 # And 75 pixels from the top
scoreturtledude.setpos(x, y) # Set the turtle's position
scoreturtledude.write('', align="right", font=SCORE_FONT)
screen.onkey(startgame, "space") # Bind space bar to startgame() so game will start when player presses space
screen.onkey(up, "Up") # Bind the up key to up()
screen.onkey(down, "Down") # Bind the down key to down()
screen.onkey(right, "Right") # Bind the right key to right()
screen.onkey(left, "Left") # Bind the left key to left()
screen.listen() # Allow the window to receive signals from the keyboard
screen.mainloop()
You've a number of issues in your code, e.g. little ones like what turtle is gameover() referring to when it does t.hideturtle()? But two major ones are:
Reread what speed(0) does -- you've misunderstood it.
You need to learn to write better comments. This is silly:
fruit.color("red") # Making the fruit red
This is pretty good:
t.listen() # The listen function allows the program to recieve signals from the key board

Keyboard input and score. Python turtle

I have found a "flappy game" project and I am modifying it to my liking.
I would like to know how I can control the definition "tap" with the keyboard and not with the mouse (I have tried the online code "onkey" and it did not work) and I would like to know if I can add a score.
Next I attach the code that I have in possession at the moment and I would appreciate some feedback.
from random import *
from turtle import *
from freegames import vector
bird = vector(0, 0)
balls = []
def tap(x, y):
"Move bird up in response to screen tap."
up = vector(0, 30)
bird.move(up)
def inside(point):
"Return True if point on screen."
return -200 < point.x < 200 and -200 < point.y < 200
def draw(alive):
"Draw screen objects."
clear()
goto(bird.x, bird.y)
if alive:
dot(10, 'green')
else:
dot(10, 'red')
for ball in balls:
goto(ball.x, ball.y)
dot(20, 'black')
update()
def move():
"Update object positions."
bird.y -= 5
for ball in balls:
ball.x -= 3
if randrange(10) == 0:
y = randrange(-199, 199)
ball = vector(199, y)
balls.append(ball)
while len(balls) > 0 and not inside(balls[0]):
balls.pop(0)
if not inside(bird):
draw(False)
return
for ball in balls:
if abs(ball - bird) < 15:
draw(False)
return
draw(True)
ontimer(move, 50)
setup(420, 420, 370, 0)
hideturtle()
up()
tracer(False)
onscreenclick(tap)
move()
done()
I've tried all kinds of functions and none has worked.
I would like to know how I can control the definition "tap" with the
keyboard and not with the mouse
That should be as simple as replacing:
onscreenclick(tap)
with
onkey(tap, "Up")
listen()
and dropping the x and y arguments to tap(). I've reworked your code below to make this change as well as eliminate:
from freegames import vector
and make it run just with what's available in turtle. I also changed the logic of keeping vectors and having the default turtle draw dots in their locations to instead making the balls and bird all turtles and just moving them as needed. Since turtles aren't garbage collected normally, I keep a list of balls that have gone off screen to reuse again:
from random import randrange
from turtle import Turtle, Screen
BIRD_RADIUS = 10
BALL_RADIUS = 20
CURSOR_SIZE = 20
WINDOW_WIDTH, WINDOW_HEIGHT = 420, 420
GALLERY_WIDTH, GALLERY_HEIGHT = WINDOW_WIDTH - BALL_RADIUS, WINDOW_HEIGHT - BIRD_RADIUS
def click():
"Move bird up in response to screen key click."
bird.forward(min(BIRD_RADIUS * 3, WINDOW_HEIGHT/2 - bird.ycor())) # flying higher
def is_ball_inside(turtle):
"Return True if point on screen."
x = turtle.xcor()
return -GALLERY_WIDTH/2 < x
def is_bird_inside(turtle):
"Return True if point on screen."
y = turtle.ycor()
return -GALLERY_HEIGHT/2 < y < GALLERY_HEIGHT/2
def draw(is_alive):
"Draw screen objects."
if not is_alive:
bird.color('red')
screen.update()
def move():
"Update object positions."
if not is_bird_inside(bird):
draw(False) # flew or fell out of gallery
return
bird.backward(BIRD_RADIUS/2) # falling to earth
for ball in balls:
ball.forward(3)
for ball in balls:
if ball.distance(bird) < (BIRD_RADIUS + BALL_RADIUS) / 2:
draw(False)
return
while balls and not is_ball_inside(balls[0]):
ball = balls.pop(0)
ball.hideturtle()
spare_balls.append(ball) # reduce, reuse, recycle
if randrange(10) == 0:
ball = spare_balls.pop() if spare_balls else Turtle("circle", visible=False)
ball.shapesize(BALL_RADIUS / CURSOR_SIZE)
ball.setheading(180)
ball.penup()
ball.goto(GALLERY_WIDTH/2, randrange(BALL_RADIUS - GALLERY_HEIGHT/2, GALLERY_HEIGHT/2 - BALL_RADIUS))
ball.showturtle()
balls.append(ball)
draw(True)
screen.ontimer(move, 50)
screen = Screen()
screen.setup(WINDOW_WIDTH, WINDOW_HEIGHT + 10) # little bit added for title bar
bird = Turtle("circle", visible=False)
bird.shapesize(BIRD_RADIUS / CURSOR_SIZE)
bird.color("green")
bird.setheading(90)
bird.penup()
bird.showturtle()
balls = []
spare_balls = []
screen.tracer(False)
screen.onkey(click, "Up")
screen.listen()
move()
screen.mainloop()
To make the turtle window the keyboard listener, you need to touch it with the mouse, to make it active, before hitting the up arrow key.
Before adding the current score to the window, you need to define how one scores. (Is it the number of ball successfully dodged?) Here I would use an additional, invisible turtle stationed in a fixed position that just does undo() and write() to update the scrore.

Categories