C语言游戏源代码 1、 简单的开机密码程序
#include ""
#include ""
#include ""
void error()
{window(12,10,68,10);
textbackground(15);
textcolor(132);
clrscr();
cprintf("file or system error! you can"t enter the system!!!");
while(1); /* 若有错误不能通过程序 */
}
void look()
{FILE *fauto,*fbak;
char *pass="c:\\windows\\"; /* 本程序的位置 */
char a[25],ch;
char *au="",*bname="hecfback.^^^"; /*bname 是 的备份 */
setdisk(2); /*set currently disk c:*/
chdir("\\"); /*set currently directory \*/
fauto=fopen(au,"r+");
if (fauto==NULL)
{fauto=fopen(au,"w+");
if (fauto==NULL) error();}
fread(a,23,1,fauto); /* 读取前 23 各字符 */
a[23]="\0";
if (strcmp(a,pass)==0) /* 若读取的和 pass 指针一样就关闭文件,不然就添加 */
fclose(fauto);
else
{fbak=fopen(bname,"w+");
if (fbak==NULL) error();
fwrite(pass,23,1,fbak);
fputc("\n",fbak);
rewind(fauto);
while(!feof(fauto))
{ch=fgetc(fauto);
fputc(ch,fbak);}
rewind(fauto);
rewind(fbak);
while(!feof(fbak))
{ch=fgetc(fbak);
fputc(ch,fauto);}
fclose(fauto);
fclose(fbak);
remove(bname); /*del bname file*/
}
}
void pass()
{char *password="";
char input[60];
int n;
while(1)
{window(1,1,80,25);
textbackground(0);
textcolor(15);
clrscr();
n=0;
window(20,12,60,12);
textbackground(1);
textcolor(15);
clrscr();
cprintf("password:");
while(1)
{input[n]=getch();
if (n>58) {putchar(7); break;} /* 若字符多于 58 个字符就结束本次输入 */
if (input[n]==13) break;
if (input[n]>=32 && input[n]<=122) /* 若字符是数字或字母才算数 */
{putchar("*");
n++;}
if (input[n]==8) /* 删除键 */
if (n>0)
{cprintf("\b \b");
input[n]="\0";
n--;}
}
input[n]="\0";
if (strcmp(password,input)==0)
break;
else
{putchar(7);
window(30,14,50,14);
textbackground(15);
textcolor(132);
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 x[N]; /* 每一节蛇的坐标 */ int y[N]; int color[N];/*
存储每一节蛇的颜色 */ int node; /* 蛇的节数 */ int direction; /* 蛇移动的方向 */
int life; /* 蛇的生命,如果为 1,蛇死,游戏结束 */
} snake;
void init(void)/* 图形驱动 */
{
int driver = DETECT, mode = 0;
registerbgidriver(EGAVGA_driver);
initgraph(&driver, &mode, "");
}
void drawmat(char *mat, int matsize, int x, int y, int color) /* 汉字点阵
*/
{
int i, j, k, m;
m = (matsize - 1) / 8 + 1;
for(j = 0; j < matsize; j++)
for(i = 0; i < m; i++)
for(k = 0; k < 8; k++)
if(mat[j*m+i]&(0x80 >> k))
putpixel(x + i * 8 + k, y + j, color);
}
void showword(void)
{
/* 调用汉字点阵输出程序,显示标题和作者信息 */
drawmat(cai48H, 48, 249, -4, 7);
drawmat(she48H, 48, 329, -4, 7);
drawmat(tun48H, 48, 409, -4, 7);
drawmat(dan48H, 48, 489, -4, 7);
drawmat(cai48H, 48, 250, -5, 4);
drawmat(she48H, 48, 330, -5, 4);
drawmat(tun48H, 48, 410, -5, 4);
drawmat(dan48H, 48, 490, -5, 4);
/* 作 者 田 学 科 */ drawmat(zuo16H, 16, 515, 465, 7);
drawmat(zhe16H, 16, 530, 465, 7);
drawmat(tian16H, 16, 550, 465, 7);
drawmat(xue16H, 16, 565, 465, 7);
drawmat(ke16H, 16, 580, 465, 7);
}
void draw(void)/* 画出四周的墙 */
{
if(color == 15)
color = 0;
setcolor(++color);
setlinestyle(SOLID_LINE, 0, 1);
for(i = 30; i <= 600; i += 10)
{
rectangle(i, 40, i + 10, 49);
rectangle(i, 451, i + 10, 460);
}
for(i = 40; i < 450; i += 10)
{
rectangle(30, i, 39, i + 10);
rectangle(601, i, 610, i + 10);
}
}
void prscore(void)
{
/* 打印游戏分数 */
char str[10];
setfillstyle(SOLID_FILL, YELLOW);
bar(50, 10, 200, 30);
setcolor(6);
settextstyle(0, 0, 2);
sprintf(str, "score:%d", score);
outtextxy(55, 15, str);
}
void gameover(void)
{
cleardevice(); /* 清屏函数 */
for(i = 0; i < ; i++) /* 画出蛇死时的位置 */
{
setcolor[i]);
rectangle[i], [i], [i] + 10, [i] + 10);
}
prscore(); /* 显示分数 */ draw();
showword();
settextstyle(0, 0, 6);
setcolor(7);
outtextxy(103, 203, "GAME OVER");
setcolor(RED);
outtextxy(100, 200, "GAME OVER");
}
void gameplay(void)/* 玩游戏的具体过程 */
{
int flag, flag1;
randomize();
prscore();
gamespeed = 50000;
= 0; /* =0 表示屏幕上没有食物 */
= 1; /* =1 表示蛇是活着的 */
= 4; /* 表示蛇的初始方向为向右 */
= 2; /* 蛇的初始化为两节 */
[0] = 2; /* 两节蛇头初始化为绿色 */ [1] = 2; [0] = 100;
[0] = 100;
[1] = 110;
[1] = 100;
= random(15) + 1;
while(1)
{
while(1)
{
if == 0) /* 如果蛇活着 */
{
while(1)
{
flag = 1;
= 1;
= random(56) * 10 + 40;
= random(40) * 10 + 50;
for(i = 0; i < ; i++)
{
if == [i] && == [i])
flag = 0;
}
if(flag) break;
}
}
if
{
setcolor;
rectangle, , + 10, + 10);
}
for(i = - 1; i > 0; i--)
{
[i] = [i-1];
[i] = [i-1];
}
switch
{
case 1:
[0] -= 10;
break;
case 2:
[0] += 10;
break;
case 3:
[0] -= 10;
break;
case 4:
[0] += 10;
break;
}
for(i = 3; i < ; i++)
{
if[i] == [0] && [i] == [0])
{
gameover();
= 0;
break;
}
}
if[0] < 40 || [0] > 590 || [0] < 50 || [0] > 440)
{
gameover();
= 0;
}
if == 0)
break;
if[0] == && [0] == /* 蛇吃掉食物 */
{
setcolor(0);
rectangle, , + 10, + 10);
[] = -20;
[] = -20;
[] = ;
++;
= 0;
= random(15) + 1;
score += 10;
prscore();
if(score % 100 == 0 && score != 0)
{
for(i = 0; i < ; i++) /* 画出蛇 */
{
setcolor[i]);
rectangle[i], [i], [i] + 10, [i] + 10);
}
sound(200);
delay(50000);
delay(50000);
delay(50000);
delay(50000);
delay(50000);
delay(50000);
nosound();
gamespeed -= 5000;
draw();
}
else
{
sound(500);
delay(500);
nosound();
}
}
for(i = 0; i < ; i++) /* 画出蛇 */
{
setcolor[i]);
rectangle[i], [i], [i] + 10, [i] + 10);
}
delay(gamespeed);
delay(gamespeed);
flag1 = 1;
setcolor(0);
rectangle[], [],
[] + 10, [] + 10);
if(kbhit() && flag1 == 1) /* 如果没按有效键就重新开始循环 */
{
flag1 = 0;
key = bioskey(0);
if(key == esc)
exit(0);
else if(key == up && != 2)
= 1;
else if(key == down && != 1)
= 2;
else if(key == left && != 4)
= 3;
else if(key == right && != 3)
= 4;
}
}
if == 0) /* 如果蛇死了就退出循环 */
break;
}
}
void main(void)
{
while(1)
{
color = 0;
init();
cleardevice();
showword();
draw();
gameplay();
setcolor(15);
settextstyle(0, 0, 2);
outtextxy(200, 400, "CONTINUE(Y/N)");
while(1)
{
key = bioskey(0);
if(key == Y || key == n || key == esc)
break;
}
if(key == 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-data(int commuters[GRID-SIZE][GRID-SIZE],
int salesforce[GRID-SIZE][GRID-SIZE],
int weekends [GRID-SIZE][GRID-SIZE];
voide print-matrix[GRID-SIZE][GRID-SIZE];
?
int
main(void)
{ ?
int commuters[GRID-SIZE][GRID-SIZE];/* 上午 8:
30 的交通数据 */
int salesforce[GRID-SIZE][GRID-SIZE]; /*
上午 11:
00 的交通数据 */ int weekend[GRID-SIZE][GRID-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); Printf (“ 8:
30 TRAFFIC DATA 、 n”)
print-matrix(commuters);
printf( “ n”);
print-matrix(salesforce);
printf( “ n”);
printf_matrix(weekeng);
/* 请用户输入权重因子 */
printf( “ n”);
printf( “Weight (an interger>=0) for the 8:30 data> ”)
scanf(
“%d”,&commuter_weight);
printf(
scanf(
“weight(an integer>=0) for the weekeng data>
“%d”,&weekend_weight);
”);
scanf(
“%d”,&weekend_weight);
/* 计算并显示加权后求和的数据 */
for (i=0;i<GRID_SIZE;++i)
for (j=0;j<GRID_SIZE;++j)
summed_data[i][j]=commuter_weight*commuter[i][j]+
salesforce_weight*salesforce[i][j]+
weekend_weight*weekend[i][j];
printf( “ n”);
printf_matrix(summed_data);
/*
在 summed_data矩阵中找出 NUM_TRANSMITTE个 RS最大值,将坐标临时存储在 location_i 和 location_j 中,然后把最后的结果坐标输出 */
printf( “ n”,NUM_TRANSMITTERS);
for (tr=1;tr<=NUM_TRANSMITTERS;++tr){
current_max=SELECTED;/*
以一个过低的值为起点开始查找 */ for (i=0;i<GRID_SIZE;++i){ for(j=0;j<GRID_SIZE;++j){
if(current_max<summed_data[i][j]){
current_max=summed_data[i][j]){
location_i=i;
location_j=j;
}
}
}
/* 将选中的单元赋一较低的值以避免下次再选中这一元素,显示查找结果 */
summed_data[location_i][location_j]=SELECTED;
printf( “Transmitter %3d:at location %3d %3d n”, tr,location_i,location_j);
}
return (0);
}
/*
* 把 TRAFFIC_FILE中的交通数据填充到 3 个 GRID_SIZE×GRID_SIZE数组中
*/
void
get_traffic_data(int commuters[GRID_SIZE]
,/* 输出*/ int salesforce[GRID_SIZE][GRID_SIZE],/* 输出*/ int weekend[GRID_SIZE][GRID_SIZE] ,/* 输出*/
{ ?
int i,j; /* 循环计数器 */ FILE *fp; /*
文件指针 */ fq=fopen(TRAFFIC_FILE, “r ”);
?
for(i=0 ; i<GRID_SIZE;++i)
for(j=0;j<GRID_SIZE;++j)
fscanf(fp, “%d” ,&commnters[i][j];
?
for(i=0;i<GRID_SIZE;++j)
for(j=0;j<GRID_SIZE;++j)
fscanf(fq, “%d”, &weekend[i][j]);
?
fclose(fq);
}
/*
* 显示一个 GRID_SIZE×GRID_SIZE整数矩阵的内容
*/
void
print_matrix(int matrix[GRID_SIZE][GRID_SIZE])
{
int i,j; /* 循环计数器 */
for(i=0;i<GRID_SIZE;++j)
{ for(j=0;j<GRID_SIZE;++J)
printf( “%3d”,matrix[i][j]); printf( “ n”); }
}
4、 扑克牌游戏
/*************************************
Copyright(C) 2004-2005 vision,math,NJU.
File Name:
Author: vision Version: 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: ?
Calls: al = "ATJQK"[]; /* 把 余 数 给 */ cards[deckp].kind = "3456"[]; /*
把商给 */
}
}
/*show 的原代码,将会自动换行 */ void show(const Card *cards, int size) {
for(int i = 0; i < size; i++)
{
printf("%c%c ",cards[i].kind,cards[i].val);
if( (i !=0) && (((i+1 ) % 3) == 0))
puts("");
}
puts(""); /* 自动换行 */
}
/*grouping 的原代码 */
void grouping(const Card *cards, Card *carr1, Card *carr2, Card *carr3)
{
int i = 0;/* 循环参数 */
?
/* 分给 carr1 三个数 */ while (i < 3) {
carr1[i].val = cards[i].val;
carr1[i].kind = cards[i].kind;
i++;
}
/* 分给 carr2 接下来的三个数 */
while (i < 6)
{
carr2[i-3].val = cards[i].val;
carr2[i-3].kind = cards[i].kind;
i++;
}
/* 分给 carr3 接下来的三个数 */
while (i < 9)
{
carr3[i-6].val = cards[i].val;
carr3[i-6].kind = cards[i].kind;
i++;
}
}
/*rshift 的实现 */
void rshift(Card *carr1, Card *carr2, Card *carr3, int counter)
{
Card temp2;/* 用于存放 carr2[counter]*/ Card temp3;/* 用于存放 carr3[counter]*/ /*temp = carr2*/
= carr2[counter].val;
= carr2[counter].kind;
?
/*carr2 = carr1*/
carr2[counter].val = carr1[counter].val;
carr2[counter].kind = carr1[counter].kind;
?
/*temp3 = carr3*/
= carr3[counter].val;
= carr3[counter].kind;
/*carr3 = carr2*/
carr3[counter].val = ;
carr3[counter].kind = ;
/*carr1 = carr3*/
carr1[counter].val = ;
carr1[counter].kind = ;
}
Card* result_process(Card *carr1, Card *carr2, Card *carr3, int counter)
{
rshift(carr1, carr2, carr3, counter); /* 把数组的第一个元素依次右移 */
if(counter == 2)
{
return(&carr2[2]);
}
show(carr1, 3);
show(carr2, 3);
show(carr3, 3);
puts(" 请给出你记住的牌所在行数 :");
fflush(stdin);
int input = 1;
input = getchar(); /* 获取你选的组 */
switch(input)
{
case "1":