I'm trying to figure out how to do a simple search/chase AI.
I know that I'm going to have to search around the mob entity to figure out if the player is close enough, I could probably get that done on my own, but what comes after is what gets me stuck. Once I have the (x,y) of the player, how do I get my mob entity to move a single cell (up, down, left, or right) until they reach the player entity?
Here's a bit of the code for reference.
Just the first part of the Board class.
Basically the portion where the grid is created once initiated.
board = Board(10, 10)
class Board(object):
'''Board class to create empty grid of cells'''
def __init__(self, x_size, y_size, char='-'):
self.x_size = x_size
self.y_size = y_size
self.char = char
self.board = [[char for _ in range(x_size)] for _ in range(y_size)]
And the Entity class. player = Entity('Player', 5, 5, 10, 'o', 'alive')
class Entity(object):
'''Basic entity object
Has position and character'''
def __init__(self, name, x, y, hp, char, state):
self.name = name
self.x = x
self.y = y
self.hp = hp
self.char = char
self.state = state
def move(self, direction, board):
'''To update entity position'''
dx = self.x
dy = self.y
valid_moves = board.valid_moves(self)
if direction == 'left' and (self.x, self.y - 1) in valid_moves:
self.y = dy - 1
elif direction == 'right' and (self.x, self.y + 1) in valid_moves:
self.y = dy + 1
elif direction == 'up' and (self.x - 1, self.y) in valid_moves:
self.x = dx - 1
elif direction == 'down' and (self.x + 1, self.y) in valid_moves:
self.x = dx + 1
else:
if self.name == 'Player':
print 'You can\'t move there'
time.sleep(0.3)