import turtle as t
import random
# Create Screen Object
screen = t.Screen()
# Specify screen background color
screen.bgcolor("lightgreen")
# drawing a fence
mypen = t.Turtle()
mypen.penup()
mypen.setposition(-300, 300)
mypen.pendown()
mypen.pensize(3)
for x in range(4):
mypen.forward(600)
mypen.right(90)
mypen.hideturtle()
# Create Turtle Object p
p = t.Turtle()
# p Make the object look like a turtle
p.shape("turtle")
# pSet object size
p.turtlesize(2, 2)
# p Set object color,
p.color("blue")
# Remove the line that follows the turtle
p.penup()
# Tortoise Motion Speed and Variables
p.speed = 1
score = 0
game_over = False
def turnleft():
p.left(30)
def turnright():
p.right(30)
def increasespeed():
global speed
speed += 1
def decreasespeed():
global speed
speed -= 1
def rand_pos():
x_cor = random.randint(-150, 150)
y_cor = random.randint(-150, 150)
return x_cor, y_cor
#Display scores
t.goto(0, 350)
t.write(f"score: {score}", False, "center", ("", 20))
#Food
food = t.Turtle()
food.ht()
food.shape("triangle")
food.up()
food.color("darkgreen")
food.setheading(90)
food.speed(0)
food.goto(rand_pos())
food.st()
#Dokcho
p_herbs = t.Turtle()
p_herbs.ht()
p_herbs.shape("triangle")
p_herbs.up()
p_herbs.color("red")
p_herbs.setheading(90)
p_herbs.speed(0)
p_herbs.goto(rand_pos())
p_herbs.st()
screen.listen()
screen.onkey(turnleft, "Left")
screen.onkey(turnright, "Right")
screen.onkey(increasespeed, "Up")
screen.onkey(decreasespeed, "Down")
while not game_over:
p.forward(p.speed)
if p.xcor() > 360 or p.xcor() < -360 or p.ycor() > 360 or p.ycor() < -360:
p.right(180)
if p.distance(food) < 20:
food.goto(rand_pos())
p_herbs.goto(rand_pos())
p.speed += 0.1
score += 1
t.clear()
t.write(f"score: {score}", False, "center", ("", 20))
if p.distance(p_herbs) < 20:
game_over = True
t.goto(0, 0)
t.write("Game Over", False, "center", ("", 50))
while True:
p.forward(p.speed)
# Fence check
if p.xcor() > 300 or p.xcor() < -300:
p.right(180)
if p.ycor() > 300 or p.ycor() < -300:
p.right(180)
If you eat poisonous weeds here, you're going to have a game over right away, so what if you want to add about three extra lives?
python turtle game
def game_not_over():
Return Life > 0
while game_not_over() :
# The following is omitted!
© 2024 OneMinuteCode. All rights reserved.