arrow_back Play with a Friend option 2
In this game, the computer controls the enemies. But, in this screencast, you’ll program the enemies so they can be controlled by a second player. You should only complete this screencast if you haven’t already programmed the other 2-player option.
You have already completed some of the programming for the sharks. Right now, every time a new clone appears they point in a random direction, then keep moving in whichever direction.
If a shark hits the edge of the screen, it bounces, then keeps moving forward again.
You still want the sharks to act like that, but now you also want them to move when a player presses keys. Often, in two-player games when player 1 uses the arrow keys, player 2 uses the leters W, A, S, and D, with “w” meaning “up,” “a” meaning “left,” “s” meaning “down,” and “d” meaning “right.”
Try programming a single key first, like the “a” key.
First, click on the shark sprite.
Then, select a “when key is pressed” event and change the value to “a.”
When the 'a' key is pressed, the sharks should turn left. So, select a “turn” block from the motion menu.
Before going any further, test your code to make sure it works.
When you press the green flag, the first shark should bounce around the screen, just like it did originally. But, when you press the “a” key, it should turn left. It does! Great.
Now that you created code for turning left and you know that it works, you can use similar code to make the shark turn right.
You can duplicate your block stack to make it easier.
Change the "turn" block to a right turn.
Then, change the key to “d.”
Now, it’s time to test the code again.
Perfect! The shark moves, and when you press “a” it turns left. Press “d,” and it turns right.
As the game progresses, player 2 builds a larger swarm of sharks, and all additional clones are also controlled by the “a” and “d” keys.