my_list
の各項目に対してdraw
とmove
メソッドを呼び出すとします。私はi.draw()
とi.move()
の代わりにmy_objects.draw()
とmy_objects.move()
を試しましたが、私はいつも同じエラーが発生します。ここに私のコードは次のとおりです。ここでPython - Pygame AttributeError:intオブジェクトに属性 'draw'がありません
import pygame
import random
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
class Rectangle():
def __init__(self):
self.x = random.randrange(0, 700)
self.y = random.randrange(0, 500)
self.height = random.randrange(20, 70)
self.width = random.randrange(20, 70)
self.change_x = random.randrange(-3, 3)
self.change_y = random.randrange(-3, 3)
def move(self):
self.x += self.change_x
self.y += self.change_y
def draw(self):
pygame.draw.rect(screen, GREEN, [self.x, self.y, self.width, self.height])
my_list = []
for number in range(10):
my_object = Rectangle()
my_list.append(my_object)
pygame.init()
screen = pygame.display.set_mode((700, 500))
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(BLACK)
for i in range(len(my_list)):
number.draw()
number.move()
pygame.display.flip()
clock.tick(60)
pygame.quit()
は誤りです:
Traceback (most recent call last):
line 53, in <module>
number.draw()
AttributeError: 'int' object has no attribute 'draw'
してください私はそれが働いていた、 'あなたが行うときnumber'がrange'に番号に対して'作成され、数が – Y0da