I'm making a game and am currently trying to code Enemies using a tile system. I have level maps that look like this (it's obviously much bigger):
level_1 = [
' ',
' ',
'P E E ',
'XXXXX XXXXXXXXXXX XXXXX',]
X's represent hitboxes where the player can't fall through. P is the player's start position and E's are enemies. I'm able to display and transition through the sprites of the player and enemies. I have a Player class:
class Player(pygame.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.reversed = False
my_spritesheet = Spritesheet('texture.png')
self.character1 = [my_spritesheet.parse_sprite('MainCharacterP1.png'), my_spritesheet.parse_sprite('MaincharacterP3.png'), my_spritesheet.parse_sprite('MainCharacterP2.png'), my_spritesheet.parse_sprite('MaincharacterP3.png'), my_spritesheet.parse_sprite('MainCharacterP4.png')]
self.revcharacter1 = [my_spritesheet.parse_sprite('RevMaincharacter.png'), my_spritesheet.parse_sprite('RevMainCharacterP2.png'), my_spritesheet.parse_sprite('RevMaincharacterP3.png'), my_spritesheet.parse_sprite('RevMainCharacterP4.png'), my_spritesheet.parse_sprite('RevMaincharacterP3.png'),my_spritesheet.parse_sprite('RevMainCharacterP2.png')]
self.character = [self.character1, self.revcharacter1]
self.index = 0
self.character_index = 0
self.animation_speed = 0.2
self.index_tracked = 0
self.indexactive = False
self.image = self.character[self.character_index][self.index]
self.rect = self.image.get_rect(topleft = pos)
self.position = self.rect
self.direction = pygame.math.Vector2(0, 0)
self.speed_x = 10
self.speed_y = 10
I have an enemy class in a different pygame file,
class Enemy(pygame.sprite.Sprite):
def __init__(self, pos):
super().__init__()
zombie_sprite = Spritesheet('zombie.png')
self.zombie = [zombie_sprite.parse_sprite('Zombie1.png'), zombie_sprite.parse_sprite('Zombie2.png'), zombie_sprite.parse_sprite('Zombie3.png'), zombie_sprite.parse_sprite('Zombie4.png'), zombie_sprite.parse_sprite('Zombie3.png')]
self.revzombie = [zombie_sprite.parse_sprite('RevZombie1.png'), zombie_sprite.parse_sprite('RevZombie2.png'), zombie_sprite.parse_sprite('RevZombie3.png'), zombie_sprite.parse_sprite('RevZombie4.png'), zombie_sprite.parse_sprite('RevZombie3.png')]
self.enemy = [self.zombie, self.revzombie]
self.character_index = 0
self.index = 0
self.image = self.enemy[self.character_index][self.index]
self.rect = self.image.get_rect(topleft = pos)
#part I need help with
def move_enemy(self):
player = Player(self)
if player.position.x < 500:
print(player.position.x)
I also have a level class (also within a different pygame file):
class Level:
#Initializes the game
def __init__(self, level_data, surface):
#level setup
self.display_surface = surface
self.setup_level(level_data)
self.world_shift = 0
self.world_y_shift = 0
#Gets the level started with getting the information in other Classes/.py files
def setup_level(self, layout):
self.tiles = pygame.sprite.Group()
self.player = pygame.sprite.GroupSingle()
self.enemy = pygame.sprite.Group()
#Goes through the level map from levels.py
for row_index, row in enumerate(layout):
for col_index, cell in enumerate(row):
x = col_index * tile_size[current_level]
y = row_index * tile_size[current_level]
if cell == 'X':
tile = Tile((x, y), tile_size[current_level])
self.tiles.add(tile)
elif cell == 'P':
player_sprite = Player((x, y))
self.player.add(player_sprite)
elif cell == 'E':
enemy_sprite = Enemy((x, y))
self.enemy.add((enemy_sprite))
I just need to know how to access the different variables from the Player and Level class inside my Enemy class.
(I'm trying to get it so that if the player is within 500 pixels from an enemy, the enemy will attack)
CodePudding user response:
Just pass a second parameter that resembles the player variable in your
def move_enemy(self)
method like this:
def move_enemy(self, player)
and then pass the player variable at the moment you call your function like:
#an_enemy and player1 declarations
an_enemy.move_enemy(player1)
Edit: remove the line player = Player(self)
that will no longer be required one you pass the player variable, all other code will still work
CodePudding user response:
Use import Player from file to access player attributes. Say the Player class in player.py file
from player import Player
...
def move_enemy(self, player):
player = Player(10,20)
if player.position.x < 500:
print(player.position.x)
In that case your player object will always be at same position. However, I think you want to access value of the attribute from a dynamic object.
You should write your function in enemy with a generic variable. Then call that function in main with player object
in enemy.py
def move_enemy(self, position):
# player = Player(10,20)
if position.x < 500:
print(position.x)
in main.py
from player import Player from enemy import Enemy
player_ = Player(x,y)
enemy = Enemy(x2,y2)
enemy.move_enemy(player_.position)