#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
#define MAXN 10
struct Node {
int x;
int y;
char n;
};
struct Node node[MAXN];
int s,lifecount,number;
int high,width,count;
int sdeep,sdeepm;
void gotoxy(int x,int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup()
{
s = 0;
lifecount = 5;
number = 0;
high = 20;
width = 60;
count = 0;
sdeep = 100;
sdeepm=300;
}
void show()
{
int i;
gotoxy(0,high+1);
for(i=0;i<width+1;i++)
printf("_");
printf("\n");
printf("级 别: %d\n",s);
printf("得 分: %d\n",number);
printf("生命值: %d\n",lifecount);
HideCursor();
}
void createchar(int n)
{
node[n].x = rand()%60;
node[n].y = -1;
node[n].n = rand()%26+'A';
}
void deletechar(int n)
{
int i;
for(i = n;i<count;i++)
node[i] = node [i+1];
}
void updateWithoutInput()
{
int i;
static int sdeep = 0;
if(sdeep%sdeepm==0&&count<MAXN)
{
createchar(count);
count++;
}
if(sdeep%sdeepm==0)
{
for(i =0;i<count;i++)
{
gotoxy(node[i].x,node[i].y);
printf(" ");
node[i].y++;
if(node[i].y==high)
{
lifecount--;
if(lifecount==0)
{
gotoxy(0,high+2);
printf("游戏结束") ;
exit(0);
}
deletechar(i);
count--;
}
else
{
gotoxy(node[i].x,node[i].y);
printf("%c",node[i].n);
}
}
}
sdeep++;
}
void updateWithInput()
{
int i;
char input;
if(kbhit())
{
input = getch();
for(i =0;i<count;i++)
if(input == node[i].n||input-32 == node[i].n)
{
gotoxy(node[i].x,node[i].y);
printf(" ");
deletechar(i);
count--;
number++;
if(number%10==0)
{
s++;
if(sdeep>20)
sdeep = sdeep -10;
if(sdeep>60)
sdeepm = sdeepm -30;
}
break;
}
}
}
int main()
{
startup();
while(1)
{
srand(time(NULL));
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
打字游戏——基于C语言课程设计与游戏开发
最新推荐文章于 2023-07-27 16:00:55 发布