So we've learned about the Player Class, and an Enemy Class and how to add our player to the screen.
In this lesson, we're going to add a Custom Event to add Sprites to our game screen so that they appear at set intervals.
Let's create a new Repl and name it PGD_13_Custom_Events.
So in order to have a fun game, we want our enemies to appear at regular intervals. So how could we do that? So in the Game Loop, there's already code that handles random Events for key down or quit. It's called the Event Loop. The Event Loop is designed to look for Random Events occurring every frame, and then deal with them appropriately. Lucky for us, Pygame lets us define our own Events.
We're going to do this in three steps. The first step is creating a new enemy. Let's create a Custom Event that's generated every few seconds, call it ” add_enemy”. Now let make the Event unique. PY Games defines Events internally as integers. So we define a new Event with a unique integer. The last Event pi game reserves is called “user_event”. So defining “add enemy” equals pygame.user.event plus 1 ensures it's unique. Make sure to add plus one to it.
Now we need to add this new Event at regular intervals throughout the game. Let's set our Event to go off every 250 milliseconds using the Set Timer Method from the Pygame library. Here's the syntax. Alright, let's add it to the program.
You call Set Timer outside the Game Loop since you only need one timer, but it will fire throughout the entire game. Here's what you should have in your editor. Looking good.
So we created a Custom Event “add_enemy” and then made it unique. And finally we defined it to trigger at set intervals every 250 milliseconds. Now need to integrate it into the main Loop of our game. So what's the best way to do that?
Well, the simplest way to include the Event is to add it to the Conditional Statement already in the main Loop. Find the IF inside the main Loop. Remember that the While Loop will continue until “running” is equal to false at which point the Loop ends.
To include the new Events, please add another ELIF that checks IF the Event type is equal to “add enemy”. Whenever the Event handlers sees the new “add enemy” Event, it creates an enemy and adds it to the enemies and all Sprites group. Since we added an enemy to the all Sprites group, it'll get drawn every frame.
So here's a question for you. What have we forgotte?
You also need to call Enemies.update which updates everything in “enemies” to ensure they move properly. Here's the complete main Loop after adding a new Event. All right! If you have any questions ask your instructor if not, let's move on to COLLISION DETECTION.
The design for our game calls for the game to end whenever an enemy collides with our player. Checking for collision. It's a pretty basic technique of game programming and usually requires a lot of math to determine whether two Sprites will overlap. This is where a framework like Pygame comes in handy. Writing collision detection code is very tedious. But Pygame has a lot of collision detection methods for you to use.
For collision, you'll use a method called Sprite.collide any this Method accepts a Sprite and a Group as Parameters. It looks at every Object in the Group and checks if it's rect intersects with the rect of the Sprite. We use a conditional statement to remove the player if the Method returns false indicating a collision. We
remove the player by using the KILL STATEMENT when the IF Statement returns false. Once the player has been killed, you still need to exit the game. To do this set “running” to false to end the Game Loop.
Alright, here's what the main Loop should look like in your editor.
Please remember, if there were any concepts we covered today that weren't clear to watch the lesson walkthrough where we walk through each line of code and the program and explain why it's there. And what it does. This step by step explanation is a great way to make sure that you understand the syntax, code and logic of the program. All right see you again soon