In Week Four we are looking at some more advanced concepts for controlling what happens in our video game projects:
Messaging: A type of communication within code or between different software components or even across systems. The sending code broadcasts a message; the receiving code is listening for that message and reacts based on the requirements. Publish/Subscribe represents a well-known Software Design Pattern that is implemented in different ways in different programming languages. In Scratch it is implemented by:
a Broadcast Event; within the event declare a name for the message; example: Broadcast GameOver
a Receive Event that is listening for the specific message and executes a script when that message is heard; example: when I receive GameOver show the end game background and play the victory tune.
Last week, an obnoxiously long texting acronym made it’s way into our conversation. I was reminded how I really need to brush up on this sort of super important information. So I wrote a Scratch app that uses two data lists to allow me to look up an acronym from a list of ten. This example uses variables, data lists, user input and a cute little orange martian guy. I wonder what would happen if I enter one that isn’t on the list? Why don’t you try it and find out!
Our second and third weeks focus on flow of control within a program:
Algorithm: Logical step by step outline of the processes to be implemented to solve a problem. Includes decision points with branches defining what to do for each case. An algorithm is language-agnostic, i.e., it does not use syntax specific to a particular programming language. For all but the simplest programs, an algorithm is an essential part of the software development process
Flowchart: A diagram that is a graphical representation of a process. Each process step is represented by a rectangle; each decision is represented by a diamond with branches coming off for each choice. A flowchart may represent one part of a larger program, breaking it down into manageable components.
Scenario: Describes a particular set of events, data values, or user choices that direct a program to flow in a predicted direction. Each scenario will have an algorithm and possibly a flowchart to define what happens in that set of circumstances.
Use Case: A style of scenario definition that describes one specific way in which the users will interact with the software.
“Happy Day” Scenario: The user(s) and data behave in the way expected; no errors happen
Alternate Scenario: Something untoward may have happened; perhaps an error; or the user behaved unconventionally (submitted “bad” data, clicked buttons out of order….)
Flow of Control: the order in which the steps of a program are carried out as determined by control structures within a program. Control structures include loops, conditionals, function or procedure calls
Loop: a flow of control structure that causes code to be repeated a specified number of times or based on a condition (while or until some condition is true); a loop that runs “forever” is called an Endless Loop
Conditional: a flow of control structure that determines what code gets executed based on a decision. The conditional to be evaluated may use a combination of several Operators. For example: If (X > Y) Or (Y < 0) uses relational operators along with a logical operator. The exact syntax will vary with language. Order of evaluation is extremely important!
Arithmetic: Addition, subtraction, multiplication, division, modulus (remainder after division); in Scratch these operators are represented by +, -, *, /, and mod
Logical: Not, And, Or, XOR (exclusive OR)
Relational: =, >, <, >=, <=
Boolean: True or False. The conditional IF block will evaluate to a Boolean result
Event: a trigger that a program or component is “listening” for so it can react
Stop or Start
Nested Control Structures: A control structure may have another control structure nested within it: nested IF’s, nested LOOPS, etc. It’s a good idea to use a flow chart, or pseudo-code (plain English statements rather than programming syntax), or otherwise draw out or act out what will happen to make sure things go in the intended order
Variable: a named data element representing a piece of information to be remembered. A variable should have a meaningful name! In our video game we may want to define a variable named Score. The code may set the variable’s value based on some condition. For example: If the hero rescues the princess increase the Score by 1000; if the hero falls into a fire pit decrease score by 50.
At our last meeting we had the question of whether we could set up our Sprites to face the direction of their movement automatically instead of having separate costumes for mirror images. It turns out that we can use the Rotation Style setting of each sprite to do that!
In this example, the characters will all start moving to the right and are programmed to make a 180 turn when touching the edge. How they look on their return trip to the left depends on the Rotation Style setting.
Here they are after the 180; they are all moving to the left although one looks to be upside down and another moves “backward”:
Design Time vs Run Time Property Settings: Setting the Rotation Style in the IDE (Integrated Development Environment) as above is an example of a Design Time Property Setting. We can also change the Rotation Style during the program execution, i.e. Run Time, by using the Set Rotation Style command in the Motion section of scripts.
If you’ve spent millions of hours perfecting your artistic rendition of a new Sprite in Scratch’s costume editor, (let’s say an adorable tiger!) and another billion hours working out the complex script for the little guy’s dance moves, you may want to re-use the costume art and/or the script in the same project and you’ll definitely want to save them for posterity.
Here’s how to“leverage existing code” (programmers like to say stuff like that and managers LOVE to hear it!)