📄 mytime.h
字号:
//************************************************************
//游戏时间类
//作者:曾铮
//时间:2004年6月
//说明:游戏中一回合计一天。此类用于游戏中时间的处理。
// 区分日、周、(大、小)月、季、(平、闰)年。
//************************************************************
#ifndef MyTime_h_
#define MyTime_h_
#include "CZDemo.h"
#include "MyMessage.h"
enum MYDATE{JAN=1,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC};
class MyTime
{
public:
int year;//游戏年份
int month;//游戏月份
int day;//游戏日期
int season;//游戏季度
int week;//游戏周
int dayindex;//用于记录过了的天数(判断周)
int lastw;//上一周
int lasty;//上一年
int lastm;//上一月
int lasts;//上一季度
char* date;//日期字符串(年/月/日)
char* dateweek;//星期char*
char* temp;//暂存用的char*
public:
MyTime();
char* TimeGoes();//游戏时钟行走,并取得日期字符
void ShowDate();//控制显示游戏日期
void SeasonChange();//到了下一个季节
void WeekChange();//到下一周
//下列函数在判断的同时,也确认过了一周。
//也就是说,下列函数“必须”在每回合调用一次,以确保及时更新。
//另外,每回合“只能”调用一次,因为第二次调用的话则得到的是更新后的数据,定为false。
bool WeekPassed();//判断是否过了一周。
bool YearPassed();//判断是否过了一年。
bool MonthPassed();//判断是否过了一月。
bool SeasonPassed();//判断是否过了一季度。
virtual ~MyTime();
};
MyTime::MyTime()
{
this->lasty=this->year=2230;
this->lastm=this->month=FEB;
// month=1;
this->day=22;
this->date=new char[30];
this->dateweek=new char[10];
this->temp=new char[4];
this->lasts=this->season=1;
this->lastw=this->week;
this->dayindex=0;
//////////////////////////////////进行开始时的初始化
for(int i=0;i<29;i++)
this->date[i]=' ';
for(int j=0;j<9;j++)
this->dateweek[j]=' ';
this->date[29]='\0';
this->dateweek[9]='\0';
_itoa(this->year,this->temp,10);
strncpy(this->date,this->temp,4);
strncpy(this->date+6,"年",2);
if(this->month<10) //只有一位的copy
{
_itoa(this->month,this->temp,10);
strncpy(this->date+9,this->temp,1);
strncpy(this->date+12,"月",2);
}
else //copy两位
{
_itoa(this->month,this->temp,10);
strncpy(this->date+9,this->temp,2);
strncpy(this->date+12,"月",2);
}
if(this->day<10) //同上
{
_itoa(this->day,this->temp,10);
strncpy(this->date+15,this->temp,1);
strncpy(this->date+18,"日",2);
}
else
{
_itoa(this->day,this->temp,10);
strncpy(this->date+15,this->temp,2);
strncpy(this->date+18,"日",2);
}
/////////////////////////////////////////
strcpy(this->dateweek,"星期一");
}
char* MyTime::TimeGoes()
{
for(int i=0;i<29;i++)
this->date[i]=' ';
this->date[29]='\0';
if((++this->day)<=31)
{
switch (this->month)
{
//在大月
case JAN: case MAR: case MAY: case JUL:
case AUG: case OCT: case DEC:
{
//只是号数递增,其它没有任何影响。
}
break;
//在小月
case APR: case JUN: case SEP: case NOV:
{
if(this->day>30)
{
this->day=1;
this->month++;
}
}
break;
//在二月
case 2:
{
if(this->year%4==0) //闰年
{
if(this->day>29) //到下一个月
{
this->day=1;
this->month++;
}
}
else //平年
{
if(this->day>28) //同上
{
this->day=1;
this->month++;
}
}
}
break;
}
}
else //日期递增大于31号了
{
if(this->month!=DEC)
{
this->day=1;
this->month++;
}
else //到下一年
{
this->day=1;
this->month=1;
this->year++;
}
}
this->WeekChange();//在适当的时候记录过了一周
this->SeasonChange();//在适当的时候切换季节
// strncpy(date,"日期:",5);
_itoa(this->year,this->temp,10);
strncpy(this->date,this->temp,4);
strncpy(this->date+6,"年",2);
//如果不采用分情况copy字符的话,就会出现问题,因为不足的位数以结束符填充,后面的无法显示。
if(this->month<10) //只有一位的copy
{
_itoa(this->month,this->temp,10);
strncpy(this->date+9,this->temp,1);
strncpy(this->date+12,"月",2);
}
else //copy两位
{
_itoa(this->month,this->temp,10);
strncpy(this->date+9,this->temp,2);
strncpy(this->date+12,"月",2);
}
if(day<10) //同上
{
_itoa(this->day,this->temp,10);
strncpy(this->date+15,this->temp,1);
strncpy(this->date+18,"日",2);
}
else
{
_itoa(this->day,this->temp,10);
strncpy(this->date+15,this->temp,2);
strncpy(this->date+18,"日",2);
}
return this->date;
}
void MyTime::ShowDate()
{
switch (this->dayindex)
{
case 1:
strcpy(this->dateweek,"星期一");
break;
case 2:
strcpy(this->dateweek,"星期二");
break;
case 3:
strcpy(this->dateweek,"星期三");
break;
case 4:
strcpy(this->dateweek,"星期四");
break;
case 5:
strcpy(this->dateweek,"星期五");
break;
case 6:
strcpy(this->dateweek,"星期六");
break;
case 7:
strcpy(this->dateweek,"星期日");
break;
default :
break;
}
PrintText(pBBuf,788,703,this->date);
PrintText(pBBuf,860,728,this->dateweek);
return;
}
void MyTime::SeasonChange()
{
switch (this->month)
{
case JAN:
case FEB:
case MAR:
this->season=1;
break;
case APR:
case MAY:
case JUN:
this->season=2;
break;
case JUL:
case AUG:
case SEP:
this->season=3;
break;
case OCT:
case NOV:
case DEC:
this->season=4;
break;
}
}
bool MyTime::MonthPassed()
{
if(this->lastm!=this->month)
{
this->lastm=this->month;//经过了一个月。
return true;
}
else
return false;
}
bool MyTime::SeasonPassed()
{
if(this->lasts!=this->season)
{
this->lasts=this->season;//经过了一个季度。
return true;
}
else
return false;
}
bool MyTime::WeekPassed()
{
if(this->lastw!=this->week)
{
this->lastw=this->week;
return true;
}
else
return false;
}
bool MyTime::YearPassed()
{
if(this->lasty!=this->year)
{
this->lasty=this->year;
return true;
}
else
return false;
}
void MyTime::WeekChange()
{
++this->dayindex;//日计数加一。
if(this->dayindex>7)//过了一周
{
this->dayindex=0;
this->week++;
}
if(this->week>51)
this->week=0;
}
MyTime::~MyTime()
{
delete[] this->temp;
delete[] this->date;
delete[] this->dateweek;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -