Download presentation
1
Mini Project II – Drawing Machine
Noadswood Science, 2014
2
Mini Project II – Drawing Machine
Saturday, April 22, 2017 To set out and solve an identified problem
3
Drawing Machine In this mini project your task is to create a program to achieve the following: - A program which draws some shapes (or instructions given by the user) A user inputs a set of simplified instructions (distance / direction) into a program with good usability (i.e. a pop-up window) The program takes these instructions and processes them A drawing is produced on screen There are options for the user to draw new shapes or quit the program
4
Drawing Machine The program Drawing Machine will turn a string of simple instructions into turtle commands to draw different shapes There are three parts to the program Function 1 > turtle_controller Function 2 > string_artist Function 3 > a user interface Function 1 (turtle_controller) takes simple commands from the user and turns it into a turtle command Function 2 (string_artist) takes the strings a user has inputted and splits them into smaller units which are then fed to the turtle_controller Function 3 (a user interface) allows the user to input (which are fed to string_artist)
5
Drawing Machine In order to write our program which can draw any shape it is useful to choose an initial shape (this will allow us to test if our program works as we already know the outcome of the shape we are after) We can start with a familiar house shape using turtle – In turtle draw this shape using the following commands: turtle import *; reset(); left(xx); forward(xx); right(xx) etc... where (xx) is an integer
6
Test Shape #Turtle House from turtle import* < loads turtle reset() < resets turtle to centre of screen left(90) forward(100) < moves turtle forward by 100 right(45) forward(70) right(90) < turtle turns by 90 degrees to the right forward(100) right(90)
7
Flow Chart Coders often plan programs on paper – one of the easiest ways is to use a flow chart showing the steps and decisions the program will need to undertake In a flow chart squares represent actions whilst diamonds represent decisions Action Decision
8
Flow Chart Produce a flow chart showing the plan for the turtle_controller function It will need to take an input “do” which will be a letter such as F, B, R and L and an input “val” which will be a number such as 45, 90 and 100 and turn them into a turtle command For example, an input of “F100” would need to be turned into the “do” command forward and the “val” command 100, The flow chart will also need to include an error function – needed if the program doesn’t recognise a letter inputted The turtle_controller will use the following characters which will be turned into different commands: N (reset); U (pen up); D (pen down); F (forward); B (backward); R (turn right); and L (turn left)
9
report unknown command
Flow Chart inputs “do” and “val” do == F ? forward(val) do == R ? right(val) do == U ? penup() report unknown command return from function
10
Flow Chart inputs “do” and “val” do == F ? forward(val) do == R ?
Each commands has two variables “do” (string) for what to do and “val” (integer) how far to do it inputs “do” and “val” If “do” = F then move turtle forward Function decides if “do” is a character it recognises do == F ? forward(val) If “do” = R then turn turtle right “do” isn’t “F” – is it “R” ? do == R ? right(val) “do” isn’t “R” – is it “U” ? do == U ? penup() If “do” = U then “penup()” which stops turtle drawing If “do” isn’t a character recognised then report an error report unknown command Once the command is finished return to the main program return from function
11
Turtle Controller The first part of the program is a function that moves the turtle – one command at a time (as planned out in the flow chart previously) The “do” and “val” functions need to be converted into movement commands within turtle Write a piece of code which incorporates the following criteria Turtle is imported Statements are defined “def” for inputs (“do” and “val”) Decisions “if”, “elif” and “else” statements are used if the user inputs “F” (forward); “B” (backwards); “R” (right); “L” (left); “U” (pen up); “D” (pen down); “N” (reset); and any other input prints out an error statement All characters are converted to upper case resolves some errors!
12
Turtle Controller #Turtle Controller from turtle import* < loads turtle def turtle_controller(do, val): < defines “do” and “val” as inputs for the function do = do.upper() < converts all letters in “do” to upper case if do == 'F': forward(val) < function changes “do” value “F” into command forward elif do == 'B': < function checks the “do” character against all defined backward(val) elif do == 'R': right(val) elif do == 'L': left(val) elif do == 'U': penup() < turtle stops drawing elif do == 'D': pendown() < turtle starts drawing elif do == 'N': reset() < command resets turtle’s position to centre of screen else: print('Unrecognised command') < if “do” value cannot be recognised message appears
13
Pseudocode Programming can be planned using pseudocode (fake code) – it allows you to write your ideas in the style of how you want the program to run without actually ‘knowing’ the programming language Clear coding is vital – it is vital that people can follow what you’ve done. It is important to make the code as easy to understand as possible, using the following techniques: - Use functions – breaking code into smaller chunks (where each function completes a task in the program) Name variables and functions – make them clear (e.g. “age_in_years” is more obvious than “aiy” Comment - #explain what is happening so when you come to read back the code it makes more sense Symbols – don’t use ones that can be confused with others (e.g. capital O looks like zero (0) and lowercase l looks like an capital I or 1)!
14
Pseudocode It is now time to plan the string_artist – this function will take a string of several “do” and “val” command inputs and break them into pairs made up of a letter and a number. It can then pass these pairs to the turtle controller, one at a time For example the following input might be given F100-R90-F50-R45 The function of the string_artist will be to take that input and process it into the following ‘F’ 100 ‘R’ 90 ‘F’ 50 ‘R’ 45 Organise a pseudocode to achieve the above
15
Pseudocode Basic functions needed: get a string from an input; split; what to do with errors; “do” command is where and what; “val” command is where and what; pass to turtle… A flow chart can really help simplify what you want to achieve (and helps visualise it) xyz xyz xyz xyz xyz
16
Pseudocode function string_artist(input – program as string):
Function takes a string of commands by user split program string into list of commands Splits strings into a list of separate commands for each command in list check it is not blank Blank commands are skipped if it is blank move onto the next item command type is the first letter First character is a “do” command if followed by more characters Following characters are “val” commands but these must be turned into numbers turn them into a number call turtle_controller(command type, number) Command passed to turtle
17
String Artist The pseudocode set out plans a function known as the string_artist – this is planned to turn a string of values into single commands that are sent to the turtle controller This pseudocode needs to be converted into real Python code… The “split()” function splits a string into a list of smaller strings where each break point is marked with a character (e.g. “-”) program = ‘N-L90-F100-R45-F70-R90-F70-R45-F100-R90-F100’ cmd_list = program.split(‘-’) cmd_list When run, the “split()” function breaks the string down as follows: - ‘N’, ‘L90’, ‘F100’, ‘R45’, ‘F70’, ‘R90’, ‘F70’, ‘R45’, ‘F100’, ‘R90’, ‘F100’
18
String Artist #String Artist def string_artist(program): cmd_list = program.split('-') for command in cmd_list: cmd_len = len(command) if cmd_len == 0: continue cmd_type = command[0] num = 0 if cmd_len > 1: num_string = command[1:] num = int(num_string) print(command, ':', cmd_type, num) turtle_controller(cmd_type, num) Whenever “-” occurs split string Loop through the list of strings, making each item a single turtle command Finds length of command string If the length of the command is 0 (i.e. a blank command) then skip to next string Take the first character of the command and set it as the command type Takes remaining characters from the command (by cutting off the first) Converts the string characters into numbers Prints the commands on the screen so you can see what the code is doing Passes command to turtle
19
‘N-L90-F100-R45-F70-R90-F70-R45-F100-R90-F100’
Input > Process The user input might resemble something as follows: - ‘N-L90-F100-R45-F70-R90-F70-R45-F100-R90-F100’ The program then converts it into the following: - N : N 0 < resets screen and turtle put back at 0 L90 : L 90 F100 : F 100 R45 : R 45 F70 : F 70 R90 : R 90 Rotate 45 Forward 100
20
User Interface The drawing machine needs an interface to make it easier to use – this will let the user enter a string from the keyboard to tell the program what to draw… A pop-up window is created with basic instructions (and a while loop allows multiple entries)
21
User Interface The user window makes the turtle far easier to control, and it also means the program does not have to be restarted for new pictures (typing ‘N’ runs the command to start a new drawing) The window will pop-up over the turtle window ready for the user to type information in (with an option to ‘OK’ when the user wants to run the program)
22
User Interface Triple quotes inform python everything including line breaks are part of the same string #User Interface instructions = '''Enter a program for the turtle: e.g. F100-R45-U-F100-L45-D-F100-R90-B50 N = New drawing U = Pen up D = Pen down F100 = Forward 100 B50 = Backward 50 R90 = Right turn 90 degrees L45 = Left turn 45 degrees Choose a numerical value for movement/turning''' screen = getscreen() while True: t_program = screen.textinput('Drawing Machine', instructions) print(t_program) if t_program == None or t_program.upper() == 'END': break string_artist(t_program) What to display in the pop-up window This line stops the program if the user types “END” or presses the cancel button Passes string to the string_artist function
23
User Interface The user interface gives the user information and advice with what to enter making the program more useable… getscreen() is the function called to produce the pop-up
24
Error The program uses the command print(‘Unrecognised command’) if the user enters a value that the function cannot recognise (although in this early stage it can still be broken)
25
House The Drawing Machine program takes instructions and passes them into the turtle program to draw – for example the instructions entered by a user below with draw a simple outline of a house N-L90-F100-R45-F70-R90-F70-R45-F100-R90-F100
26
House Window However, using the U and D commands the pen will lift, allowing for much more complicated shapes to be drawn… N-L90-F100-R45-F70-R90-F70-R45-F100-R90-F100-B10-U-R90-F10-D-F30-R90-F30-R90-F30-R90-F30
27
Owl Face Very complex shapes can be made with more and more elaborate instructions… N-F100-L90-F200-L90-F50-R60-F30-L120-F30-R60-F40-R60-F30-L120-F30-R60-F50-L90-F200-L90-F100-L90-U-F150-L90-F20-D-F30-L90-F30-L90-F30-L90-F30-R90-U-F40-D-F30-R90-F30-R90-F30-R90-F30-L180-U-F60-R90-D-F40-L120-F40-L120-F40
28
Forest Using the program create your own shapes (or even have the computer randomly draw a number of ‘trees’ to produce a computerised ‘forest’ (different every time…))
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.