Day 15: Graphics using python (Turtle)

Hello guys,
We are back here.
Previously we learn about web scraping using selenium and beautiful soup.

There is no field where we can't use python. Everywhere from data collection to processing to advance prediction systems, deep learning. Also graphics

If you ever heard about blender software which is build using python (best graphics design 3D modeling software)

For Graphics we have two options

1]  Tkinter:  For desktop application design
2]  Turtle:  Actual Graphic design

We will learn about turtle

Turtle

Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon in 1967. (official definition).

Turtle is based on the Tkinter module.

consider Turtle is like a robotic arm if that arm wants to draw a square on canvas
How that arm will draw?
1 Put a pencil on canvas
2 then move forward for a certain distance like 100 (as we know square have same all sides )
3 then it will rotate 90 degrees left
4 then again move forward for 100
5 then it will rotate 90 degrees left
6 then again move forward for 100
7 then it will rotate 90 degrees left
8 then again move forward for 100

that's it ๐Ÿ˜Ž

Try to draw user self on paper.

Now move on to our programming

Code :

from turtle import *

forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)

output:

Explain :

from turtle import * 
This is used to import turtle.

By default, the pen is down and ready to draw and the position of the pen is at the center.

forward(100)
As the name says, move forward by a certain distance here 100. 
or you can use fd(100) (nothing but just short form).

left(90)
Here that will move to left based on the angle here 90 degree 
or you can use lt(90) (nothing but just short form)

There are 4 sides we have to move forward 4 times
There are  4 angles too but we have to move left only for 3 times.


What if we want to fill this square?
Code:
from turtle import *
color('red', 'yellow')
begin_fill()
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
end_fill()

output:

Explain:
Same Code
but 
color('red', 'yellow') 
this function used to get colors (yep we programmer write colors not colours.๐Ÿ˜œ)
the first argument is border color and the second is filled color

begin_fill()
As the name says, begin fill or start fill

end_fill()
As the name says, end fill or stop fill.

For more functions

You can also make games using turtle or screen saver

Try to make such screen saver
those squares have to pop up randomly of different size at different positions.


Don't see code now 
just try to do it by yourself refer this Read This.

import turtle
import random
win = turtle.Screen()
win.bgcolor("black")
win.setup(width=1366, height=768)


def mysquare(r, col, x, y):

    c = turtle.Turtle()
    c.shape("square")
    c.shapesize(r)
    c.color(col)
    c.penup()
    c.goto(x, y)
    range = 100
    while c.ycor() <= range and range < 300:
        x = c.xcor()
        y = c.ycor()
        c.goto(x, y + 2)
        range += 10


colors = ["red", "yellow", "blue", "white", "aqua", "green"]
while True:
    x = random.randint(-600, 600)
    y = random.randint(-350, 350)
    i = random.randint(0, 5)
    r = random.randint(1, 5)
    mysquare(r, colors[i], x, y)


win.mainloop()





Space Invader Game

For Game try this Click here 





❤❤Quarantine python group link ❤❤

8805271377 WhatsApp

Follow here ❤

@mr._mephisto_ Instagram 

There will be no restrictions just feel free to learn. 

Share and take one more step to share knowledge to others. 

Believe in yourself ๐ŸคŸ you are awesome. 

Be safe, Be happy๐Ÿ˜
Take care of yourself and your family 
Of course watch movies and series๐ŸคŸ๐Ÿ˜‰ 

And follow the guidelines of government

Comments

Popular posts from this blog

Day 16 : Pandas Basics

News website using Flask and news API - Working with templates : Part 5

Day 9 : Encapsulation and polymorphism