0

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.

colappse
  • 71
  • 5
  • So, what is wrong with the new enemies being close together? You say you will be getting them to move in the future, so they might be able to move apart then. – quamrana Jun 12 '21 at 09:25
  • `Enemies(random.randint(10, 582), random.randint(20, 422), 48, 48, (255, 255, 0))` instead of `Enemies(random.randint(10, 100), random.randint(20, 100), 48, 48, (255, 255, 0))`? – Rabbid76 Jun 12 '21 at 09:25
  • I think you're right @quamrana, but once I implement a way for the enemy sprites to move they might all move in the same direction, Maybe using a `random.randint` function to make them move either left to right might fix the problem? – colappse Jun 12 '21 at 09:29
  • If you can get all the enemies to move in random directions, then they will naturally move apart. So you have a choice of how close they should start together (see the comment from @Rabbid about choosing a larger random space) and how they should move afterwards. – quamrana Jun 12 '21 at 09:33
  • Thank you for the tip @quamrana – colappse Jun 12 '21 at 09:36

0 Answers0