|
Want to thank me for making this book available for free? Just buy Special Edition Using Macromedia Director MX
Advanced Lingo For Games
| |
| Game Variations
Although the basic game is good as a memory exercise, scoring features can make it even more exciting. There are two ways to keep score: You can record the number of guesses and the number of correct matches, and you can time the player. In addition, a two-player version of the game can turn it into a competition. Keeping ScoreThe example movie on the CD-ROM includes frames and behaviors that represent all four versions of the game discussed in this chapter. If you look at the "Matching Game Frame Behavior w/Score" behavior, you will see a script similar to the basic game's script, except that script also keeps track of the score. Furthermore, if you turn off script auto coloring by choosing File, Preferences, Script, Auto Coloring, you can see that I have marked the changes in red. This will help you find the changes that are mentioned in the following paragraphs. You need to turn auto coloring off before opening the movie, however. The first such change is to add two properties that keep track of the score. You need to add a property that counts the number of guesses that the user makes, and a property that counts the number of correct matches. property pScoreGuess, pScoreMatch -- remember the score Next, in the on beginSprite handler, set these properties to 0. -- initialize the score pScoreGuess = 0 pScoreMatch = 0 showScore(me) The last line shown previously calls a new handler. This will simply display the current score in a text member. Call this in the on beginSprite handler to make sure that this text member displays the initialized score when the game begins.
on showScore me
-- show the score
member("score").text = "Score:"&&pScoreMatch&"/"&pScoreGuess
end
In the "on returnCards" handler, you need to add 1 to the "pScoreGuess" property each time a pair of cards is compared. -- record the guess pScoreGuess = pScoreGuess + 1 Further down in the same handler, add 1 to the "pScoreMatch" if a correct match has been made.
-- record the match
pScoreMatch = pScoreMatch + 1
Finally, the "on returnCards" handler should call the "on showScore" handler to display the change in the score. This completes the changes to the code. The only change to the movie beyond that is to create the "score" text member and place it on the Stage. You should also place this "score" text member on the end game frame, so that the final score is present on the screen when the game is over. Adding a TimerAn alternative way to score this sort of game is to keep track of the amount of time it takes the player to complete the puzzle. This is done by simply recording the time the game started, and then checking to see how much time has elapsed since then. The example on the CD-ROM also includes a "Matching Game Frame Behavior w/Timer" behavior. If you turn off the script auto coloring, you will see that the new pieces of code are in red. The first such line just added the "pGameTimer" property to the top of the behavior. property pGameTime -- the start time of the game After that, this property needs to be initialized in the on beginSprite handler. -- set the start time pGameTime = the ticks showTimer(me) The "on showTimer" handler is called right away to show the starting time, 0, on the screen. This same handler will be used to update the time throughout the game.
on showTimer me
-- the current time, as a string
currentTime = string((the ticks - pGameTime)/60)
-- if this is different than the time displayed, update
if member("score").text <> currentTime then
member("score").text = currentTime
end if
end
Notice that there is a lot more to this "on showTimer" handler than just placing the time in a text member. First, the time is calculated by subtracting the start time from the current time. Then this result is divided by 60 to get seconds. This number is converted to a string. This string is then compared to the text in the "score" text member. If the time is different than the number shown there, the text member is changed to show the new time. The reason this is done is simply for speed. Every time you set a text member, Director takes the time to re-render the text on the screen. Because the movie is running faster than one frame per second, you will be asking for an update to the timer several times a second. For instance, if the "score" text member shows 3, to indicate that 3 seconds have passed since the beginning of the game, then you will probably be asking for a timer update many more times before the time changes to 4. Because the time shown is the same as the actual time, setting the text member won't do any good. All that will happen is that the member will re-render the text, taking a small amount of time to do so, probably milliseconds. However, these are still wasted milliseconds, so it's better not to force the text member to update unless it really needs to. The last change you need to make to the script is to add a call to the "on showTimer" handler in the on exitFrame handler. The system property the ticks is constantly changing, so there is never a need to actually increment a timer of any sort. Just by calling this handler once per frame, you can update the timer. -- update the time showTimer(me) That's all it takes to add a timer to the game. You can see that it actually takes less code than adding the scoring functionality of the last section. Make sure that the "score" text member is in the Cast and on the Stage. You will also want this to be in the end game frame so that the user can examine his final time. Because the end game frame does not have your game behavior script on it, the "score" member is no longer updated. So the time remains frozen on the screen on the end game frame, which is exactly what you want. Two-Player GameMaking this game a two-player game is a little more difficult. However, it can be done using the same behavior framework developed here. As before, the changes described in the following paragraphs can be found in the "Matching Game Frame Behavior for Two Players" behavior marked in red. First, you need to add two new properties, one to keep track of which player is up, and the other to keep track of the scores for both players. property pPlayerTurn -- indicates whose turn it is property pScores -- a list with the player's scores These two properties need to be set at the end of the on beginSprite handler; the text members on the screen need to be set as well. -- initialize player information pPlayerTurn = 1 pScores = [0,0] showTurn(me) showScore(me) Two text members are needed for this version of the game. As before, you need the "score" text member, but this time two lines are needed to show the two scores. So make sure that the text member is large enough on the Stage to allow two lines of text. The second text member indicates which player's turn it is. We'll name this text member "turn". Here are the handlers that update these text members:
on showTurn me
member("turn").text = "Player"&&pPlayerTurn
end
on showScore me
text = ""
repeat with i = 1 to pScores.count
put "Player"&&i&":"&&pScores[i]&RETURN after text
end repeat
member("score").text = text
end
Notice that the "on showScore" behavior is not hard-coded for exactly two players. Instead, it looks to the "pScores" list to determine how many players to list. This will come in handy if you ever want to make this game for even more players to play at once. The other handler you need increments the "pPlayerTurn" property when a turn is over.
on endTurn me
pPlayerTurn = pPlayerTurn + 1
if pPlayerTurn > pScores.count then
pPlayerTurn = 1
end if
showTurn(me)
end
The "on endTurn" handler also does not assume two players, but instead looks at the number of items in "pScores" to determine when the turn should go back to player 1. This handler works with any number of players. Now the "on returnCard" handler needs to be modified to record the score and call "on endTurn". This game differs from the earlier score-keeping version in that we are only interested in matches, not the number of guesses. So we only need to increment the score in the case of a match. Here are the lists to be added:
-- record the score
pScores[pPlayerTurn] = pScores[pPlayerTurn] + 1
showScore(me)
Finally, we need to call "on endTurn" at the end of the "on returnCard" handler to indicate that the next player is up. endTurn(me) This completes the changes to the behavior. Make sure that both the "turn" and the "score" members are present on the Stage, although only the "score" member needs to be present at the end game frame. To make this game work with more than three players, you only need to add more 0's to the "pScores" list in the on beginSprite handler. This has the potential to be a challenging party game with 4, 5, or even 6 players. Each player will want to pay attention to the other player's guesses to try to find matches. Remember to expand the "turn" text member to accommodate all the extra lines of the score needed. | |