To make the cooperative game work, the characters need to move.
Click on the blue Android sprite to start.
The player will press the right-arrow key to make the sprite move right.
From the events menu, drag out the “when key pressed” block, and select “right arrow” from the dropdown menu.
To make the sprite move when the key is pressed, place the “move 10 steps” block from the “motion” menu under the “when key pressed” block.
Press the right arrow to test your code.
The character moves, but it looks kind of twitchy.
To make the character move more smoothly, make it repeat moving until the right arrow key is no longer pressed.
From the control menu, place the “repeat until” block around the “move 10 steps” block.
The character should keep moving until the right arrow key is not pressed.
From the operators menu, place a “not” operator inside the “repeat until” block.
Then, from the “sensing” menu, place a “key pressed” block inside the “not” operator.
Click the dropdown, and select “right arrow” for the key.
Now, the code reads, “When the right arrow key is pressed, keep moving 10 steps until the right arrow key is not pressed anymore.”
Test the code by pressing the right arrow.
The android moves more smoothly now!
To catch objects in the game, the android needs to move left as well.
The code that makes the android move left is almost the same as what you just created to make it move right.
So, you can copy your code to save yourself time and effort.
To copy your code, right click on the top of the code stack, and click “duplicate.”
A new, identical block stack should appear.
Next, change the names of the keys in the new stack.
Click the dropdown on the “when key pressed” block, and change it from “right arrow” to “left arrow.”
Do the same for the “right arrow” in the condition.
Finally, make the character move left instead of right by changing the 10 in the “move 10 steps” block to a negative 10.
Test your code.
When you press the right arrow, the character should move right, and when you press the left arrow, it should move left.
The other character in the game needs to move as well.
You will program the yellow android the same way you programmed the blue one.
Drag both stacks of code from the blue Android to the yellow Android in the sprites area.
Click on the yellow Android.
It should now have the same code as the blue android.
But you and your friend can’t use the same keys to play!
Typically, when two players are using the same keyboard, one uses the arrows, and the other uses the “w,’’ “a,” ’’s,” and “d” keys because they have a similar configuration to the arrow keys, but are located on the other side of the keyboard.
Click on the dropdowns to change “left arrow” to “a” and “right arrow” to “d” on the “event” and “sensing” blocks.
Test by pressing the “a” and “d” keys.
The yellow Android moves left and right.
Now it’s your turn: Program the blue Android to move right and left using “when key pressed”, “move”, “key pressed,” “not,” and “repeat until” blocks.
Copy both stacks of code to the other character sprite, and make it move when the “a” and “d” keys are pressed.
- Program the blue android to move right and left using "when key pressed," "move," "key pressed," "not," and "repeat until" blocks.
- Copy both stacks of code to the other character sprite, and make it move when the "a" and "d" keys are pressed.