CS/ENGR
101: Gameboy Session
Fall 2006 – Day 3
This handout is a C programming lab exercise that introduces selection constructs boolean expressions in the C programming language and user interaction on the Gameboy.
For this lab exercise, use the VisualHam project from the last session. To do this, click on the File menu and choose Open Project. In the open project dialog box, browse to your project using the I: drive letter (rather than through the MyDocuments network share name). If you wish to save this version of your program, you should use File->Save As to make a copy, close the edit window, then double-click on main.c in the source tree to bring back up the original file to edit.
Selection constructs
There are several selection constructs in C. We will look only at the if statement. This statement allows a program to choose between different sets of statements to be run by checking if a condition is true or false. If the condition is true, then it executes the statements for the true condition. If the condition is false, then it skips the true condition statements. The if statement can also have an else part, which is executed only if the condition is false. The conditions are usually relational expressions that compare two numbers or logical expressions, though any non-zero expression is treated as a true condition and any zero expression is treated as a false condition. VisualHam predefines the constants TRUE and FALSE to be the values of true and false, respectively.
The relational operators are < (less than), <= (less than or equal to), > (greater than), >= (greater than or equal to), == (equal to, note that is it two =’s, not one), != (not equal to). In addition, relational expressions can be combined using logical operators && (AND – true if and only if both operands are true), || (OR – true if one or both of the operands are true), and ! (NOT – negation of value, so true if operand is false and vice versa).
The C syntax for an if statement is as follows:
int x, y; /* declare some variables */
x = 3; y = 7; /* give them some values */
/* header syntax: if (<condition>)
if (x < y)
{
/* statements to be executed when <condition> is true */
}
else /* optional else part */
{
/* statements to be executed when <condition> is false */
}
User interaction
Selection constructs are used whenever the execution of some code depends on the something that may or may not be true. For example, we might want to do something when one of the buttons on the Gameboy is pressed. In order to do this, we need to find out if the button has been pressed, and then if so, do something. The mygba.h library defines some macros that allow us to determine if a button has been pressed. The have the value of true if the corresponding button has been pressed. Their names are as follows (R and L are the shoulder buttons):
Keyboard equivalent: F_CTRLINPUT_A_PRESSED Z F_CTRLINPUT_B_PRESSED X F_CTRLINPUT_SELECT_PRESSED backspace F_CTRLINPUT_START_PRESSED enter F_CTRLINPUT_RIGHT_PRESSED right arrow F_CTRLINPUT_LEFT_PRESSED left arrow F_CTRLINPUT_UP_PRESSED up arrow F_CTRLINPUT_DOWN_PRESSED down arrow F_CTRLINPUT_R_PRESSED A F_CTRLINPUT_L_PRESSED S
Here is an example of how to use these macros. The clear screen code is now executed whenever the down arrow button is pressed.
if (F_CTRLINPUT_DOWN_PRESSED)
{
for (counter = 0; counter < 160; counter++)
{
ham_PutLine (0, counter, 239, counter, COLOR_BLACK);
}
}
Exercise
For today’s exercise, modify your program from last session to do the following:
Clear the screen whenever the Start button is pressed, instead of at fixed intervals.
Change the background color whenever the Select button is pressed. This should take effect upon the next clearing of the screen.
The random lines start drawing only after the A button is pressed and stop drawing when the B button is pressed until the A button is pressed again.
10/12/06