草庐IT

贪吃蛇

EAGLE-- 2023-06-02 原文

#include<time.h>#include<conio.h>#include<stdlib.h>#include<Windows.h>using namespace std;int speed,count;typedef struct _SNAKE{ int x; int y; struct _SNAKE *next;}Snake;Snake *s_Head = NULL;typedef struct _FOOD{ int x; int y;}food;food _food = NULL;enum{up=72,down=80,left=75,right=77};void BeginGame();//开始游戏 void RunGame();//运行游戏 void Welcomeplayer();//欢迎玩家 void setCursor(int x,int y);//设置光标 void color(int m);//设置颜色 void buildmap();//建立地图void setSnake();//生成一条蛇 void MoveSnake(int direction);//移动蛇 void ProduceFood();//随机生成食物void extend(int direction);//伸长蛇身void deadPrint();//死亡画面 int dead();//碰撞死亡 int check();//判断是否吃到食物 int main(){ BeginGame(); return 0;}void BeginGame(){ system(“c:\windows\system32\mode.com con cols=120 lines=50”); Welcomeplayer();}void Welcomeplayer(){ while(1) { setCursor(39,15); printf("\t 欢迎来到贪吃蛇的世界\n"); setCursor(39,17); puts("\t在这里你将体会到一种别样的贪吃蛇\n"); puts("\n\n\n\n\n\n\n\n\n\n"); puts(“按任意键继续”); puts(“按Esc退出”); char ch=getch(); if(ch==27)return; system(“cls”); RunGame(); }}void buildmap(){ for(int i=0;i<=60;i+=2) { setCursor(i,0); printf(“■”); setCursor(i,1); printf(“■”); setCursor(i,29); printf(“■”); setCursor(i,30); printf(“■”); } for(int i=0;i<=30;i++) { setCursor(0,i); printf(“■”); setCursor(2,i); printf(“■”); setCursor(60,i); printf(“■”); setCursor(62,i); printf(“■”); }}void setSnake(){ Snake temp = (Snake)malloc(sizeof(Snake)); temp->x=20; temp->y=15; temp->next=NULL; s_Head=temp; for(int i=1;i<=3;i++) { Snake p=(Snake)malloc(sizeof(Snake)); p->x=20+2i; p->y=15; p->next=NULL; temp->next=p; temp=p; } temp=s_Head; while(temp!=NULL) { setCursor(temp->x,temp->y); printf(“■”); temp=temp->next; }}void RunGame(){ loop: setCursor(33,15); puts("\t\t 请选择游戏难度\n"); puts("\t\t\t\t\t\t 1.简单\n"); puts("\t\t\t\t\t\t 2.一般\n"); puts("\t\t\t\t\t\t 3.困难\n"); puts("\t\t\t\t\t\t 4.特别困难"); char ch=getch(); while(ch!=‘1’&&ch!=‘2’&&ch!=‘3’&&ch!=‘4’) { ch=getch(); } speed=(5-(ch-‘0’))*90; system(“cls”); buildmap(); setSnake(); int direction=right; ProduceFood(); int res=0; res= res>count?res:count; setCursor(80,15); printf(“历史最高分:\n”); setCursor(89,16); printf("%d",res); setCursor(80,20); count=0; printf(“得分:\n”); while(1) { setCursor(83,21); printf("%d",count); char ch; if(kbhit()) { ch=getch(); switch(ch) { case 72://上 if(fabs(ch-direction)!=8) direction=up; break; case 75://左 if(fabs(ch-direction)!=2) direction=left; break; case 77://右 if(fabs(ch-direction)!=2) direction=right; break; case 80://下 if(fabs(ch-direction)!=8) direction=down; break; } } MoveSnake(direction); if(check()) { extend(direction); count++; ProduceFood(); } if(dead()1) { system(“cls”); goto loop; } else if(dead()0) { system(“cls”); setCursor(55,20); printf(“游戏结束”); puts("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); system(“pause”); system(“cls”); return; } Sleep(speed); } }int check(){ Snake *p=s_Head; while(p!=NULL&&p->next!=NULL) { p=p->next; } if(p->x_food->x&&p->y_food->y) return 1; return 0;} void extend(int direction){ Snake *p=s_Head; while(p!=NULL&&p->next!=NULL) { p=p->next; } if(p->x==_food->x&&p->y==_food->y) { Snake *temp=(Snake *)malloc(sizeof(Snake)); temp->next = NULL; if(direction == right) { temp->x = p->x + 2; temp->y = p->y; } if(direction == left) { temp->x = p->x - 2; temp->y = p->y; } if(direction == up) { temp->x = p->x; temp->y = p->y - 1; } if(direction == down) { temp->x = p->x; temp->y = p->y + 1; } p->next = temp; setCursor(temp->x,temp->y); printf(“■”); }}void MoveSnake(int direction){ //找到链尾 (即蛇头) Snake *p=s_Head; while(p!=NULL&&p->next!=NULL) { p=p->next; } //增尾 Snake temp = (Snake)malloc(sizeof(Snake)); temp->next = NULL; if(direction == right) { temp->x = p->x + 2; temp->y = p->y; } if(direction == left) { temp->x = p->x - 2; temp->y = p->y; } if(direction == up) { temp->x = p->x; temp->y = p->y - 1; } if(direction == down) { temp->x = p->x; temp->y = p->y + 1; } p->next = temp; //打印 setCursor(temp->x,temp->y); printf(“■”); setCursor(s_Head->x,s_Head->y); printf(" "); //删头 temp=s_Head->next; delete s_Head; s_Head=temp; }void ProduceFood(){ srand(time(NULL)); food p=(food)malloc(sizeof(food)); while(1) { p->x=rand()%60; p->y=rand()%29; if(p->x > 4&&p->x<57&&p->y > 4&&p->y< 30&&p->x%20&&p->y%20) break; } _food=p; setCursor(p->x,p->y); color(4); printf(“●”); color(7);}int dead(){ Snake *temp=s_Head->next; while(temp!=NULL) { if(temp->xs_Head->x && temp->ys_Head->y) { deadPrint(); char ch=getch(); while(ch!=‘Y’&&ch!=‘y’&&ch!=‘N’&&ch!=‘n’) { ch=getch(); } if(ch==‘Y’||ch==‘y’)return 1; if(ch==‘N’||ch==‘n’)return 0; } temp=temp->next; } Snake *p=s_Head; while(p!=NULL&&p->next!=NULL) { p=p->next; } if((p->x == 60||p->x2)||(p->y1||p->y29)){ deadPrint(); char ch=getch(); while(ch!=‘Y’&&ch!=‘y’&&ch!=‘N’&&ch!=‘n’) { ch=getch(); } if(ch’Y’||ch==‘y’)return 1; if(ch==‘N’||ch==‘n’)return 0; }} void deadPrint(){ system(“cls”); setCursor(54,15); printf(“你已经死亡”); setCursor(54,17); puts(“是否重新开始”); setCursor(45,19); puts(“是Y\t\t\t否N “); puts(”\n\n\n\n\n\n\n”);}void color(int m){ if(m>=0 && m<=15)//参数在0-15的范围颜色 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), m); //只有一个参数,改变字体颜色 else//默认的颜色白色 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);}void setCursor(int x,int y){ COORD gotoxy{x,y}; HANDLE hand=GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hand,gotoxy);}

有关贪吃蛇的更多相关文章

  1. 100个Python实战项目(九)制作贪吃蛇游戏(评论抽奖送书) - 2

    ?个人网站:【海拥】【摸鱼小游戏】【开发文档导航】?风趣幽默的人工智能学习网站:?人工智能?想寻找共同学习交流的小伙伴,请点击【全栈技术交流群】?免费且实用的计算机相关知识题库:?进来逛逛给大家安利一个免费且实用的Python刷题(面经大全)网站,?点击跳转到网站。本文章为系列文章,共100个python实战项目。初学者可以尝试实现这些项目,并在Python编译环境中动手操作。所有项目都已收集在专栏:

  2. 强化学习实战:AI玩贪吃蛇(PyTorch) - 2

    文件game.py游戏用的是pygame库。pygame中的坐标轴init我使用了collections中的namedtuple作为坐标。游戏中的蛇头、蛇身、食物都会用Point表示。定义了方向的枚举类,用来表示方向。Point=namedtuple('Point','x,y')classDirection(Enum):LEFT=1RIGHT=2UP=3DOWN=4def__init__(self,w=640,h=480):self.W=w#窗口的宽self.H=h#窗口的高self.direction=Direction.RIGHT#一开始的方向为右self.display=pygame.d

  3. c++ - 贪吃蛇游戏: fast response vs.碰撞错误 - 2

    我有一个用SFMLC++编写的贪吃蛇游戏,我在两个选项之间左右为难。如果像这样设置控件:if(event.type==sf::Event::KeyPressed&&(event.key.code==sf::Keyboard::Up||event.key.code==sf::Keyboard::W)&&move!=Down)move=Up;elseif(event.type==sf::Event::KeyPressed&&(event.key.code==sf::Keyboard::Down||event.key.code==sf::Keyboard::S)&&move!=Up)move

  4. 学生学python编程---实现贪吃蛇小游戏+源码 - 2

    学生学python编程---实现贪吃蛇小游戏+源码前言主要设计1、蛇的表示2、蛇怎么移动?3、玩家控制小蛇移动功能的实现4、如何判定游戏结束?应用知识点1、python知识点1.1列表append()在列表未尾增加一个元素del删除最后一个元素在指定位置增加元素用insert()1.2time时间模块1.3random模块2、pygamezero知识点2.1游戏角色Acto2.2输出中文字体2.3键盘控制功能截图代码实现1、蛇的表示2、蛇的前进移动3、控制移动方向4、游戏失败5、食物的随机出现6、游戏得分总结源码获取前言这几年人工智能技术大发展,Python因此几乎成了第一位的语言。实际上,多

  5. 教你用322行Python代码编写贪吃蛇 - 2

    目录安装和导入 规则初始化设定Surface,变量和显示数字的坐标 函数线程 主要部分总结源码下载  贪吃蛇是一个很常见的小游戏,我们如何用Python去实现呢。安装和导入 pipinstallpygamepipinstallkeyboardpipinstallpickledb通过命令提示符安装所需模块。(以上非Python代码)#导入importpygame,keyboard,random,threading,time,pickledb这个程序用到了pygame作为显示模块,keyboard捕获键盘操,pickledb记录最高纪录。规则#显示规则print()print('方向键控制方向')

  6. 手把手教你使用Python写贪吃蛇游戏(pygame) - 2

    我们要使用Python编写贪吃蛇游戏,需要使用到pygame模块,即在PyCharm终端输入pipinstallpygame安装完毕即可。在pygame有三个对象比较重要,分别是pygame.display—设置场景显示,包括页面大小,页面标题,页面更新(刷新)等;pygame.time–设置一切与时间相关的设置,游戏的帧频,游戏的持续时间等;pygame.event–设置与事件相关的处理,比如鼠标点击事件,键盘按下事件等;pygame.draw–绘制图形到界面上。1.搭建初始框架要做贪吃蛇游戏,首先第一步就是搭建一个初始界面,具体包括设置界面的size,设置关闭界面事件,设置帧频以及页面背景

  7. 用java写一个贪吃蛇小游戏(源码在最后) - 2

    一、引入涉及技能:循环、分支方法的抽取数组的使用面向对象继承,子类方法的重写接口,接口的实现GUI(图像化界面编程)GUI中的组件:7.1窗口7.2弹窗7.3面板7.4文本框7.5列表框7.6按钮7.7图片7.8交互的事件:监听事件(鼠标事件,键盘事件)GUI技术,不流行了!!!界面太简单粗糙!!已经被淘汰了,为什么还学?提起兴趣分层思想锻炼监听器的思想举一反三(蛇的图片换成其他图片,食物的图片换成其他图片)工作中用到注意:学的是思想!!!二、原理图小蛇素材图像素为25*25,因此在对应xy轴上的数值也以25为步长。三、进入正题1.打开IDEA工具,新建一个模块module。2.选择java,

  8. 用java写一个贪吃蛇小游戏(源码在最后) - 2

    一、引入涉及技能:循环、分支方法的抽取数组的使用面向对象继承,子类方法的重写接口,接口的实现GUI(图像化界面编程)GUI中的组件:7.1窗口7.2弹窗7.3面板7.4文本框7.5列表框7.6按钮7.7图片7.8交互的事件:监听事件(鼠标事件,键盘事件)GUI技术,不流行了!!!界面太简单粗糙!!已经被淘汰了,为什么还学?提起兴趣分层思想锻炼监听器的思想举一反三(蛇的图片换成其他图片,食物的图片换成其他图片)工作中用到注意:学的是思想!!!二、原理图小蛇素材图像素为25*25,因此在对应xy轴上的数值也以25为步长。三、进入正题1.打开IDEA工具,新建一个模块module。2.选择java,

  9. 贪吃蛇 - 2

    #include#include#include#includeusingnamespacestd;intspeed,count;typedefstruct_SNAKE{intx;inty;struct_SNAKE*next;}Snake;Snake*s_Head=NULL;typedefstruct_FOOD{intx;inty;}food;food_food=NULL;enum{up=72,down=80,left=75,right=77};voidBeginGame();//开始游戏voidRunGame();//运行游戏voidWelcomeplayer();//欢迎玩家voidset

  10. 贪吃蛇 - 2

    #include#include#include#includeusingnamespacestd;intspeed,count;typedefstruct_SNAKE{intx;inty;struct_SNAKE*next;}Snake;Snake*s_Head=NULL;typedefstruct_FOOD{intx;inty;}food;food_food=NULL;enum{up=72,down=80,left=75,right=77};voidBeginGame();//开始游戏voidRunGame();//运行游戏voidWelcomeplayer();//欢迎玩家voidset

随机推荐