C语言贪吃蛇源码分享
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include "time.h"
int key;
int year, month;
int mon[2][13] = { { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, /*定义各个月的天数*/
{ 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
time_t lt;
int showtime() /*显示系统时间*/
{
lt = time(NULL);
printf(" 星期/月/日 时间 年\n");
printf(" %s", ctime(<)); /*输出字符串形式的系统时间*/
}
int nowyear() /*系统的年份*/
{
struct tm *local;
lt = time(NULL);
local = localtime(<);
return local->tm_year + 1900; /*系统年份从1900年为0年开始计算*/
}
int nowmonth() /*系统的月份*/
{
struct tm *local;
lt = time(NULL);
local = localtime(<);
return local->tm_mon + 1;
}
int nowday() /*系统在哪一天*/
{
struct tm *local;
lt = time(NULL);
local = localtime(<);
return local->tm_mday;
}
int getkey1(int year, int key) /*检测键盘的上下方向键*/
{
if (72 == key) /*上方向键代码是72*/
{
year++;
}
else if (80 == key) /*下方向键代码是80*/
{
year--;
}
return year;
}
int getkey2(int month, int key) /*检测键盘的左右方向键*/
{
if (77 == key) /*右方向键代码是77*/
{
month++;
}
else if (75 == key) /*左方向键代码是75*/
{
month--;
}
return month;
}
int isleap(int year) /*判断是否是闰年*/
{ /*被4整除但不被100整除的年时闰年*/
int leap; /*被400整除的是闰年*/
leap = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));
return leap;
}
int isweek(int year, int month, int day) /*判断X年X月X日是星期几*/
{
int i, j, week;
int allday = 0;
for (i = 0; i < year; i++)
{
if (1 == isleap(i)) /*闰年有366天*/
{
allday = allday + 366;
}
else
{
allday = allday + 365; /*非闰年有365天*/
}
}
for (j = 1; j < month; j++)
{
allday = allday + mon[isleap(year)][j];
}
allday = allday + day;
week = (allday - 1) % 7; /*计算当天为星期几,星期日至六为0至6*/
return week;
}
int showdata() /*显示系统月份的挂历*/
{
int i, z;
int j = 0;
printf("\n");
printf(" %d年 %d月\n", nowyear(), nowmonth());
printf(" *********************************************\n");
printf(" Sun Mon Tues Wed Thur Fri Sat\n");
printf(" *********************************************\n");
z = isweek(nowyear(), nowmonth(), 1);
for (i = 0; i < z; i++)
{
printf(" ");
}
for (i = 0; i < mon[isleap(nowyear())][nowmonth()]; i++)
{
if ((i == nowday() - 1) && (i < 9)) /*在系统时间的今天上做记号【】*/
{
printf(" [%d]", i + 1);
}
else if (i == nowday() - 1)
{
printf(" [%2d]", i + 1);
}
else if (i == nowday())
{
printf("%5d", i + 1);
}
else
{
printf("%6d", i + 1);
}
if ((0 == (z + i + 1) % 7) && ((i + 1) != mon[isleap(nowyear())][nowmonth()]))
{ /*每7天换一行*/
j++;
printf("\n");
}
}
if (j == 3) /*令提示语保持在同一行*/
{
printf("\n\n\n\n");
}
if (j == 4)
{
printf("\n\n\n");
}
if (j == 5)
{
printf("\n\n");
}
printf(" 按方向键进入查询状态");
}
int checkdata() /*显示要查询的月份的挂历*/
{
int i, z;
int j = 0;
printf("\n\n\n");
printf(" %d年 %d月\n", year, month);
printf(" ********************************************\n");
printf(" Sun Mon Tues Wed Thur Fri Sat\n");
printf(" ********************************************\n");
z = isweek(year, month, 1);
for (i = 0; i < z; i++)
{
printf(" ");
}
for (i = 0; i < mon[isleap(year)][month]; i++)
{
printf("%6d", i + 1);
if ((0 == (z + i + 1) % 7) && ((i + 1) != mon[isleap(year)][month]))
{
j++;
printf("\n");
}
}
if (j == 3) /*令提示语保持在同一行*/
{
printf("\n\n\n\n");
}
if (j == 4)
{
printf("\n\n\n");
}
if (j == 5)
{
printf("\n\n");
}
printf(" 按上下方向键查询年,按左右方向键查询月\n");
printf(" 按ESC键退回系统时间");
}
int reno() /*刷新系统时间和挂历*/
{
while (!kbhit()) /*当键盘无输入时进行循环*/
{
showtime(); /*显示时间*/
showdata(); /*显示挂历*/
Sleep(1000); /*延迟1秒*/
system("cls"); /*清除屏幕*/
}
}
int str() /*检测键盘并显示要查询月的挂历*/
{
year = nowyear(); /*初始化年月为系统时间*/
month = nowmonth();
printf("\n\n");
key = 0; /*初始化按键记录*/
while (27 != key) /*当没有按ESC时循环*/
{
year = getkey1(year, key); /*把按键的值传递给year和month */
month = getkey2(month, key);
if (month >12)
{ /*满12个月就加一年*/
month = month % 12;
year = year + 1;
}
if (month <1) /*少于1月时退一年*/
{
month = month + 12;
year = year - 1;
}
system("cls"); /*清屏*/
checkdata();
getch(); /*键盘输入*/
key = getch();
}
}
int main(void) /*主函数*/
{
while (1) /*无限循环*/
{
reno();
str();
system("cls");
}
return 0;
}
分享