C语言实现打字游戏

C语言实现打字游戏

一、效果图

打字游戏

二、代码

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <time.h>

int AllCounter, RightCounter, WrongCounter;

AllCounter = 0;//总数
RightCounter = 0;//正确个数
WrongCounter = 0;//错误个数

//键盘
char kw[][12] = {
	{'Q','W','E','R','T','Y','U','I','O','P','[',']'},
	{'A','S','D','F','G','H','J','K','L',';','\''} ,
	{'Z','X','C','V','B','N','M',',','.','/'}
};

void DelVscroll();//去除控制台垂直滚动条
void BanMax();//禁用控制台最大化
void gotoxy(int x, int y);//定位光标位置
int DrawMap();//画地图
void xy_Start();//开始
int xy_Exit();//退出
void xy_Pause();//暂停

int main()
{
	DelVscroll();
	BanMax();
	DrawMap();
	xy_Start();
	while (1)
	{
		PlayGame();
	}
	return 0;
}

//去除控制台的垂直滚动条
void DelVscroll()
{
	HANDLE Hand;
	CONSOLE_SCREEN_BUFFER_INFO Info;
	Hand = GetStdHandle(STD_OUTPUT_HANDLE);
	GetConsoleScreenBufferInfo(Hand, &Info);
	SMALL_RECT rect = Info.srWindow;
	COORD size = { rect.Right + 1 ,rect.Bottom + 1 };	//定义缓冲区大小,保持缓冲区大小和屏幕大小一致即可取消边框 
	SetConsoleScreenBufferSize(Hand, size);
}

//禁用控制台的最大化
void BanMax()
{
	HWND hWnd = GetConsoleWindow(); //获得cmd窗口句柄
	RECT rc;
	GetWindowRect(hWnd, &rc); //获得cmd窗口对应矩形
	//改变cmd窗口风格
	SetWindowLongPtr(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX);//& ~WS_MINIMIZEBOX& ~WS_VSCROLL
	//因为风格涉及到边框改变,必须调用SetWindowPos,否则无效果
	SetWindowPos(hWnd, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, NULL);

}

//定位光标位置
void gotoxy(int x, int y)
{
	COORD pos = { x,y };
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
	SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体,具体位置
}

//画键盘等信息
int DrawMap()
{
	printf("All:%d\nRight:%d\nWrong:%d", AllCounter, RightCounter, WrongCounter);
	gotoxy(72, 0);
	printf("Press Esc to exit!");
	gotoxy(72, 1);
	printf("Enter to pause!");
	gotoxy(24, 0);
	//画键盘
	for (int i = 0; i < 12; i++)
		printf("%c  ", kw[0][i]);
	gotoxy(26, 2);
	for (int i = 0; i < 11; i++)
		printf("%c  ", kw[1][i]);
	gotoxy(28, 4);
	for (int i = 0; i < 10; i++)
		printf("%c  ", kw[2][i]);
	gotoxy(15, 29);
	for (int i = 0; i < 60; i++)
		printf("=");
}

//按下任意键开始
void xy_Start()
{
	gotoxy(25, 13);
	printf("***********************************");
	gotoxy(25, 14);
	printf("***** Press any key to start! *****");
	gotoxy(25, 15);
	printf("***********************************");
	gotoxy(40, 17);
	int key = getch();//等待输入
}

//按下Enter键暂停游戏,并按任意键继续游戏
void xy_Pause()
{

	gotoxy(25, 13);
	printf("***********************************");
	gotoxy(25, 14);
	printf("*** Press any key to continue!  ***");
	gotoxy(25, 15);
	printf("***********************************");
	gotoxy(40, 17);
	int key = getch();
}

//按Esc判断是否退出游戏,Y/y退出,N/n继续
int xy_Exit()
{
	int key = '#';
	system("cls");
	gotoxy(25, 13);
	printf("***********************************");
	gotoxy(25, 14);
	printf("*****  Are you sure to exit?  *****");
	gotoxy(25, 15);
	printf("***********************************");
	gotoxy(40, 17);
	printf("Y/N:");
	//判断是否退出游戏
	while (key != 'Y'&&key != 'y'&&key != 'N'&&key != 'n')
	{
		key = getch();
		if (key == 'Y' || key == 'y')
		{
			return 1;
		}
		if (key == 'N' || key == 'n')
		{
			return 0;
		}
	}
}

//玩游戏
int PlayGame()
{
	int x, y, i, j;
	srand((unsigned)time(NULL));//随机数种子
	char ch;
	char cur;
	i = rand() % 3;//获取行值
	j = rand() % (12 - i); //获取列值
	cur = kw[i][j];//得到当前显示字母
	x = 24 + i * 2 + j * 3;//定位当前显示字母的位置
	y = 6;
	while (y < 24)
	{
		system("cls");//清屏
		DrawMap();
		gotoxy(x, y++);//让字母做自由落体
		printf("%c", cur);
		Sleep(500 - RightCounter * 5);//定时,正确个数越多,速度越快
		if (kbhit())//判断是否有按键按下
		{
			ch = getch();//获得按键值
			if (ch == cur || ch == cur + 32)//判断按键输入是否与显示相等,大小写字母都可以
			{
				RightCounter++;//正确个数自增,并退出循环进行下一个
				break;
			}
			else if (ch == 27)//如果按下Esc键
			{
				if (xy_Exit())//输入的是Y或y
				{
					system("cls");
					exit(0);//退出程序
				}
				else
				{
					system("cls");
					xy_Pause();
				}
			}
			else if (ch == '\r')//如果按下回车键,则暂停程序
			{
				system("cls");
				xy_Pause();
			}
			else
			{
				WrongCounter++;//如果按键输入与显示不等,则错误个数自增
			}
		}
	}
	AllCounter++;//总数自增
	if (y >= 24)
	{
		WrongCounter++;//如果当前字母没有按键按下,错误个数也需要自增
	}
}
  • 7
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值