【问题标题】:TypeError 'module' object is not callableTypeError“模块”对象不可调用
【发布时间】:2017-06-17 21:37:04
【问题描述】:

我在我正在从事的项目中使用 pytmx 时遇到了透明度问题,但它很长而且写得不是很好。所以我尝试制作一个刚刚制作平铺地图的较小版本(这篇文章),但我收到了这个错误。

编辑:

Traceback (most recent call last):
  File "E:/advcomp/testing/main.py", line 34, in <module>
    playGame.gameLoop()
  File "E:/advcomp/testing/main.py", line 21, in gameLoop
    self.loadMap()
  File "E:/advcomp/testing/main.py", line 30, in loadMap
    self.map_img = self.map.makeSurface()
  File "E:\advcomp\testing\loading.py", line 19, in makeSurface
    tiledSurface = pygame.surface((self.mapWidth, self.mapWidth))
TypeError: 'module' object is not callable

main.py

import pygame
from settings import *
from loading import *

class game():
    def __init__(self):
        self.screen = pygame.display.set_mode((displayWidth, displayHeight))
        pygame.display.set_caption(title)
        self.clock = pygame.time.Clock()
        self.gameRunning = True

    def loop(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.gameRunning = False

    def gameLoop(self):
        self.clock.tick(fps)
        self.loop()
        self.loadMap()
        self.editScreen()

    def editScreen(self):
        self.screen.blit(self.map_img, (0,0))
        pygame.display.update()

    def loadMap(self):
        self.map = tiledMap()
        self.map_img = self.map.makeSurface()

playGame = game()
while playGame.gameRunning == True:
    playGame.gameLoop()

加载.py

import pygame
import pytmx

pygame.init()

class tiledMap():
    def __init__(self):
        self.gameMap = pytmx.load_pygame("maps\_testingMap.tmx")
        self.mapWidth = self.gameMap.width * self.gameMap.tilewidth
        self.mapHeight = self.gameMap.height * self.gameMap.tilewidth

    def render(self, surface):
        for layer in self.gameMap.visible_layers:
            for x,y,gid in layer:
                tile = pytmx.get_tile_image_by_gid(gid)
                surface.blit(tile, (x * self.gameMap.tilewidth, y * self.gameMap.tileheight))

    def makeSurface(self):
        tiledSurface = pygame.surface((self.mapWidth, self.mapWidth))
        self.render(tiledSurface)
        return tiledSurface

【问题讨论】:

  • 发布完整的错误信息,这是一个简单的错误,但如果我们不知道是哪一行导致的,我们也无能为力。
  • 你能发布整个错误信息吗?
  • 检查pygame.surface
  • 大小写很重要。
  • 这是解决问题的好方法!您正在创建一个较小的程序来尝试重新创建您遇到的问题,这使得调试变得更加容易和快捷!这称为minimal reproducible example,在stackoverflow 上非常重要。您遇到的唯一不幸的事情是 pygame 有一个模块 pygame.surface 和一个类 pygame.Surface。请记住,类始终应以大写字母开头(极少数例外),例如MyClassGame,这样您就可以轻松识别什么是类以及什么是模块/函数/变量。

标签: python python-3.x pygame pytmx


【解决方案1】:
def makeSurface(self):
    tiledSurface = pygame.Surface((self.mapWidth, self.mapWidth))
    self.render(tiledSurface)
    return tiledSurface

请注意,我更改了上面第二行的大小写。 pygame.Surface 是你要找的类,pygame.surface 不是类。

【讨论】:

    猜你喜欢
    • 2014-09-21
    • 2011-05-30
    • 2021-11-22
    • 2017-05-08
    • 1970-01-01
    • 2021-12-24
    相关资源
    最近更新 更多