A3 - Keyboard and Collisions
Before you start...
Prior to starting this assignment, please read through the explantion of Positional vs Keyword Arguments on this page.
Run update.bat to download the assignment template.
Simple Collision Detection
There are a few ways op_arcade can handle collisions between sprites.
This is one of the simplest ways.
When we create a sprite, we have the option of adding it to a group, by using the optional group argument.
Here is an example from this assignment's template file:
# Here, we add the coin the the `collectable` group.
self.coin = oa.Sprite(300, 300, cc.Anims.Items.GOLD_COIN, group="collectable")
Then, we add the following code under on_step.
Remember that on_step gets run continuously:
In the first line, if the player is colliding with a collectable, the variable colliding_item becomes a reference it
(basically, a second variable referring to that already existing object).
Otherwise, colliding_item will take on the special value, None.
Next, if there was indeed a collision (and therefore, colliding_item has a value other than None),
we remove that sprite from the game by calling its kill method.
Sensing Key Presses
You'll notice in the template file there is a new section called on_key_press.
The code in this section gets run anytime a key is pressed on the keyboard.
As you will see from the examples, we can use if statements to see what key was pressed and handle the key press as we wish.
Your Task
- Add another sprite, and make it part of the
collectablegroup. - Make the bee part of a group called
enemy. - In
on_step, add code so that if the player collides with an enemy, the player turns red (or does something else of your choosing) - In
on_key_press, add functionality for at least two more key presses.
Feel free to add more features if you'd like.