Python - How To Interact With Class In Different Method
Solution 1:
You can't simply call Enemy.X
because Enemy
is a class definition. In the other line you have for enemy in enemy_list
which calls upon all the instances of Enemy
(instances are different and here they are enemy
with a lowercase). To follow a player you would first need a player in your code! I would also recommend you don't use a new thread for each functionality of your game. You can throw in your logic to move enemies into your normal game loop which already has its own thread.
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()
As @furas notes though you may be better off with just a mainloop
that has sub-functions to check all these things! I suspect the next thing you would want to do is implement a keyboard listener to allow your player
to move.
Also note that at this point we have two classes that look very similar. We will probably benefit from having a base class (let's say Human
) that both these classes inherit from. In this way we can add a trait to both classes with a single line of code. The child classes can still overwrite the values supplied though if needed:
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()
Solution 2:
You are trying to reach X as it is static member of the Enemy
class, which is wrong as it is attribute of class instance (object). To reach it you need to use object, which is what you keep stored in enemy_list
list.
Post a Comment for "Python - How To Interact With Class In Different Method"