C++ 寵物遊戲雛形

寵物遊戲的核心是寵物的本身,即我們需要模擬一個寵物所需要的基本屬性,不管是模擬人還是動物。

對於任何一個活生生的人或動物都需要喫,即我們需要一個 Eat 方法來控制飢餓值,而除了飢餓值以外,我們還需要一個 Play 方法來控制體力值,而心情的好壞直接被飢餓值與體力值所影響。

Pet.h

// Pet.h
#ifndef PET_H_
#define PET_H_

class Pet 
{
private:
	int Mood;   // 寵物的心情
	int Power;  // 寵物的體力
	int Hunger; // 寵物的飢餓程度
	int GetMood() const; // 獲取寵物的心情
	void PassTime(int time = 1); // 表示時間的流逝
public:
	Pet(); // 構造函數,初始化
	~Pet(); // 析構函數,銷燬對象時,調用

	void Menu(); // 寵物菜單
	void Talk(); // 寵物說話
	void Eat(int food = 4); // 寵物喫飯
	void Play(int fun = 4); // 寵物玩耍
};

#endif 

Bog.cpp

// Bog.cpp
#include <Windows.h>
#include <iostream>
#include "Pet.h"
using namespace std;
const int LMT = 3;

Pet::Pet()
{
	SYSTEMTIME SysTime;
	GetLocalTime(&SysTime);

	int index = 0;
	const char *Now[LMT] =
	{"Good morning", "Good afternoon", "Good evening"};

	if (SysTime.wHour > 6 && SysTime.wHour <= 11)
		index = 0;
	else if (SysTime.wHour > 11 && SysTime.wHour <= 18)
		index = 1;
	else
		index = 2;

	cout << "Hi, Master ";
	cout << Now[index] << endl;

	Hunger = Power = 0;
}

Pet::~Pet()
{
	cout << "Bye, I'll miss you ~" << endl;
}

int Pet::GetMood() const 
{
	return (Hunger + Power);
}

void Pet::PassTime(int time) 
{
	Hunger += time;
	Power += time;
}

void Pet::Talk()
{
	Mood = GetMood();
	cout << Mood << endl;
	if (Mood > 15)
	{
		cout << "I am very angry.\n";
	}
	else if (Mood > 10)
	{
		cout << "I feel a sense of loss.\n";
	}
	else if (Mood > 5)
	{
		cout << "I am fine.\n";
	}
	else
	{
		cout << "I am very happy.\n";
	}
	PassTime();
}

void Pet::Eat(int food)
{
	Hunger -= food;
	if (Hunger < 0)
	{
		Hunger = 0;
	}
	PassTime();
}

void Pet::Play(int fun)
{
	Power -= fun;
	if (Power < 0)
	{
		Power = 0;
	}
	PassTime();
}

void Pet::Menu()
{
	cout << "Take care of your pet:\n";
	cout << "1) Talk your pet\n"
		 << "2) Feed your pet\n"
		 << "3) Play with your pet\n";
	cout << "Hi.Want? ";
}

Pet.cpp

// Pet.cpp 
#include <iostream>
#include <string>
#include "Pet.h"
using namespace std;

int main()
{
	Pet Bog;
	string Id;
	do
	{
		Bog.Menu();
		getline(cin, Id);
		
		if (Id == "1")
		{
			Bog.Talk();
		}
		else if (Id == "2")
		{
			Bog.Eat();
		}
		else if (Id == "3")
		{
			Bog.Play();
		}
		else
			cout << "You eat the fart.\n";
		cout << '\n';
	} while (Id != "Bye");
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章