【问题标题】:TypeError: 'module' object is not callable in Python 3TypeError:“模块”对象在 Python 3 中不可调用
【发布时间】:2020-01-15 22:06:27
【问题描述】:

我正在使用 PyGame 在 Python 3.7 中制作我自己的“游戏引擎”。我想创建一个使用pygame.rect() 函数的类,但是当我运行它时,它每次都会给我这个错误或类似的错误。

main.py (./pyseed/)

# Imports
import pygame

pygame.init()

# class
class Shape:
    def __init__(self, width, height, color):
        self.width = width
        self.height = height
        self.color = color
        self.SHAPE = pygame.rect(self, width, height, color)

# RunApp() function
def runApp(width, height):
    screen = pygame.display.set_mode((width, height))
    done = False

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

        pygame.display.flip()

testing.py(import main 将更改为 import pyseed.main

# PySeed Testing Zone
import main as pyseed

myShape = pyseed.Shape(90, 90, (0, 100, 255))

pyseed.runApp(400, 300)

感谢您的帮助。

【问题讨论】:

  • 为什么你认为pygame.rect是一个函数?
  • 我想你的意思是pygame.Rect()(注意'R'不是'r'),这是一个class初始化器调用,所以不是真正的函数调用,但出于所有意图和目的,它是.
  • @Kingsley,我对程序做了一些更改,我发现我必须使用 'pygame.draw.rect()'。感谢您的帮助!

标签: python pygame python-3.7


【解决方案1】:

我的新代码:

main.py

# Made by DeBeast591
# Enjoy!

# Imports
import pygame

# Getting PyGame ready...
pygame.init()
print("Welcome to PySeed!\nMade By DeBeast591\nEnjoy!")

def setUp(width, height):
    global screen
    screen = pygame.display.set_mode((width, height))

# class
class Shape:
    def __init__(self, x, y, width, height, color):
        global screen
        self.width = width
        self.height = height
        self.x = x
        self.y = y
        self.color = color
        self.SHAPE = pygame.draw.rect(screen, self.color,pygame.Rect(self.x, self.y, self.width, self.height))

# RunApp() function
def runApp():
    global screen
    done = False

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

        pygame.display.flip()

testing.py

# PySeed Testing Zone
import main as pyseed

pyseed.setUp(400, 300)

myShape = pyseed.Shape(30, 30, 90, 90, (0, 100, 255))

pyseed.runApp()

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2021-12-24
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 2017-01-12
    • 2022-11-29
    • 2023-04-03
    • 2017-06-17
    • 2014-09-21
    相关资源
    最近更新 更多