3 ICT Gaming Essentials Lesson 3: Basic Programming Concepts LESSON SKILLS After completing this lesson, you will be able to: Explain the types and uses of variables and operators in game programming. Describe the use of events, event handlers and functions in game programming. Describe the use of parameters and arguments in game programming. Describe the use of objects, classes and instances in game programming. Describe the use of properties and methods with objects in game programming. KEY TERMS argument event object assignment operator event handler Object-oriented programming (OOP) Boolean expression operator class function parameter comparative Boolean instance property conditional Boolean method variable
ICT Communications Essentials - Lesson 3: Internet Life and Privacy 3-2 Points to Ponder These Points to Ponder are designed to help you focus on key elements in this lesson. They are also suitable for use to spark discussions or individual research. Define "variable." List three properties (or characteristics) of an object in the classroom, such as a chair. Create an example of two string variables; one should be used to contain numbers. Provide the variable name and value for each. What is an event? Provide an example of how it can be used in creating a game. Write the names of three objects that you use on a daily basis. List three properties and methods associated with each object. Create a blueprint for a cell phone object class. List three properties (or characteristics) and three methods that this object class would have.
ICT Communications Essentials - Lesson 3: Internet Life and Privacy 3-3 Overview In this lesson, students will learn basic concepts used in programming languages, including variables, events, parameters, objects and properties. These concepts are complex, but students can understand them more easily when they are applied to real-world items. Introduction to Programming Concepts Objectives 10.3.8: Explain the types and uses of variables in game programming. 10.3.9: Describe basic Boolean concepts, including logical operators, order of precedence, and expressions. 10.3.10: Describe the use of events, event handlers and functions in game programming. 10.2.11: Describe the use of parameters and arguments in game programming. 10.3.12: Describe the use of objects, classes and instances in game programming. 10.3.13: Describe the use of properties and methods with objects in game programming. Syntax Just like written languages have words, symbols and rules, so to do programming languages. In written languages, this syntax includes nouns, verbs, pronouns, adjectives and adverbs. Syntax refers to the ways that words and symbols in a programming language are written by a programmer, including correct keywords, spelling and capitalization. This same concept defines programming syntax. Certain words, punctuation marks (such as a semicolon and a curly bracket) and other symbols have different meanings. See the example code in Figure 3-1. Figure 3-1: Example programming code Variables, Expressions and Operators Variables are used to store information in the computer s memory or a database that can be used in a program when executing a program. Variables, then, are like storage containers. Variables help a program work more efficiently. Game programmers use variables to collect and store game and user-input data. This enables the program to interact with users. For example, a variable can be used when prompting the user for his or her name, and the saved name can then be displayed in a welcome message or other alerts. Variables are commonly used for tracking and displaying score information.
ICT Communications Essentials - Lesson 3: Internet Life and Privacy 3-4 A variable is assigned a descriptive name that specifies the type of data it stores, such as "playername." The program then refers to the variable name instead of the actual data it holds. For example: If the playername variable stores the value "John," then when the program instructs the computer to print playername, the name John will be displayed on the screen. If the data contained within a variable changes, then the program simply changes that data stored in the variable holder. This eliminates the need to rewrite all of the program code that refers to it. For example, if playername changes to Michael, then the program simply changes the variable data to Michael (playername = Michael). Every reference to the playername variable will update to the new data. Variables can hold different types of data and are classified based on the type of data they hold. Knowing the type of data stored in a variable enables programs to make sure the data is handled and manipulated properly. The five most common data types are listed in Table 3-1. Table 3-1: Common data types Variable Data Type Example Value Description Character String K % John Integer 45 XY3456981182-14 Floating-Point 3.25 5-15.1 Boolean True or False, T or F, 1 or 0 A single letter or symbol A sequence of characters, numbers or symbols Whole numbers that do not contain a decimal Numbers that contain a decimal Used to store the results of a comparison, such as: a > b, x <= y, z == 14 Programmers must tell the computer that a variable exists (known as "declaring a variable"), and they must assign it an initial value (known as "initializing a variable"). If the data stored in the variable is to be calculated or used in any way (such as a Score variable that adds up the points earned during a game), then the programmer must tell the computer how to perform the calculations. This is done using expressions, similar to expressions used in math. Expressions contain a combination of variables, values, functions and operators, which are evaluated and produce another value. This is similar to the expressions used in math. For example, the expression 10-2 evaluates to 8, and (2+8)*(2+1) evaluates to 30. Programming operators are the mathematical symbols that we use every day and expressions are evaluated using the mathematical order of precedence (order of operations): 1. Parentheses ( ) 2. Exponentiation xy
ICT Communications Essentials - Lesson 3: Internet Life and Privacy 3-5 3. Multiplication and division * / 4. Addition and subtraction + - You can use the mnemonic Please Excuse My Dear Aunt Sally to remember the order. When both multiplication/division or addition/subtraction are present, then the remaining operations are solved from left to right. Table 3-2: Arithmetic operators Operator Function Example + Addition 10 + 5 = 15 - Subtraction 10 5 = 5 * Multiplication 10 * 5 = 50 / Division 10 / 5 = 2 ++ Increments by 1 10++ returns 11 -- Decrements by 1 10-- returns 9 % Modulus Divides first value by second value and returns the remainder 10 % 3 returns 1 (10 / 3 = 3 remainder 1 The modulus operator returns the remainder) Table 3-3: Comparison operators Operator Function Example == Is equal to 5 = =10 (returns false) cat == dog (returns false)!= Is not equal to 5!= 10 (returns true) cat!= dog (returns true) < Is less than 5 < 10 (returns true) <= Is less than or equal to 5 <= 10 (returns true) > Is greater than 5 > 10 (returns false) >= Is greater than or equal to 5 >= 10 (returns false) Table 3-4: Logical (Boolean) operators Operator Function Example && (AND) Is true if both expressions are true (5 < 10) &&(10 < 20) (returns true) (OR) Is true if either expression is true (5 < 10) (10 > 20) (returns true)! (NOT) Is true if expression is false! 25 < 10
ICT Communications Essentials - Lesson 3: Internet Life and Privacy 3-6 The equal sign (=) is considered an assignment operator in most programming languages. The assignment operator links the variable name on the left side of the operator with a value on the right side when initializing a variable, such as var playername = "John". A Boolean (true or false) contains a condition and is often used in repetition and selection structures. Examples include: If this is true then do this, or else if false then do that. While this is true then do this, if false do that. The Scratch example in Figure 3-2 illustrates the two types of Booleans: o o Conditional Booleans check for a specific condition. Comparative Booleans compare values. Figure 3-2: Conditional and comparative Booleans Links to Learn More Scratch Wiki article about variables Overview of Variables (Adobe Flash video, 10 mins)
ICT Communications Essentials - Lesson 3: Internet Life and Privacy 3-7 Suggested activities Boolean Operators (Online) Boolean Expressions (Online) The Language of a Programmer (Hands-on) Understanding Variables in Scratch (Hands-on) Objects, Methods and Functions Object-oriented programming (OOP) is a programming language model organized around objects rather than actions and data rather than logic. It relies on defining classes, objects, instances, methods and properties. A class is defined as components that are associated with a certain object. It is a definition or blueprint used to create a particular type of object. A class provides a detailed description of the object's characteristics (such as that the object should include a size and a color), and it specifies what can be done with the object (its functions). When a class is used to build an object, the object is called an instance of the class. Multiple objects (or instances) can be created using the same class. Instances are represented by their class and properties. Properties are the various characteristics of an object, such as size, color and type. Each object created using the class contains the same properties, but the values of the properties may be different. Figure 3-3: An instance of the Football class
ICT Communications Essentials - Lesson 3: Internet Life and Privacy 3-8 Functions are actions that a class can do; but, are not part of the class description. They are named units of code that perform a task or cause an action to take place. A function is called upon or executed by a descriptive name, such as "move forward" or "jump." Actions that an object can perform and are a part of the object s description (such as move, rotate or jump) are called methods in programming. The methods that can be applied to the football object in a game program might be to bounce off walls, speed when thrown, or move up or down when the keyboard arrows are pressed. A method is a function of a class in other words, it is actions the class can perform and is part of how the class is defined. Each object created using the class shares the same methods (functions) as all the other objects created from the same class. That is, all objects in a class can do the same things, such as bounce off walls or speed when thrown. Suggested activity Objects, Methods and Functions Simplified (Hands-on) Arguments and Parameters Methods often need more information before they can execute. For example, the methods will need to know how far to move the object or how many degrees to rotate the object. The information is given to the method using arguments and parameters. An argument is the value that is passed to a method when it is called so that it knows what to do. For example, an argument given to the rotate method could be 90 or 180. A parameter is the variable that holds the argument data (value) needed by the method. For example, degrees=90 or steps=10. So in this example, degrees and steps are the parameters. The values assigned to the parameters 90 and 10 are the arguments. Events and Event Handlers An event is an action that takes place while a program is running, such as a mouse click or the pressing of a key. Programmers use functions called event handlers to tell the program what to do when an event occurs. The event handler, then, is the code that is called when an event occurs. For example, if a programmer wants an object to turn and move left when the left-arrow key is pressed, they would write an event handler that contains the instructions to turn and move the object left when the left-arrow key is pressed. Then, every time the left-arrow key is pressed while the program is running, the left-arrow-key event is triggered and the associated event handler will execute so the object will turn and move left.
ICT Communications Essentials - Lesson 3: Internet Life and Privacy 3-9 Suggested activity Objects, Arguments, Events and Event Handlers in Scratch (Hands-on) Case Study Programming Your Game and Gathering Feedback (Hands-on)