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
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)
# [...]