【问题标题】:The head of the snake is a couple points away from the body (c++ game) [closed]蛇的头部距离身体几个点(c ++游戏)[关闭]
【发布时间】:2013-03-14 18:17:29
【问题描述】:

我已经在这个项目上工作了一段时间,但我在我的项目中找不到我的问题。 网址:https://github.com/blackwolf12333/Snake 这条蛇离身体只有几分远,而且似乎一动不动。我之前有一个段错误,我修复了,但我找不到这个错误的问题!我几乎尝试过任何东西,重写代码以从头到尾阅读它。它在 Linux 中编码!

main.c

#include "snake.h"
#include "ncurses.h"

void update();
void draw_snake();
void check_move(int c);

void cleanup();

int main() {
initscr();
noecho();
cbreak();

snake = create_snake_at(20, 10);
add_body_part(&snake.head);
//add_body_part(snake.head.next);

int key;
printw("Press any key to continue...");
while((key = getch()) != 'q') { // when 'q' is pressed the game will exit.
check_move(key);
update(); // updates game logic/graphics
refresh();
}
refresh();
endwin();
cleanup();
return 0;
}

void update() {
    //clear();
    move_snake();
    draw_snake();
}

void check_move(int c) {
    switch(c) {
    case 6517: // up arrow
move_up();
break;
case 6617:
move_down();
break;
case 6817:
move_left();
break;
case 6717:
move_right();
break;
default:
break;
    }
    draw_snake();
}

void print_body_part(body_part_t *part) {
    move(part->pos.y, part->pos.x);
if(part->head) {
addch('$'); 
} else {
addch('*');
}
}

void draw_snake() {
    body_part_t *next = &snake.head;
    int i = 0;
    while(next) {
        move(i, 0);
        printw("part is head: %d\tpart x: %d\tpart y: %d\n", next->head, next->pos.x, next->pos.y);
        print_body_part(next);
        next = next->next;
        i++;
    }
}

void cleanup() {
    body_part_t *next;
    for(next = &snake.head; next != NULL; next = next->next) {
        //free(next);
    }
}

move.c

#include "move.h"
#include "snake.h"

//TODO: add collision logic here!
void move_up() {
snake.head.pos.y++;
}

void move_down() {
snake.head.pos.y--;
}

void move_left() {
snake.head.pos.x--;
}

void move_right() {
snake.head.pos.x++;
}

移动.h

#ifndef MOVE_H
#define MOVE_H

void move_up();
void move_down();
void move_left();
void move_right();

#endif

位置.c

#include "position.h"

void initialize_position(position_t *pos, int x, int y) {
pos->x = x;
pos->y = y;
}

位置.h

#ifndef POSITION_H
#define POSITION_H

typedef struct position {
int x;
int y;
} position_t;

void initialize_position(position_t *pos, int x, int y);

#endif

snake.c

#include "snake.h"

body_part_t create_body_part(int head, int x, int y);
position_t get_position_next_to(body_part_t *part);

void add_body_part(body_part_t *prev) {
body_part_t *part = malloc(sizeof(body_part_t));
part->head = prev->head & 0;
part->pos = get_position_next_to(prev);
part->dir = prev->dir;
part->next = NULL;
prev->next = part;
}

snake_t create_snake_at(int x, int y) {
snake_t *snake = malloc(sizeof(snake_t));
body_part_t head = create_body_part(1, x, y);
snake->head = head;
snake->health = MAX_HEALTH;
return *snake;
}

body_part_t create_body_part(int head, int x, int y) {
body_part_t *part = malloc(sizeof(body_part_t));
part->head = head;
initialize_position(&part->pos, x, y);
part->dir = LEFT;
part->next = NULL;
return *part;
}

position_t get_position_next_to(body_part_t *part) {
    position_t pos;
    switch(part->dir) {
    case UP:
        initialize_position(&pos, part->pos.x, part->pos.y++);
        break;
    case DOWN:
        initialize_position(&pos, part->pos.x, part->pos.y--);
        break;
    case LEFT:
        initialize_position(&pos, part->pos.x--, part->pos.y);
        break;
    case RIGHT:
        initialize_position(&pos, part->pos.x++, part->pos.y++);
        break;
    default:
        break;
    }
    return pos;
}

void move_snake() {
body_part_t *next;
for(next = &snake.head; next != NULL; next = next->next) {
switch(next->dir) {
case UP:
move_up();
break;
case DOWN:
move_down();
break;
case LEFT:
move_left();
break;
case RIGHT:
move_right();
break;
}
}
}

snake.h

#ifndef SNAKE_H
#define SNAKE_H

#include "stdlib.h"
#include "position.h"
#include "move.h"

#define MAX_HEALTH 20

typedef struct body_part body_part_t;

typedef enum DIR {
UP,
DOWN,
LEFT,
RIGHT
} direction;

typedef struct body_part {
int head;
position_t pos;
direction dir;
body_part_t *next;
} body_part_t;

typedef struct snake {
body_part_t head;
int health;
} snake_t;

/*
This is the main snake struct, if later multiple snakes will be implemented this should be replaced with something else
*/
snake_t snake;

snake_t create_snake_at(int x, int y);
void add_body_part(body_part_t *prev);
void move_snake();

#endif

【问题讨论】:

  • 请发布您的问题Short, Self Contained, Correct (Compilable), Example (sscce.org)。不要指望有人拉你的github项目,为你解决一切。
  • 整个代码不超过200行,我以为github会更容易阅读,但我很快将代码添加到第一篇文章中。
  • @JohnSmith,你错过了“短而独立”的部分。
  • 对不起,但我不确定要发布代码的哪一部分,因为我不知道我的错误在哪里。短(小)- 200 行不是那么多......自给自足,不是吗?复制、粘贴、编译,一切搞定。我不确定你的例子是什么意思?我解释了我面临的问题并寻求帮助..我尝试了一切,甚至重写了整个代码..
  • @JohnSmith 这不是发布一般调试代码的论坛;如果您将问题缩小到您不了解其行为的特定代码段并生成 SSCCE 测试用例,那么肯定会有人提供帮助

标签: c++ linux


【解决方案1】:

我认为您的问题可能部分出在 get_next_position_to 函数中,您可以在其中增加传递给函数的主体部分的位置。我认为这应该只是添加。 'RIGHT' 情况也同时修改了 X 和 Y。

position_t get_position_next_to(body_part_t *part) {
    position_t pos;
    switch(part->dir) {
    case UP:
        initialize_position(&pos, part->pos.x, part->pos.y + 1);
        break;
    case DOWN:
        initialize_position(&pos, part->pos.x, part->pos.y - 1);
        break;
    case LEFT:
        initialize_position(&pos, part->pos.x - 1, part->pos.y);
        break;
    case RIGHT:
        initialize_position(&pos, part->pos.x + 1, part->pos.y);
        break;
    default:
        break;
    }
    return pos;
}

此外,您的 move_snake 函数不会更新以下身体部位。由于您使用的是链表,因此您可以将列表的最后一个元素移动到头部之后,并将位置等从头部复制到下一个元素,然后再移动头部。为此,您需要将列表更改为双向列表。您可以使用循环缓冲区更有效地编写此代码。

您的列表清理功能是错误的,因为您在再次访问之前释放了下一个元素。您需要存储一个临时文件并进行迭代,然后释放临时文件。而且你绝对不应该释放 &snake.head 因为你没有 malloc 它。应该是这样的:

void cleanup() {
    body_part_t* tmp;
    body_part_t *next = snake.head.next;
    while (next) {
        tmp = next;
        next = next->next;
        free(tmp);
    }
}

【讨论】:

  • 终于有人决定查看我的代码并帮助我。非常感谢您的帮助,我会尽快解决我的错误。
猜你喜欢
  • 2015-06-30
  • 2019-05-17
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
  • 2012-12-29
  • 2022-11-12
  • 2023-04-07
相关资源
最近更新 更多