我使用不同的类在我使用pygame制作的游戏中创建我的角色。正如您所看到的,这些类几乎是相同的,唯一的区别是 self.walkRight
及 self.walkLeft
. 所以我想知道一种方法,我可以复制第一个类,只修改我想更改的内容。我该怎么做?守则:
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()
2条答案
按热度按时间ffscu2ro1#
使用类继承可以访问父类的所有方法,也可以添加自己的方法。
下面是类继承的一个示例。注意父类(fivenumbers)如何访问自己的方法。子类(tennumbers)可以与其父类的方法一起访问自己的方法。
xmjla07d2#
简单的继承