Python的 - 如何在不同的方法类互动(Python - How to interact wit

2019-10-31 09:38发布

我有从一个方法内的一个类变量相互作用的问题。 我想改变pygame的对象的位置。 这有一个方法来完成,因为我用threading这就需要其目标是方法。 我如何能针对特定的enemyenemy_list

我试图把整个Enemy类到EnemyMove方法,它仍然输出:

AttributeError: type object 'Enemy' has no attribute 'X'

这里的代码:

import pygame
import threading
from random import randint
from time import sleep

pygame.init()
window = pygame.display.set_mode((900, 900))
bg = pygame.image.load("Background.png").convert()

class Enemy:
    def __init__(self):
        self.W = 50
        self.H = 50
        self.X = 420
        self.Y = 850

def Gameplay():
    global enemy_list
    speed=2
    while True:
        window.blit(bg, [0, 0])
        for enemy in enemy_list:
            pygame.draw.rect(window, (255, 50, 49), (enemy.X, enemy.Y, enemy.W, enemy.H))
        pygame.display.update()

def EnemySpawn():
    global enemy_list
    while True: # make enemies forever
        print("Spawned an enemy")
        enemy_list.append(Enemy()) # make an instance of our class
        sleep(randint(1, 5))

def EnemyMove():
    print(Enemy.X) #ISSUE OCURS HERE

enemy_list = [] # to maintain records of all enemies made
enemyMovement_thread = threading.Thread(target=EnemyMove)
enemyMovement_thread.start()
game_thread = threading.Thread(target=Gameplay)
game_thread.start()
enemy_spawner_thread = threading.Thread(target=EnemySpawn)
enemy_spawner_thread.start()

我究竟做错了什么? 为什么它在工作的Gameplay ,但不是在EnemyMove

Answer 1:

你不能简单地调用Enemy.X因为Enemy是一个类定义。 在你有其他线路for enemy in enemy_list在所有的情况下,这就要求Enemy (情况是不同的,在这里,他们是enemy用小写)。 要遵循一个球员,你首先需要一个球员在你的代码! 我也建议你不要使用一个新的线程游戏的每个功能。 你可以在你的逻辑扔到敌人进入你的正常游戏圈已经有它自己的线程。

import pygame
import threading
from random import randint
from time import sleep

pygame.init()
window = pygame.display.set_mode((900, 900))
bg = pygame.image.load("Background.png").convert()

class Enemy:
    def __init__(self):
        self.W = 50
        self.H = 50
        self.X = 420
        self.Y = 850
        self.speed = 1

class Player: # make a new class so players can have better stats
    def __init__(self):
        self.W = 50
        self.H = 50
        self.X = 300
        self.Y = 300
        self.speed = 10

def Gameplay():
    global enemy_list
    global player
    while True:
        window.blit(bg, [0, 0])
        pygame.draw.rect(window, (255, 255, 255), (player.X, player.Y, player.W, player.H))
        for enemy in enemy_list:
            if enemy.X > player.X:
                enemy.X = enemy.X - enemy.speed
            else:
                enemy.X = enemy.X + enemy.speed
            if enemy.Y > player.Y:
                enemy.Y = enemy.Y - enemy.speed
            else:
                enemy.Y = enemy.Y + enemy.speed
            pygame.draw.rect(window, (255, 50, 49), (enemy.X, enemy.Y, enemy.W, enemy.H))
        pygame.display.update()

def EnemySpawn():
    global enemy_list
    while True: # make enemies forever
        print("Spawned an enemy")
        enemy_list.append(Enemy()) # make an instance of our class
        sleep(randint(1, 5))

if __name__ == "__main__":
    player = Player() # notice the difference in capitalization!
    enemy_list = [] # to maintain records of all enemies made
    game_thread = threading.Thread(target=Gameplay)
    game_thread.start()
    enemy_spawner_thread = threading.Thread(target=EnemySpawn)
    enemy_spawner_thread.start()

作为@furas笔记虽然你可能只是一个更好mainloop有子功能检查所有这些事情! 我怀疑你会想要做的下一件事就是实现键盘监听器,让您的player移动。

还要注意的是,在这一点上,我们有一个看起来非常相似两班。 我们可能会从具有基础类受益(假设Human ),这两个类继承。 通过这种方式,我们可以一个特征添加到两个班,一个单一的代码行。 子类仍然可以覆盖但如果需要提供的值:

import pygame
import threading
from random import randint
from time import sleep

pygame.init()
window = pygame.display.set_mode((900, 900))
bg = pygame.image.load("Video & Image Processing/Image Processing/InputImage.jpg").convert()

class Human:
    def __init__(self):
        self.W = 50
        self.H = 50
        self.X = 420
        self.Y = 850
        self.speed = 1

class Enemy(Human): # inherit Human
    def __init__(self):
        Human.__init__(self) # get all traits a Human has

class Player(Human): # inherit Human
    def __init__(self):
        Human.__init__(self) # get all traits a Human has
        self.X = 300 # overwrite specific traits
        self.Y = 300

def Gameplay():
    global enemy_list
    global player
    while True:
        window.blit(bg, [0, 0])
        pygame.draw.rect(window, (255, 255, 255), (player.X, player.Y, player.W, player.H))
        for enemy in enemy_list:
            if enemy.X > player.X:
                enemy.X = enemy.X - enemy.speed
            else:
                enemy.X = enemy.X + enemy.speed
            if enemy.Y > player.Y:
                enemy.Y = enemy.Y - enemy.speed
            else:
                enemy.Y = enemy.Y + enemy.speed
            pygame.draw.rect(window, (255, 50, 49), (enemy.X, enemy.Y, enemy.W, enemy.H))
        pygame.display.update()

def EnemySpawn():
    global enemy_list
    while True: # make enemies forever
        print("Spawned an enemy")
        enemy_list.append(Enemy()) # make an instance of our class
        sleep(randint(1, 5))


if __name__ == "__main__":
    player = Player() # notice the difference in capitalization!
    enemy_list = [] # to maintain records of all enemies made
    game_thread = threading.Thread(target=Gameplay)
    game_thread.start()
    enemy_spawner_thread = threading.Thread(target=EnemySpawn)
    enemy_spawner_thread.start()


Answer 2:

您正在试图达到X,因为它是静态成员Enemy类,这是错误的,因为它是类的实例 (对象)的属性。 为了达到它,你需要使用对象,这是你一直保存在什么enemy_list列表。



文章来源: Python - How to interact with class in different method