X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;ds=sidebyside;f=koperkapel%2Fgamelib%2Fenemy_roach.py;h=2645efa8778cca7dd1939465bfa341d1eea58eb0;hb=d5a55df04e88672d2275298719a9039ebcf29fea;hp=91b404b12bbdea431c1161298edda1a6f9e88d16;hpb=9e66c7b17935e886b9fbed0704727aacda3b0c38;p=koperkapel.git diff --git a/koperkapel/gamelib/enemy_roach.py b/koperkapel/gamelib/enemy_roach.py index 91b404b..2645efa 100644 --- a/koperkapel/gamelib/enemy_roach.py +++ b/koperkapel/gamelib/enemy_roach.py @@ -16,6 +16,7 @@ def get_enemy_roach(level): roach.level = level roach.move = partial(move, roach) roach.last_moved = 0 + roach.start_pos = None each_tick(roach.move) return roach @@ -24,5 +25,21 @@ def move(roach, dt): """Enemy roach move method""" roach.last_moved += dt if roach.last_moved > 0.5: - print('Moving') + if not roach.start_pos: + roach.start_pos = roach.game_pos roach.last_moved = 0 + attempt = 0 + while attempt < 4: + attempt += 1 + dx = random.randint(-1, 1) + dy = random.randint(-1, 1) + if abs(roach.game_pos[0] + dx - roach.start_pos[0]) > 2: + continue + if abs(roach.game_pos[1] + dy - roach.start_pos[1]) > 2: + continue + if roach.level.can_walk(roach.game_pos[0] + dx, roach.game_pos[1] + dy, 'floor'): + enemy = roach.level.get_enemy(roach.game_pos[0] + dx, roach.game_pos[1] + dy) + if enemy and enemy is not roach: + continue + roach.game_pos = (roach.game_pos[0] + dx, roach.game_pos[1] + dy) + break