Building keyboard controls

Let's get that kart moving! In the previous project, we learned how to control a game using the mouse. In this section, we will control the game using the keyboard.

Prepare for lift off

The controls for this game will be divided between the kart sprite and a separate control sprite. The kart sprite will be attached to this control sprite. The kart sprite will only concern itself with displaying a pretty and logical image. It tells the player where the kart is on the track and in which direction it is facing. The control sprite will manage all things "under the hood", such as movement speed and collision checks.

Separating these two functions allows us to have the imagery behave somewhat independently from the game rules and mechanics. It allows more room for animation and special effects without hindering the continuation of the game. It also allows easy replacement of the graphics altogether. One controller could "wear" different sprite costumes at any given time; for example, to represent a different character in the game.

Engage thrusters

Let's first create the control object. This is a sprite just like any other:

  1. Create a new sprite with the Paintbrush icon.
  2. Name the sprite player1.
  3. Draw a small red circle with the Ellipse tool. Remember to hold Shift for a perfect circle.
    Engage thrusters

We'll write a script for the control sprite next:

  1. Start with a when <green flag> clicked block.
  2. Attach a set size to () % block. Fill in the value 20 to make the circle very small. We should barely be able to see it, as long as the computer can still check collisions with it later.
  3. Next, add the set <ghost> effect to () % block (with the value 100) to make the control sprite completely transparent. It is still there and will interact with all of the other objects on the stage. It has just become invisible.
  4. Place the control sprite at its starting position with a go to x: () y: () block. Aim for the first starting bracket at the right side of the track. I used the values 192 and -27 respectively.
  5. Insert a point in direction () block with value 0 or straight up. The control sprite is now ready to start moving. To move forward, we will use a speed variable that will make the kart move automatically.
  6. Navigate to Make a variable and make a variable named speed1.
  7. Grab a forever block and place it underneath the script we already wrote.
  8. Inside the loop, place an if () then block. We will use this to check if we have reached top speed.
  9. Inside the condition slot, place a speed1 < () block (with the value 4).
  10. If the speed is not yet 4, we will increase it using the change <speed1> by () block (with the value 0.2).
  11. Attach a move () steps block underneath the if () then block and place the speed1 variable in the vacant slot to set the kart in motion.
  12. Add a wait () secs block (with the value 0.1) to keep the kart under control. It responds way too fast if the script is not reduced by a bit.

The following screenshot shows the final script:

Engage thrusters

The player will be able to steer left and right with the keyboard keys:

  1. Get two key () pressed blocks and place them next to each other.
  2. Select the d key for one block.
  3. Underneath it, attach a turn <clockwise> () degrees block; fill in the value 6.
  4. Select the a key for the other block.
  5. Attach a turn <counterclockwise> () degrees block to it; fill in the value 6.

The following screenshot shows the final script:

Engage thrusters

That's all you need to do to make the control sprite turn left and right. Now, we need to attach the kart sprite, so we can actually see where the control sprite is going:

  1. Drag-and-drop both key control scripts onto the kart sprite to copy them there.
  2. Click on the kart sprite to see its Scripts view.
  3. Before we go on with the scripts, we need to change a sprite property, so we press the i icon on the sprite.
  4. We change the rotation style to no rotation; this option is shown as a blue dot.
    Engage thrusters
  5. This makes sure the sprite isn't responding directly to our rotation input. Instead of actually rotating the sprite, we are going to change its costume to correspond with the current angle.
  6. Next to the key control scripts, start a new script with when <green flag> clicked.
  7. Attach the Set size to () % block and fill in 50.
  8. Now add the point in direction () block with value 0 or up. This synchronizes the kart sprite direction with the control sprite direction.
  9. Add a forever loop.
  10. Make the kart sprite go to player1 using go to <player1>. As long as the loop runs, the kart sprite will follow the control sprite around.

We need a formula to calculate which costume to show. Each costume should correspond to a certain range in direction. We can convert the number for each direction into the number for each costume as follows:

  1. Place a switch costume to () block inside the forever loop.
  2. Put a () + () operator in the slot; the second empty slot of the operator block has to be filled in with 1.
  3. The first empty slot of the operator block is filled with another operator: () / ().
  4. Place the direction variable from the Motion category in the left slot.
  5. Type the number 17.14286 in the right slot. This is approximately 360 degrees divided by 21 or the range in degrees that each costume should occupy.

The following screenshot shows the final script:

Engage thrusters

Objective complete – mini debriefing

We now have a controllable game character. Start the game by clicking on <green flag> and see the kart move forward. Steer the kart around the stage with the A and D keys. If you set up the scripts correctly, the kart sprite will change costumes as you change direction. Drive around the map to check whether the sprite animates properly.

Classified intel

Note the difference between using the ghost effect and hiding a sprite. When you use the ghost effect, a sprite is invisible but present. It will still interact with the other sprites on the stage. So, you can check collisions with an invisible ghost object.

When you hide a sprite, it is actually removed from the stage. Other objects can't collide with it anymore. This might be useful if you need an object only in a part of your program. You can show it when it's needed, and when it's not needed, you hide it to prevent it from interrupting any other processes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.163.229