贪吃蛇c++ 乱写的
来源:互联网 发布:上海知金教育 编辑:程序博客网 时间:2024/05/20 04:11
#include<iostream>#include<conio.h>#include<windows.h>#include<ctime>#include<stdlib.h>using namespace std;struct node{ int x,y; struct node *next;};int cnt,fx,fy;// 蛇长node *tou,*wei;//头、尾指针char dir;void gotoXY(short x, short y){ COORD pos = {x,y}; HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(out,pos);}void creatfood(){ srand((unsigned)time(0)); fx=rand()%36; if(fx%2==0) fx+=2; else fx++; fy=rand()%18; fy++; gotoXY(fx,fy); cout<<"卍";}void fang(){ gotoXY(5,20); char t; while(kbhit()) { t=getch(); if(t=='w'||t=='W') if(dir!='s') dir='w'; if(t=='a'||t=='A') if(dir!='d') dir='a'; if(t=='s'||t=='S') if(dir!='w') dir='s'; if(t=='d'||t=='D') if(dir!='a') dir='d'; }}void init(){ system("cls"); dir='s'; cnt=3; node* p; tou=p=(node*)malloc(sizeof(node)); p->x=4; p->y=1; p->next=(node*)malloc(sizeof(node)); p=p->next; p->x=4; p->y=2; p->next=(node*)malloc(sizeof(node)); p=p->next; p->x=4; p->y=3; p->next=NULL; wei=p; int i; for(i=0; i<40; i+=2) { gotoXY(i,0); cout<<"■"; gotoXY(i,19); cout<<"■"; } for(i=0; i<20; i++) { gotoXY(0,i); cout<<"■"; gotoXY(38,i); cout<<"■"; } gotoXY(46,5); cout<<"按a,w,s,d移动,仅供学习之用!"; creatfood(); gotoXY(46,6); cout<<"蛇长:"<<cnt;}void move(){ node *p; int x,y; if(dir=='w') { x=wei->x; y=wei->y-1; } if(dir=='s') { x=wei->x; y=wei->y+1; } if(dir=='a') { x=wei->x-2; y=wei->y; } if(dir=='d') { x=wei->x+2; y=wei->y; } gotoXY(x,y); cout<<"★"; if(wei->x==fx&&wei->y==fy) { cnt++; gotoXY(46,6); cout<<"蛇长:"<<cnt; creatfood(); p=(node*)malloc(sizeof(node)); p->x=x; p->y=y; wei->next=p; wei=p; wei->next=NULL; } else { if(tou->x!=fx||tou->y!=fy) { gotoXY(tou->x,tou->y); cout<<" "; } tou->x=x; tou->y=y; wei->next=tou; tou=tou->next; wei=wei->next; wei->next=NULL; } if(wei->x>=38||wei->y>=19||wei->y<=0||wei->x<=0) { MessageBox(NULL,"你挂了,游戏重新开始","糟糕!",MB_OK); init(); return; } p=tou; while(p->next) { if(wei->x==p->x&&wei->y==p->y) { MessageBox(NULL,"你挂了,游戏重新开始","糟糕!",MB_OK); init(); return; } p=p->next; }}void game(){ init(); while(1) { Sleep(200); fang(); move(); }}int main(){ system("color 12"); game();}
1 0
- 贪吃蛇c++ 乱写的
- 乱写的
- c语言的贪吃蛇
- 一篇关于c的杂文,无意中乱写整理
- 一个贪吃蛇的小游戏(c++API)
- 贪吃蛇的C语言源程序
- C + SDL 贪吃蛇的基础实现
- ubuntu下做的c贪吃蛇
- 基于c语言的贪吃蛇游戏
- 用c语言写的贪吃蛇
- 贪吃蛇游戏的C语言实现
- QT写的贪吃蛇(C++)
- C语言写的贪吃蛇
- C语言贪吃蛇的完善
- 贪吃蛇C实现
- 贪吃蛇C代码
- C代码 贪吃蛇
- [C++] Console 贪吃蛇
- WordPress 模板层次详细介绍
- 历届试题 带分数
- button居中的方法之一
- Add to numbers
- VS无法正常加载Wpf后台刷新ui的自定义控件
- 贪吃蛇c++ 乱写的
- 将本地项目托管到GitHub上
- 设计一个程序输入两个日期计算相隔多少天,或计算一个日期加上间隔后的日期。
- HDU 2084 数塔【dp(递推)+(记忆化)】
- 在IOS中嵌套H5页面发生空白的问题
- MySQL数据库的函数
- python实现线性表顺序存储的插入操作
- Android Stadio 代码模板 fbc的使用
- Rviz教程-Marker:点和线(C++)