Python / Pygame:TypeError:'module'对象不可调用

时间:2014-06-14 16:48:23

标签: python pygame

我的Pygame代码返回错误说明:

apple.rect=pygame.rect(apple_x,apple_y,32,37)
TypeError: 'module' object is not callable

我的完整代码如下:

import sys, pygame, random
from menu_lib import *
from credit import credit

class Player(pygame.sprite.Sprite):
    def __init__(self, *groups):
        super(Player, self).__init__(*groups)
        self.image = pygame.image.load('catcher_left.png')
        self.rect = pygame.rect.Rect((320,240), self.image.get_size())
        self.rect.bottom = 452
        self.rect.left = 320

    def update(self):
        key = pygame.key.get_pressed()
        if key[pygame.K_LEFT]:
            self.rect.x -= 10
            self.image = pygame.image.load('catcher_left.png')
            pygame.display.flip()
        if key[pygame.K_RIGHT]:
            self.rect.x += 10
            self.image = pygame.image.load('catcher_right.png')
            pygame.display.flip()
        if self.rect.left < 0:
            self.rect.left = 0
        elif self.rect.right > 640:
            self.rect.right = 640
        if self.rect.colliderect(apple.rect):
            print "Collided!"

class Game(object):
    def main(self, screen):
        clock = pygame.time.Clock()
        background = pygame.image.load('background.jpg')
        background = pygame.transform.scale(background, (640, 480))
        apple = pygame.image.load('apple.png')
        apple_x=0
        apple_y=0
        apple_count=0
        sprites = pygame.sprite.Group()
        self.player = Player(sprites)

        while 1:
            clock.tick(30)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    screen.fill((0,0,0))
                    pygame.display.flip()
                    Mainmenu()
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    screen.fill((0,0,0))
                    pygame.display.flip()
                    Mainmenu()

            if apple_y==0:
                apple_y+=10
                apple_x=random.randint(10, 630)
                apple_count+=1
            elif apple_y!=0 and apple_y<480:
                apple_y+=10
            elif apple_y>=480:
                apple_y=0
                apple_x=0
            apple.rect=pygame.rect(32,37,apple_x,apple_y)

            sprites.update()
            screen.fill((200, 200, 200))
            screen.blit(background, (0, 0))
            sprites.draw(screen)
            score_txt='Score: '+str(apple_count)
            label = myfont.render(score_txt, 1, (255,0,0))
            screen.blit(label, (500, 10))
            screen.blit(apple, (apple_x,apple_y))
            pygame.display.flip()


def Mainmenu():
    screen = pygame.display.set_mode((640, 480))
    menu = cMenu(50, 50, 20, 5, 'vertical', 300, screen,
           [('Start Game', 1, None),
            ('Credits',    2, None),
            ('Exit',       3, None)])

    menu.set_center(True, True)

    menu.set_alignment('center', 'center')

    state = 0
    prev_state = 1

    rect_list = []

    pygame.event.set_blocked(pygame.MOUSEMOTION)

    while 1:
        key = pygame.key.get_pressed()
        if key[pygame.K_ESCAPE]:
            pygame.quit()
            sys.exit()

        if prev_state != state:
         pygame.event.post(pygame.event.Event(EVENT_CHANGE_STATE, key = 0))
         prev_state = state

        e = pygame.event.wait()

        if e.type == pygame.KEYDOWN or e.type == EVENT_CHANGE_STATE:
         if state == 0:
            rect_list, state = menu.update(e, state)
         elif state == 1:
             screen = pygame.display.set_mode((640, 480))
             Game().main(screen)
         elif state == 2:
             credits()
         else:
            print 'Exit!'
            pygame.quit()
            sys.exit()

        if e.type == pygame.QUIT:
         pygame.quit()
         sys.exit()

        pygame.display.update(rect_list)

def credits():
    clock = pygame.time.Clock()
    screen=pygame.display.set_mode((640,480))
    screen.fill((0, 0, 0))
    text = "Credits \n _ _ _ _ _ _ _ _ _ _ _ _ \n\n\n Designer \n Creator \n\n Manikiran P"
    color = 0xa0a0a000
    credit(text,myfont,color)
    Mainmenu()
    while 1:
        clock.tick(30)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                screen.fill((0,0,0))
                pygame.display.flip()
                Mainmenu()
            if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                screen.fill((0,0,0))
                pygame.display.flip()
                Mainmenu()

        pygame.display.flip()


if __name__ == '__main__':
    pygame.init()
    screen=pygame.display.set_mode((640,480))
    pygame.display.set_caption("Evenure - (c) 2014")
    myfont = pygame.font.Font("captureit.ttf", 20)
    apple_x=0
    apple_y=0
    apple = pygame.image.load('apple.png')
    Mainmenu()

我正在尝试创建一个苹果矩形,以达到碰撞的目的,但最终得到了上述错误。如果我删除矩形和碰撞语句,它工作得非常好。希望尽快得到纠正。谢谢。

1 个答案:

答案 0 :(得分:1)

你想要pygame.Rect()(大写R); pygame.rect是定义Rect类型的模块,但它也可以在顶级模块中使用。

在一个位置您正确使用它:

self.rect = pygame.rect.Rect((320,240), self.image.get_size())

将有问题的行更新为:

apple.rect = pygame.Rect(32, 37, apple_x, apple_y)

apple.rect = pygame.rect.Rect(32, 37, apple_x, apple_y)

但是,apple是一个Surface object,它不会拥有 rect属性,因此会引发异常。

也许你想剪辑图像?请使用Surface.set_clip()

如果您希望Surface对象充当精灵,那么您首先需要创建一个实际的精灵对象。如果您还不知道如何创建精灵,那么您首先需要阅读精灵: