c++ EasyX 小程序--贪吃蛇(附源码) 及 EasyX 输出数字变量

游戏截图:

头文件(无知识点):

#include <bits/stdc++.h>
/*火车头形式即为:
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <windows.h>
#include <conio.h>
#include <graphics.h>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
*/

一些定义:

#define kd(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
#define mp make_pair
int part_a = 20, all_a = 800/*格子、窗口大小*/, t/*时间*/, dir = 2/*方向*/, d[4][2] = { {0,1},{1,0},{-1,0},{0,-1} }/*方向*/, nx, ny/*蛇首位置*/, s = 1/*得分*/, dir2/*当前选定方向*/;
int a[40][40]/*每格是否有蛇身体*/, b[40][40]/*格子类型(苹果、障碍……)*/;
queue < pair<long long, long long> >q;//蛇每节身体坐标

函数:

1.输出数字变量:

void print(long long x, long long y, double w) {//坐标、需输出变量
	char str[100];
	sprintf_s(str, "%.2lf", w);//将double转为char数组与printf类似,"%.2lf"可根据需求修改。
	outtextxy(x, y, str);
}

2.绘制画面:

void init() {
	EndBatchDraw();
	BeginBatchDraw();//双缓冲
	cleardevice();//清屏
	rectangle(part_a, part_a, all_a - part_a, part_a * 4);//绘制数据栏
	for (int i = 1; i < all_a / part_a - 1; i++)
		for (int j = 5; j < all_a / part_a - 1; j++) {
			rectangle(i * part_a, j * part_a, (i + 1) * part_a, (j + 1) * part_a);//绘制格子
			if (a[i - 1][j - 5])//绘制蛇
				setfillcolor(GREEN), solidcircle(i * part_a + part_a / 2, j * part_a + part_a / 2, part_a / 2 - 1), setfillcolor(WHITE);
			if (b[i - 1][j - 5] == 1)//绘制苹果
				setfillcolor(RED), solidcircle(i * part_a + part_a / 2, j * part_a + part_a / 2, part_a / 2 - 1), setfillcolor(WHITE);
			if (b[i - 1][j - 5] == 2)//绘制障碍
				fillrectangle(i * part_a, j * part_a, (i + 1) * part_a, (j + 1) * part_a);
			if (nx == i - 1 && ny == j - 5)//绘制眼睛(额……独眼似乎有点奇怪……)
				setfillcolor(BLACK), solidcircle((i + d[dir][0] / 2.0) * part_a + part_a / 2, (j + d[dir][1] / 2.0) * part_a + part_a / 2, part_a / 8 - 1), setfillcolor(WHITE);
			
			if (dir != 3 && kd(VK_DOWN))dir2 = 0;//选定方向,下同
			if (dir != 2 && kd(VK_RIGHT))dir2 = 1;
			if (dir != 1 && kd(VK_LEFT))dir2 = 2;
			if (dir != 0 && kd(VK_UP))dir2 = 3;
		}
}

3.行动:

void move() {
	dir = dir2;//确定方向
	nx += d[dir][0];
	nx += 38;
	nx %= 38;
	ny += d[dir][1];
	ny += 34;
	ny %= 34;//前进
	if (!a[nx][ny] && b[nx][ny] != 2) {
		q.push(mp(nx, ny));
		a[nx][ny] = 1;
		if (b[nx][ny] == 1) {
			int  bx = rand() % 38, by = rand() % 34;
			while (a[bx][by] || b[bx][by] == 2)bx = rand() % 38, by = rand() % 34;
			b[bx][by] = 1;//新建苹果
			b[nx][ny] = 0;
			s++;
		}
		else {
			a[q.front().first][q.front().second]=0;
			q.pop();//删除尾部
		}
	}
	else {
		t += 1000000000;//死亡
	}
}

主函数:

int main() {
	srand(time(NULL));//设置随机种子
	initgraph(all_a, all_a);
	q.push(mp(0, 0));
	int aq = rand() % 38, bq = rand() % 34;
	for (int i = 1; i < all_a / part_a - 1; i++)
		b[i - 1][bq] = 2;
	for (int j = 5; j < all_a / part_a - 1; j++)
		b[aq][j - 5] = 2;//设置障碍
	int  bx = rand() % 38, by = rand() % 34;
	while (a[bx][by] || b[bx][by] == 2)bx = rand() % 38, by = rand() % 34;//苹果
	b[bx][by] = 1;
	a[0][0] = 1;
	while (1) {
		if (kd(' ')) {//暂停
			while (kd(' '))t = clock(), print(part_a + 75, part_a + 5, t / 1000.0);
			if (dir != 3 && kd(VK_DOWN))dir2 = 0;//设置方向,下同
			if (dir != 2 && kd(VK_RIGHT))dir2 = 1;
			if (dir != 1 && kd(VK_LEFT))dir2 = 2;
			if (dir != 0 && kd(VK_UP))dir2 = 3;
			while (!kd(' '))t = clock(), print(part_a + 75, part_a + 5, t / 1000.0);
			if (dir != 3 && kd(VK_DOWN))dir2 = 0;
			if (dir != 2 && kd(VK_RIGHT))dir2 = 1;
			if (dir != 1 && kd(VK_LEFT))dir2 = 2;
			if (dir != 0 && kd(VK_UP))dir2 = 3;
			while (kd(' '))t = clock(), print(part_a + 75, part_a + 5, t / 1000.0 );
			if (dir != 3 && kd(VK_DOWN))dir2 = 0;
			if (dir != 2 && kd(VK_RIGHT))dir2 = 1;
			if (dir != 1 && kd(VK_LEFT))dir2 = 2;
			if (dir != 0 && kd(VK_UP))dir2 = 3;
		}
		if (dir != 3 && kd(VK_DOWN))dir2 = 0;
		if (dir != 2 && kd(VK_RIGHT))dir2 = 1;
		if (dir != 1 && kd(VK_LEFT))dir2 = 2;
		if (dir != 0 && kd(VK_UP))dir2 = 3;
		init();//绘制
		if (dir != 3 && kd(VK_DOWN))dir2 = 0;
		if (dir != 2 && kd(VK_RIGHT))dir2 = 1;
		if (dir != 1 && kd(VK_LEFT))dir2 = 2;
		if (dir != 0 && kd(VK_UP))dir2 = 3;
		t = clock();
		print(part_a + 75, part_a + 5, t / 1000.0);
		print(part_a + 75, part_a + 25, s);
		outtextxy(part_a + 5, part_a + 5, "TIME: ");
		outtextxy(part_a + 5, part_a + 25, "SCORE:");//输出数据
		move();//行动
		dir2 = dir;
		if (dir != 3 && kd(VK_DOWN))dir2 = 0;
		if (dir != 2 && kd(VK_RIGHT))dir2 = 1;
		if (dir != 1 && kd(VK_LEFT))dir2 = 2;
		if (dir != 0 && kd(VK_UP))dir2 = 3;
		if (t > 1000000000)break;//死亡
		while (t % 100 != 0) {//延时
			t = clock(), print(part_a + 75, part_a + 5, t / 1000.0);
			if (dir != 3 && kd(VK_DOWN))dir2 = 0;
			if (dir != 2 && kd(VK_RIGHT))dir2 = 1;
			if (dir != 1 && kd(VK_LEFT))dir2 = 2;
			if (dir != 0 && kd(VK_UP))dir2 = 3;
		}
	}
	init();
	t = clock(), print(part_a + 75, part_a + 5, t / 1000.0);
	print(part_a + 75, part_a + 25, s);
	Sleep(10000000);
	return 0;
}

完整代码:

#include <bits/stdc++.h>
using namespace std;
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
#define kd(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
#define mp make_pair
int part_a = 20, all_a = 800, t, dir = 2, d[4][2] = { {0,1},{1,0},{-1,0},{0,-1} }, nx, ny, s = 1, dir2;
int a[40][40], b[40][40];
queue < pair<long long, long long> >q;
void print(long long x, long long y, double w) {
	char str[100];
	sprintf_s(str, "%.2lf", w);
	outtextxy(x, y, str);
}
void init() {
	EndBatchDraw();
	BeginBatchDraw();
	cleardevice();
	rectangle(part_a, part_a, all_a - part_a, part_a * 4);
	for (int i = 1; i < all_a / part_a - 1; i++)
		for (int j = 5; j < all_a / part_a - 1; j++) {
			rectangle(i * part_a, j * part_a, (i + 1) * part_a, (j + 1) * part_a);
			if (a[i - 1][j - 5])setfillcolor(GREEN), solidcircle(i * part_a + part_a / 2, j * part_a + part_a / 2, part_a / 2 - 1), setfillcolor(WHITE);
			if (b[i - 1][j - 5] == 1)setfillcolor(RED), solidcircle(i * part_a + part_a / 2, j * part_a + part_a / 2, part_a / 2 - 1), setfillcolor(WHITE);
			if (b[i - 1][j - 5] == 2)fillrectangle(i * part_a, j * part_a, (i + 1) * part_a, (j + 1) * part_a);
			if (nx == i - 1 && ny == j - 5)setfillcolor(BLACK), solidcircle((i + d[dir][0] / 2.0) * part_a + part_a / 2, (j + d[dir][1] / 2.0) * part_a + part_a / 2, part_a / 8 - 1), setfillcolor(WHITE);
			if (dir != 3 && kd(VK_DOWN))dir2 = 0;
			if (dir != 2 && kd(VK_RIGHT))dir2 = 1;
			if (dir != 1 && kd(VK_LEFT))dir2 = 2;
			if (dir != 0 && kd(VK_UP))dir2 = 3;
		}
	return;
}
void move() {
	dir = dir2;
	nx += d[dir][0];
	nx += 38;
	nx %= 38;
	ny += d[dir][1];
	ny += 34;
	ny %= 34;
	if (!a[nx][ny] && b[nx][ny] != 2) {
		q.push(mp(nx, ny));
		a[nx][ny]++;
		if (b[nx][ny] == 1) {
			b[nx][ny] = 0;
			int  bx = rand() % 38, by = rand() % 34;
			while (a[bx][by] || b[bx][by] == 2)bx = rand() % 38, by = rand() % 34;
			b[bx][by] = 1;
			s++;
		}
		else {
			a[q.front().first][q.front().second]--;
			q.pop();
		}
	}
	else {
		t += 1000000000;
	}
	return;
}
int main() {
	srand(time(NULL));
	initgraph(all_a, all_a);
	q.push(mp(0, 0));
	int aq = rand() % 38, bq = rand() % 34;
	for (int i = 1; i < all_a / part_a - 1; i++)
		b[i - 1][bq] = 2;
	for (int j = 5; j < all_a / part_a - 1; j++)
		b[aq][j - 5] = 2;
	int  bx = rand() % 38, by = rand() % 34;
	while (a[bx][by] || b[bx][by] == 2)bx = rand() % 38, by = rand() % 34;
	b[bx][by] = 1;
	a[0][0] = 1;
	while (1) {
		if (l % 100 == 0) {
			int  bx = rand() % 38, by = rand() % 34;
			while (a[bx][by] || b[bx][by] == 2)bx = rand() % 38, by = rand() % 34;
			b[bx][by] = 3;
			c[bx][by] = l;
		}
		if (kd(' ')) {
			while (kd(' '))t = clock(), print(part_a + 75, part_a + 5, t / 1000.0);
			if (dir != 3 && kd(VK_DOWN))dir2 = 0;
			if (dir != 2 && kd(VK_RIGHT))dir2 = 1;
			if (dir != 1 && kd(VK_LEFT))dir2 = 2;
			if (dir != 0 && kd(VK_UP))dir2 = 3;
			while (!kd(' '))t = clock(), print(part_a + 75, part_a + 5, t / 1000.0);
			if (dir != 3 && kd(VK_DOWN))dir2 = 0;
			if (dir != 2 && kd(VK_RIGHT))dir2 = 1;
			if (dir != 1 && kd(VK_LEFT))dir2 = 2;
			if (dir != 0 && kd(VK_UP))dir2 = 3;
			while (kd(' '))t = clock(), print(part_a + 75, part_a + 5, t / 1000.0 );
			if (dir != 3 && kd(VK_DOWN))dir2 = 0;
			if (dir != 2 && kd(VK_RIGHT))dir2 = 1;
			if (dir != 1 && kd(VK_LEFT))dir2 = 2;
			if (dir != 0 && kd(VK_UP))dir2 = 3;
		}
		if (dir != 3 && kd(VK_DOWN))dir2 = 0;
		if (dir != 2 && kd(VK_RIGHT))dir2 = 1;
		if (dir != 1 && kd(VK_LEFT))dir2 = 2;
		if (dir != 0 && kd(VK_UP))dir2 = 3;
		init();
		if (dir != 3 && kd(VK_DOWN))dir2 = 0;
		if (dir != 2 && kd(VK_RIGHT))dir2 = 1;
		if (dir != 1 && kd(VK_LEFT))dir2 = 2;
		if (dir != 0 && kd(VK_UP))dir2 = 3;
		t = clock();
		print(part_a + 75, part_a + 5, t / 1000.0);
		print(part_a + 75, part_a + 25, s);
		outtextxy(part_a + 5, part_a + 5, "TIME: ");
		outtextxy(part_a + 5, part_a + 25, "SCORE:");
		move();
		dir2 = dir;
		if (dir != 3 && kd(VK_DOWN))dir2 = 0;
		if (dir != 2 && kd(VK_RIGHT))dir2 = 1;
		if (dir != 1 && kd(VK_LEFT))dir2 = 2;
		if (dir != 0 && kd(VK_UP))dir2 = 3;
		if (t > 1000000000)break;
		while (t % 100 != 0) { 
			t = clock(), print(part_a + 75, part_a + 5, t / 1000.0);
			if (dir != 3 && kd(VK_DOWN))dir2 = 0;
			if (dir != 2 && kd(VK_RIGHT))dir2 = 1;
			if (dir != 1 && kd(VK_LEFT))dir2 = 2;
			if (dir != 0 && kd(VK_UP))dir2 = 3;
		}
	}
	init();
	t = clock(), print(part_a + 75, part_a + 5, t / 1000.0);
	print(part_a + 75, part_a + 25, s);
	Sleep(10000000);
	return 0;
}

下载(免费)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值