【问题标题】:TypeError: unbound method player_init() must be called with Player instance as first argument (got int instance instead)TypeError:必须以 Player 实例作为第一个参数调用未绑定的方法 player_init()(改为获取 int 实例)
【发布时间】:2017-05-10 03:46:07
【问题描述】:
import pygame
pygame.init()

width, height = 1000, 700
window = pygame.display.set_mode((width, height))

class Player:
    def __init__(self, x, y):
        self.pos_x = x
        self.pos_y = y
        self.width, height = 1000, 700
        self.window = pygame.display.set_mode((width, height))

    def player_init(self):
        sprite = pygame.image.load("Character Sprite.png")
        self.window.blit(sprite, (self.pos_x, self.pos_y))

while 1:
    Player.player_init(500-38, 350-49)

我一直在环顾四周,但似乎无法找到为什么会出现这种类型错误的答案

TypeError: unbound method player_init() must be called with Player instance 作为第一个参数(得到int instance)

每当我创建一个类时都会弹出。

【问题讨论】:

  • player_init() 只接受一个参数self。试试Player(500-38, 350-49).player_init()
  • 注意:删除while 1,除非您希望代码永远挂起和循环。

标签: python typeerror


【解决方案1】:

您需要使用Player(x, y).player_init(),因为player_init 还需要self 参数,该参数也使用__init__ 中指定的变量。您还使用 2 个参数调用 player_init,但它只接受一个。

【讨论】:

    【解决方案2】:

    首先(在while 之前)使用预期的参数创建类Player 的实例

    player = Player(500-38, 350-49)
    

    然后使用它(但不带参数)

    player.player_init()
    

    但对我来说,你的代码应该是这样的

    import pygame
    
    # --- constants --- (UPPER_CASE names)
    
    WIDTH  = 1000
    HEIGHT = 700
    
    # --- classes --- (CamelCase names)
    
    class Player:
                   # <- empty line to make code more readable
        def __init__(self, x, y):
            self.image = pygame.image.load("Character Sprite.png")
            self.rect = self.image.get_rect()
            self.rect.x = x
            self.rect.y = y
    
        def draw(self, surface):
            surface.blit(self.image, self.rect)
    
    # --- functions --- (lower_case names)
    
    # empty
    
    # --- main ---
    
    # - init -
    
    pygame.init()
    window = pygame.display.set_mode((WIDTH, HEIGHT))
    
    # - objects -
    
    player = Player(500-38, 350-49)
    
    # - mainloop -
    
    while True:
    
        # other code
    
        player.draw(window)
    

    self.rect 对设置位置非常有用,即。 self.rect.center 它具有检查碰撞的功能。 player.rect.collidrect(enemy.rect)player.rect.collidpoint(mouse_pos)

    有用的类 pygame.spripte.Group() 期望 self.rectself.image 自动绘制所有精灵。

    【讨论】:

      【解决方案3】:

      import pygame
      
      # --- constants --- (UPPER_CASE names)
      
      WIDTH = 1000
      HEIGHT = 700
      
      # --- classes --- (CamelCase names)
      
      class Player:
      
          def __init__(self, x, y):
              self.image = pygame.image.load("Character Sprite.png")
              self.rect = self.image.get_rect()
              self.rect.x = x
              self.rect.y = y
      
          def draw(self, surface):
              surface.blit(self.image, self.rect)
      
      # --- functions --- (lower_case names)
      
      # empty
      
      # --- main ---
      
      # - init -
      
      pygame.init()
      
      # - objects -
      
      player = Player(500-38, 350-49)
      player.draw(pygame.display.set_mode((WIDTH, HEIGHT)))
      
      # - mainloop -
      
      
      while True:
          # other code
          pygame.display.flip() # add to out image from display

      【讨论】:

        猜你喜欢
        • 2018-05-10
        • 2017-04-03
        • 1970-01-01
        • 1970-01-01
        • 2017-12-09
        • 2015-12-01
        • 2018-08-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多