【问题标题】:invalid destination position for blit using class使用类的 blit 目标位置无效
【发布时间】:2021-09-06 13:05:57
【问题描述】:

有人知道这里有什么问题吗?还有其他代码但无关紧要,如果您认为问题更多,只需评论并发布整个代码即可。

class Coin():
    def __init__(self, x, y):
        self.xcord = x
        self.ycord = y
        self.coinsprite = pygame.image.load("coin.png")

def main():
    coin = Coin(30, 40)

    screen.blit(coin.coinsprite, ([coin.xcord],[coin.ycord]))

run = True
while run:
    main()
    pygame.display.flip()

**Traceback (most recent call last):
  File "/Users/gabriel/PycharmProjects/2d dungeon game/main.py", line 104, in <module>
    main()
  File "/Users/gabriel/PycharmProjects/2d dungeon game/main.py", line 94, in main
    screen.blit(coin.coinsprite, ([coin.xcord],[coin.ycord]))
TypeError: invalid destination position for blit**

编辑:我已解决问题,将硬币名称更改为 c1。不知道为什么会这样。

class Coin():
    def __init__(self, x, y):
        self.xcord = x
        self.ycord = y
        self.coinsprite = pygame.image.load("coin.png")

def main():
    c1 = Coin(30, 40)

    screen.blit(c1.coinsprite, (c1.xcord,c1.ycord))

run = True
while run:
    main()
    pygame.display.flip()

忘了写方括号,原来是这个问题。不知道我为什么要这样做。

【问题讨论】:

  • 请提供完整的回溯,而不仅仅是最后一行
  • @buran 哎呀,对不起。包括完整的回溯。

标签: python class pygame


【解决方案1】:
def main():
    coin = Coin(30, 40)

    screen.blit(coin.coinsprite, ([coin.xcord],[coin.ycord])) #<- Your problem is here

您将坐标作为列表传递。
这样做

def main():
    coin = Coin(30, 40)

    screen.blit(coin.coinsprite, (coin.xcord, coin.ycord))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 2015-04-06
    • 1970-01-01
    相关资源
    最近更新 更多