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:
JavaScript
x
138
138
1
class enemy(object):
2
def __init__(self, x, y, width, height, end):
3
self.walkRight = [pygame.image.load('resources/R1ENEMY.png'), pygame.image.load('resources/R2ENEMY.png'),
4
pygame.image.load('resources/R3ENEMY.png'),
5
pygame.image.load('resources/R4ENEMY.png'), pygame.image.load('resources/R5ENEMY.png'),
6
pygame.image.load('resources/R6ENEMY.png'),
7
pygame.image.load('resources/R7ENEMY.png'), pygame.image.load('resources/R8ENEMY.png'),
8
pygame.image.load('resources/R9ENEMY.png'),
9
pygame.image.load('resources/R10ENEMY.png'), pygame.image.load('resources/R11ENEMY.png')]
10
self.walkLeft = [pygame.image.load('resources/L1ENEMY.png'), pygame.image.load('resources/L2ENEMY.png'),
11
pygame.image.load('resources/L3ENEMY.png'),
12
pygame.image.load('resources/L4ENEMY.png'), pygame.image.load('resources/L5ENEMY.png'),
13
pygame.image.load('resources/L6ENEMY.png'),
14
pygame.image.load('resources/L7ENEMY.png'), pygame.image.load('resources/L8ENEMY.png'),
15
pygame.image.load('resources/L9ENEMY.png'),
16
pygame.image.load('resources/L10ENEMY.png'), pygame.image.load('resources/L11ENEMY.png')]
17
self.x = x
18
self.y = y
19
self.width = width
20
self.height = height
21
self.end = end
22
self.path = [self.x, self.end]
23
self.walkCount = 0
24
self.vel = 3
25
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
26
self.health = 10
27
self.visible = True
28
self.spawn = [self.x, self.y, self.width, self.height, self.end]
29
30
def draw(self, win):
31
self.move()
32
if self.visible:
33
if self.walkCount + 1 >= 33:
34
self.walkCount = 0
35
36
if self.vel > 0:
37
win.blit(self.walkRight[self.walkCount // 3], (self.x, self.y))
38
self.walkCount += 1
39
else:
40
win.blit(self.walkLeft[self.walkCount // 3], (self.x, self.y))
41
self.walkCount += 1
42
43
pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))
44
pygame.draw.rect(win, (0, 128, 0), (self.hitbox[0], self.hitbox[1] - 20, 50 - (5 * (10 - self.health)), 10))
45
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
46
47
def move(self):
48
if self.vel > 0:
49
if self.x + self.vel < self.path[1]:
50
self.x += self.vel
51
else:
52
self.vel = self.vel * -1
53
self.walkCount = 0
54
else:
55
if self.x - self.vel > self.path[0]:
56
self.x += self.vel
57
else:
58
self.vel = self.vel * -1
59
self.walkCount = 0
60
61
def hit1(self):
62
if self.health > 0:
63
self.health -= 1
64
else:
65
self.visible = False
66
soundObj = pygame.mixer.Sound('resources/hit.mp3')
67
soundObj.set_volume(0.04)
68
soundObj.play()
69
70
class boss(object):
71
def __init__(self, x, y, width, height, end):
72
self.walkRight = [pygame.image.load('resources/BOSSR1.png'), pygame.image.load('resources/BOSSR2.png'),
73
pygame.image.load('resources/BOSSR3.png'),
74
pygame.image.load('resources/BOSSR4.png'), pygame.image.load('resources/BOSSR5.png'),
75
pygame.image.load('resources/BOSSR6.png'),
76
pygame.image.load('resources/BOSSR7.png'), pygame.image.load('resources/BOSSR8.png'),
77
pygame.image.load('resources/BOSSR9.png'),
78
pygame.image.load('resources/BOSSR10.png'), pygame.image.load('resources/BOSSR11.png')]
79
self.walkLeft = [pygame.image.load('resources/BOSSL1.png'), pygame.image.load('resources/BOSSL2.png'),
80
pygame.image.load('resources/BOSSL3.png'),
81
pygame.image.load('resources/BOSSL4.png'), pygame.image.load('resources/BOSSL5.png'),
82
pygame.image.load('resources/BOSSL6.png'),
83
pygame.image.load('resources/BOSSL7.png'), pygame.image.load('resources/BOSSL8.png'),
84
pygame.image.load('resources/BOSSL9.png'),
85
pygame.image.load('resources/BOSSL10.png'), pygame.image.load('resources/BOSSL11.png')]
86
self.x = x
87
self.y = y
88
self.width = width
89
self.height = height
90
self.end = end
91
self.path = [self.x, self.end]
92
self.walkCount = 0
93
self.vel = 5
94
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
95
self.health = 10
96
self.visible = True
97
self.spawn = [self.x, self.y, self.width, self.height, self.end]
98
99
def draw(self, win):
100
self.move()
101
if self.visible:
102
if self.walkCount + 1 >= 33:
103
self.walkCount = 0
104
105
if self.vel > 0:
106
win.blit(self.walkRight[self.walkCount // 3], (self.x, self.y))
107
self.walkCount += 1
108
else:
109
win.blit(self.walkLeft[self.walkCount // 3], (self.x, self.y))
110
self.walkCount += 1
111
112
pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))
113
pygame.draw.rect(win, (0, 128, 0), (self.hitbox[0], self.hitbox[1] - 20, 50 - (5 * (10 - self.health)), 10))
114
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
115
116
def move(self):
117
if self.vel > 0:
118
if self.x + self.vel < self.path[1]:
119
self.x += self.vel
120
else:
121
self.vel = self.vel * -1
122
self.walkCount = 0
123
else:
124
if self.x - self.vel > self.path[0]:
125
self.x += self.vel
126
else:
127
self.vel = self.vel * -1
128
self.walkCount = 0
129
130
def hit1(self):
131
if self.health > 0:
132
self.health -= 0.5
133
else:
134
self.visible = False
135
soundObj = pygame.mixer.Sound('resources/hit.mp3')
136
soundObj.set_volume(0.04)
137
soundObj.play()
138
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.
JavaScript
1
36
36
1
class FiveNumbers:
2
def __init__(self):
3
self.one = 1
4
self.two = 2
5
self.three = 3
6
self.four = 4
7
self.five = 5
8
9
10
class TenNumbers(FiveNumbers):
11
def __init__(self):
12
super().__init__()
13
self.six = 6
14
self.seven = 7
15
self.eight = 8
16
self.nine = 9
17
self.ten = 10
18
19
20
# Is the parent class able to access the methods of the parent class?
21
test_1 = FiveNumbers()
22
print(test_1.one)
23
print(test_1.two)
24
25
# Is the child class able to access the methods of the parent and child class?
26
test_2 = TenNumbers()
27
print(test_2.one)
28
print(test_2.two)
29
print(test_2.six)
30
print(test_2.ten)
31
32
# Is the parent class able to access methods of the child class?
33
print(test_1.six) # This will give an error
34
print(test_1.ten) # This will give an error (assuming you fixed the previous line)
35
36