admin 管理员组

文章数量: 887019


2024年3月1日发(作者:渐变美甲亮片)

C语言游戏源代码

、 简单的开机密码程序

#include "conio.h"

#include "string.h"

#include "stdio.h"

void error

{window12;10;68;10;

textbackground15;

textcolor132;

clrscr;

cprintf"file or system error you can't enter the system";

while1; /*若有错误不能通过程序*/

}

void look

{FILE *fauto;*fbak;

char *pass="c:"; /*本程序的位置*/

char a25;ch;

char *au="";*bname="hecfback.^^^"; /*bname

的备份*/

setdisk2; /*set currently disk c:*/

chdir""; /*set currently directory */

fauto=fopenau;"r+";

if fauto==NULL

{fauto=fopenau;"w+";

是1

if fauto==NULL error;}

freada;23;1;fauto; /*读取前23各字符*/

a23='0';

if strcmpa;pass==0 /*若读取的和pass指针一样就关闭文件;不然就添加*/

fclosefauto;

else

{fbak=fopenbname;"w+";

if fbak==NULL error;

fwritepass;23;1;fbak;

fputc'n';fbak;

rewindfauto;

whilefeoffauto

{ch=fgetcfauto;

fputcch;fbak;}

rewindfauto;

rewindfbak;

whilefeoffbak

{ch=fgetcfbak;

fputcch;fauto;}

fclosefauto;

fclosefbak;

removebname; /*del bname file*/

}

}

void pass

char input60;

int n;

while1

{window1;1;80;25;

textbackground0;

textcolor15;

clrscr;

n=0;

window20;12;60;12;

textbackground1;

textcolor15;

clrscr;

cprintf"password:";

while1

{inputn=getch;

if n>58 {putchar7; break;} /*若字符多于58个字符就结束本次输入*/

if inputn==13 break;

if inputn>=32 && inputn<=122 /*若字符是数字或字母才算数*/

{putchar'*';

n++;}

if inputn==8 /*删除键*/

if n>0

{cprintf"b b";

inputn='0';

n--;}

}

inputn='0';

if strcmppassword;input==0

break;

else

{putchar7;

window30;14;50;14;

textbackground15;

textcolor132;

clrscr;

cprintf"password error";

getch;}

}

}

main

{look;

pass;

}

2、 彩色贪吃蛇

#include

#include

#define N 200

#define up 0x4800

#define down 0x5000

#define left 0x4b00

#define right 0x4d00

#define esc 0x011b

#define Y 0x1579

#define n 0x316e

int gamespeed; /* 游戏速度 */

int i; key; color;

int score = 0; /* 游戏分数 */

char cai48H =

{

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x04; 0x00; 0x18; 0x00; 0x00; 0x00; 0x0E; 0x00;

0x1C; 0x00; 0x00; 0x00; 0x1C; 0x00; 0x1C; 0x00;

0x00; 0x00; 0x20; 0x00; 0x38; 0x00; 0x00; 0x00;

0x40; 0x00; 0x78; 0x00; 0x00; 0x01; 0x80; 0x40;

0x70; 0x00; 0x00; 0x03; 0x80; 0xC0; 0xE0; 0x00;

0x00; 0x07; 0x80; 0x80; 0xC0; 0x00; 0x00; 0x0E;

0x11; 0x81; 0xC0; 0x00; 0x00; 0x08; 0x61; 0x01;

0x80; 0x00; 0x00; 0x00; 0x23; 0x03; 0x04; 0x00;

0x00; 0x02; 0x02; 0x00; 0x06; 0x00; 0x00; 0x1E;

0x04; 0x00; 0x0F; 0x00; 0x00; 0x1C; 0x1F; 0x80;

0x1E; 0x00; 0x00; 0x08; 0x3F; 0x80; 0x3C; 0x00;

0x00; 0x00; 0xFF; 0x80; 0x38; 0x00; 0x00; 0x03;

0xFF; 0x80; 0x78; 0x00; 0x00; 0x0F; 0xF8; 0x00;

0xF0; 0x00; 0x00; 0x7F; 0xF0; 0x00; 0xE0; 0x00;

0x03; 0xFF; 0xFC; 0x01; 0x80; 0x00; 0x03; 0xC0;

0xFF; 0x01; 0x03; 0x80; 0x01; 0x01; 0xFF; 0x00;

0x03; 0x80; 0x00; 0x01; 0x3F; 0x00; 0x07; 0x80;

0x00; 0x02; 0x11; 0x00; 0x07; 0x00; 0x00; 0x00;

0x10; 0x00; 0x07; 0x00; 0x00; 0x00; 0x10; 0x00;

0x0E; 0x00; 0x00; 0x08; 0x10; 0x00; 0x1C; 0x00;

0x00; 0x30; 0x10; 0x00; 0x18; 0x00; 0x00; 0x70;

0x10; 0x00; 0x30; 0x00; 0x01; 0xE0; 0x10; 0x00;

0x70; 0x00; 0x03; 0x80; 0x10; 0x00; 0x60; 0x00;

0x00; 0x00; 0x30; 0x00; 0xE0; 0x00; 0x00; 0x00;

0xF0; 0x01; 0xC0; 0x00; 0x00; 0x00; 0x70; 0x03;

0xC0; 0x00; 0x00; 0x00; 0x10; 0x07; 0x80; 0x00;

0x00; 0x00; 0x00; 0x0F; 0x00; 0x00; 0x00; 0x00;

0x00; 0x1E; 0x00; 0x00; 0x00; 0x00; 0x00; 0x3C;

0x00; 0x00; 0x00; 0x00; 0x00; 0x70; 0x00; 0x00;

0x00; 0x00; 0x01; 0xC0; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

};

char she48H =

{

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x04;

0x00; 0x00; 0x00; 0x00; 0x00; 0x0C; 0x00; 0x00;

0x00; 0x00; 0x00; 0x0E; 0x00; 0x00; 0x00; 0x00;

0x00; 0x0E; 0x00; 0x00; 0x00; 0x03; 0x00; 0x07;

0x00; 0x00; 0x00; 0x02; 0x00; 0x03; 0x00; 0x00;

0x00; 0x02; 0x00; 0x00; 0x00; 0x00; 0x00; 0x02;

0x00; 0x00; 0xF8; 0x00; 0x00; 0x02; 0x00; 0x07;

0x86; 0x00; 0x00; 0x02; 0x00; 0x18; 0x03; 0x00;

0x00; 0x02; 0x00; 0x00; 0x07; 0x80; 0x00; 0x03;

0xF0; 0x00; 0x07; 0x80; 0x00; 0x0F; 0xFC; 0x00;

0x0C; 0x00; 0x00; 0x7E; 0x3F; 0x80; 0x00; 0x00;

0x01; 0xFE; 0x1F; 0x80; 0x00; 0x00; 0x01; 0xE2;

0x39; 0x8C; 0x00; 0x00; 0x00; 0xC2; 0x30; 0x08;

0x00; 0x00; 0x00; 0xC2; 0x60; 0x08; 0x00; 0x00;

0x00; 0xC3; 0xE0; 0x08; 0x60; 0x00; 0x00; 0x7F;

0xE0; 0x01; 0xE0; 0x00; 0x00; 0x3F; 0x80; 0x1F;

0xE0; 0x00; 0x00; 0x1E; 0x00; 0x1F; 0x80; 0x00;

0x00; 0x1E; 0x00; 0x1F; 0x00; 0x00; 0x00; 0x02;

0x38; 0x1E; 0x00; 0x00; 0x00; 0x07; 0xFC; 0x1C;

0x00; 0x20; 0x00; 0x07; 0xFC; 0x18; 0x00; 0x20;

0x00; 0x1F; 0x0C; 0x10; 0x00; 0x20; 0x00; 0x7C;

0x04; 0x10; 0x00; 0x60; 0x01; 0xF0; 0x00; 0x10;

0x00; 0x60; 0x01; 0xE0; 0x00; 0x08; 0x00; 0xF0;

0x00; 0x80; 0x00; 0x08; 0x03; 0xF0; 0x00; 0x00;

0x00; 0x07; 0xFF; 0xF0; 0x00; 0x00; 0x00; 0x07;

0xFF; 0xF0; 0x00; 0x00; 0x00; 0x03; 0xFF; 0xE0;

0x00; 0x00; 0x00; 0x01; 0xFF; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

};

char tun48H =

{

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x0E; 0x00; 0x00; 0x00; 0x00; 0x00; 0x3E;

0x00; 0x00; 0x00; 0x00; 0x00; 0x7F; 0x00; 0x00;

0x00; 0x00; 0x00; 0xE0; 0x00; 0x00; 0x00; 0x00;

0x03; 0xC0; 0x00; 0x00; 0x00; 0x00; 0x1F; 0x00;

0x00; 0x00; 0x00; 0x00; 0x7C; 0x00; 0x00; 0x00;

0x00; 0x01; 0xF8; 0x00; 0x00; 0x00; 0x00; 0x03;

0xF8; 0x00; 0x40; 0x00; 0x00; 0x00; 0x06; 0x07;

0xC0; 0x00; 0x00; 0x00; 0x07; 0xFF; 0xE0; 0x00;

0x00; 0x00; 0x07; 0xFF; 0xE0; 0x00; 0x00; 0x00;

0x0F; 0xFF; 0x80; 0x00; 0x00; 0x00; 0x7F; 0xF8;

0x00; 0x00; 0x00; 0x1F; 0xFF; 0xF8; 0x00; 0x00;

0x00; 0x1F; 0xFF; 0xF8; 0x00; 0x00; 0x00; 0x1F;

0xFC; 0x3C; 0x00; 0x00; 0x00; 0x0F; 0xF8; 0x0E;

0x00; 0x00; 0x00; 0x04; 0x70; 0x07; 0x00; 0x00;

0x00; 0x00; 0x60; 0x03; 0x80; 0x00; 0x00; 0x00;

0xC0; 0x00; 0xC0; 0x00; 0x00; 0x01; 0x80; 0x00;

0x30; 0x00; 0x00; 0x01; 0x00; 0x3C; 0x18; 0x00;

0x00; 0x02; 0x03; 0xFF; 0x0C; 0x00; 0x00; 0x0C;

0x7F; 0xFF; 0x8E; 0x00; 0x00; 0x18; 0xFF; 0xFF;

0xC7; 0x80; 0x00; 0x78; 0xFE; 0x07; 0x87; 0xE0;

0x01; 0xF0; 0x70; 0x07; 0x03; 0xF8; 0x07; 0xE0;

0x70; 0x0E; 0x03; 0xFE; 0x00; 0x00; 0x38; 0x1E;

0x01; 0xFE; 0x00; 0x00; 0x3F; 0xFE; 0x00; 0x0C;

0x00; 0x00; 0x1F; 0xFE; 0x00; 0x00; 0x00; 0x00;

0x1F; 0xFE; 0x00; 0x00; 0x00; 0x00; 0x0F; 0xFE;

0x00; 0x00; 0x00; 0x00; 0x04; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

};

char dan48H =

{

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0xFC; 0x00; 0x00; 0x00; 0x00; 0x07; 0xFF;

0x00; 0x00; 0x00; 0x00; 0x7F; 0xC0; 0x80; 0x00;

0x00; 0x03; 0xFF; 0x80; 0x40; 0x00; 0x00; 0x01;

0xF1; 0x80; 0x40; 0x00; 0x00; 0x01; 0x81; 0x80;

0xE0; 0x00; 0x00; 0x00; 0x01; 0x93; 0xF0; 0x00;

0x00; 0x00; 0x01; 0xFF; 0xF0; 0x00; 0x00; 0x00;

0x21; 0xFF; 0xF0; 0x00; 0x00; 0x00; 0x21; 0xF8;

0x00; 0x00; 0x00; 0x00; 0x61; 0xC0; 0x00; 0x00;

0x00; 0x00; 0x61; 0x80; 0x00; 0x00; 0x00; 0x00;

0xF3; 0x00; 0x00; 0x00; 0x00; 0x00; 0xFF; 0x00;

0x00; 0x00; 0x00; 0x01; 0xFF; 0xC0; 0x00; 0x00;

0x00; 0x03; 0xFF; 0xF8; 0x00; 0x00; 0x00; 0x02;

0x00; 0xFC; 0x00; 0x00; 0x00; 0x04; 0x02; 0x1F;

0x00; 0x00; 0x00; 0x08; 0x03; 0x01; 0xC0; 0x00;

0x00; 0x38; 0x03; 0x00; 0x7C; 0x00; 0x00; 0xF8;

0x07; 0xF8; 0x3F; 0xC0; 0x01; 0xF0; 0x3F; 0xFE;

0x3F; 0xF8; 0x03; 0xC1; 0xFF; 0x0F; 0x1F; 0xF8;

0x00; 0x01; 0xE3; 0x0F; 0x0F; 0xF0; 0x00; 0x01;

0xC3; 0x0E; 0x00; 0x00; 0x00; 0x01; 0x83; 0xFC;

0x00; 0x00; 0x00; 0x00; 0xC7; 0xF8; 0x00; 0x00;

0x00; 0x00; 0xFF; 0xF8; 0x00; 0x00; 0x00; 0x00;

0x7F; 0xF0; 0x00; 0x00; 0x00; 0x00; 0x3F; 0x03;

0x80; 0x00; 0x00; 0x00; 0x03; 0x04; 0x00; 0x00;

0x00; 0x00; 0x03; 0xF8; 0x00; 0x00; 0x00; 0x00;

0x1F; 0xF8; 0x20; 0x00; 0x00; 0x00; 0xFF; 0xFF;

0xE0; 0x00; 0x00; 0x07; 0xFF; 0x81; 0xE0; 0x00;

0x00; 0x07; 0xE0; 0x00; 0xE0; 0x00; 0x00; 0x03;

0x00; 0x00; 0x60; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;

};

char zuo16H =

{

0x18; 0xC0; 0x18; 0xC0; 0x19; 0x80; 0x31; 0xFE;

0x33; 0xFE; 0x76; 0xC0; 0xF0; 0xFC; 0xB0; 0xFC;

0x30; 0xC0; 0x30; 0xC0; 0x30; 0xFE; 0x30; 0xFE;

0x30; 0xC0; 0x30; 0xC0; 0x30; 0xC0; 0x00; 0x00;

};

char zhe16H =

{

0x03; 0x00; 0x03; 0x0C; 0x1F; 0xCC; 0x1F; 0xD8;

0x03; 0x30; 0xFF; 0xFE; 0xFF; 0xFE; 0x03; 0x00;

0x0F; 0xF8; 0x3F; 0xF8; 0xEC; 0x18; 0xCF; 0xF8;

0x0C; 0x18; 0x0F; 0xF8; 0x0F; 0xF8; 0x00; 0x00;

};

char tian16H =

{

0x00; 0x00; 0x3F; 0xFC; 0x3F; 0xFC; 0x31; 0x8C;

0x31; 0x8C; 0x31; 0x8C; 0x3F; 0xFC; 0x3F; 0xFC;

0x31; 0x8C; 0x31; 0x8C; 0x31; 0x8C; 0x3F; 0xFC;

0x3F; 0xFC; 0x30; 0x0C; 0x00; 0x00; 0x00; 0x00;

};

char xue16H =

{

0x33; 0x18; 0x19; 0x98; 0x08; 0xB0; 0x7F; 0xFC;

0x7F; 0xFC; 0x60; 0x0C; 0x1F; 0xF0; 0x1F; 0xF0;

0x00; 0xC0; 0x7F; 0xFC; 0x7F; 0xFC; 0x01; 0x80;

0x01; 0x80; 0x07; 0x80; 0x03; 0x00; 0x00; 0x00;

};

char ke16H =

{

0x00; 0x00; 0x0C; 0x18; 0xFD; 0x98; 0xF8; 0xD8;

0x18; 0x58; 0xFE; 0x18; 0xFE; 0x98; 0x18; 0xD8;

0x3C; 0x58; 0x7E; 0x1E; 0xDB; 0xFE; 0x9B; 0xF8;

0x18; 0x18; 0x18; 0x18; 0x18; 0x18; 0x00; 0x00;

};

struct Food/*定义结构体存储食物的属性*/

{

int x; /* 食物的坐标 */

int y;

int yes; /* 值为0表示屏幕上没有食物;值为1表示屏幕上有食物 */

int color; /* 食物颜色 */

} food;

struct Snake/*定义结构体存储蛇的属性*/

{

int xN; /* 每一节蛇的坐标 */

int yN;

int colorN;/*存储每一节蛇的颜色*/

int node; /* 蛇的节数 */

int direction; /* 蛇移动的方向 */

int life; /* 蛇的生命;如果为1;蛇死;游戏结束 */

} snake;

void initvoid/*图形驱动*/

{

int driver = DETECT; mode = 0;

registerbgidriverEGAVGA_driver;

initgraph&driver; &mode; "";

}

void drawmatchar *mat; int matsize; int x; int y; int color /*汉字点阵*/

{

int i; j; k; m;

m = matsize - 1 / 8 + 1;

forj = 0; j < matsize; j++

fori = 0; i < m; i++

fork = 0; k < 8; k++

ifmatj*m+i&0x80 >> k

putpixelx + i * 8 + k; y + j; color;

}

void showwordvoid

{

/* 调用汉字点阵输出程序;显示标题和作者信息 */

drawmatcai48H; 48; 249; -4; 7;

drawmatshe48H; 48; 329; -4; 7;

drawmattun48H; 48; 409; -4; 7;

drawmatdan48H; 48; 489; -4; 7;

drawmatcai48H; 48; 250; -5; 4;

drawmatshe48H; 48; 330; -5; 4;

drawmattun48H; 48; 410; -5; 4;

drawmatdan48H; 48; 490; -5; 4;

/*作者 田学科*/

drawmatzuo16H; 16; 515; 465; 7;

drawmatzhe16H; 16; 530; 465; 7;

drawmattian16H; 16; 550; 465; 7;

drawmatxue16H; 16; 565; 465; 7;

drawmatke16H; 16; 580; 465; 7;

}

void drawvoid/*画出四周的墙*/

{

ifcolor == 15

color = 0;

setcolor++color;

setlinestyleSOLID_LINE; 0; 1;

fori = 30; i <= 600; i += 10

{

rectanglei; 40; i + 10; 49;

rectanglei; 451; i + 10; 460;

}

fori = 40; i < 450; i += 10

{

rectangle30; i; 39; i + 10;

rectangle601; i; 610; i + 10;

}

}

void prscorevoid

{

/* 打印游戏分数 */

char str10;

setfillstyleSOLID_FILL; YELLOW;

bar50; 10; 200; 30;

setcolor6;

settextstyle0; 0; 2;

sprintfstr; "score:%d"; score;

outtextxy55; 15; str;

}

void gameovervoid

{

cleardevice; /* 清屏函数 */

fori = 0; i < ; i++ /* 画出蛇死时的位置 */

{

;

; ; + 10; + 10;

}

prscore; /* 显示分数 */

draw;

showword;

settextstyle0; 0; 6;

setcolor7;

outtextxy103; 203; "GAME OVER";

setcolorRED;

outtextxy100; 200; "GAME OVER";

}

void gameplayvoid/* 玩游戏的具体过程 */

{

int flag; flag1;

randomize;

prscore;

gamespeed = 50000;

= 0; /* =0表示屏幕上没有食物 */

= 1; /* =1表示蛇是活着的 */

ion = 4; /* 表示蛇的初始方向为向右 */

= 2; /* 蛇的初始化为两节 */

0 = 2; /*两节蛇头初始化为绿色*/

1 = 2;

snake.x0 = 100;

snake.y0 = 100;

snake.x1 = 110;

snake.y1 = 100;

= random15 + 1;

while1

{

while1

{

== 0 /* 如果蛇活着 */

{

while1

{

flag = 1;

= 1;

food.x = random56 * 10 + 40;

food.y = random40 * 10 + 50;

fori = 0; i < ; i++

{

iffood.x == && food.y ==

flag = 0;

}

ifflag break;

}

}

{

;

rectanglefood.x; food.y; food.x + 10; food.y + 10;

}

fori = - 1; i > 0; i--

{

= -1;

= -1;

}

ion

{

case 1:

snake.y0 -= 10;

break;

case 2:

snake.y0 += 10;

break;

case 3:

snake.x0 -= 10;

break;

case 4:

snake.x0 += 10;

break;

}

fori = 3; i < ; i++

{

== snake.x0 && == snake.y0

{

gameover;

= 0;

break;

}

}

ifsnake.x0 < 40 || snake.x0 > 590 || snake.y0 < 50 || snake.y0 >

440

{

gameover;

= 0;

}

== 0

break;

ifsnake.x0 == food.x && snake.y0 == food.y /*蛇吃掉食物*/

{

setcolor0;

rectanglefood.x; food.y; food.x + 10; food.y + 10;

= -20;

= -20;

= ;

++;

= 0;

= random15 + 1;

score += 10;

prscore;

ifscore % 100 == 0 && score = 0

{

fori = 0; i < ; i++ /*

{

;

;

+ 10;

}

sound200;

delay50000;

delay50000;

delay50000;

delay50000;

delay50000;

delay50000;

画出蛇 */

; + 10;

nosound;

gamespeed -= 5000;

draw;

}

else

{

sound500;

delay500;

nosound;

}

}

fori = 0; i < ; i++ /* 画出蛇 */

{

;

; ; + 10; + 10;

}

delaygamespeed;

delaygamespeed;

flag1 = 1;

setcolor0;

-1; -1;

-1 + 10; -1 + 10;

ifkbhit && flag1 == 1 /*如果没按有效键就重新开始循环*/

{

flag1 = 0;

key = bioskey0;

ifkey == esc

exit0;

else ifkey == up && ion = 2

ion = 1;

else ifkey == down && ion = 1

ion = 2;

else ifkey == left && ion = 4

ion = 3;

else ifkey == right && ion = 3

ion = 4;

}

}

== 0 /*如果蛇死了就退出循环*/

break;

}

}

void mainvoid

{

while1

{

color = 0;

init;

cleardevice;

showword;

draw;

gameplay;

setcolor15;

settextstyle0; 0; 2;

outtextxy200; 400; "CONTINUEY/N";

while1

{

key = bioskey0;

ifkey == Y || key == n || key == esc

break;

}

ifkey == n || key == esc

break;

}

closegraph;

}

3、 c语言实现移动电话系统

#include

#define GRID-SIZE 5

#define SELECTED -1 /*低于矩阵中所有元素*/

#define TRAFFIC-FILE “”/*关于交通数据的文件*/

#define NUM-TRANSMITTERS 10 /*可用的发射器数量*/

void get-traffic-dataint commutersGRID-SIZEGRID-SIZE;

int salesforceGRID-SIZEGRID-SIZE;

int weekends GRID-SIZEGRID-SIZE;

voide print-matrixGRID-SIZEGRID-SIZE;

int

mainvoid

{

int commutersGRID-SIZEGRID-SIZE;/*上午8:30的交通数据*/

int salesforceGRID-SIZEGRID-SIZE; /*上午11:00的交通数据*/

int weekendGRID-SIZEGRID-SIZE;/*周末交通数据*/

int commuter-weight; /*通勤人员数据的权重因子*/

sale-weight;/*营销人员数据的权重因子*/

weekend-weight; /*周末数据的权重因子*/

int location-i; /*每个发射器的位置*/

location-j;

int current-max; /*和数据中当前的最大值*/

int i;j; /*矩阵的循环计数器*/

tr; /*发射器的循环计数器*/

/*填入并显示交通数据*/

Get-traffic-data commuters;salesforce;weekend;

print-matrixcommuters;

printf“nn WEEKEND TRAFFIC DATAnn”;

print-matrixsalesforce;

printf“nn WEEKEND TRAFFIC DATAnn”;

printf_matrixweekeng;

/*请用户输入权重因子*/

printf“nnPlease input the following value:n”;

scanf“%d”;&commuter_weight;

printf“weightan integer>=0 for the weekeng data>”;

scanf“%d”;&weekend_weight;

scanf“%d”;&weekend_weight;

/*计算并显示加权后求和的数据*/

for i=0;i

for j=0;j

summed_dataij=commuter_weight*commuterij+

salesforce_weight*salesforceij+

weekend_weight*weekendij;

printf“nnThe weighted;summed data is :nn”;

printf_matrixsummed_data;

/*在summed_data矩阵中找出NUM_TRANSMITTERS个最大值;将坐标临时存储在location_i和location_j中;然后把最后的结果坐标输出*/

printf“nnLocations of the %d transmitters:nn”;NUM_TRANSMITTERS;

for tr=1;tr<=NUM_TRANSMITTERS;++tr{

current_max=SELECTED;/*以一个过低的值为起点开始查找*/

for i=0;i

forj=0;j

ifcurrent_max

current_max=summed_dataij{

location_i=i;

location_j=j;

}

}

}

/*将选中的单元赋一较低的值以避免下次再选中这一元素;显示查找结果*/

summed_datalocation_ilocation_j=SELECTED;

printf“Transmitter %3d:at location %3d %3dn”;

tr;location_i;location_j;

}

return 0;

}

/*

*把 TRAFFIC_FILE中的交通数据填充到3个GRID_SIZE×GRID_SIZE数组中

*/

void

get_traffic_dataint commutersGRID_SIZE;/*输出*/

int salesforceGRID_SIZEGRID_SIZE;/*输出*/

int weekendGRID_SIZEGRID_SIZE;/*输出*/

{

int i;j; /*循环计数器*/

FILE *fp; /*文件指针*/

fq=fopenTRAFFIC_FILE;“r”;

fori=0;i

forj=0;j

fscanffp;“%d”;&commntersij;

fori=0;i

forj=0;j

fscanffq;“%d”;&weekendij;

fclosefq;

}

/*

*显示一个GRID_SIZE×GRID_SIZE整数矩阵的内容

*/

void

print_matrixint matrixGRID_SIZEGRID_SIZE

{

int i;j; /*循环计数器*/

fori=0;i

{ forj=0;j

printf“%3d”;matrixij;

printf“n”;

}

}

4、 扑克牌游戏

/*************************************

CopyrightC 2004-2005 vision;math;NJU.

File Name: guess_

Author: vision Version: 1.0 Data: 23-2-2004

Description: 给你9张牌;然后让你在心中记住那张牌;然后电脑分组让你猜你记住的牌在第几组;然后猜出你记住的那张牌.

Other: 出自儿童时的一个小魔术

History:修改历史

**************************************/

#include

#include

#include

#include

#include

#define CARDSIZE 52 /*牌的总张数*/

#define SUITSIZE 13 /*一色牌的张数*/

/*扑克牌结构*/

typedef struct Card

{

char val;/*扑克牌面上的大小*/

int kind :4; /*扑克牌的花色*/

}Card;

/*************************************************

Function: // riffle

Description: // 洗牌;然后随机的得到9张牌;要求九张牌不能有重复.

Calls: //

Called By: // main

Table Accessed: //被修改的表此项仅对于牵扯到数据库操作的程序

Table Updated: // 被修改的表此项仅对于牵扯到数据库操作的程序

Input: //Card card 牌结构; int size 结构数组的大小

Output: //

Return: // void

Others: // 此函数修改card的值;希望得到九张随机牌

Bug: //此函数有bug;有时会产生两个相同的牌;有待修订

*************************************************/

void riffleCard *cards; int size;

/*************************************************

Function: // show

Description: // 显示数组的内容

Calls: //

Called By: // main

Table Accessed: //被修改的表此项仅对于牵扯到数据库操作的程序

Table Updated: // 被修改的表此项仅对于牵扯到数据库操作的程序

Input: //Card *card 牌结构指针; int size 结构数组的大小

Output: //

Return: // void

Others: //

*************************************************/

void showconst Card *cards; int size;

/*************************************************

Function: // grouping

Description: //把9张牌分别放到3个数组中;每组3张;a.e分组

Calls: //

Called By: // main

Table Accessed: //被修改的表此项仅对于牵扯到数据库操作的程序

Table Updated: // 被修改的表此项仅对于牵扯到数据库操作的程序

Input: //Card *card 牌结构指针; int size 结构数组的大小

Output: //

Return: // void

Others: // 此函数修改 *carr1;*carr2;* carr3的值

*************************************************/

void groupingconst Card *cards; Card *carr1; Card *carr2; Card *carr3;

/*************************************************

Function: // result_process

Description: //用递归计算;所选的牌

Calls: // rshift

Called By: // main

Table Accessed: //被修改的表此项仅对于牵扯到数据库操作的程序

Table Updated: // 被修改的表此项仅对于牵扯到数据库操作的程序

Input: //Card *carr1; Card *carr2; Card *carr3

Output: //

Return: // void

Others: // 此函数修改 *carr1;*carr2;* carr3的值

*************************************************/

Card* result_processCard *carr1; Card *carr2; Card *carr3; int counter;

/*************************************************

Function: // rshift

Description: //右移操作

Calls: //

Called By: // result_process

Table Accessed: //被修改的表此项仅对于牵扯到数据库操作的程序

Table Updated: // 被修改的表此项仅对于牵扯到数据库操作的程序

Input: //Card *carr1; Card *carr2; Card *carr3 ;int counter

Output: //

Return: // Card*

Others: // 此函数修改 *carr1;*carr2;* carr3的值

*************************************************/

void rshiftCard *carr1; Card *carr2; Card *carr3; int counter;

void main

{

Card cards9; /*存放九张牌*/

Card carr13; /*第一组牌;cards array 1*/

Card carr23; /*第二组牌;cards array 2*/

Card carr33; /*第三组牌;cards array 3*/

int select = 0; /*玩家的选择*/

Card *selected_card;/*存放玩家所记住选的牌*/

int counter = 0;

rifflecards; 9; /*洗牌;得到九张牌*/

puts"请记住一张牌千万别告诉我最多经过下面三次我与你的对话;我就会知道你所记的那张牌";

puts"如果想继续玩;请准确的回答我问你的问题;根据提示回答";

puts"请放心;我不会问你你选了哪张牌的";

groupingcards; carr1; carr2; carr3; /*把9张牌分别放到3个数组中;每组3张;a.e分组*/

showcarr1; 3;

showcarr2; 3;

showcarr3; 3;

puts"请告诉我你记住的那张牌所在行数";

select = getchar;

switchselect/*分支猜你玩家记住的牌*/

{

case '1':

selected_card = result_processcarr1; carr2; carr3; counter;

break;

case '2':

selected_card = result_processcarr2; carr3; carr1; counter;

break;

case '3':

selected_card = result_processcarr3; carr1; carr2; counter;

break;

default:

puts"你在撒谎不和你玩了";

fflushstdin;

getchar;

exit0;

}

if selected_card ==NULL

{

fflushstdin;

getchar;

exit0;

}

puts"你猜的牌为:";

showselected_card; 1;

puts"我猜的对吧;哈哈~~~~";

fflushstdin;

getchar;

}

/*riffle的原代码*/

void riffleCard *cards; int size

{

char deckCARDSIZE;/*临时数组;用于存储牌*/

unsigned int seed;/*最为产生随机数的种的*/

int deckp = 0; /*在牌的产生中起着指示作用*/

seed = unsigned inttimeNULL;

srandseed;

/*洗牌*/

while deckp < CARDSIZE

{

char num = rand % CARDSIZE;

if memchrdeck; num; deckp == 0

{

assertmemchrdeck;num;deckp;

deckdeckp = num;

deckp++;

}

}

/*找9张牌给card*/

for deckp = 0; deckp < size; deckp++

{

div_t card = divdeckdeckp; SUITSIZE;

= "3456"; /*把商给*/

}

}

/*show的原代码;将会自动换行*/

void showconst Card *cards; int size

{

forint i = 0; i < size; i++

{

printf"%c%c ";;;

if i =0 && i+1 % 3 == 0

puts"";

}

puts""; /*自动换行*/

}

/*grouping 的原代码*/

void groupingconst Card *cards; Card *carr1; Card *carr2; Card *carr3

{

int i = 0;/*循环参数*/

/*分给carr1三个数*/

while i < 3

{

= ;

= ;

i++;

}

/*分给carr2接下来的三个数*/

while i < 6

{

= ;

= ;

i++;

}

/*分给carr3接下来的三个数*/

while i < 9

{

= ;

= ;

i++;

}

}

/*rshift的实现*/

void rshiftCard *carr1; Card *carr2; Card *carr3; int counter

{

Card temp2;/*用于存放carr2counter*/

Card temp3;/*用于存放carr3counter*/

/*temp = carr2*/

= ;

= ;

/*carr2 = carr1*/

= ;

= ;

/*temp3 = carr3*/

= ;

= ;

/*carr3 = carr2*/

= ;

= ;

/*carr1 = carr3*/

= ;

= ;

}

Card* result_processCard *carr1; Card *carr2; Card *carr3; int counter

{

rshiftcarr1; carr2; carr3; counter; /* 把数组的第一个元素依次右移*/

ifcounter == 2

{

return&carr22;

}

showcarr1; 3;

showcarr2; 3;

showcarr3; 3;

puts"请给出你记住的牌所在行数:";

fflushstdin;

int input = 1;

input = getchar; /*获取你选的组*/

switchinput

{

case '1':

returnresult_processcarr1; carr2; carr3; ++counter;

break;

case '2':

return&carr2counter;

break;

default:

puts"你在撒谎不和你玩了";

return NULL;

}

}

5、C语言实现打字游戏

#include "stdio.h"

#include "time.h"

#include "stdlib.h"

#include "conio.h"

#include "dos.h"

#define xLine 70

#define yLine 20

#define full 100

#define true 1

#define false 0

/*---------------------------------------------------------------------*/

void printScreenint level;int right;int sum;char pyLinexLine/* 刷新屏幕的输出图像 */

{

int i;j;

clrscr;

printf"level:%d Press 0 to exit;1 to pause

score:%d/%dn";level;right;sum;/* 输出现在的等级;击中数和现在已下落总数 */

printf

"----------------------------------------------------------------------n";

for i=0;i

{

forj=0;j

printf "%c";pij;

printf"n";

}/* for i */

printf

"----------------------------------------------------------------------n";

}/* printScreen */

/*---------------------------------------------------------------------*/

void leave/* 离开程序时;调用该函数结束程序.. */

{

clrscr;

printf "nnnnThank you for playing.";

delay 2500;

exit 0;

}

/*----------------------------------------------------------------------*/

int levelChoiceint level/* 进入游戏时选择游戏等级 */

{

while true/* void */

{

clrscr ;

printf"please input 1-9 to choice 0 to return.n";

level=getch;

level=level-48;

if level>0&&level<10 return level;

else if level==0

leave ;

else

printf "Please input a correct numbern";

}/* while true */

}/* levelChoice */

/*---------------------------------------------------------------------*/

int newWordint sum;char pyLinexLine/* 随生成一个新的字符并将其加入数组的首行 */

{

int j;w;

if sum=full

{

j=rand%xLine-2+1;

w=rand%26+65;

p0j=w;

return ++sum;

}/* if */

return sum;

}/* newWord */

/*---------------------------------------------------------------------*/

int movingint miss;char pyLinexLine/* 将最后一行置空;并使所有在数组中其他行的字符下降一行 */

{

int i;j;

char w;

for j=1;i=yLine-1;j

{

if pij=' '

{

miss++;

pij=' ';

}

}

for i=yLine-2;i>=0;i--/* 从倒数第二行的最后一个字符开始开始向前遍历该数组内的元素;如果该位置非空则将该字符移动至下一行 */

{

for j=xLine-2;j>0;j--

{

if pij=' '

{

w=pij;

pij=' ';

pi+1j=w;

}/* if */

}/* forj */

}/* fori */

return miss;

}/* moving */

/*---------------------------------------------------------------------*/

int wordHitchar pyLinexLine

/*判断是否有字符从键盘键入..

如果有;则从最后一行的最后一个元素开始遍历该数组;找出该字符;并把对应位置置空;且返回1..

如果有输入;但屏幕上无对应项;或无输入则返回0*/

{

int i;j;

char key;

ifkbhit/* 判断用户是否从键盘键入字符..如果kbhit返回值为 */

key=getch;

ifkey

{

if key=='0' leave;

if key=='1'

{

clrscr;

printf "Press any key to continue.";

getch;

}

for i=yLine;i>0;i--

{

for j=xLine;j>0;j--

{

if key-32==pi-1j-1

{

pi-1j-1=' ';

return true;

}/* if */

}/* for j */

}/* for i */

sound300;

}/* if key */

return false;

}/* wordHit */

/*---------------------------------------------------------------------*/

int resultint right

/*判断该次的成绩并输出对应的结果;询问用户是否继续;若继续;判断是否可以进入下一级别..*/

{

int score;

char yn;

score=right*100/full;

nosound;

clrscr;

if score==100

printf"perfectn";

else if score>=85

printf"goodn";

else if score>=70

printf"That's OKn";

else

printf"you need to play again.n";

printf"do you want to continue Y/Nn";

for ;;/* void */

{

yn=getch;

switch yn

{

case 'y' :

{

if score>=70 returntrue;

else returnfalse;

}/* case */

case 'n' : leave ;

default : printf"Please input a correct choice:";

}/* switch */

}/* for */

}/* result */

/*---------------------------------------------------------------------*/

int start/* 进入程序时调用该函数;提示其操作.. */

{

char c;

while true

{

clrscr ;

printf "nnnnn Welcome to type gamennn

to playn ";

c=getch;

switch c

{

case '0' : leave;/* 用户选择退出;退出主程序 */

case '2' :

{

clrscr;

printf "nnnnn Hit the type when you see

it on the screen.n Press 0 to exit.n Press 1 to

pausen ";

while true

{

c=getch ;

if c=='0' leave;

if c=='1' return true;

}

}

case '1' : return true;

}

}

}

/*----------------------------------------------------------------------*/

void main

{

/*---------------------------------------------------------------------*/

/*函数声明*/

void printScreenint level;int right;int sum;char pyLinexLine;

void leave;

int levelChoiceint level;

int newWordint sum;char pyLinexLine;

int movingint miss;char pyLinexLine;

int wordHitchar pyLinexLine;

int resultint right;

int start;

/*---------------------------------------------------------------------*/

char pyLinexLine;

int i;j;level;right;sum;n;m;miss;

srandtimeNULL;

start;

for i=0;i

{

for j=0;j

if j==0||j==xLine-1

pij=140;

else

pij=' ';

}/* for i */

level=levelChoicelevel;

for ;;/* 开始运行主程序 */

{ sum=0;

right=0;

miss=0;

printf"Press any key to start";

m=getch;

printScreenlevel;right;sum;p;

for n=0;m=4;;n++

{

delay20;/* 延迟 */

nosound;

ifm%4==0/* 当m为4的整数倍;即上一个字母下落3行时生成一个新的字母在首行并刷新屏幕 */

{

sum=newWordsum;p;

m=5;

printScreenlevel;right;sum;p;

}/*ifnewWord*/

if wordHitp==true/* 如果用户输入了字符并且正确;则使答对的数加一;并刷新屏幕 */

{

right++;

sound 1500;

printScreenlevel;right;sum;p;

}/* ifwordHit */

ifn==37-4*level/* 当n=37-4*level时;屏幕上的字母下落一行;并刷新屏幕;若最后一行有字母;则使错过数加一 */

{

n=0;

m++;

miss=movingmiss;p;

printScreenlevel;right;sum;p;

}/* if moving*/


本文标签: 程序 字符 屏幕 游戏 数组