Здравствуйте, делаю игру и вылезла ошибка: AttributeError: 'Enemy' object has no attribute 'move'
import pygame
pygame.init()
display_w = 800
display_h = 600
display = pygame.display.set_mode((display_w, display_h))
pygame.display.set_caption('Run in Live')
logo = pygame.image.load('Run.png')
pygame.display.set_icon(logo)
make_jump = False
make_up = False
dy = 20
dy2 = 10
dy3 = 10
class Enemy:
def __init__(self, x, y, width, height, speed):
self.x = x
self.y = y
self.width = width
self.height = height
self.speed = speed
def move(self):
if self.x >= -self.width:
pygame.draw.rect(display, (150, 20, 200), (self.x, self.y, self.width, self.height))
self.x -= self.speed
else:
self.x = display_w + 50
def create_enemies(enemies):
enemies.append(Enemy(display_w + 50, display_h - 170, 20, 70, 4))
enemies.append(Enemy(display_w + 50, display_h - 170, 300, 600, 4))
enemies.append(Enemy(display_w + 50, display_h - 170, 200, 500, 4))
enemy_width = 20
enemy_height = 70
enemy_x = display_w - 50
enemy_y = display_h - enemy_height - 100
pers_width = 60
pers_height = 100
pers_x = display_w // 4
pers_y = display_h - 100 - pers_height
def rungame():
global make_jump, make_up
game = True
enemies = []
create_enemies(enemies)
while game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
make_jump = True
if make_jump:
jump()
keys2 = pygame.key.get_pressed()
if keys2[pygame.K_UP]:
make_up = True
if make_up:
up()
display.fill((242, 243, 187))
draw_enemies(enemies)
pygame.draw.rect(display, (251, 186, 0), (pers_x, pers_y, pers_width, pers_height))
pygame.draw.rect(display, (0, 128, 0), (enemy_x, enemy_y, enemy_width, enemy_height))
pygame.display.update()
pygame.time.Clock().tick(60)
def jump():
global make_jump, pers_y, dy
if dy >= -20:
pers_y -= dy
dy -= 1
else:
dy = 20
make_jump = False
def up():
global make_up, pers_height, dy2
if dy2 >= -10:
pers_height -= dy2
dy2 -= 1
else:
dy2 = 10
make_up = False
def draw_enemies(enemies):
for x in enemies:
x.move()
rungame()
Ответы (2 шт):
Автор решения: CrazyElf
→ Ссылка
class Enemy:
def __init__(self, x, y, width, height, speed):
...
def move(self):
...
В Питоне табуляции архи важны. Ваш метод move
"выпал" из класса Enemy
и стал самостоятельной функцией, не привязанной к классу. Правильно так:
class Enemy:
def __init__(self, x, y, width, height, speed):
...
def move(self):
...