C语言写记录游戏时长的小软件

【_Charloe原创】

前言:
本来想在手机端写一个能记录学习时长的APP,但限于能力,目前还无法完成。于是,用C在电脑上写了一个记录游戏时长的小软件。
两个想法的初衷是差不多的。前者为了记录学习时长获得成就感激励学习,后者为了记录游戏时长感到羞愧激励学习。

代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <Tlhelp32.h>

(此段检测进程是否存在的代码,转自网友,侵删)

BOOL IsExistProcess(CONST CHAR* szProcessName)//检测进程是否存在
{
 PROCESSENTRY32 processEntry32;
 HANDLE toolHelp32Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
 if (((int)toolHelp32Snapshot) != -1)
 {
  processEntry32.dwSize = sizeof(processEntry32);
  if (Process32First(toolHelp32Snapshot, &processEntry32))
  {
   do
   {
    if (strcmp(szProcessName, processEntry32.szExeFile) == 0)
    {
     return TRUE;
    }
   } while (Process32Next(toolHelp32Snapshot, &processEntry32));
  }
  CloseHandle(toolHelp32Snapshot);
 }
 return FALSE;
}
int main(void) {
 int gameflag ,runflag=0;
 int hour, minute, second;
 int thour, tminute, tsecond;
 time_t gamestart,gameend;
  struct tm *lt1=0; struct tm *lt2;
 long total,gametotal=0;
 FILE *fp;
 fp = fopen("C:\\Users\\Charloe\\Desktop\\LOLtimerecord.txt", "r");
 fscanf(fp, "%d", &total);
 fclose(fp);
 for (int i = 0; i <= 50; i++)
  putchar('*');
 putchar('\n');
 for (int i = 0; i <= 12; i++)
  putchar(' ');
 printf("本程序用来记录LOL游戏时间\n");
 for (int i = 0; i <= 50; i++)
  putchar('*');
 putchar('\n');
while (1)
 {
  if (IsExistProcess("LeagueClient.exe"))//游戏未启动为0,启动为1;
   gameflag = 1;
  else
  {
   gameflag = 0;
  }
  if (gameflag && (!runflag))//游戏正在运行,开始计时。gameflag,runflag都为1;
  {
   printf("\n检测到LOL启动,祝您游戏愉快!\n");
   runflag = 1;
   time(&gamestart);
   lt1 = localtime(&gamestart);//转为时间结构。
  }
  if ((!gameflag) && runflag) //游戏关闭,停止计时。gameflag为0,runflag为1;
  {
   runflag = 0;//恢复,记录下一次游戏时间
   time(&gameend);
   gametotal = difftime(gameend, gamestart);
   total = total + gametotal;
   hour = gametotal / 3600;
   minute = gametotal / 60 - hour * 60;
   second = gametotal - hour * 3600 - minute * 60;
   thour = total / 3600;
   tminute = total / 60 - thour * 60;
   tsecond = total - thour * 3600 - tminute * 60;
   printf("\n本次游戏结束,此次游戏时长:%d小时%d分%d秒.\n", hour, minute, second);
   printf("游戏时长共计:%d小时%d分%d秒\n", thour, tminute, tsecond);
   printf("启动时刻:%02d时%02d分%02d秒\n", lt1->tm_hour, lt1->tm_min, lt1->tm_sec); 
   lt2 = localtime(&gameend);//转为时间结构
   printf("结束时刻:%02d时%02d分%02d秒\n", lt2->tm_hour, lt2->tm_min, lt2->tm_sec);
   printf("\n等待下一次游戏载入……");
   fp = fopen("C:\\Users\\Charloe\\Desktop\\LOLtimerecord.txt", "w");
   fprintf(fp, "%d\n", total);
   fprintf(fp, "从2019年2月23日至今天\n游戏总时长为:%d小时%d分%d秒\n", thour, tminute, tsecond);
   fclose(fp);
  }
  Sleep(2000);
 }
  return 0;
}

解释:
我这个代码是以LOL游戏为例子。如果记录其他游戏的话,将进程名等修改一下就好。
实际上本代码可以记录任何一个进程运行的时长。

使用了一个Sleep(2000);每两秒进行一次循环,感觉这样,程序所占的CPU会小很多。

使用该程序时,最好在该目录下C:\Users\Charloe\Desktop\LOLtimerecord.txt先新建一个LOLtimerecord.txt文件。
(其中Charloe是我的用户名)
程序会在该文件中记录一些内容,并且不要随便修改改文件,否则可能发生错误。

另外,将VC生成的执行文件.exe放在系统的启动目录下,这样程序可以开机自启动,达到监测的目的。我的系统启动目录是:C:\Users\Charloe\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
(其中Charloe是我的用户名)

问题:
刚开始写的时候,想同时记录电脑的运行时间和游戏运行时间,计算出占比。但是没有想到怎么记录下电脑的运行时间,最后只完成了记录游戏运行时间。
而且还遇到了一些其他的问题,比如对获取当前时间的函数不了解等。
如果大家有一些好的想法,可以一起讨论分享。

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值