Untitled

mail@pastecode.io avatar
unknown
python
3 years ago
1.5 kB
5
Indexable
Never
import turtle
import tkinter as tk


class App:
    def __init__(self, master):
        self.master = master
        self.master.title("Raw Turtle")
        self.canvas = tk.Canvas(master)
        self.canvas.config(width=1000, height=1000)
        self.canvas.pack(side=tk.LEFT)
        self.screen = turtle.TurtleScreen(self.canvas)
        self.screen.bgcolor("#f9f9f9")
        self.my_lovely_turtle = turtle.RawTurtle(self.screen, shape="turtle")
        self.my_lovely_turtle.color("red")
        self.turtle_pos = 'down'
        self.master.bind('<w>', self.keyboard_bind_w)
        self.master.bind('<a>', self.keyboard_bind_a)
        self.master.bind('<d>', self.keyboard_bind_d)
        self.master.bind('<space>', self.keyboard_bind_space)
        self.master.bind('<q>', self.keyboard_bind_q)

    def keyboard_bind_q(self, _):
        self.my_lovely_turtle.clear()

    def keyboard_bind_space(self, _):
        if self.turtle_pos == 'down':
            self.my_lovely_turtle.penup()
            self.turtle_pos = 'up'
        else:
            self.my_lovely_turtle.pendown()
            self.turtle_pos = 'down'

    def keyboard_bind_w(self, _):
        self.my_lovely_turtle.forward(10)

    def keyboard_bind_a(self, _):
        self.my_lovely_turtle.left(45)

    def keyboard_bind_d(self, _):
        self.my_lovely_turtle.right(45)


if __name__ == '__main__':
    root = tk.Tk()
    app = App(root)
    root.mainloop()