Python:AttributeError:'Pong'对象没有属性'render'

时间:2016-03-07 05:53:36

标签: python-3.x

在遵循Python教程创建游戏乒乓时遇到此错误:

Traceback (most recent call last):
  File "C:/Users/Gavin/PycharmProjects/lmagPONG/game.py", line 98, in <module>
    main()
  File "C:/Users/Gavin/PycharmProjects/lmagPONG/game.py", line 91, in main
    pong.render(screen)
AttributeError: 'Pong' object has no attribute 'render'

这是我用来创建pong对象的代码(渲染位于底部):

class Pong(object):
    def __init__(self, screensize):

        self.screensize = screensize

        self.centerx = int(screensize[0] * 0.5)
        self.centery = int(screensize[1] * 0.5)

        self.radius = 8

        self.rect = pygame.Rect(self.centerx - self.radius,
                                self.centery - self.radius,
                                self.radius * 2, self.radius * 2)

        self.color = (100, 100, 255)

        self.direction = [1, 1]

        self.speed = 2


        self.hit_edge_left = False
        self.hit_edge_right = False

    def update(self, player_paddle=None, ai_paddle=None):

        self.centerx += self.direction[0] * self.speed
        self.centery += self.direction[1] * self.speed

        self.rect.center = (self.centerx, self.centery)

        if self.rect.top <= 0:
            self.direction[1] = 1
        elif self.rect.bottom >= self.screensize[1] - 1:
            self.direction[1] = -1

        if self.rect.right >= self.screensize[0] - 1:
            self.hit_edge_right = True
        elif self.rect.left <= 0:
            self.hit_edge_left = True

        def render(self, screen):
            pygame.draw.circle(screen, self.color, self.rect.center, self.radius, 0)
            pygame.draw.circle(screen, (0, 0, 0), self.rect.center, self.radius, 1)

我的主要功能(在底部附近称为渲染):

def main():
    pygame.init()

    screensize = (640, 320)

    screen = pygame.display.set_mode(screensize)

    clock = pygame.time.Clock()

    pong = Pong(screensize)

    running = True

    while running:
        # FPS limiting/reporting phase
        clock.tick(64)

        # event handling phase
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False

        # Object updating phase
        pong.update()


        if pong.hit_edge_left:
            print('You Won')
            running = False
        elif pong.hit_edge_right:
            print('You Lose')
            running = False

        # Rendering phase
        screen.fill((100, 100, 100))

        pong.render(screen)

        pygame.display.flip()

    pygame.quit()

我是python的新手,所以即时通讯试图了解这一点,我不确定我是只是犯了一个简单的错误,或者我做错了什么,我只是按照教程。

0 个答案:

没有答案