1 安装带mingw的codeblocks.
2 下载EGE图形库并解压
注意:EGE图形库主站链接是:http://xege.org/,不要在主页上下载,主页上默认下载是64位的,大家可以百度一下ege,下载一个12.xx的版本即可。
3 安装图形库
步骤如下:
4 配置codeblocks以实现编译EGE项目
(一定要在自己的项目上右键选择build options,然后设置上面的各种配置)
打开codeblocks新建一个Console项目,选择C++!!!!!!!!!注意一定要是C++,复制代码粘贴进codeblocks.
5 将配置好的工程保存为模板
Ps:为什么不直接设置为默认编译模式,是因为在设置成默认编译模式以后所有的程序都会按照EGE程序的方式编译,但是这样是会出错的,也就是说编写不使用EGE的程序时又要将千辛万苦配置的东西改回来,非常麻烦。
下图是上面代码的结果.
7 下次新建项目时,选择保存的模板.
最后看一个字母雨的例子.代码如下:
#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#define High 800
#define Width 1000
#define CharSize 25
using namespace std;
int main()
{
int highNum = High/CharSize;
int widthNum = Width/CharSize;
int CharRain[Width/CharSize][High/CharSize];
int CNum[Width/CharSize];
int ColorG[Width/CharSize];
int i,j,x,y;
srand((unsigned)time(NULL));
for(i=0;i<widthNum;i++){
CNum[i] = (rand()%(highNum*9/10))+highNum/10;
ColorG[i]=255;
for(j=0;j<CNum[i];j++)
CharRain[j][i] = (rand()%26)+65;
}
initgraph(Width,High);
setfont(25,10,"Courier");
setcolor(GREEN);
while(1){
for(i=0;i<widthNum;i++){
if(CNum[i]<highNum-1){
for(j=CNum[i]-1;j>=0;j--)
CharRain[0][i]=(rand()%26)+65;
CNum[i]=CNum[i]+1;
}else{
if(ColorG[i]>40)
ColorG[i]=ColorG[i]-20;
else{
CNum[i]=(rand()%(highNum/3))+highNum/10;//这一列字符的个数
ColorG[i]=(rand()%75)+180; //这一列字符的颜色
for(j=0;j<CNum[i];j++)
CharRain[j][i]=(rand()%26)+65;
}
}
}
//输出整个字符矩阵
for(i=0;i<widthNum;i++)
{
x=i*CharSize;
for(j=0;j<CNum[i];j++)
{
y=j*CharSize;
setcolor(RGB(0,ColorG[i],0));
outtextxy(x,y,char(CharRain[j][i]));
}
}
Sleep(100);
}
getch();
closegraph();
return 0;
}
再来一个贪吃蛇的例子。
#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);
}
return 0;
}
截图如下: