Python,AttributeError:' PlayerPaddle'对象没有属性'更新'

时间:2015-04-28 19:32:26

标签: python

    import pygame
from pygame.locals import *

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

    self.screensize = screensize

    self.position_x = int(screensize[0]*0.5)
    self.position_y = int(screensize[1]*0.8)

    self.width  = 10
    self.height = 4

    self.rect = pygame.Rect(self.position_x - (self.width*0.5),
                            self.position_y - (self.height*0.5),
                            self.width, self.height)
    self.color = (100, 200, 200)

    self.speed = 5
    self.direction = 0

    def update(self):
        self.position_x += self.direction * self.speed

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

def main():
    pygame.init()

    screensize = (600, 700)
    screen = pygame.display.set_mode(screensize)

    clock = pygame.time.Clock()

    player_paddle = PlayerPaddle(screensize)

    running = True

    while running:
        clock.tick(64)


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

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    player_paddle.direction = 1
                elif event.key == pygame.K_RIGHT:
                    player_paddle.direction = -1
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    player_paddle.direction = 0
                elif event.key == pygame.K_RIGHT:
                    player_paddle.direction = 0

        player_paddle.update()

        screen.fill((100, 100, 100))

        player_paddle.render(screen)

        pygame.display.flip()

    pygame.quit()

main()

很抱歉代码很长..但是我很沮丧。为什么我会得到"' PlayerPaddle'对象没有属性'更新'" 错误!?

我已经能够理解我的def update(self)函数返回null或smth ..但那是怎么回事?在我的eventhandler中是错误的吗?是否更新了错误的位置?

1 个答案:

答案 0 :(得分:2)

看起来你的缩进是关闭的。您的方法updaterender__init__方法中缩进。将它们移出一个缩进级别。