Вы находитесь на странице: 1из 6

Faculty of Science, Engineering and Technology

Introduction to Programming
Pass Task 1.3: Creating a Procedure
Overview
Now that you have created a program that uses procedures, you can learn how to create your
own procedures.
Purpose:

Learn how to create and call your own procedures.

Task:

Use the instructions on the following pages to create your own shape
drawing procedure. Submit to Doubtfire when complete.

Time:

You will need to work on this task in week 1 and submit it for feedback before
the start of week 2.

Chapter 1 of the Programming Arcana


Swinburne CodeCasts (YouTube Channel, iTunesU)
Understanding Syntax Rules
Programs and Procedures
Syntax Videos
Introduction, Getting Started, Calling Procedures, and Creating Your
Own Procedures
Submission Details
Resources:

You must submit the following files to Doubtfire:


Picture Drawing source code with custom procedure (GameMain.pas)
Screenshot of your program in action (with your custom shape being shown).
Make sure that your task has the following in your submission:
The program must demonstrate how to create procedures and use these to show the two
bikes, and your custom shape.
Code must follow the Pascal coding convention used in the unit (layout, and use of case).
Procedure names should communicate their functionality name the procedure to match
what it does.

Introduction to Programming

Pass Task 1.3: Creating a Procedure

Instructions
Procedures can be used to group together the commands needed to perform a particular task.
For example, in the House Drawing example, we could easily group the Fill Rectangle and Fill
Triangle together in a single meaningful procedure that we could call DrawHouse.
For this task you will create a procedure that draws a red bike to the screen (at the top left),
and then create your own procedure to draw a shape of your own creation.
The bike uses the following shapes:
A Triangle from the points (10, 20), (25, 5), (40,20).
Two Circles with radius 10, one at (10, 20) the other at (40, 20)
One Line from (40, 20) to (40, 0)

"
1. Download the resources zip file from the Doubtfire task.
2. Extract the zip file to your code directory (e.g. Documents/Code)
3. Open the GameMain.pas file in the src folder using Sublime Text.
4. Compile and run the current code in GameMain.pas by doing the following:

Open a Terminal window (or use one you already have open)

Change into the BikeDrawing directory using the cd command

Compile the template program using ./build.sh

Run your program using ./run.sh it doesnt do much but it should start and stop
without error.

Tip: Leave the terminal open. You will switch back and forward between your code and
the terminal as you develop the program.

Page 2 of 6

Introduction to Programming

Pass Task 1.3: Creating a Procedure

The instructions in the Shape Drawing program were all coded directly into the Programs
statements section. While this worked, it is not the best way of managing your programs instructions. In this program we will use procedures to organise the instructions into separate
groups.
Start by creating a Main procedure that will contain the programs main instructions.
5. Declare a Main procedure in your code. Place it below uses SwinGame; and above begin.
It should perform the following actions, which are similar to those in the Shape Drawing
program.
--------------------------------Procedure: Main
--------------------------------Steps:
1: Open a Graphics Window with the title Bike Drawing',
that is 200px wide, and 200px high
2: Load the Default Colors
3: Clear the Screen to ColorWhite
4: Refresh the Screen
5: Delay for 5000ms

6. Call the Main procedure from within the programs instructions (the beginend at the bottom of the file)
7. Switch back to the Terminal and compile and run your program. This will make sure that
everything is working correctly at this stage. The following image shows the code for declaring this procedure. Notice the indentation within the procedures code.

Page 3 of 6

Introduction to Programming

Pass Task 1.3: Creating a Procedure

At this stage you have created a procedure called Main. This contains the programs main instructions. When the program starts you call the Main procedure so that these instructions are
run.
Now you can create a second procedure that will draw a red bike to the top left of the screen.
Here are the coordinates for the bike.

1. Write the code for the DrawRedBike procedure using the pseudocode below.
--------------------------------Procedure: DrawRedBike
--------------------------------Steps:
1: Draw a Triangle using ColorRed at the points
(10,20), (25,5), and (40,20)
2: Draw a Circle using ColorRed, at (10,20),
with radius 10
3: Draw a Circle using ColorRed, at (40,20),
with radius 10
4: Draw a Line using ColorRed, from (40,20), to (40,0)

Note: The compiler reads from top to bottom, and it need to know about the DrawRedBike procedure before it is called. Make sure you place the new procedure above Main.
Hint: See the table from the Shape Drawing program for a list of SwinGame shape
drawing procedures.

2. Add a call to DrawRedBike( ) in Main. Make sure you place the code after ClearScreen
and before RefreshScreen otherwise it will not appear.

Page 4 of 6

Introduction to Programming

Pass Task 1.3: Creating a Procedure

3. Switch back to the Terminal and compile and run your program (remember you have to
compile from the directory that contains the SwinGame build.sh and run.sh file). Make
sure it works correctly before going to the next step.
Terminal Tip: Sick of typing ./build.sh or any other terminal command? You can cycle
back and forward through previously used commands using the up and down arrows on
your keyboard. Use the up arrow to recall the most recent command.

4. Create a second DrawBlueBike procedure, and call it from Main as shown below. Use
the same coordinates as the Red bike, just change the color.

5. Switch back to the terminal and compile and run the program again. You should see the
red bike and then the blue bike.
6. Create your own procedure to draw a custom shape, then adjust main to call this after
showing the two bikes. Here is an example, which you can program if you cant think of
any other shape to draw. (There are three triangles, one rectangle, and a line)
20, 0

0, 20

10, 20

22, 0

22, 18

20, 20

30, 20

38, 18

40, 20

10

10, 30

20

30, 30

Page 5 of 6

Introduction to Programming

Pass Task 1.3: Creating a Procedure

7. Switch back to the terminal and compile and run the program again. You should see the
two bikes appear, followed by your custom shape.
Once you are happy with your Bike Drawing program you can prepare it for your portfolio. This
work can be placed in your portfolio as evidence of what you have learnt.
1. Use Skitch (or your preferred screenshot program) to take a screenshot of your work
in this case make sure you get a picture of your custom shape.
2. Login to Doubtfire and submit your code and screenshot to Pass Task 1.3.
3. Remember to save the document and backup your work! Storing your work in multiple
locations will help ensure that you do not lose anything if one of your computers fails, or
you lose your USB Key.

Note: Each week you should aim to submit all tasks. Submit this task to Doubtfire once
it is complete. The assessment criteria give you a list of things to check before you submit.

Page 6 of 6

Вам также может понравиться