Ad

How Do You Isolate Enemy Sprites So That They Disappear When Going Into A New Room?

I've tried few methods such as coding the enemies in the room1 class but nothing seems to work, it either doesn't spawn the intended sprite at all or the sprite spawns but doesn't disappear, when I go into other rooms (unless I collide with it). This is my code, any help at all would be much appreciated!

Side note, I've also tried to apply a different background to each room but haven't had any luck either. What needs to be done to implement that?

import pygame
import random


# Define colours

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)


# --- Classes

class Wall(pygame.sprite.Sprite): 
    def __init__(self, x, y, width, height, color):
        super().__init__()

        self.image = pygame.Surface([width, height])
        self.image.fill(color)

        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x



class Player(pygame.sprite.Sprite):

    change_x = 0
    change_y = 0

    def __init__(self, filename, x, y):
        super().__init__()

        self.image = pygame.image.load(filename).convert()
        self.image.set_colorkey(BLACK)

        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x

    def changespeed(self, x, y):
        self.change_x += x
        self.change_y += y

    def move(self, walls):
        self.rect.x += self.change_x

        block_hit_list = pygame.sprite.spritecollide(self, walls, False)
        for block in block_hit_list:
            if self.change_x > 0:
                self.rect.right = block.rect.left
            else:
                self.rect.left = block.rect.right


        self.rect.y += self.change_y

        block_hit_list = pygame.sprite.spritecollide(self, walls, False)
        for block in block_hit_list:

            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            else:
                self.rect.top = block.rect.bottom

class Enemy(pygame.sprite.Sprite):

    def __init__(self, filename):

        super().__init__()    

        self.image = pygame.image.load(filename).convert()
        self.image.set_colorkey(BLACK)
        self.rect = self.image.get_rect()



class ProjectileUp(pygame.sprite.Sprite):
    def __init__(self, filename):
        super().__init__()

        self.image = pygame.image.load(filename).convert()
        self.image.set_colorkey(BLACK)

        self.rect = self.image.get_rect()


    def update(self):
        self.rect.y -= 5


class ProjectileDown(pygame.sprite.Sprite):
    def __init__(self, filename):
        super().__init__()

        self.image = pygame.image.load(filename).convert()
        self.image.set_colorkey(BLACK)

        self.rect = self.image.get_rect()


    def update(self):
        self.rect.y += 5


class ProjectileLeft(pygame.sprite.Sprite):
    def __init__(self, filename):
        super().__init__()

        self.image = pygame.image.load(filename).convert()
        self.image.set_colorkey(BLACK)

        self.rect = self.image.get_rect()


    def update(self):
        self.rect.x -= 5


class ProjectileRight(pygame.sprite.Sprite):
    def __init__(self, filename):
        super().__init__()

        self.image = pygame.image.load(filename).convert()
        self.image.set_colorkey(BLACK)

        self.rect = self.image.get_rect()


    def update(self):
        self.rect.x += 5



class Room(object):
    wall_list = None
    enemy_sprites = None

    def __init__(self):
        self.wall_list = pygame.sprite.Group()
        self.enemy_sprites = pygame.sprite.Group()


class Room1(Room):
    def __init__(self):
        super().__init__()

        walls = [[0, 0, 20, 250, BLACK],
                 [0, 350, 20, 250, BLACK],
                 [780, 0, 20, 250, BLACK],
                 [780, 350, 20, 250, BLACK],
                 [20, 0, 760, 20, BLACK],
                 [20, 580, 760, 20, BLACK]
                ]

        for item in walls:
            wall = Wall(item[0], item[1], item[2], item[3], item[4])
            self.wall_list.add(wall)

        self.enemy_sprites.add(Enemy("Judas.png"))


class Room2(Room):
    def __init__(self):
        super().__init__()

        walls = [[0, 0, 20, 250, BLACK],
                 [0, 350, 20, 250, BLACK],
                 [780, 0, 20, 250, BLACK],
                 [780, 350, 20, 250, BLACK],
                 [20, 0, 760, 20, BLACK],
                 [20, 580, 760, 20, BLACK]
                ]

        for item in walls:
            wall = Wall(item[0], item[1], item[2], item[3], item[4])
            self.wall_list.add(wall)


class Room3(Room):
    def __init__(self):
        super().__init__()

        walls = [[0, 0, 20, 250, BLACK],
                 [0, 350, 20, 250, BLACK],
                 [780, 0, 20, 250, BLACK],
                 [780, 350, 20, 250, BLACK],
                 [20, 0, 760, 20, BLACK],
                 [20, 580, 760, 20, BLACK]
                ]

        for item in walls:
            wall = Wall(item[0], item[1], item[2], item[3], item[4])
            self.wall_list.add(wall)


def main():

    pygame.init()

    screen = pygame.display.set_mode([800, 600])

    pygame.display.set_caption('Labyrinth')

    player = Player("Isaac.png", 50, 50)
    all_sprites_list = pygame.sprite.Group()
    all_sprites_list.add(player)

    enemy_sprites = Enemy("Judas.png")
    enemy_list = pygame.sprite.Group()
    enemy_list.add(enemy_sprites)
    all_sprites_list.add(enemy_sprites)
    enemy_sprites.rect.x = 150
    enemy_sprites.rect.y = 200

    projectile_list = pygame.sprite.Group()

    rooms = []

    room = Room1()
    rooms.append(room)

    room = Room2()
    rooms.append(room)

    room = Room3()
    rooms.append(room)

    current_room_no = 0
    current_room = rooms[current_room_no]

    clock = pygame.time.Clock()



# Load background image
    background_position = [0, 0]
    background_image = pygame.image.load("Floor.png").convert()

    done = False



# ---------- MAIN PROGRAM LOOP ---------- #


    while not done:


# --- Event Processing ---

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

# Controls
            elif event.type == pygame.KEYDOWN:
                projectile = None

                if event.key == pygame.K_UP:
                    projectile = ProjectileUp("LightningUp.png")
                    projectile.rect.x = player.rect.x+10
                    projectile.rect.y = player.rect.y-30
                elif event.key == pygame.K_DOWN:
                    projectile = ProjectileDown("LightningDown.png")
                    projectile.rect.x = player.rect.x+10
                    projectile.rect.y = player.rect.y+50
                elif event.key == pygame.K_LEFT:
                    projectile = ProjectileLeft("LightningLeft.png")
                    projectile.rect.x = player.rect.x-15
                    projectile.rect.y = player.rect.y+33
                elif event.key == pygame.K_RIGHT:
                    projectile = ProjectileRight("LightningRight.png")
                    projectile.rect.x = player.rect.x+35
                    projectile.rect.y = player.rect.y+33
                elif event.key == ord('a'):
                    player.changespeed(-7, 0)
                elif event.key == ord('d'):
                    player.changespeed(7, 0)
                elif event.key == ord('w'):
                    player.changespeed(0, -7)
                elif event.key == ord('s'):
                    player.changespeed(0, 7)

                if projectile:
                    all_sprites_list.add(projectile)
                    projectile_list.add(projectile)

            elif event.type == pygame.KEYUP:
                if event.key == ord('a'):
                    player.changespeed(7, 0)
                elif event.key == ord('d'):
                    player.changespeed(-7, 0)
                elif event.key == ord('w'):
                    player.changespeed(0, 7)
                elif event.key == ord('s'):
                    player.changespeed(0, -7)

        # --- Game Logic ---

        all_sprites_list.update()

        enemy_hit_list = pygame.sprite.spritecollide(player, enemy_list, True)

        for projectile in projectile_list:
            enemy_hit_list = pygame.sprite.spritecollide(projectile, enemy_list, True)

            for enemy in enemy_hit_list:
                projectile_list.remove(projectile)

        player.move(current_room.wall_list)

        if player.rect.x < -15:
            if current_room_no == 0:
                current_room_no = 2
                current_room = rooms[current_room_no]
                player.rect.x = 790
            elif current_room_no == 2:
                current_room_no = 1
                current_room = rooms[current_room_no]
                player.rect.x = 790
            else:
                current_room_no = 0
                current_room = rooms[current_room_no]
                player.rect.x = 790

        if player.rect.x > 801:
            if current_room_no == 0:
                current_room_no = 1
                current_room = rooms[current_room_no]
                player.rect.x = 0
            elif current_room_no == 1:
                current_room_no = 2
                current_room = rooms[current_room_no]
                player.rect.x = 0
            else:
                current_room_no = 0
                current_room = rooms[current_room_no]
                player.rect.x = 0

        # --- Drawing ---

        screen.blit(background_image, background_position)

        all_sprites_list.draw(screen)
        current_room.wall_list.draw(screen)

        pygame.display.flip()

        clock.tick(60)

    pygame.quit()

if __name__ == "__main__":
    main()
Ad

Answer

I think you mean you want to permanently delete the enemy sprites from the enemy_sprites group in the room class. As an example, from room 1, self.enemy_sprites.add(Enemy("Judas.png")). If you wanted to delete that you could just do the same but change .add() to .remove(). So just type self.enemy_sprites.remove(Enemy("Judas.png")) You can only do this if the class that your in also inherits the properties of the Room class.

Hope that helped! :)

Ad
source: stackoverflow.com
Ad