c++镇国之争游戏(带存档,无bug)

这篇不要转载,谢谢配合

目录

YRGAME

YRGAME.h代码:

DEVC++安装教程

进入正题


YRGAME

最近看到一篇博客,那个作者把一些常用的函数、类的一些东西放在一个头文件里,每次用的时候就调用,不需要再打很多字了,我觉得很方便,也做了一个

YRGAME.h代码:

#ifndef YRGAME
#define YRGAME
#include<iostream>
#include<cmath>
#include<iomanip>
#include<string>
#include<time.h>
#include<cctype>
#include <stdlib.h>
#include <cstdio>
#include <conio.h>
#include <cstdlib>
#include <fstream>
#include <windows.h>
#include <algorithm>
using namespace std;

void YROUT(string xinxi)
{
	for (int item=0;item<xinxi.size();item++)
	{
		cout<<xinxi[item];
		Sleep(70);
	}
}

void colorF0()
{
	system("color F0");
}

void color(int c)//颜色
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
	return;
}


void hidecursor()//隐藏光标
{
	CONSOLE_CURSOR_INFO cursor;
	cursor.bVisible = FALSE;
	cursor.dwSize = sizeof(cursor);
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorInfo(handle, &cursor);
	return;
}

void title(string net)//
{
	string t = "title ";
	t += net;
	system(&t[0]);
	return;
}

void YRGAMEBANBEN()//查看当前YRGAME库版本信息
{
	cout<<endl<<"库名:YRGAME"<<endl;
	cout<<"类型:拓展库"<<endl;
	cout<<"版本号:YR5774 [email protected]"<<endl;
	return;
}


void cls(){system("cls");}
void retr(){//退出程序 
    HWND hWnd=GetForegroundWindow();
    ShowWindow(hWnd,SW_HIDE);
    exit(0);
}

void click_left(){//鼠标左键点击
    mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);  
    Sleep(5);//要留给某些应用的反应时间   
    mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); 
}
void click_right(){//鼠标右键点击
    mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0);  
    Sleep(5);   
    mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0);  
}


#endif

DEVC++安装教程

先进入软件,Ctrl+n新建项目

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAWVJfVA==,size_20,color_FFFFFF,t_70,g_se,x_16

 然后把刚才的代码复制进去

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAWVJfVA==,size_20,color_FFFFFF,t_70,g_se,x_16

 编译

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAWVJfVA==,size_20,color_FFFFFF,t_70,g_se,x_16

然后就会出来这个界面,点击此电脑,找到D盘,一定不要保存在文件夹里!!!

保存在D盘里!!!

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAWVJfVA==,size_20,color_FFFFFF,t_70,g_se,x_16

 改名成YRGAME.h!!!!!!!!!!!!

这一步特别重要!!!!!

一定要改名成YRGAME.h

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAWVJfVA==,size_20,color_FFFFFF,t_70,g_se,x_16

点保存。

过几秒,就会有一个源代码为编译的弹框,点确定就行了。

最后,Ctrl+w退出 

进入正题

接下来说一下这个游戏,这是我很久之前做的,今天突然看到,加了点存档,把bug修了,强化一下,就发出来了。程序名叫镇国之争。这个程序复制的时候就正常建文件,正常运行就行了。

程序:

不要乱改,因为改完可能就不能存档了

DEVC++无报错

#include<fstream>
#include <exception>
#include<direct.h>
#include<cstring>
#include<D:/YRGAME.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0) 
using namespace std;


void prrmy()
{
	ifstream infile;
	infile.open("玩家金币.txt", ios::in);
	if (!infile.is_open())
	{
		cout << "读取文件失败" << endl;
		return;
	}
	char buf[1024] = { 0 };
	while (infile>>buf)
	{
		cout << buf << endl;//输出读取的文本文件数据
	}

}

void s()
{
	cout<<endl;
}


int main()
{
	//初始界面 
	colorF0();
	cout<<endl;
	YROUT("欢迎来到{镇国之争}小游戏!!!");
	Sleep(1000);
	cout<<endl;
	YROUT("在一个战火纷飞的时代,你需要攻打敌人奥国");
	cout<<endl;
	YROUT("1.查看金币数量");
	cout<<endl;
	YROUT("2.添加金币(按一下e加10金币,按一下d退出)");
	cout<<endl;YROUT("3.攻打敌人奥国");cout<<endl;YROUT("4.退出");cout<<endl;
	//金币
	ifstream in;
	in.open("玩家金币.txt");
	int jinbi;
	in>>jinbi; 
	in.close();
	//键盘输入 
	int ch_1;
	int ch_2_1;
	while(1)
	{
		if (_kbhit())
		{
			
			ch_1 = _getch();
			//1
			if (ch_1==49) 
			{
				cout<<endl<<endl<<endl<<endl<<endl;
				YROUT("你的金币是");
				prrmy();
			}
			//2
			if (ch_1 == 50)
			{
				cout<<endl<<endl;
				while(true)
				{
					int ch_2_1 = _getch();
					if (ch_2_1 == 101) 
					{
						cout<<"金币+10"<<" ";
						jinbi += 10;
						//修改金币数量 
						ofstream out("玩家金币.txt", ios::app);
						if (out.is_open())
						{
							out << jinbi;
							out.close();
						}
						//修改 
						int fstr(jinbi);
					    try
					    {
					        fstream fout("玩家金币.txt",ios::out|ios::trunc);
					        fout<<fstr<<endl;
					        fout.close();
					 
					    }catch(exception& e)
					     {
					        cout<< "error happened:" <<e.what()<<endl;
					     }
					}
					if (ch_2_1 == 100)
					{
						cout<<"退出";
						Sleep(1000);
						system("cls");
						cout<<endl;
						YROUT("欢迎来到{镇国之争}小游戏!!!");
						Sleep(1000);
						cout<<endl;
						YROUT("在一个战火纷飞的时代,你需要攻打敌人奥国");
						cout<<endl;
						YROUT("1.查看金币数量");
						cout<<endl;
						YROUT("2.添加金币(按一下e加10金币,按一下d退出)");
						cout<<endl;YROUT("3.攻打敌人奥国");cout<<endl;YROUT("4.退出");cout<<endl;
						break;
					}
				}
			}
			if (ch_1 == 51)
			{
				srand((int)time(0));
				int fighted_ao = (rand() % (1000-0) + 0);
				int fighted_my = jinbi / 3;
				cout<<endl<<"你的战斗力:"<<fighted_my<<endl;
				cout<<"奥国战斗力:"<<fighted_ao;
				if (fighted_my - fighted_ao >=2000)
				{
					YROUT("你一下子就将奥国团灭了(^-_-),按任意键退出");
					system("pause");
					exit(0);
				 } 
				 if (fighted_my > fighted_ao)
				 {
				 	cout<<"你赢了";
				 	int w=0;
				 	w++;
					Sleep(1000);
					system("cls");
					cout<<endl;
					YROUT("欢迎来到{镇国之争}小游戏!!!");
					Sleep(1000);
					cout<<endl;
					YROUT("在一个战火纷飞的时代,你需要攻打敌人奥国");
					cout<<endl;
					YROUT("1.查看金币数量");
					cout<<endl;
					YROUT("2.添加金币(按一下e加10金币,按一下d退出)");
					cout<<endl;YROUT("3.攻打敌人奥国");cout<<endl;YROUT("4.退出");cout<<endl;
				 }
				 if (fighted_my < fighted_ao)
				 {
				 	cout<<"你输了";
				 	cout<<"退出";
					Sleep(1000);
					system("cls");
					cout<<endl;
					YROUT("欢迎来到{镇国之争}小游戏!!!");
					Sleep(1000);
					cout<<endl;
					YROUT("在一个战火纷飞的时代,你需要攻打敌人奥国");
					cout<<endl;
					YROUT("1.查看金币数量");
					cout<<endl;
					YROUT("2.添加金币(按一下e加10金币,按一下d退出)");
					cout<<endl;YROUT("3.攻打敌人奥国");cout<<endl;YROUT("4.退出");cout<<endl;
				 }
			}
			if (ch_1 == 52)
			{
				system("pause");
				exit(2);
			}					
		}
	}
	//输出金币数量
	return 0;
}


再见!

猜你喜欢

转载自blog.csdn.net/m0_64036070/article/details/122798822