Simple turtle game python

Sam Swick
2 min readApr 22, 2024

--

from turtle import Turtle, Screen
from random import randint
import time

bg = Screen()
bg.bgcolor(‘black’)
bg.tracer(0)

#time
start_time = time.time()
counter = 10
points = 0

#create spaceship 1
ss1 = Turtle()

#create food 1
f1 = Turtle()
f1.pu()
f1.setpos(50,50)
#create food 2
f2 = Turtle()
f2.pu()
f2.setpos(-50,50)

# f2 = Turtle()
# f2.pu()
# f2.setpos(-50,-50)

marker = Turtle()
marker.pu()
marker.setpos(-100,100)

def spaceshipx(color, shape, self):
self.speed(0)
self.color(color)
self.shape(shape)
self.pu()

def foodx(color, shape, self):
self.speed(0)
self.color(color)
self.shape(shape)
self.pu()

def movement_ss1(self):
bg.onkey(lambda: self.left(10), ‘Left’)
bg.onkey(lambda: self.right(10), ‘Right’)
bg.onkey(lambda: self.forward(5), ‘Up’)
bg.onkey(lambda: self.forward(-5), ‘Down’)

def contact(ss, food):
global start_time
global counter
global points
x,y = ss.pos()
x2, y2 = food.pos()
if x — 20 < x2 < x+ 20 and y-20 < y2 < y+20:
print(‘bing’)
food.setpos(randint(-100,100),randint(-100,100))
start_time = time.time()
counter -= 1
points += 1

def timer(ss):
global counter
if counter — (time.time() — start_time) <= 0:
print(‘boom’)
bg.bgcolor(‘dark red’)
ss.ht()

spaceshipx(‘blue’, ‘turtle’, ss1)
foodx(‘green’, ‘circle’, f1)
foodx(‘green’, ‘circle’, f2)
# foodx(‘green’, ‘circle’, f2)

# def writing():
# global counter
# marker.clear()
# marker.write(round(counter — (time.time() — start_time),1), ‘s’)

bg.update()
i = 0
while True:

#move spaceship 1
movement_ss1(ss1)
contact(ss1, f1)
contact(ss1,f2)
# contact(ss1, f2)
timer(ss1)
# writing()

bg.listen()
time.sleep(.5)
bg.update()
print(round(counter — (time.time() — start_time),1), ‘s’)
if counter — (time.time() — start_time) <= -0.1:
print(‘your score was’, points, ‘dang, you suck’)
bg.mainloop()
break

#walkthru
#how to round it?
#3 total food
#if timer hits 0 says they lose
#records how many food they ate and prints it out once they lose
#do score part of it together
#create 1 other food together too

--

--