/*此程序还有很多不完善的地方,有兴趣的可以继续改进
希望大家多和我交流,我的油箱是yywyai@sina.com*/
#include<stdio.h> #include <graphics.h> #include <dos.h> #include <stdlib.h> #define UP 0x48 #define DOWN 0x50 #define LEFT 0x4b #define RIGHT 0x4d #define ESC 0x01 #define P 0x19 #define ENTER 0x1c #define PAGEUP 0x49 #define PAGEDOWN 0x51 #define add_D 0x0d #define add_d 0x4e #define sub_D 0x0c #define sub_d 0x4a #define RST 0x13 #define PAUSE 0x19
long score; int brick_width=20; char str[9]={'0','0','0','0','0','0','0','0','\0'}; /*积分数组!*/ int m_speed=0; int difficulty=0;
/************************************************/ /*以下定义的是砖块的形状*/ int brick [6][4][4][4]= { { {{0,1,0,0},{0,1,1,0},{0,0,1,0},{0,0,0,0}}, {{0,1,1,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}}, {{0,1,0,0},{0,1,1,0},{0,0,1,0},{0,0,0,0}}, {{0,1,1,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}} }, { {{0,1,0,0},{1,1,1,0},{0,0,0,0},{0,0,0,0}}, {{0,1,0,0},{0,1,1,0},{0,1,0,0},{0,0,0,0}}, {{0,0,0,0},{1,1,1,0},{0,1,0,0},{0,0,0,0}}, {{0,1,0,0},{1,1,0,0},{0,1,0,0},{0,0,0,0}} }, { {{0,0,1,0},{0,1,1,0},{0,1,0,0},{0,0,0,0}}, {{0,1,1,0},{0,0,1,1},{0,0,0,0},{0,0,0,0}}, {{0,0,1,0},{0,1,1,0},{0,1,0,0},{0,0,0,0}}, {{0,1,1,0},{0,0,1,1},{0,0,0,0},{0,0,0,0}} }, { {{0,1,0,0},{0,1,0,0},{0,1,0,0},{0,1,0,0}}, {{0,0,0,0},{1,1,1,1},{0,0,0,0},{0,0,0,0}}, {{0,1,0,0},{0,1,0,0},{0,1,0,0},{0,1,0,0}}, {{0,0,0,0},{1,1,1,1},{0,0,0,0},{0,0,0,0}} }, { {{0,1,1,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}}, {{0,1,1,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}}, {{0,1,1,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}}, {{0,1,1,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}} }, { {{1,1,1,0},{1,0,0,0},{0,0,0,0},{0,0,0,0}}, {{1,1,0,0},{0,1,0,0},{0,1,0,0},{0,0,0,0}}, {{0,0,1,0},{1,1,1,0},{0,0,0,0},{0,0,0,0}}, {{1,0,0,0},{1,0,0,0},{1,1,0,0},{0,0,0,0}} } };
/*以下定义的是游戏窗口的矩阵,在游戏进行时,通过检查此矩阵来获知砖块的堆积情况*/ int board[22][12]={ {2,2,2,2,2,2,2,2,2,2,2,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,0,0,0,0,0,0,0,0,0,0,2}, {2,2,2,2,2,2,2,2,2,2,2,2} }; /*以下是程序中用到汉字的点阵——————————————————*/ char e_16[]={ /* 以下是 '俄' 的 16点阵宋体 字模,32 byte */ 0x10,0x20,0x19,0xA8,0x16,0x24,0x22,0x24, 0x3F,0xFE,0x62,0x20,0xA2,0x24,0x22,0xA6, 0x23,0x2C,0x26,0x28,0x2A,0x10,0x22,0x30, 0x22,0x4A,0x2E,0x8A,0x24,0x04,0x00,0x00, }; char luo_16[]={ /* 以下是 '罗' 的 16点阵宋体 字模,32 byte */ 0x00,0x00,0x3F,0xFC,0x22,0x44,0x22,0x44, 0x22,0x44,0x3F,0xFC,0x22,0x00,0x03,0xFC, 0x06,0x08,0x0C,0x08,0x12,0x10,0x61,0x20, 0x00,0xC0,0x03,0x00,0x0C,0x00,0xF0,0x00, }; char si_16[]={ /* 以下是 '斯' 的 16点阵宋体 字模,32 byte */ 0x22,0x0C,0x22,0x70,0xFF,0x40,0x22,0x40, 0x3E,0x40,0x22,0x7E,0x22,0x48,0x3E,0x48, 0x22,0x48,0x22,0x48,0xFF,0x48,0x00,0x48, 0x24,0x88,0x62,0x88,0x83,0x08,0x00,0x08, }; char fang_16[]={ /* 以下是 '方' 的 16点阵宋体 字模,32 byte */ 0x02,0x00,0x01,0x00,0x00,0x80,0xFF,0xFE, 0x02,0x00,0x02,0x00,0x03,0xF0,0x02,0x10, 0x04,0x10,0x04,0x10,0x08,0x10,0x08,0x10, 0x10,0x10,0x20,0x90,0xC0,0x60,0x00,0x00, }; char kuai_16[]={ /* 以下是 '块' 的 16点阵宋体 字模,32 byte */ 0x20,0x80,0x20,0x80,0x20,0x80,0x23,0xF8, 0xF8,0x88,0x20,0x88,0x20,0x88,0x20,0x88, 0x2F,0xFE,0x20,0x80,0x39,0x40,0xE1,0x20, 0x42,0x18,0x02,0x0E,0x04,0x04,0x08,0x00, }; char kai_16[]={ /* 以下是 '开' 的 16点阵宋体 字模,32 byte */ 0x00,0x00,0x7F,0xFE,0x04,0x20,0x04,0x20, 0x04,0x20,0x04,0x20,0xFF,0xFE,0x04,0x20, 0x04,0x20,0x04,0x20,0x08,0x20,0x08,0x20, 0x10,0x20,0x20,0x20,0x40,0x20,0x00,0x00, }; char shi_16[]={ /* 以下是 '始' 的 16点阵宋体 字模,32 byte */ 0x10,0x20,0x18,0x30,0x10,0x20,0x10,0x48, 0xFC,0x84,0x25,0xFE,0x24,0x84,0x44,0x00, 0x44,0xFC,0x28,0x84,0x18,0x84,0x14,0x84, 0x22,0x84,0x42,0xFC,0x80,0x84,0x00,0x00, }; char tui_16[]={ /* 以下是 '退' 的 16点阵宋体 字模,32 byte */ 0x40,0x00,0x27,0xF0,0x24,0x10,0x07,0xF0, 0x04,0x10,0x04,0x10,0xE7,0xF0,0x24,0x88, 0x24,0x50,0x24,0x20,0x25,0x10,0x26,0x18, 0x24,0x08,0x50,0x00,0x8F,0xFE,0x00,0x00, }; char chu_16[]={ /* 以下是 '出' 的 16点阵宋体 字模,32 byte */ 0x01,0x00,0x01,0x00,0x21,0x04,0x21,0x04, 0x21,0x04,0x21,0x04,0x3F,0xFC,0x21,0x04, 0x01,0x00,0x21,0x04,0x21,0x04,0x21,0x04, 0x21,0x04,0x3F,0xFC,0x20,0x04,0x00,0x00, }; char you_16[]={ /* 以下是 '游' 的 16点阵宋体 字模,32 byte */ 0x44,0x20,0x22,0x20,0x22,0x20,0x0F,0xBE, 0x84,0x40,0x54,0xBC,0x57,0x08,0x25,0x10, 0x25,0x10,0x25,0x7E,0xC5,0x10,0x49,0x10, 0x49,0x10,0x49,0x10,0x55,0x50,0x42,0x20, }; char xi_16[]={ /* 以下是 '戏' 的 16点阵宋体 字模,32 byte */ 0x00,0x40,0x00,0x50,0x7E,0x48,0x02,0x48, 0x44,0x40,0x25,0xFE,0x18,0x40,0x08,0x44, 0x0C,0x28,0x14,0x30,0x12,0x20,0x22,0x50, 0x41,0x92,0x06,0x0A,0x00,0x06,0x00,0x02, }; char xia_16[]={ /* 以下是 '下' 的 16点阵宋体 字模,32 byte */ 0x00,0x04,0x7F,0xFE,0x01,0x00,0x01,0x00, 0x01,0x00,0x01,0xC0,0x01,0x60,0x01,0x30, 0x01,0x20,0x01,0x00,0x01,0x00,0x01,0x00, 0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x00, }; char ge_16[]={ /* 以下是 '个' 的 16点阵宋体 字模,32 byte */ 0x01,0x00,0x01,0x00,0x02,0x80,0x02,0x40, 0x04,0x20,0x09,0x18,0x11,0x0E,0x61,0x04, 0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00, 0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x00, }; char tu_16[]={ /* 以下是 '图' 的 16点阵宋体 字模,32 byte */ 0x00,0x00,0x7F,0xFC,0x44,0x04,0x47,0xE4, 0x44,0x44,0x4A,0x84,0x71,0x04,0x42,0xC4, 0x4C,0x3C,0x71,0x14,0x40,0x84,0x42,0x04, 0x41,0x84,0x41,0x04,0x7F,0xFC,0x40,0x04, }; char xing_16[]={ /* 以下是 '形' 的 16点阵宋体 字模,32 byte */ 0x00,0x04,0x7F,0x86,0x12,0x0C,0x12,0x10, 0x12,0x20,0x12,0x08,0xFF,0xCC,0x12,0x18, 0x12,0x20,0x12,0x44,0x12,0x86,0x12,0x0C, 0x22,0x10,0x22,0x20,0x42,0x40,0x80,0x80, }; char fen_16[]={ /* 以下是 '分' 的 16点阵宋体 字模,32 byte */ 0x08,0x80,0x0C,0x80,0x08,0x40,0x10,0x20, 0x10,0x30,0x20,0x18,0x40,0x0E,0x9F,0xE4, 0x04,0x20,0x04,0x20,0x04,0x20,0x04,0x20, 0x08,0x20,0x10,0xA0,0x20,0x40,0x40,0x00, }; char shu_16[]={ /* 以下是 '数' 的 16点阵宋体 字模,32 byte */ 0x08,0x20,0x49,0x30,0x2A,0x20,0x1C,0x20, 0xFF,0x7E,0x1C,0x44,0x2B,0x44,0x48,0xC4, 0x08,0x28,0xFF,0x28,0x12,0x10,0x34,0x10, 0x0C,0x28,0x32,0x4E,0xC0,0x84,0x00,0x00, }; char bd_16[]={ /* 以下是 ':' 的 16点阵宋体 字模,32 byte */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00, 0x30,0x00,0x30,0x00,0x00,0x00,0x00,0x00, }; char su_16[]={ /* 以下是 '速' 的 16点阵宋体 字模,32 byte */ 0x00,0x80,0x40,0x80,0x2F,0xFC,0x20,0x80, 0x00,0x80,0x07,0xF8,0xE4,0x88,0x24,0x88, 0x27,0xF8,0x21,0xA0,0x22,0x98,0x2C,0x88, 0x20,0x80,0x50,0x80,0x8F,0xFE,0x00,0x00, }; char du_16[]={ /* 以下是 '度' 的 16点阵宋体 字模,32 byte */ 0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20, 0x22,0x20,0x2F,0xFC,0x22,0x20,0x23,0xE0, 0x20,0x00,0x27,0xF8,0x22,0x10,0x21,0x20, 0x20,0xC0,0x41,0x30,0x46,0x0E,0x98,0x04, }; char cong_16[]={ /* 以下是 '从' 的 16点阵宋体 字模,32 byte */ 0x08,0x40,0x08,0x40,0x08,0x40,0x08,0x40, 0x08,0x40,0x08,0x40,0x08,0x40,0x08,0xA0, 0x14,0xA0,0x12,0x90,0x12,0x90,0x21,0x08, 0x23,0x0C,0x42,0x06,0x84,0x04,0x08,0x00, }; char xin_16[]={ /* 以下是 '新' 的 16点阵宋体 字模,32 byte */ 0x10,0x00,0x08,0x0E,0x7F,0x70,0x22,0x40, 0x14,0x40,0xFF,0x7E,0x08,0x48,0x08,0x48, 0xFF,0x48,0x08,0x48,0x2C,0x48,0x2A,0x88, 0x4A,0x88,0x89,0x08,0x2A,0x08,0x10,0x08, }; char nan_16[]={ /* 以下是 '难' 的 16点阵宋体 字模,32 byte */ 0x00,0xA0,0x00,0x90,0xF9,0x10,0x09,0xFE, 0x8B,0x20,0x4B,0x20,0x2D,0xFC,0x11,0x20, 0x19,0x20,0x29,0xFC,0x25,0x20,0x45,0x20, 0x81,0x20,0x01,0xFE,0x01,0x00,0x01,0x00, }; char xuan_16[]={ /* 以下是 '选' 的 16点阵宋体 字模,32 byte */ 0x00,0x40,0x22,0x40,0x12,0x40,0x13,0xF8, 0x04,0x40,0x00,0x40,0xF7,0xFC,0x11,0x20, 0x11,0x20,0x12,0x24,0x12,0x24,0x14,0x1C, 0x10,0x00,0x28,0x00,0x47,0xFE,0x00,0x00, }; char ze_16[]={ /* 以下是 '择' 的 16点阵宋体 字模,32 byte */ 0x20,0x00,0x23,0xF8,0x21,0x10,0xFC,0xA0, 0x20,0x40,0x20,0xA0,0x2B,0x58,0x30,0x46, 0x63,0xF8,0xA0,0x40,0x20,0x40,0x27,0xFE, 0x20,0x40,0x20,0x40,0xA0,0x40,0x40,0x40, };
/* 函数申明 */ int getkey(); void draw_brick(int kind,int stat,int start_x,int start_y,int *position); void display_brick(int *position,int xexcursion,int yexcursion); void draw_freezebrick(int *space); void draw_mainboard(); int is_movable(int *position,int *space,int dir); void freeze(int *position,int *space); int check_bottom(int *space,int hei); void ERASE(int top,int flag); int move_down(int *position,int *space,int *top,int *flag,int speed); void begin(); void rand_select(int *kind,int *stat); void updata_data(int *space,int *top,int flag); void preview(int kind_new,int stat_new); void convert_score(long score,char *str); void drawmat(char *,int,int,int,int); /**************************************************/
/**********************************************************/ void drawmat(char *mat,int matsize,int x,int y,int color) /*依次:字模指针、点阵大小、起始坐标(x,y)、颜色*/ { int i, j, k, n; n = (matsize - 1) / 8 + 1; for(j = 0; j < matsize; j++) for(i = 0; i < n; i++) for(k = 0;k < 8; k++) if(mat[j * n + i] & (0x80 >> k)) /*测试为1的位则显示*/ putpixel(x + i * 8 + k, y + j, color); }
/*初始化游戏板巨阵!*/ void init_data(int **space) { int hei,wid,i,j; *space=(int *)malloc(264*sizeof(int)); /*分配的空间,"264" 是数组的长度*/ for(i=0;i<difficulty;i++) for(j=0;j<12;j++) board[21-i][j]=random(2); for(hei=0;hei<22;hei++) for(wid=0;wid<12;wid++) *(*space+hei*12+wid)=board[hei][wid];
/*初始化*/ score=0; }
void convert_score(long score,char *str) { int i; long SCORE; SCORE=score; for(i=7;i>=0;i--) { str[i]='0'+SCORE%10; SCORE /=10; } } /**************************************************/
/*获取键盘扫描码 */ int getkey() { union REGS key; key.h.ah=0; int86(0x16,&key,&key); return key.h.ah; }
/*****************************************************/
/*画方格图形*/ void mydraw(int start_x,int start_y,int brick_width,int color) { settextstyle(4,0,5); setfillstyle(6,8); setcolor(color); rectangle(start_x,start_y,start_x+brick_width,start_y+brick_width); rectangle(start_x+2,start_y+2,start_x+brick_width-2,start_y+brick_width-2); outtextxy(start_x+7,start_y+7,"*"); }
/******************************************************/
/* 用mydraw函数和position数组在屏幕上画方块*/ void draw_brick(int kind,int stat,int start_x,int start_y,int *position) { int wid,hei,x,y,i=0; void *buf; setviewport(100,40,300,440,1); buf=malloc(400); for(i=0;i<8;i+=2) { x=position[i];y=position[i+1]; getimage(x,y,x+brick_width,y+brick_width,buf); putimage(x,y,buf,XOR_PUT); } i=0; for(hei=0;hei<4;hei++) { y=start_y+hei*brick_width; for(wid=0;wid<4;wid++) if(brick[kind][stat][hei][wid]==1) { x=start_x+wid*brick_width; position[i++]=x; position[i++]=y; position[10]= x > position[10] ? x : position[10]; position[11]= y > position[11] ? y : position[11]; mydraw(x,y,brick_width,10); } } position[8]=start_x; position[9]=start_y;
free(buf); /* 释放 "buf" 所占的内存*/ } void display_score(char *pstr) /*显示积分和速度(10000分加一级,最大9级)*/ { void *buf;int n,j;char speed[2]; buf=malloc(3200); itoa(m_speed,&speed,10);/*需要改进*/ setviewport(479,139,560,170,1); getimage(0,0,80,40,buf); putimage(0,0,buf,XOR_PUT); settextstyle(0,0,1); setcolor(WHITE); outtextxy(0,0,pstr); outtextxy(0,22,speed); if(m_speed<=9) { n=atoi(pstr); m_speed+=n/10000; } free(buf);
}