Skip to content
Advertisement

How to spawn sprites from multiple sides?

I´m creating simple game where I play as aeroplane and i´m dodging rockets (rockets appear from right side), I did something like barrier which block enemies and kill them, but it´s too OP, so I want to nerf it, so I´m going to spawn enemies from both, right side, and top side.

But I really don´t know how to spawn them from top.

I would really appreciate any help, thanks.

P.S. class named Enemy should spawn enemies from right (this is working correctly) class AnotherEnemy should spawn enemies from top (this is not working correctly) My code:

from pygame.locals import(
    RLEACCEL,
    K_LEFT,
    K_DOWN,
    K_UP,
    K_RIGHT,
    K_ESCAPE,
    K_SPACE,
    K_w,
    K_a,
    K_s,
    K_d,
    KEYDOWN,
    QUIT,

)

SCREEN_WIDTH = 700
SCREEN_HEIGHT = 700

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super(Player, self).__init__()
        self.surf = pygame.image.load('C:Spritesjet.png').convert()
        self.surf.set_colorkey((255, 255, 255), RLEACCEL)
        self.rect = self.surf.get_rect()

    def update(self, pressed_keys):
        if pressed_keys[K_w]:
            self.rect.move_ip(0, -8)
        if pressed_keys[K_s]:
            self.rect.move_ip(0, 8)
        if pressed_keys[K_a]:
            self.rect.move_ip(-8, 0)
        if pressed_keys[K_d]:
            self.rect.move_ip(8, 0)

        if pressed_keys[K_UP]:
            self.rect.move_ip(0, -8)
        if pressed_keys[K_DOWN]:
            self.rect.move_ip(0, 8)
        if pressed_keys[K_LEFT]:
            self.rect.move_ip(-8, 0)
        if pressed_keys[K_RIGHT]:
            self.rect.move_ip(8, 0)

        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.right > SCREEN_WIDTH:
            self.rect.right = SCREEN_WIDTH
        if self.rect.top <= 0:
            self.rect.top = 0
        if self.rect.bottom >= SCREEN_HEIGHT:
            self.rect.bottom = SCREEN_HEIGHT

class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        super(Enemy, self).__init__()
        self.surf = pygame.image.load('C:Spritesmissile.png').convert()
        self.surf.set_colorkey((255, 255, 255), RLEACCEL)
        self.rect = self.surf.get_rect(
            center =(
                random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
                random.randint(0, SCREEN_HEIGHT),
            )
        )
        self.speed = random.randint(5, 17)

    def update(self):
        self.rect.move_ip(-self.speed, 0)
        if self.rect.right < 0:
            self.kill()

class AnotherEnemy(pygame.sprite.Sprite):
    def __init__(self):
        super(AnotherEnemy, self).__init__()
        self.surf = pygame.image.load('C:Spritesmissile2.png').convert()
        self.surf.set_colorkey((255, 255, 255))
        self.rect = self.surf.get_rect(
        center =(
                random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
                random.randint(0, SCREEN_HEIGHT)
            )
        )
        
        self.speed = random.randint(8, 17)

    def update(self):
        self.rect.move_ip(0, self.speed)
        if self.rect.bottom > SCREEN_HEIGHT:
            self.kill()




class Cloud(pygame.sprite.Sprite):
    def __init__(self):
        super(Cloud, self).__init__()
        self.surf = pygame.image.load('C:Spritescloud.png').convert()
        self.surf.set_colorkey((0, 0, 0), RLEACCEL)

        self.rect = self.surf.get_rect(
            center =(
                random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
                random.randint(0, SCREEN_HEIGHT),
            )
        )

    def update(self):
        self.rect.move_ip(-6, 0)
        if self.rect.right < 0:
            self.kill()

class Sun(pygame.sprite.Sprite):
    def __init__(self):
        super(Sun, self).__init__()
        self.surf = pygame.image.load('C:Spritessunnn.png').convert()
        self.surf.set_colorkey((0, 0, 0), RLEACCEL)

        self.rect = self.surf.get_rect()

class Ballon(pygame.sprite.Sprite):
    def __init__(self):
        super(Ballon, self).__init__()
        self.surf = pygame.image.load('C:Spritesgula.png').convert()
        self.surf.set_colorkey((0, 0, 0), RLEACCEL)
        self.rect = self.surf.get_rect(

            center =(
                random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
                random.randint(0, SCREEN_HEIGHT),
            )
        )
    
    def update(self):
        self.rect.move_ip(-6, 0)
        if self.rect.right < 0:
            self.kill()

class Barrier(pygame.sprite.Sprite):
    def __init__(self):
        super(Barrier, self).__init__()
        self.surf = pygame.image.load('C:Spritesshield.png').convert()
        self.surf.set_colorkey((0, 0, 0), RLEACCEL)
        self.rect = self.surf.get_rect()
    
    def update(self):
        
        if pressed_keys[K_w]:
            self.rect.move_ip(0, -8)
        if pressed_keys[K_s]:
            self.rect.move_ip(0, 8)
        if pressed_keys[K_a]:
            self.rect.move_ip(-8 , 0)
        if pressed_keys[K_d]:
            self.rect.move_ip(8 , 0)

pygame.init()

pygame.display.set_caption('Simple game')

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

ADDENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(ADDENEMY, 250)

ADDANOTHERENEMY = pygame.USEREVENT + 4
pygame.time.set_timer(ADDANOTHERENEMY, 250)

ADDCLOUD = pygame.USEREVENT + 2
pygame.time.set_timer(ADDCLOUD, 3100)

ADDBALLON = pygame.USEREVENT + 3
pygame.time.set_timer(ADDBALLON, 4500)

enemy = Enemy()
anotherenemy = AnotherEnemy()
player = Player()
barrier = Barrier()
sun = Sun()
ballon = Ballon()

enemies = pygame.sprite.Group()
anotherenemies = pygame.sprite.Group()
clouds = pygame.sprite.Group()
ballons = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
all_sprites.add(sun)
all_sprites.add(enemy)
all_sprites.add(anotherenemy)

Time = 0

clock = pygame.time.Clock()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.USEREVENT:
            Time += 0.25

        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
            
            elif event.key == K_SPACE:
                all_sprites.add(barrier)
                
        elif event.type == QUIT:
            running = False

        


        elif event.type == ADDENEMY:
            new_enemy = Enemy()
            enemies.add(new_enemy)
            all_sprites.add(new_enemy)

        elif event.type == ADDANOTHERENEMY:
            new_anotherenemy = AnotherEnemy()
            anotherenemies.add(new_anotherenemy)
            all_sprites.add(new_anotherenemy)

        elif event.type == ADDCLOUD:
            new_cloud = Cloud()
            clouds.add(new_cloud)
            all_sprites.add(new_cloud)

        elif event.type == ADDBALLON:
            new_ballon = Ballon()
            ballons.add(new_ballon)
            all_sprites.add(new_ballon)

        elif Time < -10000000:
            running = False
    

    clock.tick(30)
    screen.blit(player.surf, player.rect)

    pressed_keys = pygame.key.get_pressed()
    player.update(pressed_keys)

    anotherenemies.update()
    enemies.update()
    clouds.update()
    ballons.update()
    barrier.update()

    screen.fill((135, 206, 250))

    for entity in all_sprites:
        screen.blit(entity.surf, entity.rect)
        pygame.display.flip()

    if pygame.sprite.spritecollideany(player, enemies):
        player.kill()
        running = False
    collided_enemy = pygame.sprite.spritecollideany(barrier, enemies)
    if collided_enemy != None:
        collided_enemy.kill()
        Time -= 25

    collided_anotherenemy = pygame.sprite.spritecollideany(barrier, anotherenemies)
    if collided_anotherenemy != None:
        collided_anotherenemy.kill()

    counter, text = str(Time), '                       Score: ' + str(Time)
    pygame.time.set_timer(pygame.USEREVENT, 1000)
    font = pygame.font.SysFont('Consolas', 30)
    
    

    screen.blit(font.render(text, True, (0, 0, 0)), (32, 48))
    pygame.display.flip()


pygame.quit()

Advertisement

Answer

You just have to adjust your position and speed logic to have the movement and variable part of the position be in the y component. Because the top of the screen is 0, you have to start at a negative y value and move in positive steps instead of negative. You change the kill check to kill the enemy when it goes off the bottom of the screen (> screen height). Here’s an enemy definition that comes from the top of the screen and moves down:

class AnotherEnemy(pygame.sprite.Sprite):
    def __init__(self):
        super(AnotherEnemy, self).__init__()
        self.surf = pygame.image.load(image3).convert()
        self.surf.set_colorkey((255, 255, 255))
        self.rect = self.surf.get_rect(
            center=(
                random.randint(0, SCREEN_WIDTH),
                -100
            )
        )

        self.speed = random.randint(8, 17)

    def update(self):
        self.rect.move_ip(0, self.speed)
        if self.rect.top > SCREEN_HEIGHT:
            self.kill()
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement