0

I've been makeing a game using turtle and i've set speed to 0 using turtle.speed(0), although this is not fast enough, i've done some research but i cant find any commands for python 3.9 that will deactivate animations or increasing turtle speed anymore. Can someone please help me speed up my game? (I only have python 3.9 so i can't use tracer) Heres a code sample:

import math
import turtle
char = turtle.Turtle()
char.hideturtle()
char.speed(0)
debug = True


def left():
    update(50)


def right():
    update(-50)


def update_prep():
    update(0)


def update(x_adjust):
    char.clear()
    char.penup()
    char.setx(char.xcor() - x_adjust)
    char.pendown()
    char_1()


def char_1():
    char_start_pos = char.pos()
    char.setheading(0)
    char.left(180)
    char.fillcolor('grey')
    char.begin_fill()
    char.forward(10)
    char.right(90)
    char.forward(50)
    char.right(90)
    char.forward(20)
    char.right(90)
    char.forward(50)
    char.right(90)
    char.end_fill()
    char_pos = char.pos()
    char_head = char.heading()
    char.right(180)
    tangent(-25, 15, True)
    char.setheading(0)
    char.left(180)
    char.forward(0)
    for _ in range(36):
        char.forward(1)
        char.left(10)
    char.setheading(char_head)
    char.goto(char_pos)
    char.forward(20)
    char_pos = char.pos()
    char_head = char.heading()
    char.left(90)
    tangent(15, -25, True)
    char.setheading(0)
    char.left(180)
    char.forward(0)
    for _ in range(36):
        char.forward(1)
        char.left(10)
    char.setheading(char_head)
    char.goto(char_pos)
    char.right(90)
    char.forward(50)
    char.left(90)
    char_pos = char.pos()
    char_head = char.heading()
    tangent(-25, 15, True)
    char.setheading(0)
    char.left(0)
    char.forward(0)
    for _ in range(36):
        char.forward(1)
        char.left(10)
    char.setheading(char_head)
    char.goto(char_pos)
    char.right(180)
    char.forward(20)
    char.left(90)
    char.right(0)
    tangent(15, -25, True)
    char.setheading(0)
    char.left(0)
    char.forward(0)
    for _ in range(36):
        char.forward(1)
        char.left(10)
    char.penup()
    char.goto(char_start_pos)
    char.pendown()


def tangent(tangent_height, tangent_width, custom_heading):
    if not custom_heading:
        char.setheading(270)
    angle = math.atan(tangent_width / tangent_height)
    angle = angle * 57.295779513082321578272654463367
    char.left(angle)
    if debug:
        print(angle)
    length = tangent_height*tangent_height + tangent_width*tangent_width
    length = math.sqrt(length)
    char.forward(length)


char_1()
turtle.onkey(left, 'a')
turtle.onkey(right, 'd')
turtle.onkey(update_prep, 'w')
turtle.listen()
turtle.mainloop()
  • Please provide a [mcve] showing your game--the specifics do matter. You probably want to disable the internal turtle loop and use a custom callback to update a frame every `n` milliseconds, as shown [here](https://stackoverflow.com/questions/47879608/how-to-bind-several-key-presses-together-in-turtle-graphics/70979967#70979967). But that's just a guess. – ggorlen Jul 22 '22 at 14:26
  • I don't understand your statement, "I only have python 3.9 so i can't use tracer" The tracer functionality has been available since Python 2 and is still available in Python 3.10. – cdlane Jul 30 '22 at 18:25

1 Answers1

0

If/when you find you do have tracer, it should solve your performance issues:

from math import pi, atan, sqrt, degrees
from turtle import Screen, Turtle
from functools import partial

RADIAN = 180 / pi

def update(x_adjust=0):
    char.clear()
    char.penup()
    char.setx(char.xcor() - x_adjust)
    char.pendown()
    char_1()
    screen.update()

def char_1():
    char_start_pos = char.pos()

    char.setheading(180)
    char.fillcolor('grey')
    char.begin_fill()

    char.forward(10)
    char.right(90)
    char.forward(50)
    char.right(90)
    char.forward(20)
    char.right(90)
    char.forward(50)
    char.right(90)

    char.end_fill()

    char_pos = char.pos()
    char_head = char.heading()

    char.right(180)
    tangent(-25, 15, True)
    char.setheading(180)
    char.circle(RADIAN / 10)

    char.setheading(char_head)
    char.goto(char_pos)

    char.forward(20)

    char_pos = char.pos()

    char.left(90)
    tangent(15, -25, True)
    char.setheading(180)
    char.circle(RADIAN / 10)

    char.setheading(char_head)
    char.goto(char_pos)

    char.right(90)
    char.forward(50)
    char.left(90)

    char_pos = char.pos()

    tangent(-25, 15, True)
    char.setheading(0)
    char.circle(RADIAN / 10)

    char.setheading(char_head)
    char.goto(char_pos)

    char.right(180)
    char.forward(20)
    char.left(90)
    char.right(0)

    tangent(15, -25, True)
    char.setheading(0)
    char.circle(RADIAN / 10)

    char.penup()
    char.goto(char_start_pos)
    char.pendown()

def tangent(tangent_height, tangent_width, custom_heading):
    if not custom_heading:
        char.setheading(270)

    angle = atan(tangent_width / tangent_height)
    char.left(degrees(angle))

    length = sqrt(tangent_height * tangent_height + tangent_width * tangent_width)
    char.forward(length)

screen = Screen()
screen.tracer(False)

char = Turtle()
char.hideturtle()

update()

screen.onkey(partial(update, 50), 'a')
screen.onkey(partial(update, -50), 'd')
screen.listen()
screen.mainloop()
cdlane
  • 40,441
  • 5
  • 32
  • 81
  • I though that Tracer was removed in python 3.0, I also did some research and the sources I found said the built-in Tracer was removed. Thanks for the help – Intergalactic Carpet Aug 02 '22 at 02:42