我正试图更好地掌握如何在C/Ncurses中创建一个蛇运动。我遇到的一个问题是,当我向另一个方向移动时,这些小径会留下。“尾盘大小”稍后会被用来增加蛇的生长,但我现在把它设为3,这样就可以看到发生了什么。
下面是一个基本的功能,我想先开始工作,然后再继续。在转向时,有什么方法可以解决这个问题吗?
提前谢谢。
#include <stdio.h>
#include <ncurses.h>
void movesnake();
int main()
{
int ymax,xmax;
int wymax,wxmax;
int snake='O';
int tail='O';
int snakelocationy,snakelocationx;
int input;
int tailsize=3;
initscr();
noecho;
getmaxyx(stdscr,ymax,xmax);
WINDOW* border=newwin(ymax/2,xmax/2,ymax/4,xmax/4);
box(border,0,0);
getmaxyx(border,wymax,wxmax);
keypad(stdscr,true);
keypad(border,true);
snakelocationy=wymax/2;
snakelocationx=wxmax/2;
mvwaddch(border,snakelocationy,snakelocationx, snake);
refresh();
wrefresh(border);
input=getch();
while(1)
{
mvwaddch(border,snakelocationy,snakelocationx, snake);
input=getch();
movesnake(border, &snakelocationy, &snakelocationx, input, tailsize);
refresh();
wrefresh(border);
}
endwin();
return 0;
}
void movesnake(WINDOW* border, int *pSnakelocationy, int *pSnakelocationx , int input , int tailsize)
{
if (input==KEY_UP)//up
{
mvwaddch(border,*pSnakelocationy+tailsize,*pSnakelocationx, ' ');
(*pSnakelocationy)--;
}
else if (input==KEY_DOWN)
{
mvwaddch(border,*pSnakelocationy-tailsize,*pSnakelocationx, ' ');
(*pSnakelocationy)++;
}
else if (input==KEY_LEFT)
{
mvwaddch(border,*pSnakelocationy,*pSnakelocationx+tailsize, ' ');
(*pSnakelocationx)--;
}
else if (input==KEY_RIGHT)
{
mvwaddch(border,*pSnakelocationy,*pSnakelocationx-tailsize, ' ');
(*pSnakelocationx)++;
}
}发布于 2022-06-13 12:27:23
当你的蛇移动时,它可能会扭动和转动,所以尾巴相对于头部的位置并不总是N个位置在一个单一的方向上。
你必须保持更多的状态。
实现这一目标的一种方法是使用一个包含每个蛇部分的数组。当蛇移动:从尾巴开始,每一个蛇部件被更新的值与部分的一个更接近头部。头部被更新到最新的位置。
屏幕将被清除,并重新绘制,将蛇绘制为一个单一的实体。
下面是一个粗略的例子。你会注意到蛇的转向是没有限制的,没有什么能阻止蛇通过它自己,或者在屏幕上徘徊。这是下一个要解决的问题(提示:跟踪蛇的方向,知道它可以去哪里,不能去哪里)。
#include <ncurses.h>
#define SNAKE_HEAD '@'
#define SNAKE_TAIL 'O'
struct pos {
int y;
int x;
};
void update_snake(struct pos *s, size_t len, int kp)
{
int y = 0, x = 0;
switch (kp) {
case KEY_UP:
y--; break;
case KEY_DOWN:
y++; break;
case KEY_LEFT:
x--; break;
case KEY_RIGHT:
x++; break;
default:
return;
}
for (size_t i = len - 1; i > 0; i--)
s[i] = s[i - 1];
s[0].y += y;
s[0].x += x;
}
void draw_snake(WINDOW *ga, struct pos *s, size_t len)
{
mvwaddch(ga, s[0].y, s[0].x, SNAKE_HEAD);
for (size_t i = 1; i < len; i++)
mvwaddch(ga, s[i].y, s[i].x, SNAKE_TAIL);
}
int main(void)
{
initscr();
noecho();
halfdelay(1);
curs_set(0);
int ymax, xmax;
getmaxyx(stdscr, ymax, xmax);
WINDOW *game_area = newwin(ymax / 2, xmax / 2, ymax / 4, xmax / 4);
keypad(game_area, TRUE);
struct pos orig;
getmaxyx(game_area, orig.y, orig.x);
orig.y /= 2;
orig.x /= 2;
struct pos snake_parts[100] = { 0 };
size_t segments = 10;
for (size_t i = 0; i < segments; i++) {
snake_parts[i] = orig;
snake_parts[i].x += i;
}
while (1) {
int input = wgetch(game_area);
if ('q' == input)
break;
update_snake(snake_parts, segments, input);
wclear(game_area);
box(game_area, 0, 0);
draw_snake(game_area, snake_parts, segments);
wrefresh(game_area);
}
delwin(game_area);
endwin();
}https://stackoverflow.com/questions/72600650
复制相似问题