#include <stdio.h>
#include <conio.h>
#include <stdbool.h>
#include <time.h>
#include <ctype.h>
#include <windows.h>
#define WIRD 50
#define HIGH 25
struct Word{
int x;
int y;
char ch;
bool flag;
};
int count = 0; //分数
void gotoxy(int x, int y);
void graph(void); //初始地图
void gamestart(void); //开始游戏
void inword(struct Word *pw, int n); //初始化字符数据
int main(void)
{
srand((unsigned int)time(0));
graph(); // 地图
gamestart(); //开始 ,按掉了的字符正确输入,不分大小写;
getch();
gotoxy(0,0);
printf("游戏得分%d,拜拜",count);
return 0;
}
void inword(struct Word *pw, int n)
{
int i, m;
pw[0].ch = rand()%26 + 'A'; //随机26个大写字母
pw[0].x = rand()%WIRD +10; //随机出现的位置
pw[0].y = rand()%HIGH; //随机高度
if(pw[0].y > 19)
pw[0].y = 19;
pw[0].flag = true;
for(i=1;i<n;++i)
{
pw[i].ch = rand()%26 + 'A';
pw[i].x = rand()%WIRD +10;
pw[i].y = rand()%HIGH;
if(pw[i].y > 19)
pw[i].y = 19;
pw[i].flag = true;
for(m=0;m<i;++m)
{
if(pw[m].ch==pw[i].ch || pw[m].x == pw[i].x) //如果有相同的数据,从新写入
{
i--;
break;
}
}
}
}
void gamestart(void)
{
struct Word word[3];
int x,y;
int i,m;
char ch;
inword(word, 3); //初始化字符数据
y = word[0].y;
for(i=1;i<3;++i) //找到离底线最近的坐标
{
if(y < word[i].y)
y = word[i].y;
}
while(1)
{
for(y ; y<=HIGH; y++)
{
Sleep(400); //在标准C中是sleep(S不要大写),下面使用大写的来说明,具体
//用什么看你用什么编译器。简单的说VC用Sleep,别的一律使用sleep
for(i=0;i<3;++i) //打印出结构体数组中的字符
{
if(word[i].y != 0)
{
gotoxy(word[i].x,word[i].y-1);
printf(" ");
}
gotoxy(word[i].x, (word[i].y)++); // (word[i].y)++ 是控制每个单独字符的下落
printf("%c",word[i].ch);
}
gotoxy(70,0);
printf("分数:%d",count); //分数
if(kbhit())
{
ch = toupper(getch()); //接受输入,转为大写
for(i=0;i<3;i++)
{
if(ch==word[i].ch) //如果找到正确的 字符,分数加 1
{
count++;
gotoxy(word[i].x,word[i].y-1); //清除正确的字符
printf(" ");
word[i].flag = false;
word[i].ch = rand()%26 + 'A';
word[i].x = rand()%WIRD +10;
word[i].y = rand()%HIGH;
if(word[i].y > 19)
word[i].y = 19;
for(m=0;m<3;++m) // 防止出现重复的字符
{
if(word[m].flag && (word[i].ch == word[m].ch || word[i].x == word[m].x))
{
word[i].ch = rand()%26 + 'A';
word[i].x = rand()%WIRD +10;
word[i].y = rand()%HIGH;
if(word[i].y > 19)
word[i].y = 19;
m = -1;
}
}
y = word[0].y;
for(m=1;m<3;++m) //找到离底线最近的坐标
{
if(y < word[m].y)
y = word[m].y;
}
y--;
break;
}
}
if(i == 3) //如果有正确输入 ,i 不会等于 3
{
gotoxy((WIRD+10)/2,HIGH/2);
printf("按错字母,游戏结束\n");
return;
}
}
}
if(y > HIGH) //超过高度结束
{
gotoxy((WIRD+10)/2,HIGH/2);
printf("超出底线,游戏结束\n");
return;
}
}
}
void graph(void)
{
int x;
for(x = 10; x <=WIRD+10; x+=2) //打印一条线
{
gotoxy(x,HIGH);
printf("_");
}
}
void gotoxy(int x, int y) {
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体,具体位置
}
c语言打字母小游戏
最新推荐文章于 2021-12-25 23:11:57 发布