目录
❄️1、迷宫游戏
//迷宫游戏
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
int main()
{
int i;
//创建迷宫版图
char a[50][50] = { "##########",
"#@ # #",
"### ## # #",
"# # # #",
"# # # ",
"##########",
};
char ch;
int x, y, endx, endy, startx, starty;
startx = 1, starty = 1, endx = 10, endy = 4;//设置起点和终点坐标
x = startx;
y = starty;
while (1)
{
if (x == endx && y == endy)//如果到达终点,结束循环
break;
for (i = 0; i <= 5; i++) //打印迷宫
puts(a[i]);
ch = _getch(); //从键盘中读取字符
if (ch == 'w')//w键,上
{
if (*(*(a + (y - 1)) + x) != '#')
{
*(*(a + y) + x) = ' ';
y--;
*(*(a + y) + x) = '@';
}
}
if (ch == 's')//s键,下
{
if (*(*(a + (y + 1)) + x) != '#')
{
*(*(a + y) + x) = ' ';
y++;
*(*(a + y) + x) = '@';
}
}
if (ch == 'a')//a键,左
{
if (*(*(a + y) + (x - 1)) != '#')
{
*(*(a + y) + x) = ' ';
x--;
*(*(a + y) + x) = '@';
}
}
if (ch == 'd')//d键,右
{
if (*(*(a + y) + (x + 1)) != '#')
{
*(*(a + y) + x) = ' ';
x++;
*(*(a + y) + x) = '@';
}
}
system("cls");//清屏
}
system("cls");
printf("完成!\n");
return 0;
}
🔥运行结果:w:上移;s:下移;a:左移;d:右移;
❄️2、模拟登录(隐藏密码)
//模拟登录(隐藏密码)
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <ctype.h>
#define len 50 //定义长度
//隐藏输入密码
void getpwd(char *pwd, int pwdlen)
{
char ch = 0;
int i = 0;
while (i < pwdlen) //限制密码长度
{
ch = _getch();
if (ch == '\r') //回车结束输入
{
break;
}
else if (ch == '\b' && i > 0)//按下退格键
{
i--;
printf("\b \b");
}
else if (isprint(ch)) //输入可打印字符
{
pwd[i] = ch;
printf("*");
i++;
}
}
pwd[i] = '\0'; //字符串结束标志符
}
int main(void)
{
char a[len] = { 0 }, b[len] = { 0 }, id[len] = { 0 }, password[len] = { 0 };
int i = 0;
//保存账号和密码:
printf("请输入账号:");
gets(a);
printf("请输入密码:");
gets(b);
printf("保存成功!");
Sleep(1000);
system("cls"); //清屏
//模拟登录
printf("账号:");
gets(id);
printf("密码:");
getpwd(password, len);
//验证账号和密码
if (strcmp(id, a) == 0 && strcmp(password, b) == 0)
{
printf("\n登陆成功");
}
else
{
printf("\n失败");
}
return 0;
}
🔥运行结果:
❄️3、抽奖
//抽奖
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#include<conio.h>
#define n 5 //定义任务数量
int main()
{
char a[n][50] = {"一百个俯卧撑", "5组引体向上", "背100个英语单词", "原神启动", "手机锁屏一小时"};
char ch;
printf("按任意键开始抽奖(按0退出)\n");
while (1)
{
ch = _getch();//读取输入流
if (ch == '0') break;
srand((unsigned)time(NULL));
int k = rand() % n;//随机生成0~4数字
system("cls");//清屏
for (int j = 0; j < 2; j++)//缓冲图案
{
for (int i = 0; i < 6; i++)
{
printf(".");
Sleep(100);//延迟100毫秒
}
system("cls");
}
printf("%s\n", a[k]);
printf("按任意键开始抽奖(按0退出)\n");
}
return 1;
}
🔥运行结果:
❄️4、显示当前日期和时间
//显示当前日期和时间
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t current = time(NULL);
struct tm *timer = localtime(¤t);
char* wday_name[] = { "日", "一","二","三","四","五","六" };
printf("当前日期和时间为:%d年 %d月 %d日 星期%s %d时 %d分 %d秒\n",
timer->tm_year + 1900,
timer->tm_mon + 1,
timer->tm_mday,
wday_name[timer->tm_wday],
timer->tm_hour,
timer->tm_min,
timer->tm_sec);
return 0;
}
🔥运行结果:
如果本文章对你有帮助的话,还请给一个点赞吧(●'◡'●)。