So today I did quite a lot and spent a few hours on a project and some challenges. Without further adieu, let's get into what I learnt today.
Ways to Import Modules
Basic Import : import module
from ... import ... : eg. from turtle import Turtle, helpful so that you do not need to call upon that class again and again:
import turtle
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t3 = turtle.Turtle()
from turtle import Turtle
t1 = Turtle()
t2 = Turtle()
t3 = Turtle()
- from ... import * : eg. from turtle import * The asterisk means that you import everything, it is useful because then you can just say the fuction you want it to do rather than the module as well. eg: import random
choice([1, 2, 3])
So you don't need to say random.choice()
It can be useful but can also become very confusing as it is not clear what exactly is happening therefore it is not used a lot.
- import .... as ... (Alias modules) : This is useful for when the module has a long name. eg. import very_long_name_module as m m.action()
rather than saying :
very_long_name_module.action()
- installing modules : Not all modules are built into python. You may need to download the module before you use it.
Extra
Tuple - it is a dataset and you use () brackets for it. It is 'set in stone' meaning that once you make it you cannot modify it in any way. It's used wherever you want a fixed set of data that must not be modified in the program. If you want to modify it you will need to covert it into a list first - list(tuple)
What I built
main.py
# Setup
import turtle
import random as r
u_colours = input("Would you like cool colours or warm colours? (type c/w)").lower()
warm_colors = ["#ec536b", "#be3d50", "#9f0b23", "#bf435b", "#f8a4b3", "#a70b26", "#fd6780",
"#ef4f6b", "#c60d2d", "#a2112a", "#84081e", "#f7274a", "#46000b", "#610b1a",
"#F98B88", "#F99584", "#FFCBA5", "#FFE5B4", "#FF9899", "#FFCBC4", "#FFA7A6",
"#F7C4A5", "#F7A6A0", "#F9CFCF", "#F7AE62", "#FF9999", "#F4A6A1", "#FF69B4",
"#FF7A8A", "#FFB3A8", "#FF8C98", "#F4B7BE", "#FF00FF", "#FC6C85", "#F9A7B7"]
cool_colours = ["#9FE2BF", "#98FF98", "#B8F2E6", "#A8FFFC", "#7FFFD4", "#20B2AA", "#0D98BA",
"#40E0D0", "#83C5BE", "#1BD7BB", "#8DD9CC", "#40B5AD", "#96DED1","#008080",
"#07575B", "#004543", "#1F7A8C", "#30BFBF", "#0C99BA", "#00A36C", "#5F9EA0"]
if u_colours == "c":
colours = cool_colours
if u_colours == "w":
colours = warm_colors
from turtle import Screen
screen = turtle.Screen()
turtle.colormode(255)
screen.setup(width=1000, height=600)
if u_colours == "c":
screen.bgcolor("#005249")
if u_colours == "w":
screen.bgcolor((120,30,30))
turtle = turtle.Turtle()
turtle.shape("classic")
turtle.pensize(2)
turtle.speed(10)
if u_colours == "c":
turtle.color("#aee8f4")
if u_colours == "w":
turtle.color((220, 164, 153))
turtle.penup()
turtle.right(180)
turtle.forward(450)
turtle.left(180)
turtle.pendown()
# Challenge 1 - Draw a Square
for i in range(4):
turtle.forward(90)
turtle.left(90)
# Challenge 2 - Draw a Dashed Line
for i in range(15):
turtle.forward(10)
turtle.penup()
turtle.forward(10)
turtle.pendown()
# Challenge 3 - Drawing Different Shapes (and make each line a different color)
turtle.penup()
turtle.forward(50)
turtle.pendown()
def make_shape(no_sides):
angle = 360/no_sides
for _ in range(no_sides):
turtle.forward(90)
turtle.right(angle)
for _ in range(3,11):
selected_color = r.choice(colours)
turtle.pencolor(selected_color)
make_shape(_)
turtle.color((220, 164, 153))
turtle.penup()
turtle.forward(200)
turtle.left(90)
turtle.forward(50)
turtle.pendown()
# Challenge 4 - Generate a Random Walk
random_movements = [0, 90, 180, 270]
for _ in range(101):
move = r.choice(random_movements)
turtle.pencolor(r.choice(colours))
turtle.pensize(5)
turtle.forward(30)
turtle.setheading(move)
screen.exitonclick()
spirograph.py
import turtle
import random as r
screen = turtle.Screen()
turtle.colormode(255)
screen.setup(width=1000, height=600)
screen.bgcolor("#FFFFF0")
t = turtle.Turtle()
turtle.shape("classic")
turtle.pensize(2)
turtle.speed(10000)
def random_colour():
red = r.randint(0,255)
green = r.randint(0,255)
blue = r.randint(0,255)
chosen = (red,green,blue)
return chosen
t.color("black")
for i in range(36):
t.pencolor(random_colour())
t.circle(100)
t.setheading(t.heading() + 10)
t.circle(100)
screen.exitonclick()
notes.py
# Importer une tortue pour l'utiliser
import turtle
# Pour définir les couleurs sur RBG
turtle.colormode(255)
# Ma petite tortue
drawing_object = turtle.Turtle()
drawing_object.shape("classic")
drawing_object.color((220, 164, 153))
# Amusez-vous avec!
drawing_object.pencolor((220, 164, 153))
drawing_object.pendown()
drawing_object.speed(75)
drawing_object.pensize(2)
for i in range(37):
drawing_object.forward(100)
drawing_object.left(146)
drawing_object.penup()
drawing_object.right(180)
drawing_object.pendown()
drawing_object.forward(100)
# Set the screen
screen = turtle.Screen()
screen.setup(width=800, height=600)
screen.bgcolor(100,26,24)
screen.exitonclick()
I'm learning french so I thought it would be fun to add the comments in french(thanks google translate).
Today's Challenge
# import colorgram
# rgb_colors = []
# colors = colorgram.extract('dot_image.jpg', 20)
# for color in colors:
# r = color.rgb.r
# g = color.rgb.g
# b = color.rgb.b
#
# rgb_colors.append((r,g,b))
# print(rgb_colors)
# Setup
import turtle
import random
t = turtle.Turtle()
t.shape("classic")
t.pensize(2)
screen = turtle.Screen()
screen.bgcolor("white")
colours = [(217, 214, 201), (168, 165, 154), (105, 100, 87), (204, 206, 210), (206, 217, 207), (167, 172, 177),
(159, 170, 163), (201, 198, 168), (171, 155, 159), (158, 146, 64), (48, 39, 26), (80, 102, 119),
(216, 206, 209), (23, 34, 52), (60, 23, 34), (131, 73, 83), (116, 35, 48), (19, 53, 40), (66, 112, 99),
(183, 197, 188)]
turtle.colormode(255)
# Starting Position
t.penup()
t.right(180)
t.speed(50)
t.forward(250)
t.left(90)
t.forward(200)
t.left(90)
t.pendown()
def make_circle():
color = random.choice(colours)
t.speed(10)
t.color(color)
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.forward(50)
t.pendown()
def next_row():
t.penup()
t.left(90)
t.forward(50)
t.speed(50)
t.left(90)
t.forward(497)
t.right(180)
t.pendown()
for i in range(10):
for _ in range(10):
make_circle()
next_row()
screen.exitonclick()
Baced on this guy - (google search : damien hirst spot paintings)
Resources
https://docs.python.org/3/library/turtle.html
https://www.w3schools.com/colors/colors_rgb.asp
https://pypi.org/project/colorgram.py/
https://en.wikipedia.org/wiki/Random_walk
Top comments (0)