【问题标题】:TypeError: 'module' object is not callable in pygameTypeError:“模块”对象在 pygame 中不可调用
【发布时间】:2017-01-15 06:39:20
【问题描述】:

我是 pygame 的新手,遇到这个错误,请帮助我。

    backgrounf_image_filename = 'ubisoft.jpg'
    mouse_image_filename = 'cursor.png'

    import pygame
    from pygame.locals import *
    from sys import exit

    pygame.init()

    screen = pygame.display.set_mode((640, 480), 0, 32)
    pygame.display.set_caption("Hello World!")
    background = pygame.image.load(backgrounf_image_filename).convert()
    mouse_cursor = pygame.image(mouse_image_filename).convert_alpha()

我有这个错误:

    Traceback (most recent call last):
      File "C:/Users/ziyaa/PycharmProjects/pygame/helloworld.py", line 13, in <module>
        mouse_cursor = pygame.image(mouse_image_filename).convert_alpha()
    TypeError: 'module' object is not callable

    Process finished with exit code 1

【问题讨论】:

  • 第一篇文章和完整的堆栈跟踪包括:这很值得赞成和回答:)
  • 正如有人指出的那样,您的缩进最后有点偏离。您可能会遇到问题(但这与问题无关,我将其编辑掉)

标签: python python-3.x pygame


【解决方案1】:

你在做:

background = pygame.image.load(backgrounf_image_filename).convert()
mouse_cursor = pygame.image(mouse_image_filename).convert_alpha()

在第二条指令中,您忘记load您的图像,导致调用模块而不是函数。

只要做:

mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()

【讨论】:

  • 没有加载怎么办?碰撞?什么都没有显示?图片在哪里?
【解决方案2】:

你的问题是你的 for event.get 循环。我在我的游戏中使用了类似的方法,它是正确的方法,但是你的下几行缩进错误

x, y = pygame.mouse.get_pos()
        x -= mouse_cursor.get_width()/2
        y -= mouse_cursor.get_height()/2
        screen.blit(mouse_cursor, (x, y)) 

需要身份不明或移回

【讨论】:

  • 这不是问题,但可能是 OP next 问题。
  • 那是因为你有英文背景。
猜你喜欢
  • 1970-01-01
  • 2017-06-17
  • 2014-09-21
  • 2011-05-30
  • 2021-11-22
  • 2013-04-03
相关资源
最近更新 更多