__init__() 缺少 1 个必需的位置参数:'ship_img'

时间:2021-01-30 10:39:40

标签: python pygame

我正在 pygame 中编写游戏,但是我无法在选项菜单中更改精灵的颜色。我试图将 self.ship_img 推入一个可从选项菜单访问的 var 中。英语不是我的主要语言,所以如果有什么不清楚的地方,我很抱歉。这是我的第一个 python 项目,所以我会在这个过程中学习语言

YELLOW_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_yellow.png"))
PURPLE_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_purple.png"))
GREEN1_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_green.png"))
class Ship:
    COOLDOWN = 30

    def __init__(self, x, y, health=100):
        self.x = x
        self.y = y
        self.health = health
        self.laser_img = None
        self.lasers = []
        self.cool_down_counter = 0
class Player(Ship):
    def __init__(self, x, y, ship_img, health=100 ):
        super().__init__(x, y, health)
        self.ship_img = ship_img
        self.laser_img = YELLOW_LASER
        self.mask = pygame.mask.from_surface(self.ship_img)
        self.max_health = health
def options():
    ship_img = None
    while run:
        if button_green.collidepoint((mx, my)):
            if click:
                ship_img = GREEN1_SPACE_SHIP
        if button_purple.collidepoint((mx, my)):
            if click:
                ship_img = PURPLE_SPACE_SHIP
        if button_yellow.collidepoint((mx, my)):
            if click:
                ship_img = YELLOW_SPACE_SHIP
    return ship_img

也许解决方案很简单,但我无法解决。感谢帮助

1 个答案:

答案 0 :(得分:0)

函数options中的循环是一个无限循环。删除循环,但在应用程序循环中调用 options

def options():
    ship_img = None
    if click:
        if button_green.collidepoint((mx, my)):
            ship_img = GREEN1_SPACE_SHIP
        if button_purple.collidepoint((mx, my)):
            ship_img = PURPLE_SPACE_SHIP
        if button_yellow.collidepoint((mx, my)):
            ship_img = YELLOW_SPACE_SHIP
    return ship_img

要更改现有对象的图像,请将 change_image 方法添加到 Player 类:

class Player(Ship):
    # [...]

    def change_image(self, ship_img):
        self.ship_img = ship_img

点击按钮时调用方法:

def options(ship):
    if click:
        if button_green.collidepoint((mx, my)):
            ship.change_image(GREEN1_SPACE_SHIP)
        if button_purple.collidepoint((mx, my)):
            ship.change_image(PURPLE_SPACE_SHIP)
        if button_yellow.collidepoint((mx, my)):
            ship.change_image(YELLOW_SPACE_SHIP)