I am working on an action platformer using Pygame.
My player sprite has three melee attacks. Let's call them SLASH_1, SLASH_2, and SLASH_3. Each of these attacks has, let's say, 2 animation frames, the same hit box, and results in the same amount of damage.
I want the mechanics of my game to be such that if the user presses the attack button three times in quick succession, the sprite will do each attack in turn (regardless of whether there's an enemy around). For an example of what I'm talking about, see here.
I have currently implemented this in the following way:
I have an empty list called "attack_queue" into which I insert attack animation frames when the user presses the attack button. If they press it quick enough, the second and third attacks are added into the list. On each iteration of my game loop, I animate the first frame of the attack_queue (if it's non-empty). The animation code also generates hit boxes for each attack as well as checks for whether the attacks land on an enemy. Once a frame has been animated, it is removed from the attack_queue.
Here's some pseudo-code to give you a better idea:
...
now = get_current_time()
attack_queue = []
if attack_button pressed:
if now - last_attack > 50 ms:
n = 0
add attack_animations[n] to attack_queue
last_attack = now
else if now - last_attack < 50 ms:
n += 1
add attack_animations[n mod 3] to attack_queue
last_attack = now
animate(attack_queue)
...
This works okay for the time being, but I doubt it's the most elegant way of implementing attack chains. (One problem I am having is that this method results in a bit of input lag, since the player is able to press the attack button a third time before the second attack animation has finished.)
My questions are:
- In general, what are some other methods of implementing attack chains?
- Specifically for Pygame, what are the best ways of implementing attack chains?
A similar question is addressed on Reddit here, but I am looking for a more fully fleshed out answer.