![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Erik's Project IdeaGoal:The goal of this project to familiarize yourself with some of the basics of the Squeak language and create a fun little game. We'll create a game where the purpose is to shoot a soccar ball into a goal.In this Project we'll learn about:
ProcedureStep 1) Let's begin by dragging out a playfield out of the Supplies flap into the world and have it cover the left side of the world.![]() Step 2) The next thing we ought to do is create our goal and soccer ball. To make things simple, we should draw them from a bird's eye perspective. When creating the goal you should make the interior of the a different color from the three posts around it to differentiate the different parts of the goal. Name your ball and the goal sketches apropriately. ![]() Step 3) We're going to embed these objects into the world so they don't get away from us. You can do this by going to the red Menu and going down to embed into > Playfield. If it asks if it should create a slot, click no. Step 4) The first script we're going to create will move the ball forward. We can do this by dragging an empty script out from the ball's script panel and give it a useful name like ballmovement. For now we're going to keep this script simple so the main body of this script will just be ball forwardby 5. ![]() Step 5) If we run this script we see that the ball moves forward, we need a way to keep track of the ball and stop it when it gets into the goal. So let's write a script that does that. Let's create a script for the goal called managegoal, this will take care of all the things related to the goal. The first thing that we ought to do is stop the ball when it hits one of the posts in the gaol. Tear off a test and put it into the script. Our test condition will be if the exterior color of the goal sees a color on the ball. This test can be found in the tests panel of the goal. If this happens, then we should pause the ball movement script. ![]() Step 6) An important part of writing programs is to test test things as you create them to make sure things will work correctly in the end. So let's start up both scripts and make sure the ball stops when it is supposed to. Step 7) Now that the ball stops when it is supposed to, let's create a script that will reset the ball's location so we can do this again. To do this we need to figure out where we want to start the ball, so drag it to an appropriate starting location and note the ball's x and y position. Let's create a script that set's the ball's x and y position to these values. ![]() Step 8) Now that we have this basic system setup, let's create a way to keep track of the score. We'll need a variable to keep track of how many goals we've scored. We can do this by going to the goal and clicking on the down arrow on the top of the viewer flap. It'll ask for a name, give it something resonable, like goalscored. We can begin by setting it to 0. In addition whenever the ball is within the goal, we would want to increase the score. So let's drag out a new test and add it to our goal management script. Our test condition should be if the goal's interior color see's the border color of the ball. Under the Yes condition, we should set our goalscored to goalscored+1 ![]() Step 9) Remember how we just talked a little about testing things as you add them in? What happens now when you shoot the ball into the goal? See how the goalscored keeps going up even though the ball only went in once? There's several ways to fix this, however the solution we'll examine is the creation of another variable to see if that ball was in the goal during the last cycle. If the ball was in the goal during the last cycle, we can assume that the ball hadn't left in between our checks, if the ball wasn't in the goal and now is, we know to increment the counter. Create a variable in the same fashion as before and once again give it a useful name. We should also set this variable to be of the Boolean type because we're going to track two things, if it was in the goal or not. Step 10) We're going to need to add another test statement inside the one we added in step 8, so tear one off and put it inside the yes case. Our test condition is the boolean variable that you created in step 9. If this is true (yes) we don't need to do anything since the ball was already in the goal, if it is false (no) that means it used to be outside the goal so now it's inside, we'll increment the goals scored and set the boolean variable to true. We'll also have to set the variable when the ball isn't in the goal. How can we determine that? Remember when we used the interior color of the goal to figure out if the ball was in the goal? The no side of that test would mean that the ball is outside the goal, so under that condition, set the boolean variable to false. ![]() Step 11) Now scoring a goal is a bit too easy, so let's make this more difficult by adding a goalie. First create one with the paint tool, then embed it into the playfield. Now we need to define how this object moves. For now, we'll have it move back and forth randomly. The first half our control script for the goalie will control how it moves. We begin by moving the goalie, then if the goalie steps out of bounds, we'll set it back to the border. To begin let's create the tile to do the goalie's initial movement. ![]() Begin by dragging out the assignment tile for goalie's x and then click on the right arrow twice to create a mathmatical equation. In the first slot, drop in the goalie's x, in the second, change the number to 20 and then for the last tile, we can get a random tile from clicking on the object's name in the script window and going down to "hand me a random number" tile. Drag this tile to the last slot and change the number to 40. In addition you will need to change the operator before this random tile to a minus sign. This combination of tiles will allow the goalie to move back and forth in a range of 20 pixels each step. We also need to set some boundries so the goalie doesn't leave the goal. Let's compare the goalies's position to the left and right sides of the goal. First we'll do the left side, so after the tile you just created, add another test. We're going to see if the goalie's x is less than the goal's left, which would mean the goalie is too far left. If that is the case, we want to move the goalie back to the left side the the goal. In the no condition of the test, we'll add a test to do the same thing for the right side of the goal. The last thing we want to do is have the goalie stop the ball if they come in contact. Use the overlap test condition for this, and if you need help look back to the goal code that stops the ball. Final Script: ![]() Step 12) Another important aspect of computer science is separating specific details from how things are implemented. This process is called abstraction. Often we do this by creating variables or scripts to represent values or common procedures. This way if you need to change something, the change only needs to be made in one place rather than two. One thing that we could apply the concept of abstraction to is the ball's movement and what happens when it needs to get reset. Later on we might want to make the ball's speed change based on some outside factors, or animate the ball moving back to the start location. To begin with, lets give the ball a velocity variable. Now, rather than having the ball move forward by a specific, number have it move forward by the ball's velocity. Let's also define a stopball script that will set's the ball's velocity to 0. Let's run this script instead of pausing the ballmovement script in the goalie and goalmanage scripts. This way we can do other things in the ball movement script before resetting the ball. Talking about the ballmovement script, we need to update it to allow that functionality. Let's add a test condition to the script with the test condition of the ball's velocity=0, if it is, that means we should pause the ballmovement script and reset the ball, otherwise move the ball forward by it's velocity. ![]() Step 13) Now that most of the functionality is in place, let's add an interface to make this easier to use. Let's setup the situation, by creating a new script to control all this. This script should probably be owned by the playfield since we're setting a lot of general behaviors. In this script we want to reset the ball's position, set the score to 0 and start the goal and goalie scripts. We should then create a button that fires this script. You can do that by clicking on the object's name in the script editor and going down to it in the menu. ![]() We should also also create a button to kick the ball. Create a script to set the ball's velocity and start the ballmovement script, then create a button like we did in the previous script. The last thing we might want to display to the user is the number of goals scored. Drag out two text boxes and rename one of them to be goallabel and the other one scoredlabel. Change the text in goallabel to read "Goals:", then drag out scorelabel's numericValue tile from under text and assign goal's goalscored to it. Give this script a name like update score. We'll want to do this script everytime the player scores, so add it to the goal management script. We should also add this to the reset simulation script since the goals scored can change there too. Congratulations! You're done! Extensions) Here are some ideas you might want to try adding to this project.
Link to this Page
|