供参考,以下是我的作业链接(课程是C语言课程):Snake
嘴巴应该是"<&#;;">"," v"或" ^" ,取决于方向,身体部分应该是" *"。
首先,我的向上和向下箭头键控制被切换,但它打印出正确的字符(即打印" v"向下移动时" ^"当向上移动),我无法弄清楚为什么。
此外,当头部移动时,它只是脱离身体并且不会打印新的身体部分(参见Screencap)。
下面是我的自定义结构,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;
}
答案 0 :(得分:2)
身体脱离头部的原因是因为你的动作代码:
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值向上移动而不是向下移动)。你应该检查哪种方式,并确保你的代码匹配。