How do I stop these 2 enemy types from having the same speed when they should have different speeds?

  Kiến thức lập trình

I am trying to start adding enemy types to my game, so I thought I would start with a fast enemy. I tried to make the fast enemies have a higher speed than the normal ones,. but it ended up making both of them faster.

this is the code i used:

    for enemy in enemies:
        enemy.move_in_direction(3)
        enemy.animate()
        
        if roll > 950:
            bullet = Actor('enemy_bullet')
            bullet.x = enemy.x
            bullet.y = enemy.y
            bullet.angle = random.randint(0, 359)
            enemy_bullets.append(bullet)
            
        if enemy.y > HEIGHT or game_state == "game_over":
            enemies.remove(enemy)
            
    for fastenemy in enemies:
        fastenemy.move_in_direction(6)
        
        if fastenemy.y > HEIGHT or game_state == 'game_over':
            enemies.remove(fastenemy)

if you need it, I can show the code I used for the enemy generation too.

2

The problem is that you are iterating over the exact same enemy array and moving the exact same enemies. Try adding some sort of attribute to the enemy class that either predefines its speed or alternatively have attribute that defines it as a fast enemy e.g

self.fast = True

then inside your for loop have an if statement that checks if self.fast is true and then move it at a faster speed.

New contributor

Whatevermeen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

How do I stop these 2 enemy types from having the same speed when they should have different speeds?

I am trying to start adding enemy types to my game, so I thought I would start with a fast enemy. I tried to make the fast enemies have a higher speed than the normal ones,. but it ended up making both of them faster.

this is the code i used:

    for enemy in enemies:
        enemy.move_in_direction(3)
        enemy.animate()
        
        if roll > 950:
            bullet = Actor('enemy_bullet')
            bullet.x = enemy.x
            bullet.y = enemy.y
            bullet.angle = random.randint(0, 359)
            enemy_bullets.append(bullet)
            
        if enemy.y > HEIGHT or game_state == "game_over":
            enemies.remove(enemy)
            
    for fastenemy in enemies:
        fastenemy.move_in_direction(6)
        
        if fastenemy.y > HEIGHT or game_state == 'game_over':
            enemies.remove(fastenemy)

if you need it, I can show the code I used for the enemy generation too.

2

The problem is that you are iterating over the exact same enemy array and moving the exact same enemies. Try adding some sort of attribute to the enemy class that either predefines its speed or alternatively have attribute that defines it as a fast enemy e.g

self.fast = True

then inside your for loop have an if statement that checks if self.fast is true and then move it at a faster speed.

New contributor

Whatevermeen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT