• Homework
  • Thirds
  • Upper Thirds
  • Fourth
  • Divisions
    • ICT
    • Computer Science
  • Fifth
  • Lower Sixth
  • Upper Sixth
    • Upper Sixth Theory
  • Blog
  • Careers
  • Unity
  • Networking

Objectives

In this section you'll learn how to get input from the keyboard to make programs interactive.
You'll also learn how to create and call a procedure.  Procedures are key to the idea of structured programming and are one of the most important concepts in programming.

Making a paddle move

We're going to continue making a basic game by adding some user interaction.  We'll have a paddle at the bottom of the screen that the player can move to the left and right by pressing the 'a' and 'd' keys.  

To do this we need to read the keyboard and respond appropriately.

In this program we will introuce the idea of a procedure.
A procedure is like a little program inside a program.  It's a group of code lines that do something specific and can be called by name

We'll start by setting up the program.  We need to add the console unit to our uses section and create two variables for the x and y position of the paddle.  We then create an infinite loop using "while true do" and, for the moment, just draw the paddle wherever it happens to be.
uses
  System.SysUtils,console;

var
   xpos, ypos : integer;    // define variables for x and y

   key: char;               // and a char variable for our keypress
begin
    xpos := 40;             // set those variables to the initial position
    ypos := 12;
    while true do           // loop forever
    begin
      gotoxy(xpos,ypos);    // display the paddle
      write('XXX');
    end;
end.
That's OK but it doesn't actually do anything, just sits in the middle of the screen.  We need to read the keyboard and actually make something happen.  First we check to see if a key has been pressed.  If not there's nothing to do.   If a key has been pressed we call readkey to find out if it's a key we are interested in
     while true do
     begin

      if keypressed then            // check if a key has been pressed
      begin
          key := readkey;
          if key = 'a' then
          begin
                                    // move left
          end;
          if key = 'd' then
          begin
                                    // move right
          end;
      end;
      gotoxy(xpos,ypos);
      write('XXX');

Defining a procedure

Instead of putting all the code for moving left and right in the main part of the program, we're going to create a procedure.  We do that by adding some code before the main program starts, between the variable declaration section and the first 'begin' that indicates the main program.

To make a procedure we just use the keyword 'procedure' followed by the name we wish to give it.  The name is up to you, but as with variables, it can't start with a number or be an existing keyword.  It's best, as you'll see, to make the procedure name as descriptive as possible. 
var
   xpos, ypos : integer;
   key: char;

procedure moveleft;
begin
   xpos := xpos -1;
end;

procedure moveright;
begin
   xpos := xpos +1;
end;

begin
    xpos := 40;
    ypos := 12;
We've now created two procedures called moveleft and moveright.  It's as if we had added new keywords to Delphi.  Now whenever we want to run the code between the begin and end of the procedure, we just call it by name, like this:
      if keypressed then            // check if a key has been pressed
      begin
          key := readkey;
          if key = 'a' then
          begin

             moveleft;              // move left
          end;
          if key = 'd' then
          begin

             moveright;             // move right
          end;
      end;
Making procedures is incredibly important in programming.  It allows complex bits of code to be sectioned off into their own area and given a name that describes what the code does.  It makes programs much more readable - as you can see, and allows us to work on different bits at a time.  In addition, if we make a good general procedure we can copy it into other programs.  

This is the beginning of a big idea:

Structured programming

Structured programming is essential when our programs become large.  It means that we split the program into self-contained blocks that talk to each other.  We can draw out the structure of our program using a flowchart or structure chart - diagram it as a series of blocks.
Can you complete this program so that it displays properly (hint:  use clrscr) and doesn't flicker quite so much?
Powered by Create your own unique website with customizable templates.