C:为什么没有怪物吃这个玩家?

时间:2016-02-21 06:54:12

标签: c linked-list comparison coordinates nodes

我有一个程序可以从文本文件中读取地图(障碍物,怪物,宝藏,玩家的生成点),然后玩家可以通过WASD键移动到宝藏中,怪物M和障碍物#在路上。 / p>

有些地图有3个或更多怪物。

为什么怪物'obj'在他们处于同一坐标时不会吃掉玩家?

        if (obj->x == player->x && obj->y == player->y) {

似乎不适合进行协调比较

void move_monsters() {
    list_node *monster = monster_position;
    list_node *treasure = treasure_position;
    game_object *obj;
    game_object *obj2;
    while (monster) {
        unsigned possibilities[4][2];
        int position_count = 0;

        obj = monster->object;
        obj2 = treasure->object;

        if (isMovable(obj, obj->x + 1, obj->y)) {
            possibilities[position_count][0] = obj->x + 1;
            possibilities[position_count][1] = obj->y;
            position_count++;
        }
        if (isMovable(obj, obj->x - 1, obj->y)) {
            possibilities[position_count][0] = obj->x - 1;
            possibilities[position_count][1] = obj->y;
            position_count++;
        }
        if (isMovable(obj, obj->x, obj->y + 1)) {
            possibilities[position_count][0] = obj->x;
            possibilities[position_count][1] = obj->y + 1;
            position_count++;
        }
        if (isMovable(obj, obj->x, obj->y - 1)) {
            possibilities[position_count][0] = obj->x;
            possibilities[position_count][1] = obj->y - 1;
            position_count++;
        }
        // If the monster (obj) eats the treasure (obj2), the game is over.
        if (obj->x == obj2->x && obj->y == obj2->y) {
            printf("GAME OVER\n");
            free_memory();
            printf(KNRM);
            exit(0);
        }

        // If the monster (obj) eats the player, the game is over.
        if (obj->x == player->x && obj->y == player->y) {
            printf("GAME OVER\n");
            free_memory();
            printf(KNRM);
            exit(0);
        }

        // Do nothing if no moves are available.
        if (position_count) {
            int target = rand() % position_count;
            move_object(monster->object, possibilities[target][0], possibilities[target][1]);
        }
        monster = monster->next;
    }
}

1 个答案:

答案 0 :(得分:0)

这些怪物是相当愚蠢的:只要一步之遥,它们就不会移动去吃掉宝藏或玩家。玩家和任何怪物之间的碰撞只是偶然的,玩家可以在怪物在下一次迭代中吃掉它之前摆脱危险。

如果这种情况从未发生过,我怀疑玩家不会心甘情愿地遇到怪物而isMovable()函数认为玩家的位置不可用,阻止怪物进入玩家。