第二次做动画显示效果的小程序,心血来潮想做下儿时的经典游戏----贪吃蛇。由于时间有限,只是简单地做了基本功能,有时间后再完善更多功能。

由于个人水平有限,可能代码有些地方可以改进。不足之处敬请指出。

注:要运行该代码需要正确配置,连接好ege图形库的头文件,做好准备工作。具体教程请看http://jingyan.baidu.com/article/4b07be3c40d35f48b380f3c7.html

编程环境:codeblocks   windows10  corei5 cpu

源代码:

#include<stdio.h>
#include<graphics.h>
#include<windows.h>
#include<stdlib.h>
#define Random(x) (rand() % x)
struct position
{
int x;int y;
position* next;
};
class snake
{
private:
int direction;
public:
position *tail;
position *head;
position* gettail(){return tail;}
void settail(position* p){tail=p;}
position* gethead(){return head;}
void sethead(position* p){head=p;}
snake()
{
tail=new position;head=new position;
head->x=75;head->y=15;
tail->x=15;tail->y=15;
tail->next=new position;
tail->next->x=45;tail->next->y=15;
tail->next->next=head;
direction=4;
}
void setdirection(int a){direction=a;}
int getdirection(){return direction;}
void move(int x0,int y0,bool &newfood)
{
switch(direction)
{position*p;int xt;int yt;
case 1:{head->next=new position;xt=head->x;yt=head->y;head=head->next;head->x=xt;head->y=yt-30;p=tail->next;delete tail;tail=p;break;}
case 2:{head->next=new position;xt=head->x;yt=head->y;head=head->next;head->x=xt;head->y=yt+30;p=tail->next;delete tail;tail=p;break;}
case 3:{head->next=new position;xt=head->x;yt=head->y;head=head->next;head->x=xt-30;head->y=yt;p=tail->next;delete tail;tail=p;break;}
case 4:{head->next=new position;xt=head->x;yt=head->y;head=head->next;head->x=xt+30;head->y=yt;p=tail->next;delete tail;tail=p;break;}
}
if(head->x==x0&&head->y==y0)
{
newfood=1;
switch(direction)
{int xt;int yt;
case 1:{head->next=new position;xt=head->x;yt=head->y;head=head->next;head->x=xt;head->y=yt-30;break;}
case 2:{head->next=new position;xt=head->x;yt=head->y;head=head->next;head->x=xt;head->y=yt+30;break;}
case 3:{head->next=new position;xt=head->x;yt=head->y;head=head->next;head->x=xt-30;head->y=yt;break;}
case 4:{head->next=new position;xt=head->x;yt=head->y;head=head->next;head->x=xt+30;head->y=yt;break;}
}
}
}
void show()
{
position *p=gettail();int sum=0;
while(p!=gethead()->next)
{sum++;
if(head->x>0&&head->x<900&&head->y>0&&head->y<900)
{
setfillcolor(EGERGB(255,0,0));
bar(p->x-15,p->y-15,p->x+15,p->y+15);
setfillcolor(EGERGB(0,0,255));
bar(p->x-13,p->y-13,p->x+13,p->y+13);
p=p->next;
}
else
{
setcolor(EGERGB(255,0,0));
setfontbkcolor(EGERGB(0x80,0x00,0x80));
setfont(100,0,"宋体");
outtextxy(250,360,"GAMEOVER");
if(sum<20)outtextxy(250,460,"菜鸟水平");
if(sum>=20&&sum<40)outtextxy(250,460,"高手水平");
setfont(70,0,"宋体");
if(sum>=40&&sum<60)outtextxy(100,460,"你骨骼精奇,是个奇才");
if(sum>=60)outtextxy(200,360,"你已经成仙了");
Sleep(1000);
getch();
}
int hx,hy;
hx=head->x;hy=head->y;
position *p2=gettail();
while(p2!=gethead())
{
if(hx==p2->x&&hy==p2->y)
{
setcolor(EGERGB(255,0,0));
setfontbkcolor(EGERGB(0x80,0x00,0x80));
setfont(100,0,"宋体");
outtextxy(250,360,"GAMEOVER");
if(sum<20)outtextxy(250,460,"菜鸟水平");
if(sum>=20&&sum<40)outtextxy(250,460,"高手水平");
setfont(70,0,"宋体");
if(sum>=40&&sum<60)outtextxy(100,460,"你骨骼精奇,是个奇才");
if(sum>=60)outtextxy(200,360,"你已经成仙了");
Sleep(1000);
getch();
}
p2=p2->next;
}
}
}
};
int main()
{
INITGRAPH(900,900);
setfillcolor(EGERGB(255,0,0));
snake s1;
char k;
bool newfood=1;
for(;;)
{
delay_fps(8);
int fx;int fy;
if(newfood)
{
//下面的代码是食物的显示
L:fx=Random(30)+1;fy=Random(30)+1;//贪吃蛇的食物产生位置是随机的
fx=fx*30-15;fy=fy*30-15;
bool bo=1;
position *p=s1.gettail();
while(p!=s1.gethead()->next)
{
if(p->x==fx&&p->y==fy){bo=0;break;}//食物不能产生在蛇身体的位置;
p=p->next;
}
if(!bo)goto L;
newfood=0;
}
//手动模式:
if(kbhit())
{k=getch();
switch(k)
{
case 38:if(s1.getdirection()!=2)s1.setdirection(1);break;//根据键值修改对应的方向
case 40:if(s1.getdirection()!=1)s1.setdirection(2);break;
case 37:if(s1.getdirection()!=4)s1.setdirection(3);break;
case 39:if(s1.getdirection()!=3)s1.setdirection(4);break;
}
}
s1.move(fx,fy,newfood);
cleardevice();
s1.show();
setfillcolor(EGERGB(0,0,255));
bar(fx-15,fy-15,fx+15,fy+15);
setfillcolor(EGERGB(0,255,0));
bar(fx-15,fy-15,fx+15,fy+15);
//Sleep(80);
}
}

运行效果截图:

ege图形库之简单贪吃蛇(c++)的更多相关文章

  1. 用OpenGL简单编写的一个最简单贪吃蛇游戏

    刚学OpenGL的时候,写的一个最简单的贪吃蛇游戏代码 如下: //贪吃蛇游戏 #include<stdio.h> #include<stdlib.h> #include< ...

  2. java 简单贪吃蛇

    1. [代码]java 简单程序     跳至 [1] [全屏预览]package com.snake;import java.awt.*;import javax.swing.*;import ja ...

  3. Python实例:贪吃蛇(简单贪吃蛇编写)🐍

    d=====( ̄▽ ̄*)b 叮~ Python -- 简易贪吃蛇实现 目录: 1.基本原理 2.需要学习的库 3.代码实现 1.基本原理 基本贪吃蛇所需要的东西其实很少,只需要有一块让蛇动的屏幕, 在 ...

  4. python版本的简单贪吃蛇

    先看看效果,白色的条是蛇(简单勿怪,有研究的同学请告知做的美观点),做了一个笑脸是糖果,背景弄了一个图, 代码也是从其他人那边弄来的,改了一部分直接可以在window上直接运行 代码如下: #codi ...

  5. 贪吃蛇—C—基于easyx图形库(上):基本控制函数实现 画图程序

    自从学了c语言,就一直想做个游戏,今天将之付之行动,第一次写的特别烂,各种bug:就不贴了.今天网上看了好几个贪吃蛇,重新写了一次,做出来的效果还可以. p.s.  easyx图形库是为了方便图形学教 ...

  6. 一个简单的c# 贪吃蛇程序

    一个简单的c#贪吃蛇程序 程序分为界面设计和程序设计:界面设计和程序设计均参考了一些游戏实例,但是所有代码内容是本人编写. 由于看到别人写的程序并没有署名,这里的署名全部都是csdn官网. 游戏界面设 ...

  7. 贪吃蛇(C++实现,VC6.0编译,使用了EasyX图形库)

    程序效果: 代码: //main.cpp 1 #include <iostream> #include<fstream> #include <graphics.h> ...

  8. 做梦想起来的C#简单实现贪吃蛇程序(LinQ + Entity)

    最近一直在忙着单位核心开发组件的版本更新,前天加了一个通宵,昨天晚上却睡不着,脑子里面突然不知怎的一直在想贪吃蛇的实现方法.以往也有类似的情况,白天一直想不通的问题,晚上做梦有时会想到更好的版本,于是 ...

  9. 使用TypeScript实现简单的HTML5贪吃蛇游戏

    TypeScript是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类型和基于类的面向对象编程.安德斯·海尔斯伯格,C#的首席架构师,已 ...

随机推荐

  1. Go语言学习之数据类型

    ### Go语言学习之数据类型 数据类型的转换 1.Go语言不允许隐式类型转换(显示转换才可以) 2.别名和原有类型也不能进行隐式类型转换 例子: func TestImplicit(t *testi ...

  2. opencv实现人脸识别(四) 人脸识别模块

    到这一步就是进行人脸识别了. 流程图: 代码: import cv2 def recognize(cam): recognizer = cv2.face.LBPHFaceRecognizer_crea ...

  3. k8s之网络插件flannel及基于Calico的网络策略

    1.k8s网络通信 a.容器间通信:同一个pod内的多个容器间的通信,通过lo即可实现; b.pod之间的通信:pod ip <---> pod ip,pod和pod之间不经过任何转换即可 ...

  4. 怎样查看 MySQL 版本号

    1. 在命令行中直接查看版本号 mysql -V 2. 在 mysql --help 中查找与版本相关的信息 mysql --help | grep Ver 3. 在mysql命令行里面查看版本信息 ...

  5. vue中$router 与 $route区别

    vue-router中经常会操作的两个对象\(route和\)router两个. 1.$route对象 $route对象表示当前的路由信息,包含了当前 URL 解析得到的信息.包含当前的路径,参数,q ...

  6. 工作单元 — Unit Of Work

    在进行数据库添加.修改.删除时,为了保证事务的一致性,即操作要么全部成功,要么全部失败.例如银行A.B两个账户的转账业务.一方失败都会导致事务的不完整性,从而事务回滚.而工作单元模式可以跟踪事务,在操 ...

  7. http 协议相关问题

    http 协议相关问题 来源 https://www.cnblogs.com/lingyejun/p/7148756.html 1.说一下什么是Http协议? 对器客户端和 服务器端之间数据传输的格式 ...

  8. 如何确定asp.net请求生命周期的当前处理事件

    1 首先在全局应用程序里面添加如下代码 using System; using System.Collections.Generic; using System.Linq; using System. ...

  9. 关于iReport5.6.0无法正常启动或者闪退或者JDK8不兼容的解决方案

    参考网址: https://blog.csdn.net/erlian1992/article/details/76359191?locationNum=6&fps=1 说白了 ,即 jaspe ...

  10. Linux的bg和fg命令

    我们都知道,在 Windows 上面,我们要么让一个程序作为服务在后台一直运行,要么停止这个服务.而不能让程序在前台后台之间切换.而 Linux 提供了 fg 和 bg 命令,让我们轻松调度正在运行的 ...