欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

c++可视化打怪小游戏

程序员文章站 2022-06-23 07:50:39
本代码用到了easyx图形库,下面是贴图,注意和代码放一个文件夹里哦:0.jpg1.jpg2.jpg(就是空白)3.jpg4.jpg(本图片均可在网上找到)代码:#include #include #include #include using namespace std;IMAGE h[5];HWND hwnd = GetHWnd();in...

本代码用到了easyx图形库,下面是贴图,注意和代码放一个文件夹里哦:
0.jpg
c++可视化打怪小游戏
1.jpg
c++可视化打怪小游戏
2.jpg
c++可视化打怪小游戏
(就是空白)
3.jpg
c++可视化打怪小游戏
4.jpg
c++可视化打怪小游戏
(本图片均可在网上找到)
代码:

#include <iostream>
#include <conio.h>
#include <graphics.h>
#include <time.h>
using namespace std;
IMAGE h[5];
HWND hwnd = GetHWnd();
int smz=5,sum = 0, e[5] = { 0,1,2,3,4 };
int x, y, a[100][100] = {
	{1,1,1,1,1,1,1,1,1,1,1,1},
	{1,7,7,7,7,7,7,7,7,7,7,1},
	{1,7,7,7,7,7,7,7,7,7,7,1},
	{1,7,7,7,2,7,7,7,7,7,7,1},
	{1,7,7,7,7,7,7,7,7,7,7,1},
	{1,7,7,7,7,7,7,7,7,7,7,1},
	{1,7,7,7,7,7,7,7,7,7,7,1},
	{1,1,1,1,1,1,1,1,1,1,1,1},
};
char ch;
void zp() {
	for (int i = 0; i < 5; i++) {
		char l[100] = "";
		sprintf_s(l, "%d.jpg", e[i]);
		loadimage(h + i, l);
	}
}
void dr() {
	system("cls");
	int i, j,x,y;
	char t[100] = "",r[100]="";
	sprintf_s(t, "分数%d", sum);
	settextcolor(WHITE);
	settextstyle(0, 20, "楷体");
	outtextxy(800, 100,t);
	sprintf_s(r, "生命值:%d", smz);
	settextstyle(0, 20, "楷体");
	outtextxy(800, 200, r);
	if (smz == 0) {
		MessageBox(hwnd, TEXT("你失败了!"), TEXT("游戏结束"), MB_OK | MB_ICONWARNING);
		exit(0);
	}
	for (i = 0; i < 20; i++) {
		for (j = 0; j < 20; j++) {
			x = j * 64;
			y = i * 64;
			switch (a[i][j]) {
				case 2:
					putimage(x, y, h + 0);
					break;
				case 1:
					putimage(x, y, h + 1);
					break;
				case 7:
					putimage(x, y, h + 2);
					break;
				case 3:
					putimage(x, y, h + 3);
					break;
				case 4:
					putimage(x, y, h + 4);
					break;
			}
		}
		cout << endl;
	}
}
void people() {
	int i, j;
	for (i = 0; i < 20; i++) {
		for (j = 0; j < 20; j++) {
			if (a[i][j] == 2) {
				x = i;
				y = j;
				break;
			}
		}
		if (a[i][j] == 2) {
			x = i;
			y = j;
			break;
		}
	}
	ch = _getch();
	switch (ch) {
		case 'w':
			if (a[x - 1][y] != 1) {
				if (a[x - 1][y] == 4)
					smz--;
				a[x][y] = 7;
				x--;
				a[x][y] = 2;
			}
			break;
		case 's':
			if (a[x + 1][y] != 1) {
				if (a[x + 1][y] == 4)
					smz--;
				a[x][y] = 7;
				x++;
				a[x][y] = 2;
			}
			break;
		case 'a':
			if (a[x][y - 1] != 1) {
				if (a[x][y - 1] == 4)
					smz--;
				a[x][y] = 7;
				y--;
				a[x][y] = 2;
			}
			break;
		case 'd':
			if (a[x][y + 1] != 1) {
				if (a[x][y + 1] == 4)
					smz--;
				a[x][y] = 7;
				y++;
				a[x][y] = 2;
			}
			break;
		case 'j':
			a[x][y] = 3;
			if (a[x + 1][y]==4 || a[x - 1][y] ==4|| a[x][y + 1]==4||a[x][y - 1] == 4) {
				if (a[x + 1][y] == 4)
					a[x + 1][y] = 7;
				else if (a[x - 1][y] == 4)
					a[x - 1][y] = 7;
				else if (a[x][y - 1] == 4)
					a[x][y - 1] = 7;
				else
					a[x][y + 1] = 7;
				sum++;
			}
			break;
	}
}
void gw() {
	srand(time(NULL));
	int k = rand()%9 + 1;
	for (int i = 0; i < k; i++) {
		int x = rand() % 6 + 1;
		int y = rand() % 9 + 1;
		a[x][y] = 4;
	}
}
int main() {
	initgraph(1000, 600);
	zp();
	BeginBatchDraw();
	MessageBox(hwnd, TEXT("w上,s下,a左,d右,j攻击"), TEXT("游戏操作"), MB_OK | MB_ICONWARNING);
	while (1) {
		dr();
		FlushBatchDraw();
		people();
		gw();
	}
	EndBatchDraw();
	getchar();
	closegraph();
	return 0;
}

好了,转载请声明一下哦,希望对你有用o( ̄▽ ̄)ブ

本文地址:https://blog.csdn.net/JH_duangduang/article/details/108184889

相关标签: c++