用easyx图形库做一个简单的c++小游戏---贪吃蛇

7 篇文章 2 订阅
4 篇文章 1 订阅

用easyx图形库做一个简单的c++小游戏———贪吃蛇

贪吃蛇游戏博客链接:(方法一样,语言不一样)
c++贪吃蛇:https://blog.csdn.net/weixin_46791942/article/details/106850986
python贪吃蛇:https://blog.csdn.net/weixin_46791942/article/details/110383746
java贪吃蛇:https://blog.csdn.net/weixin_46791942/article/details/112095059

正文开始
开发环境:visual c++6.0
库:easyx图形库 下载地址>>> (https://easyx.cn/downloads/)

我把游戏分成三个界面,第一个是初始界面,第二个是游戏界面,第三个就是死亡界面。分别写到3个函数里去调用。

成果展示:
初始界面:

在这里插入图片描述
游戏界面:

在这里插入图片描述
死亡界面:

在这里插入图片描述

附上代码:

#include <easyx.h>//图形库graphics.h
#include <iostream>//cpp头文件
#include <conio.h>//按键操作的头文件
#include <time.h>//提取时间,用来生成随机数
using namespace std;
void area1();//开始界面
void area2();//游戏界面
void area3();//结束界面

int main(){
	initgraph(500,500);//初始一个图形区域(宽500,长500)
	BeginBatchDraw();//开始绘图
	area1();//开始界面
	area2();//游戏界面
	area3();//死亡界面
	return 0;
}

void area1(){//开始界面的函数
	do{
		setbkmode(TRANSPARENT);
		setfillcolor(WHITE);//设置背景颜色
	    solidrectangle(0,0,500,500);
		settextcolor(GREEN);//设置字体颜色
	    settextstyle(50,0,"楷体");//设置字体样式;
	    outtextxy(110,100,"贪吃蛇游戏");//输出字体
	    settextcolor(BLACK);
	    settextstyle(30,0,"楷体");
	    outtextxy(200,200,"START");
	    settextstyle(20,0,"楷体");
	    outtextxy(150,230,"(按回车进入下一步)");
		settextcolor(LIGHTGRAY);
		outtextxy(150,250,"A代表左   D代表右");
		outtextxy(150,270,"W代表上   S代表下");
	    FlushBatchDraw();//输出绘图
        getch();//停止,等待操作
	}while(GetKeyState(13)>=0);//回车判断
}

void area2(){//游戏界面的函数
	int s[52][52];
	int lon=3;//蛇的长度
	int way=1;//蛇的方向
	int x=25,y=25;//蛇的初始位置
	srand(int(time(0)));
	int rx=1+rand()%49;//食物的横坐标
	int ry=1+rand()%49;//食物的纵坐标
	for(int i=1;i<=50;i++)
		for(int j=1;j<=50;j++)
			s[i][j]=0;//空地的值为0
		while(s[rx][ry]!=0){
			rx=1+rand()%49;
			ry=1+rand()%49;
		}
		s[rx][ry]=-1;//食物的值为-1
		while(1){//主循环开头
			Sleep(100);
			setfillcolor(WHITE);//设置背景颜色
	        solidrectangle(0,0,500,500);
			if(GetKeyState(65)<0&&way!=2)//a,向左,同时确保不反向自杀
				way=1;
			if(GetKeyState(68)<0&&way!=1)//d,向右,同时确保不反向自杀
				way=2;
			if(GetKeyState(83)<0&&way!=4)//s,向下,同时确保不反向自杀
				way=3;
			if(GetKeyState(87)<0&&way!=3)//w,向上,同时确保不反向自杀
				way=4;
			if(way==1)x--;
			if(way==2)x++;
			if(way==3)y++;
			if(way==4)y--;
			if(x<=0||x>50||y<=0||y>50||s[x][y]>0)//撞墙或自食则游戏结束
				break;
			s[x][y]=lon;//蛇的值为正数
			for(int i=1;i<=50;i++){
				for(int j=1;j<=50;j++){
					if(s[i][j]>0){
						s[i][j]--;
						setfillcolor(GREEN);//设置当前颜色为绿色(蛇)
						solidrectangle((i-1)*10,(j-1)*10,i*10,j*10);//形成无边框矩形
					}
					if(s[i][j]==-1){
						setfillcolor(RED);//设置当前颜色为红色(食物)
						solidrectangle((i-1)*10,(j-1)*10,i*10,j*10);//形成无边框矩形
					}
					if(x==rx&&y==ry){//蛇吃到食物
						lon++;
						while(s[rx][ry]!=0){
							rx=1+rand()%49;
							ry=1+rand()%49;
						}//while
						s[rx][ry]=-1;
					}//if
				}//for
			}//for
			FlushBatchDraw();//输出绘图
		}//主循环结尾
}

void area3(){//死亡界面的函数
	setbkmode(TRANSPARENT);
	setfillcolor(WHITE);//设置背景颜色
	solidrectangle(0,0,500,500);
	settextcolor(BLACK);//设置字体颜色
	settextstyle(50,0,"楷体");//设置字体样式;
	outtextxy(120,200,"有待提高!");//输出字体
	settextstyle(20,0,"楷体");;
	outtextxy(150,270,"(3秒后自动关闭)");
	FlushBatchDraw();//输出绘图
	Sleep(3000);//单位以毫秒计算
	getch();
	closegraph();
}

做得比较简易,但基本功能都能实现。(这是我之前写的代码,或许有点乱,但基本上我都有写上注释)。

  • 29
    点赞
  • 103
    收藏
    觉得还不错? 一键收藏
  • 19
    评论
好的,以下是一个使用 EasyX 图形库实现的贪吃蛇游戏的示例代码: ```c++ #include <graphics.h> #include <conio.h> #include <time.h> const int WIDTH = 600; // 游戏窗口宽度 const int HEIGHT = 600; // 游戏窗口高度 const int CELL_SIZE = 20; // 每个蛇身格子的大小 const int ROWS = HEIGHT / CELL_SIZE - 1; // 游戏窗口中行数 const int COLS = WIDTH / CELL_SIZE - 1; // 游戏窗口中列数 struct Snake { int x, y; struct Snake *next; }; enum Direction { UP, DOWN, LEFT, RIGHT }; Direction direction = RIGHT; // 蛇的初始方向 Snake *head = NULL; // 蛇头指针 int score = 0; // 得分 void initSnake() { head = new Snake; head->x = 5; head->y = 5; head->next = NULL; } void drawSnake() { setfillcolor(YELLOW); for (Snake *p = head; p; p = p->next) { solidrectangle(p->x * CELL_SIZE, p->y * CELL_SIZE, (p->x + 1) * CELL_SIZE, (p->y + 1) * CELL_SIZE); } } void moveSnake() { int x = head->x, y = head->y; switch (direction) { case UP: y--; break; case DOWN: y++; break; case LEFT: x--; break; case RIGHT: x++; break; } Snake *p = new Snake; p->x = x; p->y = y; p->next = head; head = p; if (x < 0 || x > COLS || y < 0 || y > ROWS) { // 撞墙 closegraph(); printf("Game Over!\n"); exit(0); } for (Snake *p = head->next; p; p = p->next) { // 撞自己 if (p->x == head->x && p->y == head->y) { closegraph(); printf("Game Over!\n"); exit(0); } } if (x == food_x && y == food_y) { // 吃到食物 score += 10; food_x = rand() % COLS; food_y = rand() % ROWS; } else { Snake *p = head; while (p->next->next) { p = p->next; } delete p->next; p->next = NULL; } } void drawFood() { setfillcolor(RED); solidcircle(food_x * CELL_SIZE + CELL_SIZE / 2, food_y * CELL_SIZE + CELL_SIZE / 2, CELL_SIZE / 2); } void updateScore() { setbkmode(TRANSPARENT); settextcolor(WHITE); char buf[32]; sprintf(buf, "Score: %d", score); outtextxy(10, 10, buf); } int food_x, food_y; int main() { initgraph(WIDTH, HEIGHT); setbkcolor(DARKGRAY); initSnake(); food_x = rand() % COLS; food_y = rand() % ROWS; while (true) { cleardevice(); drawSnake(); drawFood(); updateScore(); moveSnake(); Sleep(100); if (_kbhit()) { switch (_getch()) { case 'W': case 'w': if (direction != DOWN) direction = UP; break; case 'S': case 's': if (direction != UP) direction = DOWN; break; case 'A': case 'a': if (direction != RIGHT) direction = LEFT; break; case 'D': case 'd': if (direction != LEFT) direction = RIGHT; break; } } } return 0; } ``` 在这个示例代码中,我们使用了 EasyX 图形库来实现游戏窗口和绘制图形。我们使用 Snake 结构体来表示蛇身,其中的 next 指针指向下一个蛇身。当蛇移动时,我们将一个新的 Snake 节点插入到蛇头位置,然后删除蛇尾节点,从而实现蛇的移动。当蛇头碰到墙壁或者自己的身体时,游戏结束。当蛇头碰到食物时,得分加 10 分,并在随机位置生成一个新的食物。 在游戏循环中,我们使用 cleardevice() 函数清空屏幕,然后分别绘制蛇身、食物和得分。我们使用 Sleep(100) 函数控制游戏的帧率,从而实现动画效果。我们使用 _kbhit() 和 _getch() 函数来读取键盘输入,从而控制蛇的方向。
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值