Art 321 Lecture 7 Dr. J. Parker Programming • In order to ‘make things happen’ on a computer, you really have to program it. • Programming is not hard and does not require advanced math skills. It does require that you understand how limited a computer really is, and know how to use a particular language to speak to it. Programming • There are many ways to program a computer. Ones uses a ‘drag and drop’ interface where you click and drag icons into a sequence. Another is to use a scripting language, which is a highly structured way of giving a computer instructions on how to do something. Programming Computers do not think. (want to discuss this?) They do precisely what you tell them to do. If a computer does something incorrectly, it is caused by either a flaw in the electronics or my a mistake in the program it was given. Mostly the latter. Programming HTML is a simple programming language, in a sense. It describes how a computer should display a document. We will look at one of the simplest ways to program a computer – using ‘turtle graphics’ and Logo. Blockly We are going to use a drop and drop coding system created by Google, called blockly. http://code.google.com/p/blockly/ Programming Programming The ‘turtle graphics’ concept has a pen (carried by a turtle, one supposes) move along a paper. One can go forward, back, turn left and right, Lift and drop the pen, change colour, and so on. We select an action using the mouse and place it in the window, then click on ‘run program’. Programming Programming Programming Programming Programming Programming The Abstract Explanation A variable is a name (IE a symbol) that represents some value, usually a numeric value. The name is often indicative of the nature of the value – so the variable score represents a value that is also called a “score” in English. This is a coincidence; the game score could just as easily be kept in a variable called “fred”. The Engineering Explanation The variable is another name for the address in memory where the value is kept. The programming language takes the name that it sees and replaces it with a memory address. So score = 12 becomes memory[10020] = 12 Where 10020 is the address of score. The Common Sense Explanation A variable is a place to put something; it’s the name of that place. What can we do with variables? We can save things for later. We can add to them, accumulate values. EG - How many enemies have we shot? - How much fuel do we have left? - Where is my ‘ball’ object? We can test it against sentinal/target values. - Am I out of fuel (is fuel = 0)? - Am I near to a water hole (distance < 20)? Programming Draw a set of lines Draw a square. Draw a square. Draw a triangle Draw a triangle Draw a collection of triangles Draw a collection of triangles Results are often surprising Random positions … • Repeat many times – select a random place each time from which to begin. Details of the ‘loop’ Concept of ‘procedure’ ‘a bunch of things you want to do that has a name’. Here, ‘triangle’ is a procedure. Now ‘reposition’ is a procedure too. It places the turtle at a new random point. The new program draws a triangle and then repositions, 100 times. Draw Change colors – RGB values can be set. Assignment 1. Draw a "+" figure 150 units high by 150 units wide. 2. Draw the letter 'W'. (Bonus marks: draw the word "WHY" 3. draw 5 parallel horizontal lines 400 units long separated by 25 units. 4. Use turtle graphics to draw a sequence of squares (100 units per side) that have a common lower left corner, but are each drawn at a different angle. There should be at least 10 squares in the sequence. 5. Draw a set of 9 squares in a 3x3 arrangement. Draw one in each of the colours: black, yellow, green, red, blue, purple, grey, orange, and a colour of your choosing. 6. Draw a circle.