C语言简易打字母游戏代码,纯C语言写的简单打字游戏

# include "Typer.h"

# include

# include

# include

//download by 链接已屏蔽

void main()

{

BOOL bQuit=FALSE;/* 是否退出 */

BOOL bPause=FALSE;/* 是否暂停 */

int tm1,tm2;

BYTE Key;

randomize();/* 初始化随机数种子 */

SetGraphMode();

SelectLevel();

ShowBar();

tm1=CreateTimer(1,MoveLetter);/* 创建移动字符对象时钟 */

tm2=CreateTimer(Interval,CreateLetter);/* 创建产生字符对象时钟 */

CreateLetter();

Key=AscKey();

while (!bQuit)

{

TimerEvent();

switch (Key)

{

case NULL:

break;

case KEY_ESC:

bQuit=TRUE;

break;

case KEY_SPACE:

bPause=!bPause;

tmTM[tm1].Enable=!bPause;

tmTM[tm2].Enable=!bPause;

break;

default:

if (!bPause) Check(Key);

}

Key=AscKey();

}

CloseGraphMode();

}

void SetGraphMode()

{

int Device=VGA,Mode=VGAHI;

initgraph(&Device,&Mode,"");

settextstyle(TRIPLEX_FONT,HORIZ_DIR,1);

setfillstyle(SOLID_FILL,0);

setcolor(7);

}

void CloseGraphMode()

{

restorecrtmode();

}

/* 从键盘缓冲区内直接读出ASC码 */

BYTE AscKey(void)

{

int start,end;

WORD key=0;

start=peek(0,0x41a);

end=peek(0,0x41c);

if (start==end) return(0);

else

{

key=peek(0x40,start);

start+=2;

if (start==0x3e) start=0x1e;

poke(0x40,0x1a,start);

return(key&0xff);

}

}

void MoveLetter(void)

{

int i;

for (i=0;i

{

if (Letter[i].Used)

{

HideLetter(i);

Letter[i].y+=Step;

ShowLetter(i);

/* 字符对象下落到最底部 */

if (Letter[i].y>MAX_HEIGHT) KillLetter(i);

}

}

}

void KillLetter(int LetterID)

{

if (Letter[LetterID].Used)

{

Letter[LetterID].Used=FALSE;

LetterUsed--;

HideLetter(LetterID);

}

/* 删除字符对象后马上再创建一个 */

CreateLetter();

}

void CreateLetter()

{

int i=0;

int x;

BYTE val;

if (LetterUsed==MAX_LETTER) return; /* 无字符对象可用则返回 */

while (Letter[++i].Used);/* 找到第一个空闲的字符对象,产生一个字符对象 */

x=i;

Letter[i].x=x*640/MAX_LETTER;

Letter[i].y=0;

Letter[i].val=random(26)+'A';

Letter[i].Used=TRUE;

LetterUsed++;

}

void HideLetter(int ID)

{

/* 用填充矩形来消隐字符 */

bar(Letter[ID].x,Letter[ID].y,Letter[ID].x+16,Letter[ID].y+20);

}

void ShowLetter(int ID)

{

char str[2]={0,0};

str[0]=Letter[ID].val;

outtextxy(Letter[ID].x,Letter[ID].y,str);

}

void Check(BYTE Key)

{

int i;

char str[6];

Hits++;

for (i=0;i

/* 击中 */

if (Letter[i].Used&&Letter[i].val==toupper(Key))

{

sound(1000);

delay(10);

KillLetter(i);

Right++;

nosound();

}

/* 显示状态 */

setfillstyle(SOLID_FILL,5);

bar(260,430,320,450);

bar(410,430,470,450);

setcolor(2);

sprintf(str," %4ld",Hits);

outtextxy(260,430,str);

sprintf(str," %4ld",Right);

outtextxy(410,430,str);

setcolor(7);

setfillstyle(SOLID_FILL,0);

}

void ShowBar()

{

FILE *bmp;

BYTE r,g,b,t;

int i,x,y;

bmp=fopen("bar.bmp","rb");

fseek(bmp,54,SEEK_SET);

for (i=0;i<16;i++)

{

setpalette(i,i);

b=fgetc(bmp)>>2;

g=fgetc(bmp)>>2;

r=fgetc(bmp)>>2;

t=fgetc(bmp)>>2;

setrgbpalette(i,r,g,b);

}

for (y=0;y<80;y++)

for (x=0;x<320;x++)

{

t=fgetc(bmp);

putpixel(x*2,479-y,t>>4);

putpixel(x*2+1,479-y,t&15);

}

fclose(bmp);

}

void SelectLevel()

{

int Steps[3]={1,2,4};

int Intervals[3]={18,9,5};

int Sel=0;

FILE *bmp;

BYTE r,g,b,t,Key;

int i,x,y;

bmp=fopen("sel.bmp","rb");

fseek(bmp,54,SEEK_SET);

for (i=0;i<16;i++)

{

setpalette(i,i);

b=fgetc(bmp)>>2;

g=fgetc(bmp)>>2;

r=fgetc(bmp)>>2;

t=fgetc(bmp)>>2;

setrgbpalette(i,r,g,b);

}

for (y=0;y<200;y++)

for (x=0;x<160;x++)

{

t=fgetc(bmp);

putpixel(x*2+160,339-y,t>>4);

putpixel(x*2+161,339-y,t&15);

}

fclose(bmp);

while (TRUE)

{

Key=toupper(AscKey());

if (Key=='A') Sel=1;

if (Key=='B') Sel=2;

if (Key=='C') Sel=3;

if (Sel) break;

}

Step=Steps[Sel-1];

Interval=Intervals[Sel-1];

cleardevice();

}



更多源码 | 好库简介 | 网站地图 | 帮助中心 | 版权说明

Copyright© 2009-2012 OKBASE.NET All Rights Reserved 好库网 版权所有

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值