Skip to content
Advertisement

How do I make it so that if a turtle is close to a turtle, it will increase a variable?

I am trying to make a more interactive game, basically, I want to make it so that if a turtle is close to another one, it will increase a variable(in this case, the variable is score).

The code is over here

import turtle
import random
import time
screen = turtle.Screen()


mapmaker = turtle.Turtle()
turtler = turtle.Turtle()
mapmaker.shape('classic')
turtler.shape('turtle')
turtler.penup()
turtler.forward(10)
colours = ["red", "orange", "yellow", "green", "blue", "violet", "indigo"]
for i in range(360):
  mapmaker.color(colours[i % 7])
  mapmaker.width(i / 100 + 1)
  mapmaker.forward(i + 10)
  mapmaker.right(270)
  mapmaker.speed(100000000000000000000000000000000000000)
  
move_speed = 10
turn_speed = 10

def forward():
  turtler.forward(move_speed)
  
def backward():
  turtler.backward(move_speed)
  
def right():
  turtler.right(turn_speed)
  
def left():
  turtler.left(turn_speed)
  
screen.onkey(forward, "up")
screen.onkey(backward, "down")
screen.onkey(left, "left")
screen.onkey(right, "right")
screen.listen()
screen.onkey(forward, "w")
screen.onkey(backward, "s")
screen.onkey(left, "a")
screen.onkey(right, "d")

fruit = turtle.Turtle()
fruit.shape('square')

x = random.randint(-175,175)
y = random.randint(-175,175)
score = 0

random_time = random.randint(2,5)
while score < 10:
  if turtler.distance(fruit) < 15:
    score + 1
    fruit.penup()
    fruit.goto(x,y)
    

if score > 10:
  print("Congratulations, you have won!")
  screen.bye()

Does anyone know how to make it so that if the turtle is distanced close to it, it will do what make the other one go to a random position and increase the score?

Advertisement

Answer

Your collision detection:

if turtler.distance(fruit) < 15:

is correct, it’s the rest of your code that has issues. For example: score + 1 in isolation does nothing; fruit.goto(x,y) only makes sense if you’re changing x and y each time, not just once before the loop; your code is structured such that it locks out input events. Below is a simplified, working version of your code that you can build on:

from turtle import Screen, Turtle
from random import randint

MOVE_SPEED = 10
TURN_ANGLE = 10

def forward():
    turtle.forward(MOVE_SPEED)
    check_collision()

def backward():
    turtle.backward(MOVE_SPEED)
    check_collision()

def right():
    turtle.right(TURN_ANGLE)

def left():
    turtle.left(TURN_ANGLE)

score = 0

def check_collision():
    global score

    if score < 10:
        if turtle.distance(fruit) < 15:
            score += 1

            x = randint(-175, 175)
            y = randint(-175, 175)

            fruit.goto(x, y)
    else:
        print("Congratulations, you have won!")
        screen.bye()

screen = Screen()

fruit = Turtle()
fruit.shape('square')
fruit.penup()

x = randint(-175, 175)
y = randint(-175, 175)
fruit.goto(x, y)

turtle = Turtle()
turtle.shape('turtle')
turtle.penup()

screen.onkey(forward, 'Up')
screen.onkey(backward, 'Down')
screen.onkey(left, 'Left')
screen.onkey(right, 'Right')
screen.listen()

screen.mainloop()
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement