import turtle
polygon = turtle.Turtle()
num_sides = 6
side_length = 70
angle = 360.0 / num_sides
for i in range(num_sides):
polygon.forward(side_length)
polygon.right(angle)
turtle.done()
Wednesday, August 28, 2019
Basic star
import turtle
star = turtle.Turtle()
for i in range(5):
star.forward(50)
star.right(144)
turtle.done()
star = turtle.Turtle()
for i in range(5):
star.forward(50)
star.right(144)
turtle.done()
Thursday, August 22, 2019
Rainbow Spiral
import turtle
colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow']
t = turtle.Turtle()
for x in range(360):
t.pencolor(colors[x%6])
t.width(x/100 + 1)
t.forward(x)
t.left(59)
colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow']
t = turtle.Turtle()
for x in range(360):
t.pencolor(colors[x%6])
t.width(x/100 + 1)
t.forward(x)
t.left(59)
Spiral Helix Pattern
import turtle
loadWindow = turtle.Screen()
turtle.speed(2)
for i in range(100):
turtle.circle(5*i)
turtle.circle(-5*i)
turtle.left(i)
turtle.exitonclick()
loadWindow = turtle.Screen()
turtle.speed(2)
for i in range(100):
turtle.circle(5*i)
turtle.circle(-5*i)
turtle.left(i)
turtle.exitonclick()
Square Spiral Basic
import turtle #Outside_In
wn = turtle.Screen()
wn.bgcolor("light green")
skk = turtle.Turtle()
skk.color("blue")
def sqrfunc(size):
for i in range(4):
skk.fd(size)
skk.left(90)
size = size-5
skk.speed(9)
sqrfunc(146)
sqrfunc(126)
sqrfunc(106)
sqrfunc(86)
sqrfunc(66)
sqrfunc(46)
sqrfunc(26)
sqrfunc(16)
skk.left(360*8)
wn = turtle.Screen()
wn.bgcolor("light green")
skk = turtle.Turtle()
skk.color("blue")
def sqrfunc(size):
for i in range(4):
skk.fd(size)
skk.left(90)
size = size-5
skk.speed(9)
sqrfunc(146)
sqrfunc(126)
sqrfunc(106)
sqrfunc(86)
sqrfunc(66)
sqrfunc(46)
sqrfunc(26)
sqrfunc(16)
skk.left(360*8)
import turtle #Inside_Out wn = turtle.Screen() wn.bgcolor("light green") skk = turtle.Turtle() skk.color("blue") def sqrfunc(size): for i in range(4): skk.fd(size) skk.left(90) size = size + 5 sqrfunc(6) sqrfunc(26) sqrfunc(46) sqrfunc(66) sqrfunc(86) sqrfunc(106) sqrfunc(126) sqrfunc(146) Colorful Spiral
from turtle import *
from random import randint # from the random module import the function randint
#like turtle it is a module, read ahead for use
speed(0)
x = 1
while x < 400:
r = randint(0,255) #makes variables r,g,b whose value is an integer,
g = randint(0,255) # which is between 0 and 255. It is random, and
b = randint(0,255) # changes every time the loop runs
pencolor(r,g,b) # changes the color of the pen to the rgb coordinates
# obtained by the variables r, g, b changing each time
fd(50 + x)
rt(90.911)
x = x+1
exitonclick()
from random import randint # from the random module import the function randint
#like turtle it is a module, read ahead for use
speed(0)
x = 1
while x < 400:
r = randint(0,255) #makes variables r,g,b whose value is an integer,
g = randint(0,255) # which is between 0 and 255. It is random, and
b = randint(0,255) # changes every time the loop runs
pencolor(r,g,b) # changes the color of the pen to the rgb coordinates
# obtained by the variables r, g, b changing each time
fd(50 + x)
rt(90.911)
x = x+1
exitonclick()
Basic Spiral
from turtle import * # imports the module turtle,
#* stands for all, which makes things easier
speed(0) # sets the speed of drawing to 0, which is the fastest
pencolor("#000000") # sets the color of the pen/lines to white
# sets the color of the background/canvas to black
x = 0 # creates a variable x with value 0
up() # lifts up the pen, so no lines are drawn
#note fd() means move forward, bk() means move back
# rt() or lt() means tilt right by a certain angle
rt(45)
fd(90)
rt(135)
down() # sets down the pen, so that turtle can draw
while x < 120: # while the value of x is lesser than 120,
#continuously do this:
fd(200)
rt(61)
fd(200)
rt(61)
fd(200)
rt(61)
fd(200)
rt(61)
fd(200)
rt(61)
fd(200)
rt(61)
rt(11.1111)
x = x+1 # adds 1 to the value of x,
# so that it is closer to 120 after every loop
exitonclick() # When you click, turtle exits.
Wednesday, August 21, 2019
Math quiz with randint
import random
x = random.randint(1,10)
y = random.randint(1,10)
Guess = int(input("What is {} X {}?".format(x, y)))
if Guess == x*y:
print(str(Guess) + ' is correct!')
else:
print('You said ' + str(Guess) + '. I got ' + str(x*y))
x = random.randint(1,10)
y = random.randint(1,10)
Guess = int(input("What is {} X {}?".format(x, y)))
if Guess == x*y:
print(str(Guess) + ' is correct!')
else:
print('You said ' + str(Guess) + '. I got ' + str(x*y))
Math Quiz
Guess = int(input("What is 2 X 7?"))
if Guess == 2*7:
print(str(Guess) + ' is correct!')
else:
print('You said ' + str(Guess) + '. I got ' + str(2*7))
Turtle Race with Winner Announcement
from turtle import *
from random import randint
penup()
speed(0)
goto(-140,140)
for step in range(12):
write(step, align='center')
right(90)
forward(10)
pendown()
forward(150)
penup()
backward(160)
left(90)
forward(20)
#declare players
p1 = Turtle()
p1.color('blue')
p1.shape('turtle')
#leave no mark when the turtles move
p1.penup()
p1.goto(-160,100)
p1.right(360)
p1.pendown()
#declare players
p2 = Turtle()
p2.color('red')
p2.shape('turtle')
#leave no mark when the turtles move
p2.penup()
p2.goto(-160,50)
p2.left(360)
p2.pendown()
#race start
for turn in range(160):
p1.forward(randint(1,2))
p2.forward(randint(1,2))
x = p1.xcor()
y = p2.xcor()
if x > y:
p1.left(360)
p1.forward(20)
p1.write(" -----> Winner !", align="left")
p2.write("..... . . :( ")
else:
p2.left(360)
p2.forward(20)
p2.write(" -----> Winner !", align="left")
p1.write("....... . . :(")
from random import randint
penup()
speed(0)
goto(-140,140)
for step in range(12):
write(step, align='center')
right(90)
forward(10)
pendown()
forward(150)
penup()
backward(160)
left(90)
forward(20)
#declare players
p1 = Turtle()
p1.color('blue')
p1.shape('turtle')
#leave no mark when the turtles move
p1.penup()
p1.goto(-160,100)
p1.right(360)
p1.pendown()
#declare players
p2 = Turtle()
p2.color('red')
p2.shape('turtle')
#leave no mark when the turtles move
p2.penup()
p2.goto(-160,50)
p2.left(360)
p2.pendown()
#race start
for turn in range(160):
p1.forward(randint(1,2))
p2.forward(randint(1,2))
x = p1.xcor()
y = p2.xcor()
if x > y:
p1.left(360)
p1.forward(20)
p1.write(" -----> Winner !", align="left")
p2.write("..... . . :( ")
else:
p2.left(360)
p2.forward(20)
p2.write(" -----> Winner !", align="left")
p1.write("....... . . :(")
Turtle circle 2
import turtle
mina = turtle.Turtle()
mina.speed(100)
colors = ["red", "orange", "yellow", "green", "blue", "purple", "black", "blue"]
mina.penup()
mina.goto(-50,-50)
mina.pendown()
for each_color in colors:
angle = 360 / len(colors)
mina.color(each_color)
mina.circle(40)
mina.penup()
mina.right(angle)
mina.forward(30)
mina.pendown()
mina.penup()
mina.hideturtle()
mina = turtle.Turtle()
mina.speed(100)
colors = ["red", "orange", "yellow", "green", "blue", "purple", "black", "blue"]
mina.penup()
mina.goto(-50,-50)
mina.pendown()
for each_color in colors:
angle = 360 / len(colors)
mina.color(each_color)
mina.circle(40)
mina.penup()
mina.right(angle)
mina.forward(30)
mina.pendown()
mina.penup()
mina.hideturtle()
Thor from Asgard
class hero :
# Class Attribute
planet = 'Asgard'
# Initializer / Instance Attributes
def __init__(self, name, age):
self.name = name
self.age = age
# instance method
def description(self):
return "{} is {} years old".format(self.name, self.age)
# instance method
def speak(self, sound):
return "{} says '{}'".format(self.name, sound)
# Instantiate the object
thor = hero("Thor", 36)
# call our instance methods
print(thor.description())
print(thor.speak("I am the son of God"))
print ("{} is form {}".format(thor.name, thor.planet))
# Class Attribute
planet = 'Asgard'
# Initializer / Instance Attributes
def __init__(self, name, age):
self.name = name
self.age = age
# instance method
def description(self):
return "{} is {} years old".format(self.name, self.age)
# instance method
def speak(self, sound):
return "{} says '{}'".format(self.name, sound)
# Instantiate the object
thor = hero("Thor", 36)
# call our instance methods
print(thor.description())
print(thor.speak("I am the son of God"))
print ("{} is form {}".format(thor.name, thor.planet))
Tuesday, August 20, 2019
For loops in Turtle
import turtle
tina = turtle.Turtle()
tina.hideturtle()
tina.speed(100)
tina.color("green")
for number in range(100):
tina.forward(number*2)
tina.left(60)
mina =turtle.Turtle()
mina.hideturtle()
mina.speed(100)
mina.pensize(width=10)
mina.color("red")
for number in range(100):
mina.forward(number*2)
mina.left(-60)
dina =turtle.Turtle()
dina.speed(100)
dina.color("purple")
dina.goto(0,0)
tina = turtle.Turtle()
tina.hideturtle()
tina.speed(100)
tina.color("green")
for number in range(100):
tina.forward(number*2)
tina.left(60)
mina =turtle.Turtle()
mina.hideturtle()
mina.speed(100)
mina.pensize(width=10)
mina.color("red")
for number in range(100):
mina.forward(number*2)
mina.left(-60)
dina =turtle.Turtle()
dina.speed(100)
dina.color("purple")
dina.goto(0,0)
OOP oldest dog
class Dog:
species = 'mammal'
def __init__(self, name, age):
self.name = name
self.age = age
goofy = Dog("Goofy", 7)
brian = Dog("Brian", 9)
pluto = Dog("Pluto", 4)
# Determine the oldest dog
def get_biggest_number(*args):
return max(args)
def get_oldest_dog():
if (goofy.age > brian.age) and (goofy.age > pluto.age) :
return "Goofy"
elif (pluto.age > brian.age) and (pluto.age > goofy.age) :
return "Pluto"
else :
return "Brian"
# Output
print("The oldest dog is {} years old.".format(
get_biggest_number(goofy.age, brian.age, pluto.age)))
print ("{} is the oldest freak !!!". format(get_oldest_dog()))
species = 'mammal'
def __init__(self, name, age):
self.name = name
self.age = age
goofy = Dog("Goofy", 7)
brian = Dog("Brian", 9)
pluto = Dog("Pluto", 4)
# Determine the oldest dog
def get_biggest_number(*args):
return max(args)
def get_oldest_dog():
if (goofy.age > brian.age) and (goofy.age > pluto.age) :
return "Goofy"
elif (pluto.age > brian.age) and (pluto.age > goofy.age) :
return "Pluto"
else :
return "Brian"
# Output
print("The oldest dog is {} years old.".format(
get_biggest_number(goofy.age, brian.age, pluto.age)))
print ("{} is the oldest freak !!!". format(get_oldest_dog()))
basic of declaring class
class Dog:
# Class Attribute
species = 'mammal'
# Initializer / Instance Attributes
def __init__(self, name, age):
self.name = name
self.age = age
# Instantiate the Dog object
philo = Dog("Philo", 5)
mikey = Dog("Mikey", 6)
# Access the instance attributes
print("{} is {} and {} is {}.".format(philo.name, philo.age, mikey.name, mikey.age))
# Is Philo a mammal?
if philo.species == "mammal":
print("{} is a {}!".format(philo.name, philo.species))
# Class Attribute
species = 'mammal'
# Initializer / Instance Attributes
def __init__(self, name, age):
self.name = name
self.age = age
# Instantiate the Dog object
philo = Dog("Philo", 5)
mikey = Dog("Mikey", 6)
# Access the instance attributes
print("{} is {} and {} is {}.".format(philo.name, philo.age, mikey.name, mikey.age))
# Is Philo a mammal?
if philo.species == "mammal":
print("{} is a {}!".format(philo.name, philo.species))
The class to create monsters
# The class to create monsters
class Monster:
def __init__(self, color, heads):
self.color = color
self.heads = heads
def attack(self):
print("Just attacked a Hero, Mu...hahahaha!!!")
def announce(self):
print("Behold ! I am here")
# Creating some real monsters
fogthing = Monster("Black", 5)
mournsnake = Monster("Yellow", 4)
tangleface = Monster("Red", 3)
# Checking whether those monsters got different existence in memory or not
print('I have ' + str(fogthing.heads) + ' heads and I\'m ' + fogthing.color)
print('I also have ' + str(mournsnake.heads) + ' heads and I\'m ' + mournsnake.color)
print('I got ' + str(tangleface.heads) + ' heads and I\'m ' + tangleface.color)
print("\n \n mournsnake comes in \n \n ")
# Making mournsnake talk
print('I am a ' + str(mournsnake.heads) + ' headed monster.')
# Announce ownself
mournsnake.announce()
# Make an attack by the mournsnake
mournsnake.attack()
class Monster:
def __init__(self, color, heads):
self.color = color
self.heads = heads
def attack(self):
print("Just attacked a Hero, Mu...hahahaha!!!")
def announce(self):
print("Behold ! I am here")
# Creating some real monsters
fogthing = Monster("Black", 5)
mournsnake = Monster("Yellow", 4)
tangleface = Monster("Red", 3)
# Checking whether those monsters got different existence in memory or not
print('I have ' + str(fogthing.heads) + ' heads and I\'m ' + fogthing.color)
print('I also have ' + str(mournsnake.heads) + ' heads and I\'m ' + mournsnake.color)
print('I got ' + str(tangleface.heads) + ' heads and I\'m ' + tangleface.color)
print("\n \n mournsnake comes in \n \n ")
# Making mournsnake talk
print('I am a ' + str(mournsnake.heads) + ' headed monster.')
# Announce ownself
mournsnake.announce()
# Make an attack by the mournsnake
mournsnake.attack()
Flies with Turtle
# making some flies...
import turtle
from random import randint
fly1 = turtle.Turtle()
fly2 = turtle.Turtle()
fly3 = turtle.Turtle()
fly1.penup()
fly1.speed(100)
fly1.goto(0,100)
fly1.pendown()
fly2.penup()
fly2.speed(100)
fly2.goto(0,100)
fly2.pendown()
fly3.penup()
fly3.speed(100)
fly3.goto(0,100)
fly3.pendown()
fly1.penup()
fly2.penup()
fly3.penup()
for step in range(600):
fly1.left(randint(4,8))
fly1.forward(randint(4,8))
fly1.right(randint(4,18))
fly2.left(randint(4,8))
fly2.forward(randint(4,8))
fly2.right(randint(4,18))
fly3.forward(randint(4,8))
fly3.right(randint(4,18))
import turtle
from random import randint
fly1 = turtle.Turtle()
fly2 = turtle.Turtle()
fly3 = turtle.Turtle()
fly1.penup()
fly1.speed(100)
fly1.goto(0,100)
fly1.pendown()
fly2.penup()
fly2.speed(100)
fly2.goto(0,100)
fly2.pendown()
fly3.penup()
fly3.speed(100)
fly3.goto(0,100)
fly3.pendown()
fly1.penup()
fly2.penup()
fly3.penup()
for step in range(600):
fly1.left(randint(4,8))
fly1.forward(randint(4,8))
fly1.right(randint(4,18))
fly2.left(randint(4,8))
fly2.forward(randint(4,8))
fly2.right(randint(4,18))
fly3.forward(randint(4,8))
fly3.right(randint(4,18))
Monday, August 19, 2019
Turtle Circle
import turtle
def draw_circle(turtle, color, size, x, y):
turtle.penup()
turtle.color(color)
turtle.fillcolor(color)
turtle.goto(x,y)
turtle.begin_fill()
turtle.pendown()
turtle.circle(size)
turtle.penup()
turtle.end_fill()
turtle.pendown()
tommy = turtle.Turtle()
tommy.shape("turtle")
tommy.speed(500)
draw_circle(tommy, "green", 50, 25, 0)
draw_circle(tommy, "blue", 50, 0, 0)
draw_circle(tommy, "yellow", 50, -25, 0)
tommy.penup()
tommy.goto(0,-50)
tommy.color('black')
tommy.write("Let's Learn Python!", align="center", font=(None, 16, "bold"))
tommy.goto(0,-80)
def draw_circle(turtle, color, size, x, y):
turtle.penup()
turtle.color(color)
turtle.fillcolor(color)
turtle.goto(x,y)
turtle.begin_fill()
turtle.pendown()
turtle.circle(size)
turtle.penup()
turtle.end_fill()
turtle.pendown()
tommy = turtle.Turtle()
tommy.shape("turtle")
tommy.speed(500)
draw_circle(tommy, "green", 50, 25, 0)
draw_circle(tommy, "blue", 50, 0, 0)
draw_circle(tommy, "yellow", 50, -25, 0)
tommy.penup()
tommy.goto(0,-50)
tommy.color('black')
tommy.write("Let's Learn Python!", align="center", font=(None, 16, "bold"))
tommy.goto(0,-80)
Turtle Race
from turtle import *
from random import randint
penup()
speed(0)
goto(-140,140)
for step in range(12):
write(step, align='center')
right(90)
forward(10)
pendown()
forward(150)
penup()
backward(160)
left(90)
forward(20)
#declare players
p1 = Turtle()
p1.color('blue')
p1.shape('turtle')
#leave no mark when the turtles move
p1.penup()
p1.goto(-160,100)
p1.pendown()
#declare players
p2 = Turtle()
p2.color('red')
p2.shape('turtle')
#leave no mark when the turtles move
p2.penup()
p2.goto(-160,50)
p2.pendown()
#race start
for turn in range(160):
p1.forward(randint(1,2))
p2.forward(randint(1,2))
from random import randint
penup()
speed(0)
goto(-140,140)
for step in range(12):
write(step, align='center')
right(90)
forward(10)
pendown()
forward(150)
penup()
backward(160)
left(90)
forward(20)
#declare players
p1 = Turtle()
p1.color('blue')
p1.shape('turtle')
#leave no mark when the turtles move
p1.penup()
p1.goto(-160,100)
p1.pendown()
#declare players
p2 = Turtle()
p2.color('red')
p2.shape('turtle')
#leave no mark when the turtles move
p2.penup()
p2.goto(-160,50)
p2.pendown()
#race start
for turn in range(160):
p1.forward(randint(1,2))
p2.forward(randint(1,2))
Subscribe to:
Posts (Atom)