I am creating a game called Dodger using the pygame module where the player must avoid hitting enemy sprites. I have more to implement in the game though, such as boosts. But the problem within my game is that the newly spawned sprites are close together.
How can I implement such a solution in the form of code?
Dodger: Current project
import random
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("DODGER - The game")
font = pygame.font.SysFont('comicsans', 30, True)
clock = pygame.time.Clock()
class Enemies(object): # TODO Implement a way to spawn multiple enemy sprites
def __init__(self, x, y, height, width, color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
def move_sprites(self, screen):
pass
def draw_char(self, screen):
pygame.draw.rect(screen, self.color, (self.x, self.y, self.height, self.width))
sprite_count = random.randint(10, 15)
enemies = []
for i in range(sprite_count): # Spawns multiple enemies
new_enemy = Enemies(random.randint(10, 100), random.randint(20, 100), 48, 48, (255, 255, 0))
enemies.append(new_enemy)
def blit(): # Make everything appear here
for enemy in enemies:
enemy.draw_char(screen)
def main():
run = True
while run:
clock.tick(160)
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
blit()
pygame.display.update()
main()
As you can see in my Dodger game, the yellow hot mess at the top left of the screen are a bunch of enemy sprites. They aren't moving yet because the code is unfinished.