Aim: Learn how to draw flags of the world by using just Python code.
To complete this task, we will use Anaconda as a platform, in which Jupytor notebook is used an interative editing and running application. If you are working on a university lab machine, Anaconda has already be installed. For your personal machine, please refer to Anaconda installation guidance.
After Anaconda is successfully installed, you can start following the step-by-step instructions below to draw flags!
The computer is stupid. It needs to know exactly what you mean when you give it instructions. It has no ability to guess. To become a programmer, you have to work on three skills as a beginner:
In this drawing flag task, we will use graphics.py library, which is a simple object oriented graphics library designed to make it very easy for novice programmers to experiment with computer graphics in an object oriented fashion. To install graphics.py library, we use package manager pip.
!pip install graphics.py
from graphics import *
In graphics.py, the graphics are drawn in a window. Let us perform the following steps:
This is accomplished with the following lines of code. Enter them precisely into Python.
win = GraphWin()
win.setBackground('white')
win.close()
Windows have coordinates. (0, 0)
is the top-left corner. (w, h)
is the bottom-right corner – where w
is the window width and h
the height.
A rectangle has a start point and an end point. We can also set its colour. Let us do the following:
name
with a size of 600 by 400.As mentioned in step 5, GraphWin() can have optional parameters, GraphWin(title, width, height)
win = GraphWin('name', 600, 400)
Windows have coordinates. (0, 0) is the top-left corner. (w, h) is the bottom-right corner, where w is the window width and h the height. A Rectangle has a start point and end point.
rect = Rectangle(Point(0, 0), Point(100, 200))
rect.setFill('blue')
rect.draw(win)
win.close()
So far we have learnt how to create rectangle with specific colour. Think about the French flag (as below). How can we draw it by python?
If you cannot complete this, the following instructions will help you complete the flag.
france = GraphWin('France', 600, 400)
The French flag consists of three rectangles with colour blue, white and red.
rect = Rectangle(Point(0, 0), Point(200, 400))
rect.setFill('blue')
rect.draw(france)
rect = Rectangle(Point(200,0), Point(400, 400)))
rect.setFill('white')
rect.draw(france)
Try the red bar by yourself. Also, remember to close the window when you are finished.
Write code to draw the following flags.
circle = Circle(Point(100, 100), 50)
Similar to rectangle, we can set the colour fill to the circle, and then draw it on the window.
circle.setFill(‘red’)
circle.draw(win)
line = Line(Point(0,0), Point(600, 400))
line.setOutline('red')
line.setWidth(80)
line.draw(win)
We use Polygon() to draw polygons, where the polygon corners are defined by parameters. For example, to draw a triangle, we can draw a polygon with three points.
tri = Polygon(Point(0,0), Point(200,200), Point(0,400))
Also, the colour can be set by setFill().
tri.setFill('black')
tri.draw(win)
.
.
.
Now I believe you have got enough confidence to draw more challenging flags. Here some advanced flags are prepared for you to play with.
.
.
.
Good luck and have fun!