gpt4 book ai didi

Python,属性错误: 'PlayerPaddle' object has no attribute 'update'

转载 作者:行者123 更新时间:2023-12-01 04:43:53 25 4
gpt4 key购买 nike

    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.. 但这是怎么回事?错误是在我的事件处理程序中吗?是不是更新位置错误了?

最佳答案

您的缩进似乎已关闭。您的方法 updaterender__init__ 方法内缩进。将它们移出一级缩进。

关于Python,属性错误: 'PlayerPaddle' object has no attribute 'update' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29928295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com