Skip to content

Instantly share code, notes, and snippets.

@mibi88
Last active December 18, 2021 18:28
Show Gist options
  • Save mibi88/d0adc351be63c377ca2a88c1bd83318c to your computer and use it in GitHub Desktop.
Save mibi88/d0adc351be63c377ca2a88c1bd83318c to your computer and use it in GitHub Desktop.
MibiTurtle
###################
## MibiTurtle ##
## by mibi88 ##
## ------------- ##
## v.0.1 ##
## ------------- ##
## License : ##
## GNU GPL v2 ##
## ------------- ##
## 2021/12/08 ##
## to the ##
## 2021/12/09 ##
###################
try:
import tkinter as Tk
except:
try:
import Tkinter as Tk
except:
print("Tkinter wasn't found.")
try:
import math
except:
print("Math wasn't found.")
class Turtle():
def __init__(self):
### Vars ###
self.angle = 0
self.width = 1
self.pendown = True
self.color = "#000"
self.bg = "white"
self.x = 0
self.y = 0
############
self.window = Tk.Tk()
self.window.title("MibiLogo --- Turtle")
self.canvas = Tk.Canvas(self.window, background = self.bg)
self.canvas.pack(fill = "both", expand = True)
self.window.update()
self.showturtle = True
### Vars ###
self.nullx = self.canvas.winfo_width() / 2
self.nully = self.canvas.winfo_height() / 2
############
self.buildturtle()
def buildturtle(self):
# We draw a cross at the x and the y of the turtle :
coords = [self.nullx + self.x + 3, self.nully + self.y + 4,
self.nullx + self.x + 4, self.nully + self.y + 3,
self.nullx + self.x + 1, self.nully + self.y,
self.nullx + self.x + 4, self.nully + self.y - 3,
self.nullx + self.x + 3, self.nully + self.y - 4,
self.nullx + self.x, self.nully + self.y - 1,
self.nullx + self.x - 3, self.nully + self.y - 4,
self.nullx + self.x - 4, self.nully + self.y - 3,
self.nullx + self.x - 1, self.nully + self.y,
self.nullx + self.x - 4, self.nully + self.y + 3,
self.nullx + self.x - 3, self.nully + self.y + 4,
self.nullx + self.x, self.nully + self.y + 1]
self.turtle = self.canvas.create_polygon(coords, fill = self.color)
#turtle = self.canvas.create_polygon(10, 50, 15, 20, 20, 50)
def drawturtle(self): # We redraw the turtle.
self.canvas.delete(self.turtle)
if self.showturtle == True:
self.buildturtle()
def forward(self, distance): # We go forward.
x2 = self.x + math.cos(math.pi * (self.angle - 90) / 180) * distance
y2 = self.y + math.sin(math.pi * (self.angle - 90) / 180) * distance
if self.pendown == True:
self.canvas.create_line(self.nullx + self.x, self.nully + self.y, self.nullx + x2, self.nully + y2, fill=self.color, width=self.width)
self.x = x2
self.y = y2
self.drawturtle()
def goto(self, x, y): # We go to x, y.
if self.pendown == True:
self.canvas.create_line(self.nullx + self.x, self.nully + self.y, self.nullx + x, self.nully + y, fill=self.color, width=self.width)
self.x = x
self.y = y
self.drawturtle()
def right(self, angle): # We turn to the right.
self.angle += angle
self.drawturtle()
def left(self, angle): # We turn to the left.
self.angle -= angle
self.drawturtle()
def up(self): # Pen up.
self.pendown = False
self.drawturtle()
def down(self): # Pen down.
self.pendown = True
self.drawturtle()
def setangle(self, angle): # Set the angle of the turtle.
self.angle = angle
self.drawturtle()
def hideturtle(self): # We hide the turtle.
self.showturtle = False
self.drawturtle()
def showturtle(self): # We show the turtle.
self.showturtle = True
self.drawturtle()
def changecolor(self, color): # We change the color.
self.color = color
self.drawturtle()
def penwidth(self, width): # We set the pen width.
self.width = width
self.drawturtle()
def clear(self): # Clear the canvas.
self.canvas.delete("all")
"""
### Demo ############
### Draw a turtle ###
turtle = Turtle()
turtle.penwidth(2)
turtle.changecolor("red")
turtle.right(21)
turtle.left(90)
turtle.forward(5)
turtle.left(180)
turtle.forward(10)
turtle.right(180)
turtle.forward(5)
turtle.right(90)
turtle.forward(20)
lenght = math.sqrt(pow(5, 2) + pow(20, 2))
print(lenght)
turtle.right(180 + 45/4)
turtle.forward(lenght)
turtle.right(180)
turtle.forward(lenght)
turtle.left(180 + 45/2)
turtle.forward(lenght)
#turtle.hideturtle()
#turtle.clear()
######################
"""
###################
## MibiTurtle ##
## by mibi88 ##
## ------------- ##
## v.0.1-py2 ##
## ------------- ##
## For : ##
## Python 2.2+ ##
## ------------- ##
## License : ##
## GNU GPL v2 ##
## ------------- ##
## 2021/12/08 ##
## to the ##
## 2021/12/18 ##
###################
try:
import tkinter as Tk
except:
try:
import Tkinter as Tk
except:
print("Tkinter wasn't found.")
try:
import math
except:
print("Math wasn't found.")
class Turtle:
def __init__(self):
### Vars ###
self.angle = 0
self.width = 1
self.pendown = 1
self.color = "#000"
self.bg = "white"
self.x = 0
self.y = 0
############
self.window = Tk.Tk()
self.window.title("MibiLogo --- Turtle")
self.canvas = Tk.Canvas(self.window, background = self.bg)
self.canvas.pack(fill = "both", expand = 1)
self.window.update()
self.showturtle = 1
### Vars ###
self.nullx = self.canvas.winfo_width() / 2
self.nully = self.canvas.winfo_height() / 2
############
self.buildturtle()
def buildturtle(self):
# We draw a cross at the x and the y of the turtle :
coords = [self.nullx + self.x + 3, self.nully + self.y + 4,
self.nullx + self.x + 4, self.nully + self.y + 3,
self.nullx + self.x + 1, self.nully + self.y,
self.nullx + self.x + 4, self.nully + self.y - 3,
self.nullx + self.x + 3, self.nully + self.y - 4,
self.nullx + self.x, self.nully + self.y - 1,
self.nullx + self.x - 3, self.nully + self.y - 4,
self.nullx + self.x - 4, self.nully + self.y - 3,
self.nullx + self.x - 1, self.nully + self.y,
self.nullx + self.x - 4, self.nully + self.y + 3,
self.nullx + self.x - 3, self.nully + self.y + 4,
self.nullx + self.x, self.nully + self.y + 1]
self.turtle = self.canvas.create_polygon(coords, fill = self.color)
#turtle = self.canvas.create_polygon(10, 50, 15, 20, 20, 50)
def drawturtle(self): # We redraw the turtle.
self.canvas.delete(self.turtle)
if self.showturtle == 1:
self.buildturtle()
def forward(self, distance): # We go forward.
x2 = self.x + math.cos(math.pi * (self.angle - 90) / 180) * distance
y2 = self.y + math.sin(math.pi * (self.angle - 90) / 180) * distance
if self.pendown == 1:
self.canvas.create_line(self.nullx + self.x, self.nully + self.y, self.nullx + x2, self.nully + y2, fill=self.color, width=self.width)
self.x = x2
self.y = y2
self.drawturtle()
def goto(self, x, y): # We go to x, y.
if self.pendown == 1:
self.canvas.create_line(self.nullx + self.x, self.nully + self.y, self.nullx + x, self.nully + y, fill=self.color, width=self.width)
self.x = x
self.y = y
self.drawturtle()
def right(self, angle): # We turn to the right.
self.angle += angle
self.drawturtle()
def left(self, angle): # We turn to the left.
self.angle -= angle
self.drawturtle()
def up(self): # Pen up.
self.pendown = 0
self.drawturtle()
def down(self): # Pen down.
self.pendown = 1
self.drawturtle()
def setangle(self, angle): # Set the angle of the turtle.
self.angle = angle
self.drawturtle()
def hideturtle(self): # We hide the turtle.
self.showturtle = 0
self.drawturtle()
def showturtle(self): # We show the turtle.
self.showturtle = 1
self.drawturtle()
def changecolor(self, color): # We change the color.
self.color = color
self.drawturtle()
def penwidth(self, width): # We set the pen width.
self.width = width
self.drawturtle()
def clear(self): # Clear the canvas.
self.canvas.delete("all")
"""
### Demo ############
### Draw a turtle ###
turtle = Turtle()
turtle.penwidth(2)
turtle.changecolor("red")
turtle.right(21)
turtle.left(90)
turtle.forward(5)
turtle.left(180)
turtle.forward(10)
turtle.right(180)
turtle.forward(5)
turtle.right(90)
turtle.forward(20)
lenght = math.sqrt(pow(5, 2) + pow(20, 2))
print(lenght)
turtle.right(180 + 45/4)
turtle.forward(lenght)
turtle.right(180)
turtle.forward(lenght)
turtle.left(180 + 45/2)
turtle.forward(lenght)
#turtle.hideturtle()
#turtle.clear()
######################
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment