简体   繁体   English

Python,AttributeError:“ PlayerPaddle”对象没有属性“ update”

[英]Python, AttributeError: 'PlayerPaddle' object has no attribute 'update'

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

Sorry for long code.. But I'm getting so frustrated. 抱歉,很长的代码。但是,我感到非常沮丧。 Why am i getting "'PlayerPaddle' object has no attribute 'update'" Error !? 为什么我收到“'PlayerPaddle'对象没有属性'update'”错误!

For what i have been able to understand its my def update(self) function that is returning null or smth.. But how is that? 对于我已经能够理解它的def update(self)函数,该函数返回null或smth。。但是那怎么办? Is it in my eventhandler that the error is? 错误在我的事件处理程序中吗? Is it updating the position wrong? 更新位置是否错误?

It looks like your indentation is off. 看来您的缩进已关闭。 Your methods update and render are indented inside the __init__ method. 您的方法updaterender__init__方法内缩进。 Move them out one indentation level. 将它们移出一个压痕级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:…对象没有属性'update' - AttributeError: … object has no attribute 'update' Python AttributeError: Object 没有属性 - Python AttributeError: Object has no attribute Python/Airflow AttributeError:“AwsLambdaHook”对象没有属性“update_relative” - Python/Airflow AttributeError: 'AwsLambdaHook' object has no attribute 'update_relative' (python-telegram-bot)AttributeError:“更新”对象没有属性“机器人” - (python-telegram-bot) AttributeError: 'Update' object has no attribute 'bot" Python 复制错误:AttributeError:'function' 对象没有属性 'update' - Python Replit Error: AttributeError: 'function' object has no attribute 'update' AttributeError: 'Price' 对象没有属性 'update' - AttributeError: 'Price' object has no attribute 'update' AttributeError:“列表”对象没有属性“更新” - AttributeError: 'list' object has no attribute 'update' AttributeError: 'Person' object 没有属性 'update' Django - AttributeError: 'Person' object has no attribute 'update' Django AttributeError:'NoneType'对象没有属性'update' - AttributeError: 'NoneType' object has no attribute 'update' AttributeError 'DictCursor' object 没有属性 'update' - AttributeError 'DictCursor' object has no attribute 'update'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM