非常好用, 废话不多说(我猜你们也是直接复制粘贴的),直接上代码:
#include<iostream> #include "stdio.h" #include "conio.h" #include <conio.h> #include<ctime> #include <windows.h> using namespace std; int fdx,fdy; int length=3; int speed=300; void gotoxy(int x,int y) //控制光标坐标 { COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos); } void print_noodle()//打印边框 { for(int i=1;i<=21;i++)//左右边框 { gotoxy(29,i); cout<<"#"; gotoxy(81,i); cout<<"#"; gotoxy(83,10); cout<<"当前长度:"; gotoxy(83,11); cout<<"目标长度:29"; gotoxy(83,12); cout<<"当前速度:"; } for(int i=30;i<81;i++)//上下边框 { gotoxy(i,1); cout<<"#"; gotoxy(i,21); cout<<"#"; } } void in_food()//打印食物 { fdx=rand()%51+30; fdy=rand()%19+2; gotoxy(fdx,fdy); cout<<"*"; } struct snack//蛇结构体 { int x[30],y[30]; } ; snack s1; struct snack*p_snack=&s1; void grow_snack()//蛇增长 { length++; } void mov_snack(struct snack*p_snack)//移动蛇 { for(int i=30-length;i<29;i++) { p_snack->x[i]=p_snack->x[i+1]; p_snack->y[i]=p_snack->y[i+1]; } if(p_snack->x[29]==fdx&&p_snack->y[29]==fdy)//蛇吃食物和消除食物 { gotoxy(fdx,fdy); cout<<" "; in_food(); grow_snack(); mov_snack(&s1); gotoxy(92,10); cout<<length-1; speed=speed-10;//控制蛇速度 gotoxy(92,12); cout<<speed; } } int cond_snack()//蛇撞到自己退出游戏 { int l=0; for(int i=30-length;i<29;i++) { if(p_snack->x[29]==p_snack->x[i]&&p_snack->y[29]==p_snack->y[i]) l=1; } return l; } void print(struct snack*p_snack)//打印蛇 { for(int i=29;i>(29-length);i--) { gotoxy(p_snack->x[i],p_snack->y[i]); gotoxy(p_snack->x[i],p_snack->y[i]); cout<<"0"; } gotoxy(p_snack->x[30-length],p_snack->y[30-length]); cout<<" ";//消除尾巴 } int main() { p_snack->x[29]=40; p_snack->x[28]=39; p_snack->x[27]=38; p_snack->y[29]=9; p_snack->y[28]=9; p_snack->y[27]=9; print(&s1); char a; in_food(); print_noodle(); while(1) { a=getch(); while(!kbhit()) { switch(a) { case 'a': mov_snack(&s1); p_snack->x[29]-=1; break; case 'd': mov_snack(&s1); p_snack->x[29]+=1; break; case 's': mov_snack(&s1); p_snack->y[29]+=1; break; case 'w': mov_snack(&s1); p_snack->y[29]-=1; break; } if(p_snack->x[29]==29||p_snack->x[29]==81|| p_snack->y[29]==1||p_snack->y[29]==21||cond_snack()) { goto lost; } print(&s1); Sleep(speed); } } lost: system("cls"); gotoxy(20,10); cout<<"oh,失败了,不要灰心,我会一直陪着你的!"; gotoxy(18,18); cout<<"玩的开心,制作者--wizard.gray//昔非今比"; gotoxy(50,20); cout<<"\n"; }okok,这是原创欧,复制完后记得点个小心心在关注一下欧!!!
笔芯~~~! 原创:wizard.gray (防抄袭标记不用在意)