推荐一个好代码

是文曲星里的,感觉很有用,转载一下。

#loadall

#define WHEELSPEED 12 //皮带速度
#define CHI 1
#define LEFTWHEEL 2
#define RIGHTTWHEEL 3
#define TANHUANG 4
#define FANBAN 5
#define MANXSPEED 35 //人的横移速度
#define LEFT_KEY 0x17
#define RIGHT_KEY 0x16
#define CR_KEY 0xd
#define ESC_KEY 0x1b
#define FTIME 40*256/1000 //50毫秒一帧
#define G 5
#define R_END 0
#define R_LEFT 1
#define R_RIGHT 2
#define R_LEFTS 3
#define R_RIGHTS 4

struct FLOOR
{
int x,y,type,flag,step;
};

char floor_bmp[][15]={
0xff,0xff,0xff,0xff,0xff,0xff,0xc0,0x00,0x03,0xff,0xff,0xff,0xff,0xff,0xff,
0x44,0x44,0x44,0xee,0xee,0xee,0xff,0xff,0xfe,0xff,0xff,0xfe,0xff,0xff,0xfe,
0x3f,0xff,0xff,0x66,0x66,0x66,0xcc,0xcc,0xcc,0x66,0x66,0x66,0x3f,0xff,0xff,
0xff,0xff,0xfc,0x66,0x66,0x66,0x33,0x33,0x33,0x66,0x66,0x66,0xff,0xff,0xfc,
0xff,0xff,0xff,0x69,0xa6,0x9a,0x59,0x65,0x96,0x69,0xa6,0x9a,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xff,0xff,0xff,0x79,0xc7,0x9c,0xff,0xff,0xff,0x00,0x00,0x00};
char ball_bmp[][8]={
0xc3,0x81,0x00,0x00,0x00,0x00,0x81,0xc3,
0x3c,0x7e,0xff,0xff,0xff,0xff,0x7e,0x3c};
char top_bmp[]={
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,0xee,
0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44};
char fail_bmp[]={
0x7,0xe0,0x18,0x18,0x20,0x4,0x40,0x2,
0x52,0x4a,0x8c,0x31,0x80,0x1,0x80,0x1,
0x80,0x1,0x80,0x1,0x83,0xc1,0x44,0x22,
0x48,0x12,0x20,0x4,0x18,0x18,0x7,0xe0};
char sec_bmp[]={
0x7,0xe0,0x18,0x18,0x20,0x4,0x4c,0x32,
0x52,0x4a,0x80,0x1,0x80,0x1,0x80,0x1,
0x80,0x1,0x80,0x1,0x90,0x9,0x48,0x12,
0x47,0xe2,0x20,0x4,0x18,0x18,0x7,0xe0};

struct FLOOR floor[6];
int manx,many,manjump,manstop,man_realx,man_hp;
int lev,layer,layer_id;
int gh; //Y方向速度
char fan_tbl[]={0,1,1,1,2,2,2,1,1,1};
char tan_tbl[]={0,4,4,4,4,4,0,0,0,0};
char level_tbl[][6]={
8,12,14,16,20,24,
6,10,12,14,19,24,
4,10,12,14,18,24};

int record_flag,record_p,last_key,key_count;


//以下用于存档,不要随便更改变量位置
int max_layer[3];
long seed;
int have_record,record_lev;
char record[4066]; //此缓冲区要放在变量区尾

int rand_floor()
{
int t;

t=rand()%24;
if (t<level_tbl[lev][0]) t=0;
else if (t<level_tbl[lev][1]) t=1;
else if (t<level_tbl[lev][2]) t=2;
else if (t<level_tbl[lev][3]) t=3;
else if (t<level_tbl[lev][4]) t=4;
else t=5;
return t;
}

void init_floor()
{
int i;

for (i=0;i<6;i++) {
floor[i].y=i*13+10;
floor[i].x=(rand()%10+1)*8;
floor[i].type=rand_floor();
floor[i].flag=0;
floor[i].step=0;
}
floor[3].type=0; //确保人初始脚踏实地
manjump=0;
manstop=3;
manx=(floor[3].x+8)<<4;
many=(floor[3].y-8)<<4;
gh=0;
}

void write_man()
{
int x,y;

x=(manx+8)>>4;
y=(many+8)>>4;
WriteBlock(x,y,8,8,4,ball_bmp[0]);
WriteBlock(x,y,8,8,3,ball_bmp[1]);
}

void disp_hp()
{
int i;

Block(112,8,159,13,0);
for (i=0;i<man_hp;i++) {
Block(113+i*4,8,115+i*4,13,1);
}
Rectangle(113,8,159,13,1);
}

void disp_lev()
{
char s[10];

sprintf(s,"L:%d",lev);
TextOut(120,18,s,1);
}

void disp_layer()
{
char s[10];

Block(120,32,159,44,0);
sprintf(s,"F:%d",layer);
TextOut(120,32,s,1);
}

void disp_max()
{
char s[10];

Block(120,45,159,57,0);
sprintf(s,"M:%d",max_layer[lev]);
TextOut(120,45,s,1);
}

void man_state()
{
int x,y,y0,y2,t;

y=((many)>>4)+8;
y2=((many+gh)>>4)+8;
if (y2<=y) { //不降或者上升
many=many+gh;
return;
}
x=(manx+8)>>4;
y0=floor[0].y;
if (y<=floor[5].y) {
t=(y+12-y0)/13;
if (y2>=floor[t].y) {
if (x+8>floor[t].x+2 && x<floor[t].x+22) {
manjump=0;
manstop=t;
many=(floor[t].y-8)<<4;
gh=0;
t=floor[t].type;
if (t==CHI) {
man_hp=man_hp-5;
if (man_hp<0) man_hp=0;
} else {
man_hp++;
if (man_hp>12) man_hp=12;
}
disp_hp();
return;
}
}
}
many=many+gh;
}

void record_key(char key)
{
if (last_key==LEFT_KEY) {
if (key_count==1) {
record[record_p++]=R_LEFT;
} else {
record[record_p++]=R_LEFTS;
record[record_p++]=key_count;
}
} else if (last_key==RIGHT_KEY) {
if (key_count==1) {
record[record_p++]=R_RIGHT;
} else {
record[record_p++]=R_RIGHTS;
record[record_p++]=key_count;
}
} else {
while (key_count>=240) {
record[record_p++]=250;
key_count=key_count-240;
}
if (key_count) record[record_p++]=key_count+10;
}
last_key=key;
key_count=1;
}

void main()
{
int i,t;
char key;
char ms;
char fp;
char block_buf[48*32/8];

fp=fopen("/LavaData/hole.sav","r");
if (fp) {
fread(max_layer,1,4080,fp);
fclose(fp);
} else {
have_record=0;
memset(max_layer,0,3);
}
lev=0;
layer=0;
layer_id=0;
man_hp=12;
ClearScreen();
TextOut(32,8,"无底洞",0x81);
TextOut(16,24,"L:难度选择",0x81);
TextOut(16,40,"R:录象回放",0x81);
TextOut(16,56,"S:保存记录",0x81);
xxx:
Block(1,0,7,79,1);
Block(104,0,111,79,1);
WriteBlock(8,0,96,3,1,top_bmp);
for (;;) {
record_flag=0;
disp_lev();
disp_hp();
disp_layer();
disp_max();
Refresh();
key=getchar();
if (key=='l') {
lev++;
if (lev>2) lev=0;
} else if (key=='r' && have_record) {
record_flag=1;
break;
} else if (key=='s') {
GetBlock(32,24,48,32,0,block_buf);
Block(38,30,73,49,0);
Rectangle(38,30,73,49,1);
TextOut(40,32,"Yes?",0x81);
Refresh();
if (getchar()=='y') {
MakeDir("/LavaData");
fp=fopen("/LavaData/hole.sav","w");
if (fp) {
fwrite(max_layer,1,4080,fp);
fclose(fp);
}
}
WriteBlock(32,24,48,32,1,block_buf);
Refresh();
} else if (key==CR_KEY) break;
}
Block(112,60,159,79,0);
record_p=0;
last_key=0;
key_count=0;
if (record_flag) {
lev=record_lev;
disp_lev();
disp_max();
} else {
record_lev=lev;
seed=rand()+Getms();
}
srand(seed);
init_floor();
layer=0;
layer_id=0;
man_hp=12;
disp_hp();
disp_layer();
for (;;) {
ms=Getms();
WriteBlock(8,0,96,3,1,top_bmp);
Block(8,3,103,79,0);
if (floor[0].y<=2) {
memcpy(floor[0],floor[1],sizeof(struct FLOOR)*5);
floor[5].y=floor[4].y+13;
floor[5].x=(rand()%10+1)*8;
floor[5].type=rand_floor();
floor[5].flag=0;
floor[5].step=0;
if (!manjump) {
manstop--;
if (manstop<0) {
manjump=1;
gh=2;
}
}
layer_id++;
if (layer_id>=6) {
layer_id=0;
layer++;
disp_layer();
if (layer>max_layer[lev]) {
max_layer[lev]=layer;
disp_max();
}
}
}
for (i=0;i<6;i++) {
floor[i].y--;
t=floor[i].type;
WriteBlock(floor[i].x,floor[i].y,24,5,1,floor_bmp[t+floor[i].flag]);
if (t==FANBAN) {
if (t=floor[i].step) {
t++;
if (t<10) {
manjump=1;
gh=2;
} else t=0;
floor[i].step=t;
floor[i].flag=fan_tbl[t];
}
} else if (t==TANHUANG) {
if (t=floor[i].step) {
t++;
if (t==2) {
manjump=1;
gh=-48;
} else if (t==10) t=0;
floor[i].step=t;
floor[i].flag=tan_tbl[t];
}
}
}
if (!manjump) many=many-16;
if (many<32) {
many=32;
manjump=1;
gh=2;
man_hp=man_hp-5;
if (man_hp<0) man_hp=0;
disp_hp();
}
write_man();
Refresh();
key=CheckKey(128);
if (key && key<13) getchar();
if (key==ESC_KEY) break;
if (record_flag) {
if (key_count==0) {
t=record[record_p++];
if (t==R_LEFT) key=LEFT_KEY;
else if (t==R_RIGHT) key=RIGHT_KEY;
else if (t==R_LEFTS) {
key=LEFT_KEY;
key_count=record[record_p++]-1;
} else if (t==R_RIGHTS) {
key=RIGHT_KEY;
key_count=record[record_p++]-1;
} else if (t==R_END) break;
else {
key=0;
key_count=t-10-1;
}
last_key=key;
} else {
key=last_key;
key_count--;
}
} else {
if (record_p<4000) {
if (key!=LEFT_KEY && key!=RIGHT_KEY) key=0;
if (key==last_key) {
key_count++;
} else record_key(key);
}
}

//处理x方向移动
if (key==LEFT_KEY) manx=manx-MANXSPEED;
else if (key==RIGHT_KEY) manx=manx+MANXSPEED;
if (!manjump) {
t=floor[manstop].type;
if (t==LEFTWHEEL) manx=manx-WHEELSPEED;
else if (t==RIGHTTWHEEL) manx=manx+WHEELSPEED;
else if (t==FANBAN) {
floor[manstop].step=1;
floor[manstop].flag=1;
} else if (t==TANHUANG) {
floor[manstop].step=1;
floor[manstop].flag=8-TANHUANG;
}
}
if (manx<8*16) manx=8*16;
else if (manx>96*16) manx=96*16;

man_realx=(manx+8)>>4;
if (manjump) {
man_state();
gh=gh+G;
if (gh>64) gh=64;
} else {
if (man_realx+8<floor[manstop].x+2 || man_realx>floor[manstop].x+22) {
manjump=1;
gh=2;
}
}
if (many>=78*16 || man_hp<=0) break;
ms=Getms()-ms;
if (ms<FTIME) {
if ((t=((FTIME-ms)*1000)>>8)>3) Delay(t);
}
}
if (!record_flag) record_key(0);
have_record=1;
if (layer<max_layer[lev]) WriteBlock(128,60,16,16,1,fail_bmp);
else WriteBlock(128,60,16,16,1,sec_bmp);
ReleaseKey(128);
goto xxx;
}
展开阅读全文
©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读