Teks tutorial
Step 1: Create the sprites and backdrop
The first thing we’ll need to do is create all the sprites and the backdrop we need for the game. Let’s start by creating the backdrop. For the soccer field we can choose a plain green backdrop, then draw a white line and circle down the middle. After the backdrop is complete it will look like this.
Next, it’s time to create the sprites. In this example, we’ll use two circles to represent the players. Make sure that the players look different from one another to make them easy to identify. Next, choose a sprite to act as the soccer ball. We also need to create some soccer nets. Create these by drawing a box, then filling it in with a grid to act as a net. Because both nets will be the same, we can duplicate this sprite to make the second net. Once completed the sprites and backdrop should look like this
Step 2: Code the player sprites
Once we've created our sprites, we can get on to creating the code for each player’s sprite. We will begin by coding the left player. Start by taking a “when I receive” block and create a new message called position. Under this message, create two new variables to represent the player’s velocity in the X and Y directions, and set them both to 0. In this case, the variables are titled “x vel” and “y vel”. These variables will later be used to control the ball’s motion throughout the game. Then, make the sprite go to (-200, 0). This will be the starting point for each ball; 200 pixels away from the center of the field. Next, we will use a “when I receive” block and create a message called “start” and add a forever loop. Now it’s time to add the code that will make the players move. Start by setting the X velocity to itself times 0.85. Then, use a motion block to change x by the x velocity. Hint: You can control the speed that the players move at by changing what the velocity is multiplied by. Higher numbers make the game faster and lower ones slow it down.
Step 3: Code the goal message
Now that the player sprites have been created, it’s time to create the code for the goal sprite. Start by dragging a green flag block and a “when I receive” block into the workspace. Underneath the green flag block, broadcast the position and start messages. This will cause all the sprites to go to their correct locations as well as reset the players’ x and y velocities. Next, create two new variables to act as scores for each player. In this case, they’re called “red score” and “blue score”. Finally, attach a hide block to the end to make sure that the sprite is hidden when the game first starts. Now it’s time to move on to the “when I receive” block. Start by creating a new message called “goal” that will be broadcasted whenever a goal is scored. Next, add blocks to show the sprite, wait 1 second, then hide it again. Finally, broadcast the position and start messages again to reset all the sprites to their original positions. Once this step is done the code should be.
Step 4: Code the soccer ball sprite
Once the player and goal sprites have been completed we can move on and code the ball sprite. Start by taking a “when I receive position” block and underneath set both the x and y velocities to 0. This will stop the ball from moving until hit by a player. Then, use a “go to “block to set the ball’s starting location to (0, 0). Next, bring out a “when I receive” block and select the start message.
Add a forever loop and an “if on edge bounce” block underneath. This will ensure that the ball stays in view the entire game. Next, set the X velocity to itself multiplied by 0.95 and change the ball’s x position by the x-velocity. Then do the same thing for the y velocity and position. At this point the code will l ok like this.Finally, all that’s left to do is make the goal code for the right player. Start by duplicating all of the code that is inside of the forever loop. Place the copied code inside the forever loop, under the code we already completed. Then, simply change the “if touching” statement to check if the ball is touching the right goal and make sure that the left player’s score will increase at the end. After this step is complete, the project is complete and the code should look like this.
Komentar
Posting Komentar