Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle, math, random, winsound
- screen = turtle.Screen()
- screen.bgcolor("black")
- screen.setup(600,600,700,0)
- #borders
- b = turtle.Pen()
- b.penup()
- b.speed(0)
- b.pencolor("orange")
- b.setpos(-235,-235)
- for i in range(4):
- b.pendown()
- b.forward(470)
- b.left(90)
- # Snake
- s = turtle.Turtle()
- s.color("orange")
- s.fillcolor("black")
- s.shape("square")
- s.shapesize(0.5)
- s.penup()
- s.speed(0)
- speed = 1
- direction = "stop"
- tail = 30
- print(tail)
- # Apple
- a = turtle.Turtle()
- a.color("red")
- a.fillcolor('black')
- a.shape("circle")
- a.shapesize(0.2)
- a.penup()
- a.speed(0)
- a.setpos(random.randrange(-220,200), random.randrange(-220,200))
- print(a.xcor())
- def move():
- global speed
- global direction
- global tail
- if direction =="left":
- s.goto(s.xcor() - speed, s.ycor())
- s.stamp()
- if s.stamp() > tail:
- s.clearstamps(2)
- if is_contact(s, a):
- winsound.Beep(1000,40)
- speed += 0.5
- a.setpos(random.randrange(-220,200), random.randrange(-220,200))
- if s.xcor() < -220:
- direction = "stop"
- elif direction =="right":
- s.goto(s.xcor() + speed, s.ycor())
- s.stamp()
- if s.stamp() > tail:
- s.clearstamps(2)
- if is_contact(s, a):
- winsound.Beep(1000,40)
- speed += 0.5
- a.setpos(random.randrange(-220,200), random.randrange(-220,200))
- if s.xcor() > 220:
- direction = "stop"
- elif direction == "up":
- s.goto(s.xcor() , s.ycor() + speed)
- s.stamp()
- if s.stamp() > tail:
- s.clearstamps(2)
- if is_contact(s, a):
- winsound.Beep(1000,40)
- a.setpos(random.randrange(-220,200), random.randrange(-220,200))
- speed += 0.5
- if s.ycor() > 220:
- direction = "stop"
- elif direction == "down":
- s.goto(s.xcor(), s.ycor() -speed)
- s.stamp()
- if s.stamp() > tail:
- s.clearstamps(2)
- if is_contact(s, a):
- winsound.Beep(1000,40)
- a.setpos(random.randrange(-220,200), random.randrange(-220,200))
- speed += 0.5
- if s.ycor() < -220:
- direction = "stop"
- else:
- s.goto(s.xcor(), s.ycor())
- def move_left():
- global direction
- direction = "left"
- def move_right():
- global direction
- direction = "right"
- def move_up():
- global direction
- direction = "up"
- def move_down():
- global direction
- direction = "down"
- turtle.listen()
- turtle.onkey(move_left,"Left")
- turtle.onkey(move_right,"Right")
- turtle.onkey(move_up,"Up")
- turtle.onkey(move_down,"Down")
- def is_contact(object_1, object_2):
- a = object_1.xcor() - object_2.xcor()
- b = object_1.ycor() - object_2.ycor()
- distance = math.sqrt((a ** 2) + (b ** 2))
- if distance < 10:
- return True
- else:
- return False
- stamp_list = []
- while True:
- move()
- #print(s.xcor(),s.ycor())
- #print(s.stamp())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement