Re: Trying to learn BGT again

Here's a more elaborate example, it actually has a bit of game to it. Comments are sprinkled liberally throughout. It's more elaborate than you need, but hopefully it will help you. Keep in mind, though, that computers are very literal machines. Forget one semicolon, fail to capitalize something that needs to be capitalized, etc. and the whole thing will come crashing down.

/* Simple Example by Jayson Smith

The concept behind this game is very simple. The player is confined to a game world consisting of fifty positions, numbered 0 through 49 from left to right. The player uses his or her right arrow to move one space to the right, and the left arrow to move one space to the left. The game keeps track of how many times the player has moved successfully. However, if the player is at the left edge (position 0) and attempts to walk left, or at the right edge (position 49) and attempts to walk right, he or she fails, and bumps the edge of the world. The game also keeps track of how many times the player has bumped up against an edge of the world. The objective of the game is to move successfully from one position to another 100 times before bumping an edge more than five times.

A sound is played before the game starts. This is where a company name or logo would be heard. Then a looping background sound is played. Sounds are played to indicate walking, and to indicate bumping up against either edge of the world. A sound is played if the player wins, and another sound is played if the player loses. Upon either victory or defeat, the game exits.
*/

/* Now we define sound objects. Think of a sound object like a high-tech cassette player. You put a cassette in the player, then you can play, rewind, set the volume, and other functions. At this point we create several of these virtual cassette players, each of which will eventually be loaded with a different sound the game needs. It is not important at this point to know just what is going on under the hood. All you need to know is that the following lines define these sound objects, which we will refer to by name later in the code. */

sound logo; // Starting sound.
sound amb; // Background sound.
sound walk; // Movement sound.
sound bumpl; // Bump left edge.
sound bumpr; // Bump right edge.
sound youlose;
sound youwin;

/* Now we set up some variables we'll be using later. You can think of a variable as a place in the computer's memory set aside to store one number of our choosing, and which we can change as we see fit. For now, don't worry about the 'int' designation. */

int position=0; // The player's position on the board.
int bumps=0; // How many times we've bumped an edge.
int moves=0; // How many times we've moved successfully.

/* Now we write our first function. You can think of a function as a set of instructions to the computer to have it carry out some task. In this case, however, the 'main' function is the place where execution of our game code will begin. For this reason, every game written in BGT must have a 'main' function. */

void main() {

/* Remember those sound objects we defined earlier? Those virtual cassette players? Now we're going to put tapes in all those players. This is very easy, we don't have to know or worry about exactly how it's done. All we have to do is tell the sound objects what to do, as follows. */

amb.load("amb.wav");
bumpl.load("bumpl.wav");
bumpr.load("bumpr.wav");
logo.load("logo.wav");
walk.load("walk.wav");
youlose.load("youlose.wav");
youwin.load("youwin.wav");

/* Now we're going to tell one of our freshly-loaded sound objects to play. But while it's playing, we don't want the game to do anything else. In other words, we want to start this sound playing, then just sit there doing nothing until it's done. */

logo.play_wait();
// Wait a second after the logo finishes playing before going on.
wait(1000);

/* First we want to make it easier for JAWS users to play our game, so we install the keyhook. Once that's done, we want to show the game window, since the game's about to begin. */

install_keyhook();
show_game_window("Jayson's Test Game");

/* Now we give a few commands to another one of our sound objects. First we tell it to turn down its volume to a softer level than normal. Then we tell it to play in an endless loop, over and over and over again. Naturally, we go on to do other stuff while it's playing. */

amb.volume = -10.0;
amb.play_looped();

/* Here comes the main game loop. This is a piece of game code which will run forever and ever and ever, without end, until we tell it to stop by saying we've won or lost. Some of this might get a bit technical, but here we go. */

while (true) { // This seems a bit strange at first glance. What we're saying here, in effect, is to do something while 1 is equal to 1. This seems silly, since 1 will always and forever be equal to 1, but that's the whole point. We've just made a loop which will run forever, until something happens to break it, which we'll see soon.

if (bumps > 5) { // Now we're checking to see if our bumps variable is greater than 5. If you recall, this is the place where we store how many times the player has bumped an edge of the world. If this condition is true, the player has lost.

// You lose!
amb.stop(); // Stop our looping background sound
wait(50); // Wait a bit of time
youlose.play_wait(); // Play the losing sound, and wait around while it plays
wait(100); // Wait a bit of time
exit(); // Close the program.
}
if (moves >= 100) { // This is a similar condition check, except this time, we're checking the winning condition! If this check is true, the player has won! As it turns out, the winning sequence is the same as the losing sequence, only a different sound plays.

// You win!
amb.stop();
wait(50);
youwin.play_wait();
wait(100);
exit();
}

/* Now we do a few more checks. We need to see if the player is pressing the right or left arrows. The key_down function, built into BGT itself, handles this for us. The constants KEY_RIGHT and KEY_LEFT are also provided by BGT for this very purpose. If either of these keys are down, we tell the computer to stop its work in our current 'main' function and execute another function as appropriate, then when that function is done, return to where we are. We'll define the move_right and move_left functions later. */

if (key_down(KEY_RIGHT)) move_right();
if (key_down(KEY_LEFT)) move_left();

} // We're do ne with the main game loop...
} // And we're also done with the 'main' function! All it really did was to set things up so the game could start, then get the game loop going.

/* Now we need to define the move_right and move_left functions to actually move the player. */

// Move right.
void move_right() { // Start of move_right function
if (position == 49) { // Is the player already at position 49, the right edge? If so, we know what to do...
// Oops. Too far. Bump.
bumpr.play_wait(); // Play the right bump sound, and wait around while it plays.
wait(100); // Wait a bit longer
bumps ++; // Increment (add 1 to) the bumps variable, which keeps track of how many times we've bumped. Remember, this can't go over 5 or we lose!
return; // Break out of this move_right function early, since we don't want to actually move the player.
} // Okay, so that's what happens if we need to bump. But what if we d on't?
moves ++; // Increment the moves variable, which stores the number of times we've moved successfully. If this brings it to 100, the next time the game loop runs, we win!
position ++; // Add 1 to our position in the game world. We moved to the right, after all.
walk.play_wait(); // Play the walk sound, and wait around while it plays.
wait(50); // Wait a bit longer.
} // End of move_right function.

// Move left.
void move_left() { // Start of move_left function. This does exactly the same as move_right, but everything is reversed.
if (position == 0) { // Are we at the left edge?
// Oops. Too far. Bump.
bumpl.play_wait();
wait(100);
bumps ++;
return;
}
moves ++;
position --; // Remember, we're subtracting from the position in this case.
walk.play_wait();
wait(50);
}

URL: http://forum.audiogames. net/viewtopic.php?pid=160328#p160328

_______________________________________________
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
http://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • Re: [Audiogame... AudioGames.net Forum — General Game Discussion: jaybird
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: CAE_Jones
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: Guitarman
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: SLJ
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: jaybird
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: CAE_Jones
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: lukas
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: dbzfan94
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: Guitarman
    • Re: [Audi... AudioGames.net Forum — General Game Discussion: SLJ

Reply via email to