【打字母游戏_C语言实现】

1.项目需求:

随机产生一个字母从屏幕下落,玩家输入字母,如果和显示字母相同,就消去字母;游戏会再随机产生一个字母,继续游戏,如果字母落出屏幕,玩家失败,游戏结束;

2.项目分析:

项目由两个模块构成:
1.显示模块 : 显示模块由二维数组构成,把随机产出的字母赋值到二维数组中。
2.处理模块 : 处理模块功能有:随机产生字母 ,输入字母比较,字母是否落出屏幕,字母下降功能;

3.项目设计:

3.1设计字母结构体:

struct LetterNode
{
	char ch;
	int row;
	int col;
};

3.2屏幕大小:

#define ROWSIZE 20									// 行
#define COLSIEE 70									// 列
typedef char GridArray[ROWSIZE][COLSIEE + 1];		//二维数组的 网格
#define LETSIZE 1									// 产生的字母数

3.3 添加二维数组空格函数 LnitGrid(GridArray ga)

void LnitGrid(GridArray ga)			//
{
	for (int i = 0; i < ROWSIZE; i++)
	{
		memset(ga[i], ' ', sizeof(char) * COLSIEE);
		ga[i][COLSIEE] = '\0';
	}
}

3.4 显示网格 ShowGrid(GridArray ga, struct LetterNode* px, int n)

清屏函数:system(“cls”); 头文件: #include <windows.h>

void ShowGrid(GridArray ga, struct LetterNode* px, int n)
{
	assert(px != NULL);
	system("cls");				//清屏
	LnitGrid(ga);
	for (int i = 0; i < n; ++i)
	{
		ga[px[i].row][px[i].col] = px[i].ch;
	}
	for (int i = 0; i < ROWSIZE; ++i)
	{
		printf("%s \n", ga[i]);
	}
}

3.5 产生随机字母函数 RandLetter(struct LetterNode* px, int n)

void RandLetter(struct LetterNode* px, int n)
{
	assert(px != NULL);
	srand((int)time(NULL));
	for (int i = 0; i < n; i++)
	{
		px[i].ch = rand() % 26 + 'a';
		px[i].row = 0;
		px[i].col = rand() % COLSIEE;
	}
}

3.6 main函数

_kbhit() : //键盘是否有输入 头文件:#include<conio.h>

int main()
{
	GridArray ga;
	char ch;
	struct LetterNode x[LETSIZE] = { 0 };
	RandLetter(x, LETSIZE);

	while (1)
	{
		ShowGrid(ga, x, LETSIZE);
		Sleep(1000);			//1000毫秒 == 1 秒
		if (_kbhit())			//键盘是否有输入
		{
			//ch = getchar();
			ch = _getch();		//无需等待回车输入
			if (ch == x[0].ch)
			{
				x[0].ch = rand() % 26 + 'a';
				x[0].row = -1;
				x[0].col = rand() % COLSIEE;
			}
		}
		x[0].row += 1;
		if (x[0].row >= ROWSIZE)
		{
			printf("游戏结束 \n");
			break;
		}
	}
	

	return 0;
}

3.7 调用的头文件

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<assert.h>
#include<Windows.h>
#include<time.h>
#include<conio.h>		
#include<ctype.h>

运行结果:
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千北@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值