Reputation: 1100
what i am trying to achieve I am attempting to get the enemy to do a set of movements with a picture swap that are timed and in a ordered sequence that repeats.
the timing is not doing milliseconds?
what is happening After a long time the enemy does first movement then long pause images some times swap sometimes not.
I am not getting any exceptions or errors when I run it.
Also when I add the userevent into the game loop above the keydown events I break the player movement.
Github to images and full code Below is the most successful of the things I tried so far
I created the desired numbered USEREVENT in ordered sequence
# Create a bunch of AI events
ai_uppercut_event = pygame.USEREVENT + 1
ai_l_punch_event = pygame.USEREVENT + 2
ai_r_punch_event = pygame.USEREVENT + 3
ai_r_dodge_event = pygame.USEREVENT + 4
ai_l_dodge_event = pygame.USEREVENT + 5
Gave the enemy ai character a start position
enemy_x = 235
enemy_y = 145
gave the enemy ai a movement amount
aimovex = 40
aimovey = 40
defined the images to swap out for the different moves
# Define ai images for different moves
enemy_default = pygame.image.load("enemy_default.png")
ai_uppercut_image = pygame.image.load("enemy_uppercut.png")
ai_l_punch_image = pygame.image.load("enemy_left.png")
ai_r_punch_image = pygame.image.load("enemy_right.png")
ai_r_dodge_image = pygame.image.load("enemy_dodge_right.png")
ai_l_dodge_image = pygame.image.load("enemy_dodge_left.png")
set up the clock and tick and timer for events
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
start_time = pygame.time.get_ticks()
# set timer for the ai movement events
pygame.time.set_timer(ai_uppercut_event, MOVE_UPPERCUT)
pygame.time.set_timer(ai_l_punch_event, MOVE_PUNCH_LEFT)
pygame.time.set_timer(ai_r_punch_event, MOVE_PUNCH_RIGHT)
pygame.time.set_timer(ai_r_dodge_event, MOVE_DODGE_RIGHT)
pygame.time.set_timer(ai_l_dodge_event, MOVE_DODGE_LEFT)
in the main event loop
# -------- Main Program Loop -----------
paused = False
running = True
while running:
# --- Main Event Loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
for event in pygame.event.get():
if event.type == ai_uppercut_event:
aimovex =+ 80
activeEnemyImage = ai_uppercut_image
elif event.type == ai_l_punch_event:
aimovex =+ 80
activeEnemyImagee = ai_l_punch_image
elif event.type == ai_r_punch_event:
aimovex =- 80
activeEnemyImage = ai_r_punch_image
elif event.type == ai_r_dodge_event:
aimovey =+ 80
activeEnemyImage = ai_r_dodge_image
elif event.type == ai_l_dodge_event:
aimovey =- 80
activeEnemyImage = ai_l_dodge_image
enemy_x += aimovex
enemy_y += aimovey
#elif event.type == reloaded_event:
# when the reload timer runs out, reset it
#reloaded = True
#pygame.time.set_timer(reloaded_event, 0)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
if event.key == pygame.K_SPACE:
paused = not paused
# --- Event Processing ---
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
movex =- 40
activeCharacterImage = player_left
elif event.key == pygame.K_RIGHT:
movex =+ 40
activeCharacterImage = player_right
Upvotes: 0
Views: 436
Reputation: 104712
I suspect your issue has to do with your doubled event polling loops:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
for event in pygame.event.get():
if event.type == ai_uppercut_event:
# ...
Because there are two loops, you'll often have your events lost to the first loop which only checks if they're QUIT
events and ignores all other kinds of events.
To fix this, combine the two loops and use an elif
for the first non-QUIT
event check:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == ai_uppercut_event:
# ...
Upvotes: 1