C++学习笔记(四):打字小游戏

文章目录

    • 声明
    • 一、介绍
    • 二、代码
    • 三、查看效果
      • (一)开始界面
      • (二)游戏界面
      • (三)游戏失败
      • (四)游戏通关

声明

本文为直播网课的笔记。
笔记对原代码做出了一定的修改,注释,添加,删减。

一、介绍

本项目生成一个打字小游戏。

二、代码

/*
打字母小游戏
需要游戏音效:welcome.mp3,fail.mp3,success.mp3
*/

#include  
#include 
#include 
#include 
//#include 
#include 
#include 
#include 
#pragma comment (lib,"winmm.lib")
#pragma comment ( lib, "ws2_32.lib " )

HWND hwnd;
using namespace std;
int hight = 480, width = 640;

int flick()
{
	while (!_kbhit())
	{

		settextcolor(RGB(255, 127, 80));
		outtextxy(width / 3 - 20, hight - 40, "Press random button");
		Sleep(600);
		settextcolor(RGB(255, 215, 0));
		outtextxy(width / 3 - 20, hight - 40, "Press random button");
		Sleep(400);
	}
	return _getch();
}

//设置开始界面
int welcome()
{
	//1.设置窗口大小
	initgraph(width, hight);
	//2.音乐
	mciSendString("open welcome.mp3 alias wel", 0, 0, 0);
	mciSendString("play wel repeat", 0, 0, 0);
	//3.背景
	loadimage(NULL, "1.jpg");
	//4.标题
	setbkmode(0);   //??????
	settextcolor(RGB(255,127,80));
	settextstyle(50, 0, "华文楷体");
	outtextxy(330, 150, "九层妖塔");
	settextcolor(RGB(255,127,80));
	settextstyle(30, 0, "华文楷体");
	outtextxy(270, 210, "注意要把输入法切换成英文");
//	outtextxy(340, 250, "Press random button");
	flick();
	mciSendString("stop wel", 0, 0, 0);
	return 0;
}

void gameover(int final_score,int floor)
{
	/*
	函数用于生成游戏结束的提示框
	INPUT:
	final_score:结束时候的分数
	floor:层数
	*/
	string messageboxout;	
	messageboxout = "游戏结束\n最终得分为:" + to_string(final_score) + "\n你倒在了第" + to_string(floor) + "层";
	LPCSTR msgbt;
	msgbt = messageboxout.c_str();
	mciSendString("stop game", 0, 0, 0);
	mciSendString("open fail.mp3 alias fail", 0, 0, 0);
	mciSendString("play fail from 100", 0, 0, 0);
	MessageBox(hwnd, msgbt, "game over", MB_SYSTEMMODAL);
	exit(0);
}

void game()
{
	mciSendString("open game.mp3 alias game", 0, 0, 0);
	mciSendString("play game from 8900 repeat", 0, 0, 0);
	int score = 0; //记录得分
	int speed = 10; //最开始的速度
	int floor = 0;
	int start = clock();
	while (score != 50)
	{
		char obj;//掉落的字母
		char key;//键盘按键字母
		int objx, objy;
		obj = rand() % 26 + 'a';
		objx = rand() % (width - 30) + 5;//字母出现的x轴
//		int spd_alter = score / 10;
		for (objy = 0; objy < hight; objy++)   //让字母缓缓下坠
		{			
			cleardevice();
			settextstyle(20, 0, "黑体");  //0代表自适应
			outtextxy(20, 20, ("得分:" + to_string(score)).c_str());
			outtextxy(20, 50, ("层数:" + to_string(floor)).c_str());
			settextstyle(40, 0, "黑体");  //0代表自适应
			outtextxy(objx, objy, obj);
			floor = score / 5;  //层数
			speed = 10 - floor / 5 * 3;
			Sleep(speed);  //根据得分改变速度
			if (_kbhit())  //如果有键盘输入
			{
				key = _getch();
				if (key == obj)
				{
					score++;
					break;
				}
				else
					if (key == 32)  //空格暂停游戏
					{
						mciSendString("stop game", 0, 0, 0);
						mciSendString("play wel repeat", 0, 0, 0);
						system("pause");
						mciSendString("stop wel", 0, 0, 0);
						mciSendString("play game repeat", 0, 0, 0);
					}
					else				
					{					
						gameover(score,floor);				
					}
			}
		}
		if (objy >= hight)  //游戏结束
		{
			gameover(score,floor);
		}
	}
	float duration = (clock() - start) / 1000.0000;
	mciSendString("stop game", 0, 0, 0);
	mciSendString("open success.mp3 alias win", 0, 0, 0);
	mciSendString("play win repeat", 0, 0, 0);
	MessageBox(hwnd, ("恭喜你来到了第九层!\n消耗时长:" + to_string(duration) + "s").c_str(), "游戏通关", MB_SYSTEMMODAL);
	exit(0);
}

int main()
{
	welcome();
	game();
//	system("pause");
	return 0;
}

三、查看效果

(一)开始界面

C++学习笔记(四):打字小游戏_第1张图片

(二)游戏界面

按空格键可暂停。
C++学习笔记(四):打字小游戏_第2张图片

(三)游戏失败

按错或者按慢了则游戏失败。
可按回车结束游戏。
C++学习笔记(四):打字小游戏_第3张图片

(四)游戏通关

C++学习笔记(四):打字小游戏_第4张图片

你可能感兴趣的:(编程练习)