Skip to content
Advertisement

How can I copy a class in Python?

I’m using separate classes to create my characters in my game that I made using pygame. As you can see the classes are almost identical, with the only differences being the self.walkRight and self.walkLeft. So I would like to know a way that I can copy the first class and modify only the things that I want to change. How can I do that? The code:

class enemy(object):
    def __init__(self, x, y, width, height, end):
        self.walkRight = [pygame.image.load('resources/R1ENEMY.png'), pygame.image.load('resources/R2ENEMY.png'),
                          pygame.image.load('resources/R3ENEMY.png'),
                          pygame.image.load('resources/R4ENEMY.png'), pygame.image.load('resources/R5ENEMY.png'),
                          pygame.image.load('resources/R6ENEMY.png'),
                          pygame.image.load('resources/R7ENEMY.png'), pygame.image.load('resources/R8ENEMY.png'),
                          pygame.image.load('resources/R9ENEMY.png'),
                          pygame.image.load('resources/R10ENEMY.png'), pygame.image.load('resources/R11ENEMY.png')]
        self.walkLeft = [pygame.image.load('resources/L1ENEMY.png'), pygame.image.load('resources/L2ENEMY.png'),
                         pygame.image.load('resources/L3ENEMY.png'),
                         pygame.image.load('resources/L4ENEMY.png'), pygame.image.load('resources/L5ENEMY.png'),
                         pygame.image.load('resources/L6ENEMY.png'),
                         pygame.image.load('resources/L7ENEMY.png'), pygame.image.load('resources/L8ENEMY.png'),
                         pygame.image.load('resources/L9ENEMY.png'),
                         pygame.image.load('resources/L10ENEMY.png'), pygame.image.load('resources/L11ENEMY.png')]
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.end = end
        self.path = [self.x, self.end]
        self.walkCount = 0
        self.vel = 3
        self.hitbox = (self.x + 17, self.y + 2, 31, 57)
        self.health = 10
        self.visible = True
        self.spawn = [self.x, self.y, self.width, self.height, self.end]

    def draw(self, win):
        self.move()
        if self.visible:
            if self.walkCount + 1 >= 33:
                self.walkCount = 0

            if self.vel > 0:
                win.blit(self.walkRight[self.walkCount // 3], (self.x, self.y))
                self.walkCount += 1
            else:
                win.blit(self.walkLeft[self.walkCount // 3], (self.x, self.y))
                self.walkCount += 1

            pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))
            pygame.draw.rect(win, (0, 128, 0), (self.hitbox[0], self.hitbox[1] - 20, 50 - (5 * (10 - self.health)), 10))
            self.hitbox = (self.x + 17, self.y + 2, 31, 57)

    def move(self):
        if self.vel > 0:
            if self.x + self.vel < self.path[1]:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.walkCount = 0
        else:
            if self.x - self.vel > self.path[0]:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.walkCount = 0

    def hit1(self):
        if self.health > 0:
            self.health -= 1
        else:
            self.visible = False
        soundObj = pygame.mixer.Sound('resources/hit.mp3')
        soundObj.set_volume(0.04)
        soundObj.play()

class boss(object):
    def __init__(self, x, y, width, height, end):
        self.walkRight = [pygame.image.load('resources/BOSSR1.png'), pygame.image.load('resources/BOSSR2.png'),
                 pygame.image.load('resources/BOSSR3.png'),
                 pygame.image.load('resources/BOSSR4.png'), pygame.image.load('resources/BOSSR5.png'),
                 pygame.image.load('resources/BOSSR6.png'),
                 pygame.image.load('resources/BOSSR7.png'), pygame.image.load('resources/BOSSR8.png'),
                 pygame.image.load('resources/BOSSR9.png'),
                 pygame.image.load('resources/BOSSR10.png'), pygame.image.load('resources/BOSSR11.png')]
        self.walkLeft = [pygame.image.load('resources/BOSSL1.png'), pygame.image.load('resources/BOSSL2.png'),
                pygame.image.load('resources/BOSSL3.png'),
                pygame.image.load('resources/BOSSL4.png'), pygame.image.load('resources/BOSSL5.png'),
                pygame.image.load('resources/BOSSL6.png'),
                pygame.image.load('resources/BOSSL7.png'), pygame.image.load('resources/BOSSL8.png'),
                pygame.image.load('resources/BOSSL9.png'),
                pygame.image.load('resources/BOSSL10.png'), pygame.image.load('resources/BOSSL11.png')]
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.end = end
        self.path = [self.x, self.end]
        self.walkCount = 0
        self.vel = 5
        self.hitbox = (self.x + 17, self.y + 2, 31, 57)
        self.health = 10
        self.visible = True
        self.spawn = [self.x, self.y, self.width, self.height, self.end]

    def draw(self, win):
        self.move()
        if self.visible:
            if self.walkCount + 1 >= 33:
                self.walkCount = 0

            if self.vel > 0:
                win.blit(self.walkRight[self.walkCount // 3], (self.x, self.y))
                self.walkCount += 1
            else:
                win.blit(self.walkLeft[self.walkCount // 3], (self.x, self.y))
                self.walkCount += 1

            pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))
            pygame.draw.rect(win, (0, 128, 0), (self.hitbox[0], self.hitbox[1] - 20, 50 - (5 * (10 - self.health)), 10))
            self.hitbox = (self.x + 17, self.y + 2, 31, 57)

    def move(self):
        if self.vel > 0:
            if self.x + self.vel < self.path[1]:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.walkCount = 0
        else:
            if self.x - self.vel > self.path[0]:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.walkCount = 0

    def hit1(self):
        if self.health > 0:
            self.health -= 0.5
        else:
            self.visible = False
        soundObj = pygame.mixer.Sound('resources/hit.mp3')
        soundObj.set_volume(0.04)
        soundObj.play()

Advertisement

Answer

Using class inheritance gives you access to all of the methods of the parent class as well as adding your own methods.

Here is one example of class inheritance. Notice how the parent class (FiveNumbers) can access its own methods. The child class (TenNumbers) can access its own methods along with its parent’s method.

class FiveNumbers:
    def __init__(self):
        self.one = 1
        self.two = 2
        self.three = 3
        self.four = 4
        self.five = 5
        

class TenNumbers(FiveNumbers):
    def __init__(self):
        super().__init__()
        self.six = 6
        self.seven = 7
        self.eight = 8
        self.nine = 9
        self.ten = 10


# Is the parent class able to access the methods of the parent class?
test_1 = FiveNumbers()
print(test_1.one)
print(test_1.two)

# Is the child class able to access the methods of the parent and child class?
test_2 = TenNumbers()
print(test_2.one)
print(test_2.two)
print(test_2.six)
print(test_2.ten)

# Is the parent class able to access methods of the child class?
print(test_1.six)  # This will give an error
print(test_1.ten)  # This will give an error (assuming you fixed the previous line)

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement