Introduction to the Turing Programming Language

Total Page:16

File Type:pdf, Size:1020Kb

Introduction to the Turing Programming Language

A Car Race Using the Turing Programming Language TIJ 1O Exploring Technologies Turing Day 1 - Graphics

Turing Graphics An easy way to get started with Turing is to draw graphics. When drawing graphics in Turing, the Run Window is divided into pixels which can be thought of as tiny dots of lights on the monitor. Each pixel has an x-coordinate and y-coordinate which tells the computer where to draw the pixel on the screen. Each pixel must also have a colour. The pixel at the bottom left of the window has the coordinate (0,0). y-axis Run Window

pixel (100, 150)

150

pixel (0,0) 100 x-axis

In the diagram above, the pixel has the x-coordinate of 100 and the y-coordinate of 150. The number of pixels on the window depends on the size of the Run Window used.

Turing Drawing Commands Listed below are some of the drawing commands in Turing. Note that the letters in the brackets represent data that you must provide as shown in the examples. drawdot (x, y, c) - Draws a coloured dot at the pixel - x is the x-coordinate - y is the y-coordinate - c is the colour. - For example, drawdot (50, 20, red) will draw a red dot at the pixel with the x- coordinate of 50 and the y-coordinate of 20. (Note you will have to look closely!) drawbox (x1, y1, x2, y2, c) - Draws a coloured box - x1 is the x-coordinate of the bottom left pixel of the box - y1 is the y-coordinate of the bottom left pixel of the box - x2 is the x-coordinate of the top right pixel of the box - y2 is the y-coordinate of the top right pixel of the box - c is the colour of the box - For example, drawbox (250, 100, 300, 400, blue) will draw a blue box. The bottom left corner will have the x-coordinate of 250 and the y- coordinate of 100. The top right corner will have the x-coordinate of 300 and the y-coordinate of 400. A Car Race Using the Turing Programming Language TIJ 1O Exploring Technologies Turing Day 1 - Graphics drawfillbox (x1, y1, x2, y2, c) - Draws a coloured filled box - Data is the same as drawbox drawoval (x, y, xRad, yRad, c) - Draws an oval - x is the x-coordinate of the pixel at the centre of the oval - y is the y-coordinate of the pixel at the centre of the oval - xRad is the length of the x radius of the oval (ie. the length of the radius of the oval along the x axis) - yRad is the length of the y radius of the oval (ie. the length of the radius of the oval along the y axis) - c is the colour of the oval. - For example, drawoval (150, 100, 100, 50, magenta) will draw a magenta oval. The centre of the oval will have the x-coordinate of 150 and the y-coordinate of 100. The x radius will be 100 pixels long while the y radius will be 50 pixels long. - Note that if the x radius and the y radius are the same, a circle will be drawn. drawfilloval (x, y, xRad, yRad, c) - Draws a filled oval - Data is the same as drawoval A Car Race Using the Turing Programming Language TIJ 1O Exploring Technologies Turing Day 1 - Graphics

Exercise #2 Type the following small graphic drawing program. Save the program as Few Shapes

put "The largest value of x is: ", maxx % Outputs the largest possible x-coordinate put "The largest value of y is: ", maxy % Outputs the largest possible y-coordinate drawdot (50, 20, red) drawbox (250, 100, 300, 350, blue) drawfillbox (450, 150, 400, 350, yellow) drawoval (150, 100, 100, 50, magenta) drawfilloval (500, 100, 100, 50, green)

Improve your program by drawing: 1) A black dot at pixel 30, 40. 2) A purple box with the bottom left at pixel 40, 67 and the top right at pixel 89, 93. (Note that pixel 40, 67 means the pixel has an x-coordinate of 40 and a y-coordinate of 67). 3) A red filled box with the bottom left at pixel 350, 67 and the top right at pixel 503, 93. 4) A magenta oval with centre at pixel 150, 100, an x radius of 40 and a y radius of 50. 5) A brown oval with centre at pixel 550, 350, an x radius of 30 and a y radius of 30. 6) Any other shape you want. Try the drawline, drawmapleleaf and the drawstar commands. They all have the same data as drawbox. Re-save your program as Few Shapes. A Car Race Using the Turing Programming Language TIJ 1O Exploring Technologies Turing Day 1 - Graphics

Exercise #3 Drawing shapes one at a time is time consuming. You will create a program which will draw many shapes using a loop. In programming, a loop will allow the computer to automatically repeat some commands. This program also uses variables. A variable is a storage spot which is used to store data in a program. This data can be changed at any time. Type the program below and save it as Many Shapes.

var xCoord : int % A variable (storage spot) for the x coordinate var yCoord : int % A variable (storage spot) for the y coordinate var shapeColour : int % A variable which stores the colour for count : 1 .. 300 % The start of a loop which will repeat the folowing commands 300 times. randint (xCoord, 1, 400) % Will assign a random value between 1 and 400 in xCoord. randint (yCoord, 1, 400) % Will assign a random value between 1 and 400 in yCoord. randint (shapeColour, 1, 255) % Will assign a random value between 1 and 255 in shapeColour. drawdot (xCoord, yCoord, shapeColour) locate (1,1) put "Dot #", count % Outputs the value of the counter variable delay (50) end for % Ends the loop delay (2000) % Delays the program for 2 seconds

for count : 1 .. maxcolour locatexy (275, 375) put "This is colour #: ", count drawfillbox (300, 300, 350, 330, count) delay (100) end for A Car Race Using the Turing Programming Language TIJ 1O Exploring Technologies Turing Day 1 - Graphics

Exercise #4 – Landscape You have been hired to create a new video game. Your first task is to create a snapshot of an exciting scene from the game in order to use as a marketing tool. Of course, you’ll do this in Turing. Make sure that you use at least 3 different drawing commands. Be colourful but tasteful.

Put a vertical space (ie. hit the Enter key twice) between sections of your code that go together (and do this from now on if you haven’t been already).

Give a brief description (1-3 sentences) of your game, as well as a brief description of the scene that you’re illustrating. Put these in the comments at the top of the program (in addition to the usual comments).

Marking scheme /20  3 – header comments  1 – description of game  1 – description of scene from the game  3 – comments explaining blocks of code  1 – white space  2 – design & colour that makes some sense according to the scene  9 – 3 each for (at least) usage of 3 different drawing commands

Recommended publications