1

I want to do that enemies spawn random but nothing never happens. My idea was that on some time like 0.1 second there is new random coordinate to spawn enemy but I don't know how to use time.sleep in loop without stopping ALL of the code.

CODE:

import random
import pygame
import sys
pygame.init()
from pygame.locals import*

ran_coordinates_x = random.randint(260, 1160)
ran_coordinates_y = random.randint(260, 730)


def add_enemy(ran_coordinates_x, ran_coordinates_y):
    pygame.draw.circle(screen, (255, 0, 255), (ran_coordinates_x, ran_coordinates_y), 20)


while running:
    digit = random.randint(1, 100)
    if digit < 50:
        add_enemy(ran_coordinates_x, ran_coordinates_y)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
HIpex
  • 75
  • 5
  • Does this answer your question? [How to wait some time in pygame?](https://stackoverflow.com/questions/18839039/how-to-wait-some-time-in-pygame) – mkrieger1 Oct 27 '22 at 19:24
  • 2
    You are generating your coordinates x and y only once at the beginning of your code, and using the same values again and again – Rodrigo Rodrigues Oct 27 '22 at 19:31
  • Yea i know but still nothing is appering. – HIpex Oct 27 '22 at 20:03
  • If you want a time interval between the spawns, then instead of using time.sleep, get the current time in ms or ns when the game starts. Then on each pass of the main loop, get the current time and check the difference. If the difference is large enough, then spawn an enemy and update the reference time to what the current time was.. – nigh_anxiety Oct 27 '22 at 20:51
  • Can you explain in code please. – HIpex Oct 28 '22 at 06:15

1 Answers1

0

You need to call

pygame.display.update()

to show the circle on the screen.

bitflip
  • 3,436
  • 1
  • 3
  • 22