Pygame-TypeError:缺少1个必需的位置参数

法布尔

我正在开发游戏,并且我不断遇到这个错误。我似乎无法解决它。我认为问题出在底部的“ main”函数或“ Level”和“ Level01”类中。如果您发现有一种方法可以改善我的代码,那么您也可以告诉我,因为我只是在学习如何使用OOP构建游戏。

 File "C:/Users/fabma/Documents/PythonGames/RPG/Scroller!.py", line 148, in main
currentLevel.drawer(display)
TypeError: drawer() missing 1 required positional argument: 'display1'

这是我的代码:

import pygame
# Colours + Global constants
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WIDTH = 800
HEIGHT = 600
SIZE = (WIDTH, HEIGHT)
# CLASSES
# Block is the common platform


class Block(pygame.sprite.Sprite):
    def __init__(self, length, height, colour):
        super().__init__()
        # Making image
        self.image = pygame.Surface([length, height])
        self.image.fill(colour)
        self.rect = self.image.get_rect()
        # Setting Y coordinates
        self.rect.y = HEIGHT * 0.95


class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        # Is it touching the floor?
        self.velocity = 0
        self.standing = True
        # Rendering image and creating some variables
        self.height = 40
        self.length = 40
        self.sprite_x_change = 0
        self.sprite_y_change = 0
        self.image = pygame.Surface([self.height, self.length])
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.y = HEIGHT * 0.884
        self.level = None
    # Mobility: Left, right, up and stop
    def move_right(self):
        self.sprite_x_change = 15

    def move_left(self):
        self.sprite_x_change = -15

    def move_up(self, platform):
        # Seeing if we hit anything if so then we can jump!
        self.rect.y -= 2
        hit_list = pygame.sprite.spritecollide(self, platform, False)
        if len(hit_list) > 0 or self.rect.bottom >= HEIGHT - Block.height:
            self.change_y = -10

    def stop(self):
        self.sprite_x_change = 0

    def updater(self):
        self.gravity()
        platforms_hit = pygame.sprite.spritecollide(self, self.level.platforms, False)
        for blocks in platforms_hit:
            self.sprite_y_change = 0
            # Going down
            if self.sprite_y_change > 0:
                self.rect.bottom = blocks.rect.top
                self.velocity = 0
                self.standing = True
            # Going up
            if self.sprite_y_change < 0:
                self.rect.top = blocks.rect.bottom
                self.standing = False
            if self.sprite_x_change > 0:
                self.rect.right = blocks.rect.left
            if self.sprite_x_change < 0:
                self.rect.left = blocks.rect.right
            if self.sprite_x_change == 0 and self.sprite_y_change == 0:
                self.rect.y = HEIGHT * 0.884

        if self.standing == False:
            self.velocity += 1
        self.rect.x += self.sprite_x_change
        self.rect.y += self.sprite_y_change

    def gravity(self):
        self.sprite_y_change += 0.980665*self.velocity


class Level:
    def __init__(self):
        # Creating groups
        self.sprites = pygame.sprite.Group()
        self.all_things = pygame.sprite.Group()
        self.platforms = pygame.sprite.Group()

    def drawer(self, display1):
        display1.fill(BLUE)
        self.all_things.draw(display1)


class Level01(Level):
    def __init__(self, player1):
        # Initialise level1
        Level.__init__(self)
        # Level01 things
        block = Block(WIDTH, HEIGHT * 0.05, RED)
        Level.all_things = self.all_things
        self.sprites.add(player1)
        self.platforms.add(block)
        self.all_things.add(player1, block)


def main():
    # Init pygame
    pygame.init()
    # Set screen
    display = pygame.display.set_mode(SIZE)
    # Creating FPS thingy
    clock = pygame.time.Clock()
    # Making levels  + Player
    player = Player()
    level_1 = Level01(player)
    # Choosing level
    levelList = []
    levelList.append(Level01)
    currentLevelNumber = 0
    currentLevel = levelList[currentLevelNumber]
    # Game loop
    loop = True
    while loop == True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RIGHT:
                        player.move_right()
                    if event.key == pygame.K_LEFT:
                        player.move_left()
                    if event.key == pygame.K_UP:
                        player.move_up(currentLevel.platforms)

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT and player.sprite_x_change < 0:
                    player.stop()
                if event.key == pygame.K_RIGHT and player.sprite_x_change > 0:
                    player.stop()
        # Update things
        currentLevel.all_things.update()
        currentLevel.drawer(display)
        # Refresh screen
        clock.tick(30)
        pygame.display.update()
    pygame.quit()

if __name__ == "__main__":
    main()
琥珀色

您需要创建关卡的实例,而不仅仅是将类本身添加到列表中:

levelList.append(Level01)

应该...

levelList.append(level_1)

就目前而言,您使用的是类对象而不是其实例,这意味着display您要传递self的对象将被放入参数中(因为该类对象不会传递一个实例,因为它不是一个实例) 。在类的实例上调用它之后,self参数将自动传递,因此您的display参数将传递到正确的位置。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Pygame - 无法将图像绘制到屏幕:TypeError: draw() 缺少 1 个必需的位置参数:'surface'

pygame 中缺少位置参数

TypeError:detail()缺少1个必需的位置参数:“ request”

TypeError:predict()缺少1个必需的位置参数:“ params”

TypeError:fit()缺少1个必需的位置参数:'y'

TypeError:append()缺少1个必需的位置参数:“ values”

geopy TypeError:geocode()缺少1个必需的位置参数:“ query”

TypeError:setDocumentLocator()缺少1个必需的位置参数:“ locator”

python decorator TypeError缺少1个必需的位置参数

TypeError:__call __()缺少1个必需的位置参数:“ inputs”

TypeError:<lambda>()缺少1个必需的位置参数:“ w”

Scrapy:TypeError:__init __()缺少1个必需的位置参数:'url'

TypeError:缺少1个必需的位置参数:'self'

sklearn:TypeError:fit()缺少1个必需的位置参数:“ x”

TypeError:__init __()缺少1个必需的位置参数:'id'

/ save() 处的 TypeError 缺少 1 个必需的位置参数:'self'

TypeError:<lambda>()缺少1个必需的位置参数:

TypeError:save()缺少1个必需的位置参数:“ self”

Keras TypeError:fit()缺少1个必需的位置参数:“ y”

TypeError:__init __()缺少1个必需的位置参数:'figure'

TypeError:endturn()缺少1个必需的位置参数:“ self”

TypeError:draw()缺少1个必需的位置参数:“ win”

TypeError:assertEqual()缺少1个必需的位置参数:“ second”

TypeError:update()缺少1个必需的位置参数:“ document”

TypeError:initialize()缺少1个必需的位置参数:“ url”

TypeError:get()缺少1个必需的位置参数:'id'

StandardScaler: TypeError: fit() 缺少 1 个必需的位置参数:'X'

TypeError:relu()缺少1个必需的位置参数:“ x”

TypeError:函数缺少1个必需的位置参数