大一课设——黄金矿工

大一的时候做的小游戏,记录一下防丢失

#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <cmath>
#include <time.h>
#include <Windows.h>
#include <conio.h>
#include <mmsystem.h>
#pragma once
#pragma comment(lib,"winmm.lib")
#define Stuff_Max 80  //物品数量
#define PI 3.1426926
#define _CRT_SECURE_NO_WARNINGS
IMAGE img[13],m,n,p1,p2,p3; //定义

using namespace std;

enum state
{
	extend,//伸长
	shorten,//缩短
	nomal,//正常
	left,//左
	right,//右
};

typedef struct _Role//角色
{
	int x;//位置
	int y;
	int width;//角色宽度
	int height;//角色高度
	int state;//角色状态控制
	int skin;//角色皮肤
}Role;
typedef struct _Hook//钩子
{
	double x;//钩子开始坐标
	double y;
	double enx;//钩子结束坐标
	double endy;
	double angle;//钩子转动角度
	double vx;   //速度分量(伸长是以什么速度增长)
	double vy;
	int state;//状态
	int dir;//方向
	double len;//钩子长度
	int index;//抓到物品号数
}Hook;
typedef struct _Stuff//物品
{
	int size;      //物品大小
	double place_x;//位置指标
	double plaxe_y;
	int type;      //类型
	bool exsit;	   //是否存在
	int worth;     //物品价值
}stf;//typedep要再定义,stf是另一个类型名,stf=struct_Stuff
Role* role = (Role*)calloc(1, sizeof(Role));//创建角色
Hook* hook = (Hook*)calloc(1, sizeof(Hook));//创建钩子
stf stuf[Stuff_Max];//创建物品
int score = 0;//统计分数
void init_IMAGE();//加载图片
void chartlet();//贴图
void init_role();//角色初始化
void init_stste();//钩子初始化
void swing(double angle);//钩子的摆动
void flex(double speed);//钩子的伸缩
void grab();//抓取物品的判断


void init_IMAGE()//加载图片
{
	for (int i = 1; i < 13; i++)
	{
		char temp[1020] = " ";
		sprintf_s(temp, "./图片包/%d.bmp", i);//格式化字符串//"”这个里面先写文件夹,再写%d,类似./images/%d.bmp
		loadimage(&img[i], temp);
	}

}

void menu()

 {

	initgraph(1080, 640);
	loadimage(&m, "背景1.bmp");
		//setbkcolor(GREEN);不能填充颜色,不是背景的颜色,是另一个窗口的颜色,会覆盖了游戏界面
	putimage(0, 0, &m);//终于把图片弄进去了呜呜呜呜呜呜呜
	setfillcolor(YELLOW);

	fillrectangle(700, 370, 420, 320);
	fillrectangle(700, 440, 420, 390);
	settextstyle(40, 0, "隶书");
	settextcolor(RED);
	setbkmode(TRANSPARENT);

	outtextxy(485, 330, "开始游戏");
	outtextxy(485, 402, "退出游戏");

	MOUSEMSG m;//从一下开始控制鼠标
	
	while (1)
	{
		m = GetMouseMsg();
	
		if (m.x > 420 && m.x < 700 && m.y>320 && m.y < 370)
		{
			setlinecolor(BLUE);
			rectangle(700 +20, 370 +20, 420 -20, 320-20);
			if (m.uMsg == WM_LBUTTONDOWN)
			{
				break;
			}	
		}
		else if (m.x > 420 && m.x < 700 && m.y>390 && m.y < 440)
		{
			setlinecolor(GREEN);
			rectangle(700 + 20, 440 + 20, 420 - 20, 390 - 20);
			if (m.uMsg == WM_LBUTTONDOWN)
			{
				exit(0);
			}
		}


	}
	cleardevice();

	//getchar();

 }
void slect()
{
	initgraph(1080, 640);

	loadimage(&n, "背景2.bmp");
	putimage(0, 0, &n);
	
	char ep[30] = "";
	sprintf_s(ep, "选择你的英雄");
	settextcolor(YELLOW);
	setbkmode(TRANSPARENT);
	settextstyle(100, 0, "隶书");
	outtextxy(260, 60, ep);

	
	/*fillrectangle(1000,600,750,230);
	fillrectangle(650,600,400,230);
	fillrectangle(300,600,50,230);*/
	

	//setbkmode(TRANSPARENT);
	//cleardevice();
	MOUSEMSG m;//从一下开始控制鼠标

	while (1)
	{
		m = GetMouseMsg();
		if (m.x > 750 && m.x < 1000 && m.y>230 && m.y < 600)
		{
			setlinecolor(RED);
			rectangle(1000 + 20, 600 + 20, 750 - 20, 230 - 20);
			if (m.uMsg == WM_LBUTTONDOWN)
			{
				role->skin = 1;
				break;
			}
		}
		else if (m.x > 400 && m.x < 650 && m.y>230 && m.y < 600)
		{
			setlinecolor(BLUE);
			rectangle(650 + 20, 600 + 20, 400 - 20, 230 - 20);
			if (m.uMsg == WM_LBUTTONDOWN)
			{
				role->skin = 2;
				break;
			}
		}
		else if (m.x > 50 && m.x < 300 && m.y>230 && m.y < 600)
		{
			setlinecolor(GREEN);
			rectangle(300 + 20, 600 + 20, 50 - 20, 230 - 20);
			if (m.uMsg == WM_LBUTTONDOWN)
			{
				role->skin = 3;
				break;
			}
		}
		else
		{

			setlinecolor(WHITE);
			rectangle(1000 + 20, 600 + 20, 750 - 20, 230 - 20);
			rectangle(650 + 20, 600 + 20, 400 - 20, 230 - 20);
			rectangle(300 + 20, 600 + 20, 50 - 20, 230 - 20);
		}

	}

	cleardevice();
}
void chartlet()//贴图//绘制元素
{
	cleardevice();
	putimage(0, 0, &img[1]);//贴背景,
	
	switch (role->skin)
	{
	case 1:
		putimage(role->x, role->y, &img[12], SRCAND);
		putimage(role->x, role->y, &img[13], SRCPAINT);
		break;
	case 2:
		putimage(role->x, role->y, &img[11], SRCAND);
		putimage(role->x, role->y, &img[10], SRCPAINT);
		break;
	case 3:
		putimage(role->x, role->y, &img[3], SRCAND);
		putimage(role->x, role->y, &img[2], SRCPAINT);
		break;
	default:
		break;
	}
	//钩子的绘制
	setlinecolor(YELLOW);//钩子的颜色
	setlinestyle(PS_SOLID, 5);//钩子实线,并且粗为5
	line((int)hook->x, (int)hook->y, (int)hook->enx, (int)hook->endy);//钩子的位置
	solidcircle((int)hook->enx, (int)hook->endy, 5);
	//物品的绘制
	for (int i = 0; i < Stuff_Max; i++)
	{	
		if (stuf[i].exsit == true)
		{
			switch (stuf[i].type)
			{
			case 0://金块
				putimage((int)stuf[i].place_x, (int)stuf[i].plaxe_y, &img[5], SRCAND);
				putimage((int)stuf[i].place_x, (int)stuf[i].plaxe_y, &img[4], SRCPAINT);
				stuf[i].worth = 50;//物品价值
				break;
			case 1://钱袋
				putimage((int)stuf[i].place_x, (int)stuf[i].plaxe_y, &img[7], SRCAND);
				putimage((int)stuf[i].place_x, (int)stuf[i].plaxe_y, &img[6], SRCPAINT);
				stuf[i].worth = 10;
				break;
			case 2://石头
				putimage((int)stuf[i].place_x, (int)stuf[i].plaxe_y, &img[9], SRCAND);
				putimage((int)stuf[i].place_x, (int)stuf[i].plaxe_y, &img[8], SRCPAINT);
				stuf[i].worth = 1;
				break;
			}
		}

	}
	//分数绘制
	char str[30] = "";
	sprintf_s(str, "分数:%d", score);
	settextcolor(BLUE);
	setbkmode(TRANSPARENT);
	settextstyle(40, 0, "隶书");
	outtextxy(20, 20, str);
	//绘制封面按钮矩形
	setfillcolor(GREEN);
	
}
void init_role()//角色初始化
{
	role->height = 180;
	role->width = 140;
	role->x = (getwidth() - role->width) / 2;//居中
	role->y = 25;
	role->state=0;
	//role->skin=0;
}
bool size_line(Hook* hook)//求当前线的长度
{
	double size = sqrt((hook->enx - hook->x) * (hook->enx - hook->x) + (hook->endy - hook->y) * (hook->endy - hook->y));
	return size <= hook->len;
}
void init_stste()//初始化钩子
{
	hook->len = 50;
	hook->x = 1080 / 2 - 25;
	hook->y = role->height / 2 + 35;
	hook->enx = hook->x;
	hook->endy = hook->y + hook->len;
	hook->angle = 0;
	hook->state = nomal;
	hook->vx = 0;
	hook->vy = 0;
	hook->index = -1;
}
//控制台输入输出
void keyDonw()
{
	if (_kbhit())
	{
		char choice = _getch();
		switch (choice)
		{
		case 'a':
		case 'A':
		case 75:
			//putimage(0, 0, &img[1]);
			putimage(role->x, role->y, &img[3], SRCAND);
			putimage(role->x, role->y, &img[2], SRCPAINT);//这里很重要,
			role->x -= 5;
			hook->x -= 5;
			hook->enx -= 5;
			hook->state == 0;
			break;
		case 'd':
		case 'D':
		case 77:
			//putimage(0, 0, &img[1]);
			putimage(role->x, role->y, &img[3], SRCAND);
			putimage(role->x, role->y, &img[2], SRCPAINT);//这里很重要,
			role->x += 5;
			hook->x += 5;
			hook->enx += 5;
			hook->state ==0;
			break;
		case 'w':
		case 'W':
		case 72:
			putimage(role->x, role->y, &img[3], SRCAND);
			putimage(role->x, role->y, &img[2], SRCPAINT);//这里很重要,
			role->y -= 5;
			hook->y -= 5;
			break;
		case 's':
		case 'S':
		case 80:
			putimage(role->x, role->y, &img[3], SRCAND);
			putimage(role->x, role->y, &img[2], SRCPAINT);//这里很重要,
			role->y += 5;
			hook->y+= 5;
			break;
		}
	}
}
void swing(int angle)//摆动
{
	if ( hook->state == nomal)//正常状态下时转动,伸缩时不转
	{
		hook->endy = cos(PI / 180 * hook->angle) * hook->len + hook->y;//求钩子的纵坐标,相当于一个圆
		hook->enx = sin(PI / 180 * hook->angle) * hook->len + hook->x;//求钩子的横坐标,相当于一个圆
		
		if (hook->angle > 80)
		{
			hook->dir = left;//如果角度大于80就往左走
		}
		else if (hook->angle < -80)
		{
			hook->dir = right;
		}

		if (hook->dir == right)
		{
			hook->angle += angle;
		}
		else
		{
			hook->angle -= angle;
		}
	}
}
void stuff()//物品
{
	//用随机数生成物品位置
	for (int i = 0; i < Stuff_Max; i++)
	{
		stuf[i].size = 50;
		stuf[i].place_x = rand() % (1080 - stuf[i].size);
		stuf[i].plaxe_y = rand() % (1080 - 150) + 150;
		stuf[i].type = rand() % 3;
		stuf[i].exsit = true;
	}
}
void flex(int speed)//伸缩
{
	if (GetAsyncKeyState(VK_SPACE))//伸长
	{

		hook->state = extend;
		hook->vy = cos(PI / 180 * hook->angle) * speed;//速度分量
		hook->vx = sin(PI / 180 * hook->angle) * speed;
	}
	if (hook->state == extend)
	{
		hook->endy += hook->vy;
		hook->enx += hook->vx;
		
	}
	else if (hook->state == shorten)
	{
		hook->endy -= hook->vy;
		hook->enx -= hook->vx;
		if (size_line(hook))
		{
			hook->state = nomal;
		}
		
	}
	//边界
	if ((hook->enx >= getwidth() || hook->enx <= 0) || (hook->endy >= getheight() || hook->endy <= 0))
	{
		hook->state = shorten;
	}
}
void grab()//抓取
{
	for (int i = 0; i < Stuff_Max; i++)
	{
		//根据图片大小来决定在哪个范围抓取
		if (stuf[i].type == 0||stuf[i].type == 1 || stuf[i].type == 2)
		{
			if (stuf[i].exsit == true
				&&
				hook->enx < (stuf[i].place_x + stuf[i].size) && hook->enx >stuf[i].place_x
				&& 
				hook->endy < (stuf[i].plaxe_y + stuf[i].size) && hook->endy > stuf[i].plaxe_y)
			{
				hook->index = i;
				break;
			}
		}
	
		
	}
	if (hook->index != -1)
	{
		hook->state = shorten;//缩短
		stuf[hook->index].place_x = hook->enx - 30;
		stuf[hook->index].plaxe_y = hook->endy - 30;

		if (size_line(hook))
		{
			score += stuf[hook->index].worth;//统计分数
			stuf[hook->index].exsit = false;
		
			hook->index = -1;
			hook->state = nomal;//缩短
		}
	}

}

int main()
{
//music();
	menu();


	slect();
	//music();
	init_IMAGE();//加载图片

	srand((unsigned)time(NULL));

	init_role();//初始化角色


	init_stste();//初始化钩子
	stuff();//物品
	init_IMAGE();//加载图片
	BeginBatchDraw();//双缓冲
	


		DWORD t1,t2;
	t1 = t2 = GetTickCount();
	
		while (1)
		{

			if (t2 - t1 > 10)
			{
				void swing();
				t1 = t2;
			}

			keyDonw();
			chartlet();//贴图
			FlushBatchDraw();//双缓冲
			flex(1);//伸缩
			swing(1);//摆动
			grab();//抓取

			if (score >= 300)
				break;

		}
	
		cleardevice();
	
	
	return 0;
}

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值