Mini Project II – Drawing Machine

Slides:



Advertisements
Similar presentations
Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
Advertisements

Noadswood Science,  To understand the flow procedure when writing programs Thursday, January 15, 2015.
P1PMF Split1 QBASIC. P1PMF Split2QBasic Command Prompt Will launch the emulator DOS operating system? Press Alt + Enter to display the widescreen.
Chapter 2 - Problem Solving
Introduction to C Programming
Computer Science 1000 LOGO I. LOGO a computer programming language, typically used for education an old language (1967) the basics are simple: move a.
Computer Science 1620 Loops.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to Python
Introduction to C Programming
Chapter 3 Planning Your Solution
Introduction to TouchDevelop
Noadswood Science,  To set out and solve an identified problem Monday, August 10, 2015 Box ‘a’Box ‘b’ 23 Box a is smaller than Box b…
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
INTRODUCTION TO THE SCRATCH PROGRAMMING ENVIRONMENT.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Python Programming Fundamentals
Noadswood Science,  To know the basics of Python coding and decoding Monday, September 07, 2015.
Introduction to Python
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
General Programming Introduction to Computing Science and Programming I.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
LOGO SOFTWARE BY: SAVE 9S. INTRODUCTION Logo is a software that can be found at : Shared area> High School > ICT > take home software > LOGO32. This is.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 2 - Algorithms and Design
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Input, Output, and Processing
By the end of this session you should be able to...
1 CSC 221: Computer Programming I Fall 2011 Fun with turtle graphics  turtle module  relative motion (setup, reset, left, right, forward, backward) 
Introduction to TouchDevelop
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Lesson 4 Using Variables in Python – Creating a Simple ChatBot Program.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Slide 1 Project 1 Task 2 T&N3311 PJ1 Information & Communications Technology HD in Telecommunications and Networking Task 2 Briefing The Design of a Computer.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Python Let’s get started!.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Controlling Program Flow with Decision Structures.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Programming Basics - RobotC Introduction to Robotics.
CSE 110: Programming Language I Matin Saad Abdullah UB 404.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Algorithms and Flowcharts
Introduction to Computing Science and Programming I
Topics Designing a Program Input, Processing, and Output
FLOWCHARTS Part 1.
Python Let’s get started!.
IF statements.
UNIT 3 – LESSON 5 Creating Functions.
Engineering Innovation Center
Print slides for students reference
Selection CIS 40 – Introduction to Programming in Python
Programming Basics - RobotC
ARRAYS 1 GCSE COMPUTER SCIENCE.
Problem Solving Designing Algorithms.
Python programming exercise
Flowcharts and Pseudo Code
Topics Designing a Program Input, Processing, and Output
COMPUTATIONAL THINKING COMPUTATIONAL THINKING IN PROGRAMMING
Topics Designing a Program Input, Processing, and Output
Selection Statements Chapter 3.
Presentation transcript:

Mini Project II – Drawing Machine Noadswood Science, 2014

Mini Project II – Drawing Machine Saturday, April 22, 2017 To set out and solve an identified problem

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

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)

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

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)

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

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)

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

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

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!

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

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)!

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

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

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

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’

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

‘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

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)

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)

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

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

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)

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

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

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

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…))