IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 黄金矿工两百行基于Easyx -> 正文阅读

[C++知识库]黄金矿工两百行基于Easyx

直接上效果图
在这里插入图片描述使用:按空格伸出钩子,再没抓到东西之前,再按一次空格,钩子会收回。
由于图片音乐资源的限制,这里不好放,所以直接给网盘链接。

网盘链接点我
提取码:vdyl
程序代码,注意看有一个头文件tools.hpp

#include<stdio.h>
#include<easyx.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include"tools.hpp"
#pragma comment(lib,"Winmm.lib");
#define MAX_ANGLE 80		//钩子最大幅度
#define MANHATTAN_DIS(hook) sqrt((hook->x-hook->endx)*(hook->x-hook->endx)+(hook->y-hook->endy)*(hook->y-hook->endy));
#define MAXNUM 10
int flag = 0;
enum Dir { Left,Right };
enum HookState {Normal,Long,Short};
enum CoinType{Gold,Wallet,Stone,Max};
IMAGE img_bk;
IMAGE imgs[4];
struct Role {
	float x;
	float y;
	int  width;
	int  height;
	int money;
};
struct Hook {
	float x;
	float y;
	float endx;
	float endy;
	float lenth;
	float angle;//角度
	int state;
	int dir;
	int index ;
};
struct Object {
	int x;
	int y;
	int width;
	int height;
	int value;
	bool isdie;
	int type;
};
Role role;
Hook hook;
Object ob[MAXNUM];
void init();
void initResource();
void draw();
void initResource() {
	loadimage(&img_bk, "./res/bk.jpg",0,getheight()-120);
	for (int i = 0; i < 4; i++) {
		char str[40] = { 0 };
		sprintf(str, "./res/%d.png", i);
		loadimage(imgs + i, str);
	}
}
void init_ob(Object* ob,int x,int y,int type) {
	ob->x = x;
	ob->y = y;
	ob->type = type;
	ob->isdie = false;
	ob->height = imgs[ob->type].getheight();
	ob->width = imgs[ob->type].getwidth();
	
}
void ob_draw(Object* ob) {
	for (int i = 0; i < MAXNUM; i++) {
		if(!ob->isdie)
		drawImg(ob->x, ob->y, imgs+ob->type);
	}
}
void init_role(Role* role,int x,int y) {
	role->x = x;
	role->y = y;
	role->width = imgs[3].getwidth();
	role->height = imgs[3].getheight();
	role->money = 0;
}
void role_draw(Role* role) {
	drawImg((getwidth() - imgs[3].getwidth())/2, 0, imgs + 3);
	settextstyle(48, 0, "正楷");
	settextcolor(RED);
	setbkmode(TRANSPARENT);
	char buf[256] = { 0 };
	sprintf(buf, "金币数:%d ", role->money);
	outtextxy(20, (role->height - textheight(buf)) / 2, buf);
}
void init_hook(Hook* hook,int x,int y) {
	hook->x = x;
	hook->y = y;
	hook->lenth = 50;
	hook->endx = x;
	hook->endy = y + hook->lenth;
	hook->angle = 0;
	hook->state=Normal;
	hook->dir = Right;
	hook->index = -1;
	
}
void hook_draw(Hook* hook) {
	//设置线条的样式:
	setlinestyle(PS_SOLID, 3);
	setlinecolor(BLACK);
	line(hook->x, hook->y, hook->endx, hook->endy);
	setfillcolor(BROWN);
	solidcircle(hook->endx, hook->endy,5);
}
void hook_swing(Hook* hook,float speed) {
	if (hook->state == Normal) {
		if (hook->angle > MAX_ANGLE) {
			hook->dir = Left;
		}
		else if (hook->angle < -MAX_ANGLE) {
			hook->dir = Right;
		}
		if (hook->dir == Left) {
			hook->angle -= speed;
		}
		else if (hook->dir == Right) {
			hook->angle += speed;
		}
		hook->endx = sin(3.1415 / 180 * hook->angle) * hook->lenth + hook->x;
		hook->endy = cos(3.1415 / 180 * hook->angle) * hook->lenth + hook->y;
	}
}
void control_hook(Hook* hook,float speed) {
	if (GetAsyncKeyState(VK_SPACE) & 0x8000&&hook->state==Normal) {
		hook->state = Long;
		flag = 1;
	}
	if (GetAsyncKeyState(VK_SPACE) & 0x8000 && hook->state == Long && flag == 1&&hook->lenth>280) {
		hook->state = Short;
			flag = 0;
	}
	if (hook->state == Long) {
		hook->lenth += speed;
		hook->endx = hook->x + sin(3.1415 / 180 * hook->angle) * hook->lenth;
		hook->endy = hook->y + cos(3.1415 / 180 * hook->angle) * hook->lenth;
		if (hook->endx<0 || hook->endx>getwidth() ||
			hook->endy > getheight()) {
			hook->state = Short;
		}
	}
	if (hook->state == Short) {
		hook->lenth -= speed;
		hook->endx = hook->x + sin(3.1415 / 180 * hook->angle) * hook->lenth;
		hook->endy = hook->y + cos(3.1415 / 180 * hook->angle) * hook->lenth;
		if (hook->lenth < 50) {
			hook->state = Normal;
		}
	}
}
void HookGet(Object*ob) {
	if (hook.state == Normal && hook.index != -1) {
		ob[hook.index].isdie = true;
		role.money += rand() % 100;
		hook.index = -1;
	}
	if (hook.state == Normal) {
		return;
	}
	for (int i = 0; i < MAXNUM; i++) {
		//printf("haha!\n");
		if (!(ob+i)->isdie&&hook.endx > (ob + i)->x && hook.endx<(ob+i)->x+(ob+i)->width &&
			hook.endy>(ob + i)->y && hook.endy < (ob+i)->y+(ob + i)->height) {
			hook.index = i;
			break;
		}
	}
	if (hook.index != -1) {
		hook.state = Short;
		(ob + hook.index)->x = hook.endx-(ob + hook.index)->width/2;
		(ob + hook.index)->y = hook.endy-(ob+hook.index)->height/2;
		
	}
	
}
void draw() {

	putimage(0, 120, &img_bk);
	setfillcolor(YELLOW);
	solidrectangle(0, 0, getwidth(), imgs[3].getheight());
	role_draw(&role);
	hook_draw(&hook);
	for (int i = 0; i < MAXNUM; i++) {
		if(!ob[i].isdie)
		ob_draw(&ob[i]);
	}
}
void init() {
	mciSendString("./res/open aa.mp3 ", 0, 0, 0);//打开音乐
	mciSendString("play ./res/aa.mp3 repeat", 0, 0, 0);//播放音乐
	initResource();
	init_role(&role, (getwidth() - imgs[3].getwidth()) / 2, 0);
	init_hook(&hook, role.x + 40, role.y + 95);
	for (int i = 0; i < MAXNUM; i++) {
		int x = rand() % (getwidth() - 40) + 20;
		int y = rand() % (getheight() - role.height) + role.height;
		init_ob(&ob[i], x,y	,rand()%Max);
	}
}
int main() {
	initgraph(1080, 640, EW_SHOWCONSOLE);
	srand(time(0));
	init();

	while (1){
		BeginBatchDraw();
		draw();
		EndBatchDraw();
		hook_swing(&hook,0.3);
		control_hook(&hook,3);
		HookGet(ob);
	}
	return 0;
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-04-27 11:09:20  更:2022-04-27 11:10:33 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 21:57:09-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码