我想要实现的目标 我试图让敌人做一组带有图片交换的动作,这些动作是按时间顺序重复的有序序列。
时间不是毫秒?
正在发生的事情 经过很长一段时间,敌人先行动,然后长时间暂停图像,有时不交换。
运行时我没有遇到任何异常或错误。
此外,当我将userevent添加到keydown事件之上的游戏循环中时,我打破了玩家的移动。
Github to images and full code 以下是迄今为止我尝试过的最成功的事情
我按顺序
创建了所需编号的USEREVENT# 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
给敌人ai角色一个开始位置
enemy_x = 235
enemy_y = 145
给敌人一个移动量
aimovex = 40
aimovey = 40
定义要换出不同动作的图像
# 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")
设置时钟和事件的计时器和计时器
# 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)
主事件循环中的
# -------- 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
答案 0 :(得分:1)
我怀疑你的问题与你的双重事件轮询循环有关:</ p>
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:
# ...
因为有两个循环,所以你经常会把你的事件丢失到第一个循环,它只检查它们是QUIT
个事件并忽略所有其他类型的事件。
要解决此问题,请合并两个循环并使用elif
进行第一次非QUIT
事件检查:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == ai_uppercut_event:
# ...