/* 简单模拟键盘打字练习程序 */
typedef struct _circle
{
int x;
int y;
int life;
char ch;
}stu;
#define N 1
#define R 10
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "graphics.h"
stu data[N];
/* ------------- */
/* 图形初始化 */
/* ------------- */
void init()
{
int gd=DETECT, gm;
registerbgidriver(EGAVGA_driver);
initgraph( &gd, &gm, "" );
}
/* ------------- */
/* 数据初始化 */
/* ------------- */
void initdata()
{
int i;
int x=200, y=50;
for( i=0; i<N; i++ )
{
data[i].x = x+i*25;
data[i].y = y-i*10;
data[i].life = 1;
data[i].ch = srand_(1); /* 产生一个随即字符 */
}
}
/* ------------- */
/* 字符形状设计 */
/* ------------- */
void circle_()
{
int i;
static char ch[2]; /* 字符缓冲区 */
for( i=0; i<N; i++ ) /* 此循环解决 life=0时的处理 [按对按妞及越界] */
{
if( 0 == data[i].life )
{
setcolor( BLACK );
circle( data[i].x, data[i].y, R);
setcolor(0);
ch[0]= data[i].ch;
outtextxy( data[i].x-3, data[i].y-3, ch );
data[i].y = 0;
data[i].ch = srand_(1);
data[i].life = 1;
}
}
for( i=0; i<N; i++ ) /* 此循环抹掉旧圆 */
{
setcolor( BLACK );
circle( data[i].x, data[i].y, R);
setcolor(0);
ch[0]= data[i].ch;
outtextxy( data[i].x-3, data[i].y-3, ch );
}
for( i=0; i<N; i++ ) /* 此循环更新出最新的圆 */
{
setcolor( GREEN );
data[i].y++;
circle( data[i].x, data[i].y, R);
setcolor(7);
ch[0]= data[i].ch;
outtextxy( data[i].x-3, data[i].y-3, ch );
}
}
/* ------------- */
/* 逻辑检查区域 */
/* ------------- */
void if_( int key )
{
int i=0;
for( i=0; i<N; i++ )
{
if( 1 == data[i].life ) /* 该圆存在屏幕中进行检查 */
{
if( 485 == data[i].y ) /* 越解检查 */
{
data[i].life = 0;
}
if( (char)key == data[i].ch )
{
data[i].life = 0;
score();
}
}
}
}
/* -------------------- */
/* 生产随即字母时间间隔 */
/* -------------------- */
int shijian( void )
{
time_t temp; /* 时间变量 */
struct tm *p=NULL; /* 时间结构体指针变量 */
static int tmp=-1; /* 比较变量 */
while(1)
{
time( &temp ); /* 取得系统当前日期和时间 */
p = localtime( &temp ); /* 保存信息到p */
if( tmp == -1 )tmp = p->tm_sec + 1; /* 取得快1秒时间 */
if( tmp == 60 )tmp = 0; /* 秒时间最大为59 */
if( p->tm_sec == tmp ) /* 达到间隔时差 */
{
tmp = -1; /* 初始化该变量 */
return 1;
}
else
return 0;
}
}
/* ------------------------ */
/* 产生字符及分配字符函数 */
/* 参数 1取字符 0产生字符 */
/* ------------------------ */
int srand_( int mode )
{
static char str[256];
static int i=0;
static int j=0;
int data;
if( 3 == mode )
{
outtextxy( 10,10, str );
return 0;
}
if( 1 == mode )
{
return str[i++]; /* 以取字符形式调用 */
}
srand( (unsigned)time(NULL) );
data = rand()%57+65;
while( 91 <= data && 96 >= data )
{
data = rand()%65+57;
}
if( 255 == j){ j=0; } /* J用来保存随机字母下标变量 */
if( 255 == i){ i=0; }
str[j++] = data;
}
/* ------------- */
/* 提示函数 */
/* ------------- */
void hello()
{
int i=N;
static char ch[20]="Time ";
while(i--)
{
while( !shijian() ); /* 多余时间浪费在空循环中 */
srand_(0); /* 制作字符 */
setcolor(0);
outtextxy( 300,240, ch );
ch[5] = i+47;
setcolor(7);
outtextxy( 300,240, ch );
if(!i) /* 即将跳出循环前执行如下代码 */
{
setcolor(0);
outtextxy( 300,240, ch );
strcpy( ch,"Game Start!" );
setcolor(GREEN);
outtextxy( 300,240, ch );
while( !shijian() ); /* 多余时间浪费在空循环中 */
setcolor(0);
outtextxy( 300,240, ch );
}
}
}
/* ------------- */
/* 成绩显示 */
/* ------------- */
int score()
{
static char str[20];
static int s=0;
s++;
setfillstyle(SOLID_FILL,BLACK);
bar( 600,460,680,470 );
sprintf(str,"%d",s);
setcolor( 11 );
outtextxy( 600,460, str );
}
/* ------------- */
/* 核心控制 */
/* ------------- */
void gameplay()
{
int key=0;
while(1)
{
if( 0x11b == key){ break; }/* ESC退出 */
if( 0x5000 == key )srand_(3);
while( !kbhit() ) /* 无任何动作时自动循环 */
{
circle_(); /* 更新屏幕内容 */
if( shijian() )
{
srand_( 0 ); /* 间隔1秒产生一个字母进缓冲区 */
}
if_( key ); /* 逻辑检查 越界..敲入键盘信息符合 */
key = NULL; /* key 再初始化 */
delay( 20000 );
}
key = bioskey(0); /* 键盘信息取得 */
}
}
/* ------------- */
/* 主函数 */
/* ------------- */
int main( void )
{
void init()
init(); /* 图形初始化 */
hello(); /* 欢迎提示 */
initdata(); /* 数据初始化 */
gameplay(); /* 核心控制 */
getch();
closegraph(); /* 结束图形 */
}
因为用到图形函数所以C++等编译器不能编译
我吧连接的程序发在我的资源页面 没法运行的 可以去下载看看 呵呵
说下就是模拟金山打字通中的打字游戏 字母落下键盘敲击对应键 按ESC退出