【问题标题】:Getting an error saying super takes 1 argument but 0 given?收到错误说 super 需要 1 个参数但给出 0?
【发布时间】:2015-12-11 00:32:22
【问题描述】:

所以我收到一个错误,说 super() 接受 1 个参数,但它想要接受什么参数???我真的不知道它要求的是什么!有人可以帮我解决它,或者至少告诉我出了什么问题吗?

import pygame

# Define some colors
BLACK    = (   0,   0,   0)
WHITE    = ( 255, 255, 255)
GREEN    = (   0, 255,   0)
RED      = ( 255,   0,   0)



class Me(pygame.sprite.Sprite):
    super().__init__()
    def __init__(self, color, width, height):
        self.image = pygame.Surface([width, height])
        self.image.fill(color)

        self.rect = self.image.get_rect()


you = pygame.sprite.Group()

all_sprites_list = pygame.sprite.Group()

player = Player(GREEN, 20, 15)
all_sprites_list.add(player)
pygame.init()

# Set the width and height of the screen [width, height]
size = (800, 600)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Karl's Game")

#Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()
#Loading pictures
background_image = pygame.image.load("back.jpg").convert()

#Variables
# Speed in pixels per frame
x_speed = 0
y_speed = 0

# Current position
x_coord = 10
y_coord = 10
# -------- Main Program Loop -----------
while not done:
    # --- Main event loop
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True # Flag that we are done so we exit this loop


        elif event.type == pygame.KEYDOWN:
            # Figure out if it was an arrow key. If so
            # adjust speed.
            if event.key == pygame.K_LEFT:
                x_speed = -3
            elif event.key == pygame.K_RIGHT:
                x_speed = 3
                if x_coord > 200:
                    x_speed = 0
            elif event.key == pygame.K_UP:
                y_speed = -3
            elif event.key == pygame.K_DOWN:
                y_speed = 3
        elif event.type == pygame.KEYUP:
            # If it is an arrow key, reset vector back to zero
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_speed = 0
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                y_speed = 0



    x_coord = x_coord + x_speed
    y_coord = y_coord + y_speed

    player.rect.x = pos[x_coord]
    player.rect.y = pos[y_coord]
    # First, clear the screen to white. Don't put other drawing commands
    # above this, or they will be erased with this command.


    # --- Drawing code should go here
    screen.blit(background_image, [0,0])
    drawSquare(screen, x_coord, y_coord)

    # --- Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

    # --- Limit to 60 frames per second
    clock.tick(45)

# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()

【问题讨论】:

  • 看看这个 - stackoverflow.com/questions/10838596/…。您可能在super().__init__() 的括号中缺少self
  • 你在使用 Python 2 吗?无参数 super 是 Python 3 的东西。 (另外,super 调用需要进入__init__ 方法。)
  • 是的,我在 python 2 上,进入 init 是什么意思
  • 将超级行移到 init 方法中,如下所示:super(pygame.sprite.Sprite, self).__init__(width, height) ?

标签: python class pygame super


【解决方案1】:

你必须在你的类 init(self): 函数中移动你的 super() 调用。它应该是方法旁边的第一行。

另外,超级调用应该是这样的。

super(Me, self).__init__()

【讨论】:

    猜你喜欢
    • 2021-04-13
    • 1970-01-01
    • 2019-03-18
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    相关资源
    最近更新 更多