C/C++——简单游戏框架
来源:互联网 发布:二叉树遍历算法 编辑:程序博客网 时间:2024/06/03 21:15
需求分析:
写个简单的框架融合一些设计模式
编译环境:
visual studio 2010
解决方案:
创建一个简单的命令行程序:
[MyGame.cpp]
// MyGame.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <Windows.h>#include <string>#include <list>// 所有类型声明定义都在该文件中// 实际可以根据需要进行分类///////////////////////////////////////////////////武将信息class CGeneral{// 武将public: CGeneral( const char* pName ); BOOL m_bBusy; const char* GetName() const { return m_szName; }private: void Init(); int m_nLeaderShip; // 统帅 int m_nPower; // 武力 int m_nWisdom; // 智谋 int m_nPoliticalAbility; // 政治 char m_szName[16];};CGeneral::CGeneral( const char* pName ){// 构造函数 strncpy( m_szName, pName, 15 ); m_bBusy = FALSE; Init();}void CGeneral::Init(){ m_nLeaderShip = rand() % 100; m_nPower = rand() % 100; m_nWisdom = rand() % 100; m_nPoliticalAbility = rand() % 100;}///////////////////////////////////////////////////游戏事务class Affair{public: Affair( const char* pName ){ strncpy( m_szName, pName, 15 ); m_pNext = NULL; } void SetNext( Affair* pNext ){ m_pNext = pNext; } Affair* GetNext() const { return m_pNext; } virtual void Process(); void NewRoun() { m_ProcessGeneralList.clear(); } void AssignGeneral( CGeneral aGeneral ){ m_ProcessGeneralList.push_back( aGeneral ); }protected: Affair* m_pNext; char m_szName[16]; std::list<CGeneral> m_ProcessGeneralList;};void Affair::Process() { printf( "执行%s:\n", m_szName ); std::list<CGeneral>::iterator it; for( it = m_ProcessGeneralList.begin(); it != m_ProcessGeneralList.end(); it++ ){ printf( "%s ", it->GetName() ); } printf( "\n" );}class DomesticAffair : public Affair{public: DomesticAffair() : Affair( "内政" ) { }};class MilitaryAffair : public Affair{public: MilitaryAffair() : Affair( "军事" ) { }};class DiplomaticAffair : public Affair{public: DiplomaticAffair() : Affair( "外交" ) { }};///////////////////////////////////////////////////国家信息class Country {public: Country( const char* pName ){ strncpy( m_szName, pName, 15 ); DomesticAffair* pDomestic = new DomesticAffair(); MilitaryAffair* pMilitary = new MilitaryAffair(); DiplomaticAffair* pDiplomatic = new DiplomaticAffair(); pDomestic->SetNext( pDiplomatic ); pDiplomatic->SetNext( pMilitary ); m_pCountryAffair = pDomestic; } void AddGeneral( const char* pName ); virtual void RoundProcess(); BOOL operator==( const Country& refCountry ){ if( strcmp( refCountry.m_szName, m_szName ) == 0 ) return TRUE; else return FALSE; } const char* GetName() const{ return m_szName; } std::list<CGeneral> m_GeneralList;private: Affair* m_pCountryAffair; char m_szName[16];};void Country::RoundProcess(void){ std::list<CGeneral>::iterator it; for( it = m_GeneralList.begin(); it != m_GeneralList.end(); it++ ){ it->m_bBusy = FALSE; } Affair* pArrair = m_pCountryAffair; while( pArrair ){ pArrair->NewRoun(); for( it = m_GeneralList.begin(); it != m_GeneralList.end(); it++ ){ if( rand() % 2 && !it->m_bBusy ){ pArrair->AssignGeneral( *it ); it->m_bBusy = TRUE; } } pArrair->Process(); pArrair = pArrair->GetNext(); }}void Country::AddGeneral( const char* pName ){ if( pName == NULL ) return; m_GeneralList.push_back( CGeneral(pName) );}///////////////////////////////////////////////////游戏主体class Game{private: Game(){ m_bGameOver = FALSE; }public: static Game* GetGame(); BOOL IsGameOver() { return m_bGameOver; } // 游戏主循环 void MainLoop(); void Defeat( Country defeatCountry ); void AddCountry( Country vCountry );private: static Game* m_pGame; std::list<Country> m_CountryList; BOOL m_bGameOver;};void Game::Defeat( Country defeatCountry ){ m_CountryList.remove( defeatCountry ); if( m_CountryList.size() == 1 ) m_bGameOver = TRUE;}void Game::AddCountry( Country vCountry ){ m_CountryList.push_back( vCountry );}void Game::MainLoop(){ std::list<Country>::iterator it; for( it = m_CountryList.begin(); it != m_CountryList.end(); it++ ){ printf( ">>> %s \n", it->GetName() ); it->RoundProcess(); printf( "\n\n" ); }}Game* Game::m_pGame = new Game();Game* Game::GetGame(){ return m_pGame;}int _tmain(int argc, _TCHAR* argv[]){ srand( GetTickCount() ); Game* pGame = Game::GetGame(); Country MyCountry( "帝国" ); MyCountry.AddGeneral( "孙尚香" ); MyCountry.AddGeneral( "大乔" ); MyCountry.AddGeneral( "小乔" ); MyCountry.AddGeneral( "貂蝉" ); MyCountry.AddGeneral( "甄宓" ); MyCountry.AddGeneral( "蔡琰" ); MyCountry.AddGeneral( "黄月英" ); MyCountry.AddGeneral( "歩婉" ); MyCountry.AddGeneral( "卞玉儿" ); MyCountry.AddGeneral( "吴凤熙" ); pGame->AddCountry( MyCountry ); Country EnemyCountry( "敌人" ); EnemyCountry.AddGeneral( "曹操" ); EnemyCountry.AddGeneral( "董卓" ); pGame->AddCountry( EnemyCountry ); static int Round = 1; while( !pGame->IsGameOver() ) { system("cls"); printf( "[Round %d] \n", Round); pGame->MainLoop(); Sleep( 5000 ); Round++; } return 0;}
运行结果:
阅读全文
1 0
- C/C++——简单游戏框架
- C#—打字游戏
- C++—迷宫游戏
- C语言应用:游戏框架
- C游戏 简单迷宫游戏开发
- c语言—扫雷游戏
- 卡牌游戏框架实现(C)
- 三子棋游戏(简单)————C语言实现
- c简单的扫雷扫雷游戏
- c语言简单实现扫雷游戏
- c语言编程实现简单三子棋游戏
- 简单c语言飞碟射击游戏
- C语言实现简单24点游戏
- 用c语言实现简单三子棋游戏
- 用c语言简单实现三子棋游戏
- 简单的C语言猜数游戏
- 简单的三子棋游戏(C语言版)
- c语言编程实现简单三子棋游戏
- centos7 minimal virtualbox下无法联网问题
- MFC程序链接
- error C2375 重定义错误的解决方式,Winsock2.h
- 核心动画
- myeclipse10中maven项目maven test报错:-Dmaven.multiModuleProjectDirectory system property is not set.原因
- C/C++——简单游戏框架
- PCI-E 1x, 4x, 8x, 16x 接口定义
- JDK安装和环境变量配置
- 初学Linux,首先需要弄清Linux 标准目录结构
- JavaScript的闭包问题
- iText导出pdf、word、图片
- C++内存分配秘籍—new,malloc,GlobalAlloc详解
- I2C 挂死,SDA一直为低问题分析
- 如何进bios设置ssd固态硬盘为第一启动