设为首页 加入收藏

TOP

贪食蛇游戏(二)
2014-11-23 23:21:01 来源: 作者: 【 】 浏览:3
Tags:游戏
[0]=RIGHT_EDGE;
turn_point[0][1]=0;
/*游戏参数初始化完毕*/

generate_food();//投下第一粒食物
gotoxy(init_position[0],init_position[1]);//将光标移动到到初始化位置
for(i=snake_length;i>0;i--){
putchar(snake[i-1]);
}
}

/*接收键盘控制命令*/
void control(){
char command;//存放接收到命令
while(1){
command=getch();
if(command==-32){
//F11,F12:-123,-122
command=getch();
if(command=='H' && (direction==LEFT || direction==RIGHT)){//光标上移
direction=UP;
turn_point[head_turn_num%TURN_NUM][0]=head[0];
turn_point[head_turn_num%TURN_NUM][1]=head[1];
head_turn_num++;
}else if(command=='P' && (direction==LEFT || direction==RIGHT)){//光标下移
direction=DOWN;
turn_point[head_turn_num%TURN_NUM][0]=head[0];
turn_point[head_turn_num%TURN_NUM][1]=head[1];
head_turn_num++;
}else if(command=='K' && (direction==UP || direction==DOWN)){//光标左移
direction=LEFT;
turn_point[head_turn_num%TURN_NUM][0]=head[0];
turn_point[head_turn_num%TURN_NUM][1]=head[1];
head_turn_num++;
}else if(command=='M' && (direction==UP || direction==DOWN)){//光标右移
direction=RIGHT;
turn_point[head_turn_num%TURN_NUM][0]=head[0];
turn_point[head_turn_num%TURN_NUM][1]=head[1];
head_turn_num++;
}else if(command==-122 || command==-123);

}else if(command==0){
command=getch();//接收Fn的下一个字符
//F1~F10:59~68
}else if(command>=1&&command<=26){
//Ctrl+a~z:1~26
if(command==3){
break;
}
}else{
//do nothing!
}
move();
if(head[0]==food[0] && head[1]==food[1]){
eat_food();//吃掉一粒事物
generate_food();//投下新食物
}
}
}

/*蛇的身体运动*/
void move(){
if(direction==UP){
//printf("Moving up!\n");
move_up();
}else if(direction==DOWN){
//printf("Moving down!\n");
move_down();
}else if(direction==LEFT){
//printf("Moving left!\n");
move_left();
}else if(direction==RIGHT){
//printf("Moving right!\n");
move_right();
}
}

/*向上运动*/
void move_up(){
if(cursor[1]>=UP_EDGE){
gotoxy(head[0],head[1]);
putchar('*');
gotoxy(head[0],head[1]-1);
putchar(snake[0]);
gotoxy(tail[0],tail[1]);
//_sleep(1000);//查看尾部光标
putchar(0);
update_tail_position();
head[1]-=1;
cursor[0]=head[0];
cursor[1]=head[1]-1;
gotoxy(cursor[0],cursor[1]);
}else{
gotoxy(head[0],head[1]);
}
}

/*向下运动*/
void move_down(){
if(cursor[1]<=DOWN_EDGE){
gotoxy(head[0],head[1]);
putchar('*');
gotoxy(head[0],head[1]+1);
putchar(snake[0]);
gotoxy(tail[0],tail[1]);
//_sleep(1000);//查看尾部光标
putchar(0);
update_tail_position();
head[1]+=1;
cursor[0]=head[0];
cursor[1]=head[1]+1;
gotoxy(cursor[0],cursor[1]);
}else{
gotoxy(head[0],head[1]);
}
}

/*向左运动*/
void move_left(){
if(cursor[0]>=LEFT_EDGE){
gotoxy(head[0],head[1]);
putchar('*');
gotoxy(head[0]-1,head[1]);
putchar(snake[0]);
gotoxy(tail[0],tail[1]);
//_sleep(1000);//查看尾部光标
putchar(0);
update_tail_position();
head[0]-=1;
cursor[0]=head[0]-1;
cursor[1]=head[1];
gotoxy(cursor[0],cursor[1]);
}else{
gotoxy(head[0],head[1]);
}
}

/*向右运动*/
void move_right(){
if(cursor[0]<=RIGHT_EDGE){
gotoxy(head[0],head[1]);
putchar('*');
putchar(snake[0]);
gotoxy(tail[0],tail[1]);
//_sleep(1000);//查看尾部光标
putchar(0);
update_tail_position();
head[0]+=1;
cursor[0]=
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇二分查找 下一篇Valgrind的使用

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: