Games without fair challenges rarely offer me the rejuvenation from my pathetic attempts at self-improvement.
Enemies are often essential elements in gaming that add challenge and excitement to gameplay. In Pygame, creating interactive enemies involves designing their behavior, movement patterns, and interactions with the player.
In classic literatures, the antagonist is a parallel of the protagonist. Reflections of each other with notable similarities and glaring differences. The enemy in our game shares much with our player, but in its design, the former contains many glaring differences of its own.
Our enemy can move like the player, but cannot jump or shoot at all.
When our enemy moves, it animates istelf like our player.
Unlike the player, who we can pilot freely with our keyboard, the enemy moves from its starting point to another on the x-axis, forever repeating this cycle of horizontal movement unless commandeed otherwise.
The enemy right now is merely a walking background piece, but it can do more with extra coding and ideas about player interation.
For flexibility reasons, I recommend referencing existing variables to create hitboxes. You can adjust their values flexibly afterwards.
For debugging purposes, we can build a collision status and time tracker.
If so, we then need to build a hitbox collision function in the enemy class that toggles the hit status and activates the countdown timer for a purpose we will shortly discuss.
The timer's purpose is for this debugging application: when an enemy hitbox overlaps with a projectile, its frame's color will change from red to yellow. After 1000 ticks/milliseconds, it will return to its original color (red). This shows that the enemy's hit function really works.
To track both objects' overlapping, the system treats them as separate rectangles. If the bullet's y- and x-position are within the enemy hitbox's ranges, the enemy class runs the function I just mentioned, is removed from the bullets array, and changes the relevant variable to mark it as a hit enemy.
For a more visual imagination of the hitbox:
Start at the top-left corner (x, y).
Draw a line to the right by width units.
Draw a line down by height units.
Draw a line to the left by width units.
Draw a line up by height units to close the rectangle.
Onto player-to-enemy interactions!
For further balancing and performance concerns, alongside the projectile amount limit, I gave my shooting function cooldown time after each firing.
First, create a variable that houses the cooldown time. My iteration is a 0-tick timer by default.
Every time the shoot function is run, and cooldown time is 0, a bullet can be fired to the right or left. Then, cooldown time has value that will gradually subtract to 0. Until it does, no amount of key-pressing will spawn a bullet into the bullets array (basically no bullets spawn).
BANG, BANG, BANG!