作为参考,这里是我的作业链接(该课程是一门C语言课程):
蛇根据方向,嘴巴应该是"<"、">"、"v"或"^",身体部分应该是"*"。
所以首先,我的向上和向下箭头键控件是切换的,但它打印了正确的字符(即向下移动时打印"v",向上移动时打印"^"),我无法弄清楚为什么。
此外,当头部移动时,它只是与身体分离,不会打印新的身体片段(请参阅屏幕盖)。
以下是我的自定义结构,main,scene_render和scene_update函数(我们不允许修改此作业的主函数):
struct Point{
int x;
int y;
};
struct Snake {
struct Point segments[MAX_SEGMENTS];
int num_segments;
int dir; //0=up, 1=down, 2=left, 3=right
};
struct Scene {
// TODO: add fields
struct Snake snake;
struct Point fruit;
};
主要
int main(void) {
// Important: do NOT modify the main function
struct Scene scene;
scene_init(&scene);
int keep_going = 1;
while (keep_going == 1) {
scene_render(&scene);
cons_update();
scene_delay(&scene);
keep_going = scene_update(&scene);
}
return 0;
}
scene_render
void scene_render(const struct Scene *s) {
// TODO: add your code
int num=s->snake.num_segments;
cons_clear_screen();
cons_move_cursor(s->snake.segments[0].y, s->snake.segments[0].x);
if(s->snake.dir==0)
{
cons_printw("v");
}
else if(s->snake.dir==1)
{
cons_printw("^");
}
else if(s->snake.dir==2)
{
cons_printw(">");
}
else
{
cons_printw("<");
}
for(int i=1; i <num; i++)
{
cons_move_cursor(s->snake.segments[i].y, s->snake.segments[i].x);
cons_printw("*");
}
cons_move_cursor(23, 79); //just to get blinking cursor out of the way
}
scene_update
int scene_update(struct Scene *s) {
// This function should return 0 if the player presses 'q',
// 1 otherwise.
struct Scene temp=*s;
int num=temp.snake.num_segments;
int key = cons_get_keypress();
// TODO: add your code
if(key=='q')
{
return 0;
}
if(key==UP_ARROW && temp.snake.dir!=1)
{
temp.snake.dir=0;
temp.snake.segments[0].y=(temp.snake.segments[0].y)+1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
else if(key==DOWN_ARROW && temp.snake.dir!=0)
{
temp.snake.dir=1;
temp.snake.segments[0].y=(temp.snake.segments[0].y)-1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
else if(key==LEFT_ARROW && temp.snake.dir!=3)
{
temp.snake.dir=2;
temp.snake.segments[0].x=(temp.snake.segments[0].x)-1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
else if(key==RIGHT_ARROW && temp.snake.dir!=2)
{
temp.snake.dir=3;
temp.snake.segments[0].x=(temp.snake.segments[0].x)+1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
else if(key==-1)
{
if(temp.snake.dir==0)
{
temp.snake.segments[0].y=(temp.snake.segments[0].y)+1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
else if(temp.snake.dir==1)
{
temp.snake.segments[0].y=(temp.snake.segments[0].y)-1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
else if(temp.snake.dir==2)
{
temp.snake.segments[0].x=(temp.snake.segments[0].x)-1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
else
{
temp.snake.segments[0].x=(temp.snake.segments[0].x)+1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
}
temp.fruit.x=s->fruit.x;
temp.fruit.y=s->fruit.y;
*s=temp;
return 1;
}
身体与头部分离的原因是因为您的动作代码:
if(temp.snake.dir==0)
{
temp.snake.segments[0].y=(temp.snake.segments[0].y)+1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i].y;
temp.snake.segments[i].x=s->snake.segments[i].x;
}
}
应该是:
if(temp.snake.dir==0)
{
temp.snake.segments[0].y=(temp.snake.segments[0].y)+1;
for(int i=1; i<num; i++)
{
temp.snake.segments[i].y=s->snake.segments[i-1].y;
temp.snake.segments[i].x=s->snake.segments[i-1].x;
}
}
换句话说,身体的第一个部位应该移动到头部曾经的位置。 第二个身体部分应该移动到第一个部分以前的位置,依此类推。 你现在拥有它的方式,所有的身体部位都一直呆在同一个地方。 您需要在很多地方进行此更改,或者更好的是创建一个函数来移动正文,这样您就不会到处都有重复的代码。
至于为什么向上是向下,反之亦然,我只能想象你屏幕的y坐标与你认为的相反(增加y值向上移动与向下移动)。 您应该检查哪种方式是哪种方式,并确保您的代码匹配。