32
Piñata Game: Keeping Piñata Game: Keeping Score in Alice Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009 www.cs.duke.edu/csed/alice/aliceInSchools

Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Embed Size (px)

Citation preview

Page 1: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Piñata Game: Keeping Score in AlicePiñata Game: Keeping Score in Alice

By Maggie BashfordProfessor Susan Rodger

Duke UniversityJuly 2009

www.cs.duke.edu/csed/alice/aliceInSchools

Page 2: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Download the Alice World called piñataGameStart.a2w that goes along with this tutorial.

Adding the Text ObjectAdding the Text Object

We will use this world to create a game that allows players to We will use this world to create a game that allows players to hit a swinging piñata with a bat. Many of the objects have hit a swinging piñata with a bat. Many of the objects have already been created and placed in the world. We must create already been created and placed in the world. We must create an object to display and update the score, and we will also an object to display and update the score, and we will also write the code to play the game.write the code to play the game.

Click add objects and add a new 3D Text Object to the world.

Page 3: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Once you have created the new text object, right click on it in the object tree and rename it score.

Reposition the score where you can see it. Use resize and turn to face the camera.

Adding the Text ObjectAdding the Text Object

When it asks what to say, type score: 0.0.

Page 4: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Click on score in your object tree, and then go to the properties tab. Click on the create new variable button.

Name the variable scoreValue, select the type Number, and click the arrow next to value to select other and type 0.

Creating VariablesCreating Variables

Page 5: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Creating VariablesCreating Variables

We need to keep track of a few more things during our game. To do this, we will create two world variables. Notice under the world properties that a list called pinataParts has already been created. This list is used later in the explode method that breaks open the piñata.

You can play around with these You can play around with these values later to make the game values later to make the game easier or hardereasier or harder

Now let’s create a variable to remember the number of hits needed to break the piñata. Create a world variable called numWinningHits, and set its value to 10. We will use this later to determine if the player has won the game.

We also need to set the total amount of time. Create a world variable called time, and sets its value to 15.

Page 6: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Create a class-level method for score called initialize. Click on scoreValue in the properties tab, drag it into the initialize method, and set its value to 0.

Now go back to the properties tab and find text. Drag text into the initialize method, and set its value to score: 0.0. Change the duration for both to 0 seconds.

Initialize MethodInitialize Method

Page 7: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Now we need to write a method that will add one point to scoreValue variable when the piñata is hit. Create another class-level method, called addPoints.

Find scoreValue in the properties tab and drag it into the method, and choose to increment it by 1. Change the duration to 0 seconds.

Add Points MethodAdd Points Method

Page 8: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Next drag text into the addPoints method, set its value to “score: ” and set its duration to 0 seconds.

Next, click on world in the object tree, and find its functions tab.

Find a joined with b and drag it into where you typed “score: ” and select default string for b.

Then drag what as a string into default string, and select score.scoreValue from expressions.

Add Points MethodAdd Points Method

Page 9: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Now let's divide the game into three parts: the set up and instructions, the game play, and what happens when the game is over.

Click on world in the object tree and create a new world method called setUp. Now click on score in the object tree and drag its method initialize into the setUp method.

Set Up MethodSet Up Method

Page 10: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Now let's add instructions for playing the piñata game. The text object called instructions has already been added to the world, and its text is “Break the piñata!”

Click on instructions in the object tree and select its properties. Drag isShowing into the setUp method, and choose true. Next drag text into setUp, and set it to “Press the up arrow...” Repeat this step with the text and set it to “...to swing the bat.” Set each duration to 2 seconds so there is enough for the player to read the instructions.

Set Up MethodSet Up Method

Page 11: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

For fun, let’s swing the bat during the last instruction. Drag a Do together into the setUp method, then the last instruction “…to swing the bat” inside it. Then drag a Do in order below that.

Now let’s swing the bat. Click on baseballBat in the object tree and drag the turn method into the Do in order, then select backward and ¼ revolution. Set the duration to .25 seconds and the style to abruptly. Make a copy of this line, and change its direction to forward.

Set Up MethodSet Up Method

Page 12: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Now let's prepare the player to start the game. Select instructions from the object tree and drag text three times into the setUp method outside of the Do together. First set the text to “Ready?” then “Set…” then “Go!”

Now we want to hide the instructions. Drag in the isShowing property and set it to false, then change its duration to 0 seconds. The final step in the setUp method is to start the timer. Find timer in the object tree and drag its initialize method into the bottom of setUp and select the parameter world.time from expressions.

Set Up MethodSet Up Method

The timer and its methods were created in the Timer Tutorial. See the Alice The timer and its methods were created in the Timer Tutorial. See the Alice Repository for more information.Repository for more information.

Page 13: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Set Up MethodSet Up Method

Check your setUp method to see if you missed anything! You can test it by selecting it When the world starts, but remember to set it back to my first method when you are done!

Page 14: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Now that the clock has been set, we need to swing the piñata. Create a new world method called playGame. Drag a while loop into the playGame method. Now click on the functions tab and drag a>=b into the condition, then select timer.timeLeft from expressions for a and 0 for b.

Now we want the piñata to move while the timer goes down, so drag a Do together into the while loop. Next drag two copies of a Do in order inside the Do together.

Play Game MethodPlay Game Method

Page 15: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

In the first Do in order, we will make the timer count down. Click on timer in the object tree, and click on the properties tab. Drag text into the first Do in order, and choose default string for now. Change its duration to 0 seconds. Then go back to the world functions and replace default string with what as a string. Select timer.timeLeft from expressions as the what.

Play Game MethodPlay Game Method

Page 16: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

We want to count down the seconds, so drag Wait from the bottom of the screen, and select 1 second. Next find timeLeft from the timer properties and drag it into the Do in order. Select decrement timer.timeLeft by 1.

Play Game MethodPlay Game Method

Page 17: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Now let's use the second Do in order to make the piñata swing! Go to the piñata methods and drag in turn, then select right and other to type in .1 revolutions. Now change the style to abruptly, duration to .5 seconds, and asSeenBy to camera. Right-click and make a copy of this line, and change the direction to left.

Play Game MethodPlay Game Method

Page 18: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

To perform the post-game animation, create a new world method called gameOver. The first step after the game ends is to move the piñata back to the center. Click on piñata and drag the turn method into gameOver. Select right and.05 revolutions, then change asSeenBy to camera and duration to .5 seconds.

Now we will base our actions on whether or not the player won the game. Drag an If/Else statement into the method. Go to the world functions and drag a>=b into the If condition.From expressions, select score.scoreValue then world.numWinningHits.

Game Over MethodGame Over Method

Page 19: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Click on baseballBat in the object tree and drag the roll method into the If statement. Select right then other and type 1.25 revolutions.

A text object called gameOverText is included in the world, and it has already been set to say “¡Felicidades!”. Go to its properties tab, then drag isShowing into the If statement and select true. Set the duration to 0 seconds. The explode method has already been written to make the piñata burst. Find explode in the world methods and drag it into the If statement.

Game Over MethodGame Over Method

If you don’t like If you don’t like “¡Felicidades!”“¡Felicidades!”(which means (which means “Congratulations!” “Congratulations!” in Spanish), you in Spanish), you can change the can change the text and color in text and color in the gameOverText the gameOverText properties. properties.

Page 20: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Now let’s create the animation for when the player loses. Drag a Do together into the Else.

Find baseballBat in the object tree, and drag the roll method into the Do together, then select right and .25 revolutions. Now make a copy of this line and change the object to the piñata.

Game Over MethodGame Over Method

Page 21: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Now we need to change the text for when the player loses. Click on the gameOverText and find its properties. Drag text into the Else statement under the Do Together, then set it to “Que lastima!” with a duration of 0 seconds. “¡Qué lástima!” is “What a shame!” in Spanish.

See the next page for how to type accents

Next, drag in color and set it to light gray with a duration of 0 seconds. In the final step, drag isShowing into the Else, select true, and set the duration to 0 seconds.

Game Over MethodGame Over Method

Page 22: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Mac:

á = Option + e, then aé = Option + e, then eí = Option + e, then ió = Option + e, then oú = Option + e, then uñ = Option + n, then n¡ = Option + 1¿ = Option + Shift + ?

PC: (use the keypad and make sure you have num lock on)*á = Alt + 0225é = Alt + 0233í = Alt + 0237ó = Alt + 0243ú = Alt + 0250ñ = Alt + 0241¡ = Alt + 0161¿ = Alt + 0191

These are just some of the many different foreign characters you These are just some of the many different foreign characters you can create. Try searching for “keyboard accent codes” to find can create. Try searching for “keyboard accent codes” to find more. more.

Typing AccentsTyping AccentsNow add accents so the text says “¡Qué lástima!”

*On some laptops you will have to press the fn key and use the blue numbers

Page 23: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Now we need to call the three methods we have made in my first method. Click on the my first method tab, then find the new methods and drag them in.

My First MethodMy First Method

Do this for the setUp, playGame, and gameOver methods. This is all that we will include in my first method.

Change the first event back to when world starts, do my first method

Page 24: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

If you try playing the game, you will see that the piñata swings across the screen but there is no way to hit it. Create a new world method called batHit. This method will turn the bat toward the piñata, check to see if the piñata has been hit, then turn the bat back.

Bat Hit MethodBat Hit Method

Drag an If/Else statement into the batHit method. Then find the world functions and drag a>b into the If condition, then select timer.timeLeft from expressions and 0.

Click on the baseballBat and drag its turn method into the If statement. Select backward and ¼ revolution, then change the duration to .3 and the style to abruptly.

Page 25: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Make a copy of the turn method, and change its direction to forward. Now drag another If/Else statement between the two turn methods.

Bat Hit MethodBat Hit Method

Next find Wait at the bottom and drag it below the second turn method, still inside the If statement. Select other and type .3 seconds.

Our piñata will be turning during a 1 second period. Try Our piñata will be turning during a 1 second period. Try experimenting with the durations in this method to make it experimenting with the durations in this method to make it easier or harder to hit.easier or harder to hit.

Page 26: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Now we want to check that the bat has hit the piñata. Go to the world functions, find both a and b, and drag it into the inner If condition.

Bat Hit MethodBat Hit Method

Drag a<b into both parts and choose any values.

Click on baseballBat and go to its functions tab. Drag baseballBat distance to the left of into the first number and choose piñata. Then drag baseballBat distance to the right of into the third number and choose piñata.

Page 27: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Now click on piñata in the object tree and find its functions. Drag piñata’s width into the two remaining spots.

Bat Hit MethodBat Hit Method

But since the measurement is from the center, we must divide piñata’s width by 2. Click the arrow next to it and select math, piñata’s width /, then 2. Do this for both places where you dragged piñata’s width.

Page 28: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

To finish the method, click on score in the object tree, then drag the addPoints method into the If statement. This will give the player points only when the bat has hit the piñata.

Bat Hit MethodBat Hit Method

Page 29: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Now we need to create two events: one to recognize when the player wins and another to swing the bat when the player presses the up key.

Click create new event in the top of the window, and select While something is true.

EventsEvents

Right click on the blue area of the new event and select change to then When something becomes true.

Page 30: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Go to the world functions and drag a>=b into the condition. Select score.scoreValue from expressions then world.numWinningHits from expressions.

EventsEvents

Click on timer in the object tree then go to the properties tab. Drag timeLeft into the do Nothing and select set value to and choose 0. This will end the game when the player reaches the required number of hits.

Page 31: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Now click create new event and select When a key is typed. Change any key to Up. Then click the arrow next to Nothing and change it to batHit.

EventsEvents

Page 32: Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July 2009

Finishing UpFinishing Up

Now you are ready to play the game! Click play and try your best to break the piñata.

Keeping track of a score Keeping track of a score can be used in many can be used in many games. Some games games. Some games require a certain score to require a certain score to reach the next level. Others reach the next level. Others may keep track of the may keep track of the players’ high scores.players’ high scores.

You can create different You can create different events and methods to events and methods to update your score. All you update your score. All you need to do is keep track of need to do is keep track of your score and update its your score and update its display.display.