Turtle Python Drawing Flags with Python L 2


















- Slides: 18

Turtle Python Drawing Flags with Python L 2 J 276 2. 2

Starter Hint: Load Thonny Create a program to draw a Hint 2: rectangle 400 pixels wide and 200 pixels tall. Can you alter the program: Blue background Use a variable for the height and width

Python Turtle Most flags are made up of different coloured rectangles We could draw a French flag like this: Blue rectangle Red rectangle This is called deconstructing a problem. Put the border on 05/03/202 1 Horbury Academy ICT and Business Studies 3

Functions A function is a piece of code that can be called from your program. This rectangle function will draw a rectangle whenever we ask it to. These lines of code call the function. 05/03/202 1 Horbury Academy ICT and Business Studies 4

Functions This function has three parameters. We can call the function using different parameters each time, so we can have rectangles with different width, height and colours. 05/03/202 1 Horbury Academy ICT and Business Studies 5

Functions This time we want a rectangle 100 x 50 and with a blue background 05/03/202 1 Horbury Academy ICT and Business Studies 6

Functions The width, height and colour are passed to the function. 05/03/202 1 Horbury Academy ICT and Business Studies 7

Functions The functions runs, using these values. 05/03/202 1 Horbury Academy ICT and Business Studies 8

Functions Draw these flags: Austria Gabon Germany Hungary Netherlands Abu. Dahbi Ireland 05/03/202 1 Horbury Academy ICT and Business Studies 9

Slightly harder… How would you do this? Lets "deconstruct the problem"… One blue rectangle… One white rectangle going across the page. A smaller red rectangle going across the page. A white rectangle going up the page. A smaller red rectangle going up the page. 05/03/202 1 Horbury Academy ICT and Business Studies 10

Slightly harder… One blue rectangle… width = 120 height = 90 colour = "blue" One white rectangle… but we have to move first! Where to? 0, - 35 width = 120 height = 20 colour = "white" 05/03/202 1 Horbury Academy ICT and Business Studies 11

Problems! What has gone wrong? One white box has gone over the top of the red one – draw both white boxes at once! There is a red line – lift the pen up when you move Correct version: 05/03/202 1 Horbury Academy ICT and Business Studies 12

Success! 05/03/202 1 Horbury Academy ICT and Business Studies 13

Slightly harder… Sudan Deconstruct the problem… Draw a red box Draw a white box Draw a black box Draw a green triangle How do we draw a triangle? We need a new function! 05/03/202 1 Horbury Academy ICT and Business Studies 14

Repetition This flag has nine rectangles You can draw six of them like this: rectangle (20, 300) move to new position …repeat six times Complete the flag… 05/03/202 1 Horbury Academy ICT and Business Studies 15

Slightly harder… The Bahamas Kuwait Jamaica 05/03/202 1 Horbury Academy ICT and Business Studies Guyana 16

Repetition The top left part of the flag has a "double repetition" We call this a nested for loop 05/03/202 1 Horbury Academy ICT and Business Studies 17

Mega challenge! 05/03/202 1 Horbury Academy ICT and Business Studies 18