我是靠谱客的博主 个性大白,最近开发中收集的这篇文章主要介绍c++小游戏火柴人,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

首先,需要在vs里下载EasyX插件,链接:https://easyx.cn/ 。之后需要在项目文件夹中添加需要的图片:

项目文件
之后是编写代码,需要用到一些绘图和单链表相关知识:

#include<graphics.h>
#include<stdio.h>
#include<assert.h>
#include<time.h>
#include<stdlib.h>
using namespace std;

//开始界面
void beginLayout() {

	//准备文字
	char beginString[] = "开始游戏";
	char closeString[] = "退出游戏";
	char tipString[] = "游戏说明:跳跃:j/J 翻滚:空格";

	//界面布局
	setfillcolor(LIGHTBLUE);
	solidrectangle(240, 100, 380, 150);
	solidrectangle(240, 160, 380, 160+50);
	solidrectangle(0, 360, 640, 400);
	//文字布局
	settextcolor(RED);
	setbkmode(TRANSPARENT);//去掉文字背景
	settextstyle(30, 0, "楷体");
	outtextxy(240 + 10, 100 + 10, beginString);
	outtextxy(240 + 10, 160 + 10, closeString);
	outtextxy(120, 250, tipString);
	//鼠标交互
	while (1)
	{
		MOUSEMSG m = GetMouseMsg();
		if (m.x >= 240 && m.x <= 380 & m.y <= 150 && m.y >= 100) {
			setlinecolor(RED);
			rectangle(240-5, 100-5, 380+5, 150+5);
			if (m.uMsg == WM_LBUTTONDOWN) {
				break;
			}
		}
		else if(m.x >= 240 && m.x <= 380 & m.y <= 210 && m.y >= 160) {
			setlinecolor(RED);
			rectangle(240 - 5, 160 - 5, 380 + 5, 210 + 5);
			if (m.uMsg == WM_LBUTTONDOWN) {
				exit(0);
			}
		}
		else
		{
			setlinecolor(WHITE);
			rectangle(240 - 5, 160 - 5, 380 + 5, 210 + 5);
			rectangle(240 - 5, 100 - 5, 380 + 5, 150 + 5);
		}

	}
}


//定时器
int Timer(time_t num, int id) {
	static time_t start[10];
	time_t end = clock();
	if (end - start[id] > num) {
		start[id] = end;
		return 1;
	}
	return 0;

}

//障碍物
struct  Bricks
{
	int x;
	int y;
	int width;
	int height;
};

struct Node* list = NULL;

//创建障碍物
struct Bricks* createBricks(int x, int y, int width, int height) {
	struct Bricks* pB = (struct Bricks*)malloc(sizeof(struct Bricks));
	assert(pB);//断言
	pB->x = x;
	pB->y = y;
	pB->width = width;
	pB->height = height;
	return pB;
}
//画障碍物
void drawBricks(struct Bricks* pB) {
	solidrectangle(pB->x, pB->y, pB->x + pB->width, pB->y + pB->height);

}
//移动障碍物
void moveBricks(struct Bricks* pB) {
	pB->x -= 25;
}
//单链表
struct Node
{
	struct Bricks* pB;
	struct Node* next;
};
struct Node* createList() {
	struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
	assert(headNode);
	headNode->pB = NULL;
	headNode->next = NULL;
	return headNode;
}
struct Node* createNode(struct Bricks* pB) {
	struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
	assert(newNode);
	newNode->pB = pB;
	newNode->next = NULL;
	return newNode;
}
void insertNode(struct Node* headNode, struct Bricks* pB) {
	struct Node* newNode = createNode(pB);
	newNode->next = headNode->next;
	headNode->next = newNode;
}
void printList(struct Node* headNode) {
	struct Node* pMove = headNode->next;
	while (pMove != NULL)
	{
		drawBricks(pMove->pB);
		moveBricks(pMove->pB);
		pMove = pMove->next;
	}
}


//火柴人
IMAGE roll[8];
IMAGE move[8];
IMAGE jump;

HWND hwnd = NULL;//弹窗

//碰撞
int hitBricks(int x, int y, int width, int height, struct Node* list) {
	struct Node* pMove = list->next;
	while (pMove!=NULL)
	{
		if (pMove->pB->x >= (x-pMove->pB->width) && pMove->pB->x <= x+width) {
			if (pMove->pB->y>=y-pMove->pB->height && pMove->pB->y <=y+height)
			{
				return 1;
			}
		}
		pMove = pMove->next;
	}
	return 0;
}


void loadResource() {
	loadimage(&jump,"jump.jpg",70,100);
	for (int i = 1; i <= 8;++i) {
		char fileName[20] = "";
		sprintf_s(fileName,"gun8_%d.jpg",i);
		loadimage(roll+i-1,fileName,50,50);
		sprintf_s(fileName, "move8_%d.jpg", i);
		loadimage(move + i - 1, fileName, 70, 100);
	}
}
//1、跑步
void roleMove(int frameNum) {
	BeginBatchDraw();
	int i = 0;
	while (i<frameNum) {
		cleardevice();
		solidrectangle(0, 360, 640, 400);
		putimage(50,260,move+i);
		if (hitBricks(30,260,70,100,list))
		{
			MessageBox(hwnd,"游戏结束!", "游戏结束!",MB_OK);
			exit(0);
		}
		i++;
		printList(list);
		Sleep(30);
		FlushBatchDraw();
	}
	EndBatchDraw();
 }
//2、滚动
void roleRoll(int frameNum) {
	BeginBatchDraw();
	int i = 0;
	while (i < frameNum) {
		cleardevice();
		solidrectangle(0, 360, 640, 400);
		putimage(50, 310, roll + i);
		if (hitBricks(30, 310, 70, 50, list))
		{
			MessageBox(hwnd, "游戏结束!", "游戏结束!", MB_OK);
			exit(0);
		}
		i++;
		printList(list);
		Sleep(30);
		FlushBatchDraw();
	}
	EndBatchDraw();
}
//3、跳跃
void roleJump() {
	BeginBatchDraw();
	int y = 260;
	for (int i = 0; i < 7;i++) {			//上升
		cleardevice();
		solidrectangle(0, 360, 640, 400);
		putimage(50, y, &jump);
		if (hitBricks(30, y, 70, 100, list))
		{
			MessageBox(hwnd, "游戏结束!", "游戏结束!", MB_OK);
			exit(0);
		}
		y -= 30;
		printList(list);
		Sleep(50);
		FlushBatchDraw();
	}
	for (int i = 0; i < 7; i++) {			//下降
		cleardevice();
		solidrectangle(0, 360, 640, 400);
		putimage(50, y, &jump);
		if (hitBricks(30, y, 70, 100, list))
		{
			MessageBox(hwnd, "游戏结束!", "游戏结束!", MB_OK);
			exit(0);
		}
		y += 30;
		printList(list);
		Sleep(50);
		FlushBatchDraw();
	}
	EndBatchDraw();
}

void keyDown() {
	if (GetAsyncKeyState(' ')) {
		roleRoll(10);
	}
	if (GetAsyncKeyState('J')) {
		roleJump();
	}
}


int main()
{
	list = createList();
	srand((unsigned int)time(0));

	loadResource();

	hwnd=initgraph(640,400);
	setbkcolor(WHITE);//背景颜色
	cleardevice();//一定要刷新

	beginLayout();

	while (1) {
		roleMove(8);
		keyDown();
		if (Timer(5000,0))
		{
			insertNode(list,createBricks(640,300-rand()%100,50,50));
		}
	};

	closegraph();
	return 0;
}

运行结果:
在这里插入图片描述
在这里插入图片描述

最后

以上就是个性大白为你收集整理的c++小游戏火柴人的全部内容,希望文章能够帮你解决c++小游戏火柴人所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部