Python / Pygame : TypeError : '모듈'개체를 호출 할 수 없습니다.

Evenure

내 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()

충돌을 위해 사과 직사각형을 만들려고했지만 위의 오류가 발생했습니다. 사각형과 충돌 문을 제거하면 매우 잘 작동합니다. 가능한 한 빨리 정정을 받기를 바랍니다. 감사합니다.

Martijn Pieters

원하는 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객체 하지 않는, rect 이 예외가 발생합니다, 그래서 속성.

이미지를 자르고 싶습니까? Surface.set_clip()그것을 위해 사용하십시오 .

Surface객체가 스프라이트처럼 작동 할 것으로 예상했다면 먼저 실제 스프라이트 객체를 만들어야합니다. 스프라이트를 만드는 방법을 아직 모르는 경우 먼저 스프라이트에 대해 읽어야합니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Python TypeError : '모듈'개체를 호출 할 수 없습니다.

분류에서Dev

Python 모듈 문제 : TypeError : '모듈'개체를 호출 할 수 없습니다.

분류에서Dev

pygame '모듈'개체를 호출 할 수 없습니다.

분류에서Dev

Python timeit-TypeError : '모듈'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError '모듈'개체를 호출 할 수 없습니다.

분류에서Dev

Python / Pygame TypeError : 'str'객체는 호출 할 수 없습니다.

분류에서Dev

'모듈'개체의 TypeError는 호출 할 수 없습니다.

분류에서Dev

Pytorch 1.7.0 | DataLoader 오류-TypeError : '모듈'개체를 호출 할 수 없습니다.

분류에서Dev

/ api / register / '모듈'개체의 TypeError를 호출 할 수 없습니다.

분류에서Dev

EasyGUI 및 SimpleCV- TypeError : '모듈'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : '모듈'객체는 Python3을 호출 할 수 없습니다.

분류에서Dev

TypeError : '모듈'객체는 Spacy Python에서 호출 할 수 없습니다.

분류에서Dev

Python Script TypeError : 'int'개체를 호출 할 수 없습니다.

분류에서Dev

Python 오류 : TypeError : 'list'개체를 호출 할 수 없습니다.

분류에서Dev

Python-TypeError : 'list'개체를 호출 할 수 없습니다.

분류에서Dev

Python setter TypeError : 'int'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : '모듈'개체는 Django 3 렌더링 함수를 호출 할 수 없습니다.

분류에서Dev

Python 클래스 : TypeError : 'NoneType'개체를 호출 할 수 없습니다.

분류에서Dev

모델 적합 / TypeError : 'NoneType'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'pygame.Surface'개체는 호출 할 수 없습니다 (Pygame의 배열 문제).

분류에서Dev

TypeError : '_IncompatibleKeys'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'NoneType'개체는 CircleCI를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'CurrencyConverter'개체를 호출 할 수 없습니다.

분류에서Dev

firebase = firebase (config) TypeError : 'module'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : '목록'개체를 호출 할 수 없습니다-assertWarns ()

분류에서Dev

TypeError : '목록'개체를 호출 할 수 없습니다-assertWarns ()

분류에서Dev

Q : TypeError : '_csv.reader'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'Int64Index'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'Player'개체는 Django를 호출 할 수 없습니다.

Related 관련 기사

  1. 1

    Python TypeError : '모듈'개체를 호출 할 수 없습니다.

  2. 2

    Python 모듈 문제 : TypeError : '모듈'개체를 호출 할 수 없습니다.

  3. 3

    pygame '모듈'개체를 호출 할 수 없습니다.

  4. 4

    Python timeit-TypeError : '모듈'개체를 호출 할 수 없습니다.

  5. 5

    TypeError '모듈'개체를 호출 할 수 없습니다.

  6. 6

    Python / Pygame TypeError : 'str'객체는 호출 할 수 없습니다.

  7. 7

    '모듈'개체의 TypeError는 호출 할 수 없습니다.

  8. 8

    Pytorch 1.7.0 | DataLoader 오류-TypeError : '모듈'개체를 호출 할 수 없습니다.

  9. 9

    / api / register / '모듈'개체의 TypeError를 호출 할 수 없습니다.

  10. 10

    EasyGUI 및 SimpleCV- TypeError : '모듈'개체를 호출 할 수 없습니다.

  11. 11

    TypeError : '모듈'객체는 Python3을 호출 할 수 없습니다.

  12. 12

    TypeError : '모듈'객체는 Spacy Python에서 호출 할 수 없습니다.

  13. 13

    Python Script TypeError : 'int'개체를 호출 할 수 없습니다.

  14. 14

    Python 오류 : TypeError : 'list'개체를 호출 할 수 없습니다.

  15. 15

    Python-TypeError : 'list'개체를 호출 할 수 없습니다.

  16. 16

    Python setter TypeError : 'int'개체를 호출 할 수 없습니다.

  17. 17

    TypeError : '모듈'개체는 Django 3 렌더링 함수를 호출 할 수 없습니다.

  18. 18

    Python 클래스 : TypeError : 'NoneType'개체를 호출 할 수 없습니다.

  19. 19

    모델 적합 / TypeError : 'NoneType'개체를 호출 할 수 없습니다.

  20. 20

    TypeError : 'pygame.Surface'개체는 호출 할 수 없습니다 (Pygame의 배열 문제).

  21. 21

    TypeError : '_IncompatibleKeys'개체를 호출 할 수 없습니다.

  22. 22

    TypeError : 'NoneType'개체는 CircleCI를 호출 할 수 없습니다.

  23. 23

    TypeError : 'CurrencyConverter'개체를 호출 할 수 없습니다.

  24. 24

    firebase = firebase (config) TypeError : 'module'개체를 호출 할 수 없습니다.

  25. 25

    TypeError : '목록'개체를 호출 할 수 없습니다-assertWarns ()

  26. 26

    TypeError : '목록'개체를 호출 할 수 없습니다-assertWarns ()

  27. 27

    Q : TypeError : '_csv.reader'개체를 호출 할 수 없습니다.

  28. 28

    TypeError : 'Int64Index'개체를 호출 할 수 없습니다.

  29. 29

    TypeError : 'Player'개체는 Django를 호출 할 수 없습니다.

뜨겁다태그

보관