蛇的头部离身体有几个点的距离(c++游戏)
The head of the snake is a couple points away from the body (c++ game)
我在这个项目上工作了一段时间了,我在项目中找不到我的问题。URL: https://github.com/blackwolf12333/Snake蛇离尸体只有几个点的距离,似乎没有移动。我之前有一个段错误,我修复了,但我找不到这个错误的问题!我几乎什么都试过了,从头到尾重写了代码。它是用Linux编写的!
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: %dtpart x: %dtpart y: %dn", 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++;
}
move.h
#ifndef MOVE_H
#define MOVE_H
void move_up();
void move_down();
void move_left();
void move_right();
#endif
position.c
#include "position.h"
void initialize_position(position_t *pos, int x, int y) {
pos->x = x;
pos->y = y;
}
position.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
我认为你的问题可能部分是在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函数不会更新以下身体部位。因为你使用的是链表,你可以将列表的最后一个元素移动到头部之后,并在移动头部之前将位置等从头部复制到下一个元素。为此,您需要将列表更改为双向列表。使用循环缓冲区可以更有效地编写此代码。
你的列表清理函数是错误的,因为你在再次访问下一个元素之前释放了它。您需要存储一个临时对象并进行迭代,然后释放临时对象。你绝对不应该放蛇。头,因为你没有把它弄乱。应该是这样的:
void cleanup() {
body_part_t* tmp;
body_part_t *next = snake.head.next;
while (next) {
tmp = next;
next = next->next;
free(tmp);
}
}
相关文章:
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 用C++程序计算圆锥体的体积、球体的体积、八边形的面积和两点之间的距离
- 计算所有对之间的曼哈顿距离
- 如何使用发送数据包所花费的时间计算两个节点之间的距离?
- Levenshtein 两个文件的距离花费了太多时间
- 两个有符号数字之间的距离
- 计算车辆之间的距离并设置速度,使距离保持不变,例如 5 米
- 计算两个迭代器之间的距离时"Vector Iterators Incompatible"
- 浮动距离为80位长双精度
- 查找彼此之间具有不同最小距离的随机数
- 相同字符之间的最小距离
- 如何根据距离矩阵确定节点?
- 如何排列二进制字符串以最小化它们之间的距离
- 曼哈顿距离比曼哈顿距离+线性冲突效果更好
- 使用C++中的链表对与原点的距离、x 和 y 坐标进行排序
- 应该如何编写用于计算最近点距离的C++函数?
- 有效地计算像素到其对应核线的距离
- 堆和堆栈之间的距离
- std::使用迭代器映射查找距离,程序不会终止
- OpenLayers距离公式