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
JavaScript
x
64
64
1
import turtle
2
import random
3
import time
4
screen = turtle.Screen()
5
6
7
mapmaker = turtle.Turtle()
8
turtler = turtle.Turtle()
9
mapmaker.shape('classic')
10
turtler.shape('turtle')
11
turtler.penup()
12
turtler.forward(10)
13
colours = ["red", "orange", "yellow", "green", "blue", "violet", "indigo"]
14
for i in range(360):
15
mapmaker.color(colours[i % 7])
16
mapmaker.width(i / 100 + 1)
17
mapmaker.forward(i + 10)
18
mapmaker.right(270)
19
mapmaker.speed(100000000000000000000000000000000000000)
20
21
move_speed = 10
22
turn_speed = 10
23
24
def forward():
25
turtler.forward(move_speed)
26
27
def backward():
28
turtler.backward(move_speed)
29
30
def right():
31
turtler.right(turn_speed)
32
33
def left():
34
turtler.left(turn_speed)
35
36
screen.onkey(forward, "up")
37
screen.onkey(backward, "down")
38
screen.onkey(left, "left")
39
screen.onkey(right, "right")
40
screen.listen()
41
screen.onkey(forward, "w")
42
screen.onkey(backward, "s")
43
screen.onkey(left, "a")
44
screen.onkey(right, "d")
45
46
fruit = turtle.Turtle()
47
fruit.shape('square')
48
49
x = random.randint(-175,175)
50
y = random.randint(-175,175)
51
score = 0
52
53
random_time = random.randint(2,5)
54
while score < 10:
55
if turtler.distance(fruit) < 15:
56
score + 1
57
fruit.penup()
58
fruit.goto(x,y)
59
60
61
if score > 10:
62
print("Congratulations, you have won!")
63
screen.bye()
64
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:
JavaScript
1
2
1
if turtler.distance(fruit) < 15:
2
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:
JavaScript
1
59
59
1
from turtle import Screen, Turtle
2
from random import randint
3
4
MOVE_SPEED = 10
5
TURN_ANGLE = 10
6
7
def forward():
8
turtle.forward(MOVE_SPEED)
9
check_collision()
10
11
def backward():
12
turtle.backward(MOVE_SPEED)
13
check_collision()
14
15
def right():
16
turtle.right(TURN_ANGLE)
17
18
def left():
19
turtle.left(TURN_ANGLE)
20
21
score = 0
22
23
def check_collision():
24
global score
25
26
if score < 10:
27
if turtle.distance(fruit) < 15:
28
score += 1
29
30
x = randint(-175, 175)
31
y = randint(-175, 175)
32
33
fruit.goto(x, y)
34
else:
35
print("Congratulations, you have won!")
36
screen.bye()
37
38
screen = Screen()
39
40
fruit = Turtle()
41
fruit.shape('square')
42
fruit.penup()
43
44
x = randint(-175, 175)
45
y = randint(-175, 175)
46
fruit.goto(x, y)
47
48
turtle = Turtle()
49
turtle.shape('turtle')
50
turtle.penup()
51
52
screen.onkey(forward, 'Up')
53
screen.onkey(backward, 'Down')
54
screen.onkey(left, 'Left')
55
screen.onkey(right, 'Right')
56
screen.listen()
57
58
screen.mainloop()
59