Po学校Mark老师教的打字母游戏

//在Po学校听了Mark老师的一节公开课后,自己尝试完成的打字母游戏
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <windows.h>

void entry_space(int num)
{
	for (int i = 0; i < num;i++)
	{
		printf("%c", ' ');
	}

}

void entry_line(int num)
{
	for (int i = 0; i < num; i++)
	{
		printf("\r\n");
	}

}

void game_interface(int levels, int score)
{
	system("cls");
	entry_space(10);
	printf("levels:%d级", levels);
	entry_space(10);
	printf("score:%d分\r\n", score);
	entry_space(10);
	printf("数字1:暂停");
	entry_space(10);
	printf("数字0:退出\r\n");
	for (int i = 0; i < 50; i++)
	{
		putchar('-');
	}

}

void judge_condition(int* levels, int score)//处理一些条件
{
	if (score<-50)
	{
		entry_line(15);
		entry_space(40);
		printf("Game Over\n");
		exit(0);//退出程序
	}
	if (score>(*levels)*100)
	{
		(*levels)++;//升级
	}

}

void play_game(int* levels,int* score)//开始游戏
{
	time_t ts;
	srand(time(&ts));
	char outputch='a'+ rand() % 26;//为了改变字符
	int spaceNum = rand() % 50;//控制字符的横坐标位置
	int lineNum = 0;//用于保存换了几行,超过行数代表失败
	while (true)
	{	
		lineNum++;
		entry_line(1);
		entry_space(spaceNum);
		putchar(outputch);//输出字符
		Sleep(1000/(*levels));
		printf("\b ");//退一个,将当前位置输出空格
		if (kbhit())//获取键盘输入,保存在输入流
		{
			char inputch = getch();//获取输入流的一个数据
			if (inputch==outputch || inputch==(outputch-32))
			{
				*score += 10;//分数加十
				break;
			}
		}
		if (lineNum>25)
		{
			*score -= 10;//分数减十
			break;
		}
	}

}

void start_game()
{
	int levels = 1;//代表难度
	int score = 0;//代表分数
	while (true)
	{
		game_interface(levels,score);
		play_game(&levels,&score);
		judge_condition(&levels, score);
		
	}

}

int main()
{
	entry_line(15);
	entry_space(40);
	//system("pause");
	printf("Please input anykey to start\r\n");
	getch();
	start_game();


	
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值