Mar-10-2019, 03:14 AM
The while loop for distance is a big efficiency sink. Every one of those method calls takes time to call and they're the same methods repeatedly. There are 34 method calls in a loop that iterates up to 200 times so, it's potentially 6,800 method calls. It would be faster to call those methods once, store the value in a variable, and then do the logic test; this would equate to 400 methods calls maximum.
Plus, the structure of that if statement should be if...elif...else instead of using break continually.
Plus, the structure of that if statement should be if...elif...else instead of using break continually.
while distance != 200: turt.tracer(0, 0) lis2[i] = 0 # bounderies x = turt.xcor() y = turt.ycor() if x > 50: turt.setx(50) lis2[i] = 1 elif y < -50: turt.sety(-50) lis2[i] = 2 elif y > 50: turt.sety(50) lis2[i] = 2 elif x < -50: turt.setx(-50) lis2[i] = 1 # block elif 30 < x < 31.5 and -20 < y < 20: lis2[i] = 3 elif 37 < x < 38.5 and -20 < y < 20: lis2[i] = 3 elif 20 < x < 21 and 30 < y < 38.5: lis2[i] = 4 elif -21 < x < -20 and 30 < y < 38.5: lis2[i] = 4 # block 2 elif 10 < x < 11.5 and -20 < y < 20): lis2[i] = 3 elif 17 < x < 18.5 and -20 < y < 20: lis2[i] = 3 elif 20 < x < 21 and 10 < y < 18.5: lis2[i] = 4 elif -21 < x < -21 and 10 < y < 18.5: lis2[i] = 4 if lis2[i] != 0: break turt.forward(1) distance += 1The coloring loop at the end can be simplified with a small dictionary too.
colors = {1: "Blue", 2: "Mediumblue", 3: "Red", 4: "Crimson"} for i in range(96): draw.color(colors.get(lis2[i]))