Is there a better way to spawn enemy locations?

前端 未结 1 437
不思量自难忘°
不思量自难忘° 2021-01-27 04:56

I\'m making a pygame, survive the hord type game. I have it set up to where enemy\'s will walk towards the player. If they hit the player, they take some of his health. if the p

相关标签:
1条回答
  • 2021-01-27 05:28

    Use the application loop to spawn the enemies. Create a random position and try to place the enemy. If the enemy cannot spawn because it would be placed too close to another enemy, skip them and wait for the next frame. Try to spawn enemies as long as the number of enemies is less than the number of enemies requested:

    import math
    
    no_of_enemies = 10
    minimum_distance = 100
    
    # application loop
    while True:
    
        if len(all_sprites_list.sprites()) < no_of_enemies:
    
            # try to spawn enemy  
            x = random.randrange(-200,screen_width+200)
            y = random.randrange(-200,screen_height+200) 
    
            spawn = True
            for enemy in enemy_1pt_list:
                ex, ey = enemy.rect.center
                distance = math.hypot(ex - x, ey - y)
                if distance < minimum_distance: 
                    spawn = False
                    break
    
            if spawn:
                enemy_1pt = Enemy_1pt(fire_drake_image,1.5,1)
                enemy_1pt.rect.center = x, y
                enemy_1pt_list.add(enemy_1pt)
                all_sprites_list.add(enemy_1pt)
    
        # [...]
    
    0 讨论(0)
提交回复
热议问题