⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 russia.c

📁 俄罗斯方块。很好玩得小游戏
💻 C
字号:

/*俄罗斯方块=====主程序
=============Russia Game  Head File
=============CopyRight (C) LiGungcun  李广存
=============QQ:66855618
=============E-Mail: hoya-lee@sohu.com
*/

/*===========head file include============*/
#include "d:/book/my_code/els/graph.h"      /*256色+320*200分辨率+写屏*/
#include "d:/book/my_code/els/key.h"        /*定义了游戏中要用的按键宏*/
#include "d:/book/my_code/els/timer.h"      /*1ch中断构造的计时器*/
/*===========macro defined================*/
#define FRIME_COLOR (4)             /*外围框的颜色(红)*/
/*===========globe variable===============*/
const int g_unit=8;                 /*每个单元方块的边长*/
const int g_gm_left=111,g_gm_top=11,g_gm_right=206,g_gm_bottom=186;
                                    /*主框架在屏幕中的位置*/
int   g_gm[12][22];                 /*主框架数组12*22 (包括边框)*/
static int     block[32][4][4] = {
	{                               /*各个积木的形状数组(可随意增减)*/
		{65,11,123,44},
		{0,0,0,0},
		{0,0,0,0},
		{0,0,0,0},
	},
	{
		{0,0,0,65},
		{0,0,0,11},
		{0,0,0,123},
		{0,0,0,44},
	},
	{
		{65,11,123,44},
		{0,0,0,0},
		{0,0,0,0},
		{0,0,0,0},
	},
	{
		{0,0,0,65} ,
        {0,0,0,11} ,
        {0,0,0,123} ,
        {0,0,0,44} ,
    },     /*棍*/
    {
		{0,0,0,0},
        {65,11,0,0},
		{0,123,0,0},
		{0,44,0,0},
    },
	{
		{0,0,0,0},
		{0,0,0,0},
        {0,0,65,0},
        {44,123,11,0},
    },
    {
        {0,0,0,0},
		{0,44,0,0},
        {0,123,0,0},
		{0,11,65,0},
	},
    {
		{0,0,0,0},
		{0,0,0,0},
		{44,123,11,0},
		{1,0,0,0},
	},    /*7*/
	{
		{0,0,0,0},
		{11,65,0,0},
		{123,0,0,0},
		{44,0,0,0},
	},
	{
		{0,0,0,0},
		{0,0,0,0},
		{44,123,11,0},
		{0,0,65,0},
	},
	{
		{0,0,0,0},
		{0,44,0,0},
		{0,123,0,0},
		{65,11,0,0},
	},
	{
		{0,0,0,0},
		{0,0,0,0},
		{65,0,0,0},
		{11,123,44,0},
	},      /*反7*/
	{
		{0,0,0,0},
		{0,65,11,0},
		{0,123,44,0},
		{0,0,0,0},
	},
	{
		{0,0,0,0},
		{0,11,65,0},
		{0,44,123,0},
		{0,0,0,0},
	},
	{
		{0,0,0,0},
		{0,44,123,0},
		{0,11,65,0},
		{0,0,0,0},
	},
	{
		{0,0,0,0},
		{0,123,44,0},
		{0,11,65,0},
		{0,0,0,0},
	},    /*方*/
	{
		{0,0,0,0},
		{0,0,0,0},
		{0,65,0,0},
		{11,123,44,0},
	},
	{
		{0,0,0,0},
		{11,0,0,0},
		{123,65,0,0},
		{44,0,0,0},
	},
	{
		{0,0,0,0},
		{0,0,0,0},
		{44,123,11,0},
		{0,65,0,0},
	},
	{
		{0,0,0,0},
		{0,44,0,0},
		{65,123,0,0},
		{0,11,0,0},
	},    /*  _|_  */
	{
		{0,0,0,0},
		{0,0,0,0},
		{65,11,0,0},
		{0,123,44,0},
	},
	{
		{0,0,0,0},
		{0,65,0,0},
		{123,11,0,0},
		{44,0,0,0},
	},
	{
		{0,0,0,0},
		{0,0,0,0},
		{65,11,0,0},
		{0,123,44,0},
	},
	{
		{0,0,0,0},
		{0,65,0,0},
        {123,11,0,0},
		{44,0,0,0},
    },     /*  z  */
	{
		{0,0,0,0},
        {0,0,0,0},
        {0,11,65,0},
		{44,123,0,0},
	},
	{
        {0,0,0,0},
        {65,0,0,0},
        {11,123,0,0},
        {0,44,0,0},
	},
    {
		{0,0,0,0},
		{0,0,0,0},
        {0,11,65,0},
        {44,123,0,0},
	},
	{
		{0,0,0,0},
        {65,0,0,0},
        {123,11,0,0},
		{0,44,0,0},
	}, /*反 z */
	{
	    {0,0,0,65},
	    {0,0,123,0},
		{0,11,0,0},
		{44,0,0,0},
	},
	{
		{44,0,0,0},
		{0,11,0,0},
		{0,0,123,0},
		{0,0,0,65},
	},
	{
		{0,0,0,65},
		{0,0,123,0},
		{0,11,0,0},
		{44,0,0,0},
	},
	{
		{44,0,0,0},
		{0,11,0,0},
		{0,0,123,0},
		{0,0,0,65},
	},
};
int block_x;                        /*积木下落时记录其x值*/
int block_y;                        /*。。。。。记录其y值*/
int block_no;                       /*。。。。。记录其在block数组中的编号 如block[block_no][m][n]*/
int g_score;                        /*。。。。。记录得分*/
int g_level;                        /*记录水平的变量,其高低可影响下落速度*/
/*===========data structure===============*/
/*===========function declare=============*/
void    init_gm_win     (void);     /*把面板g_gm[][]初始化*/
void    draw_unit       (int x,int y,unsigned char color);
                                    /*在xy处绘制单元方块,x,y为g_gm[][]中相对坐标*/
void    show_gm_win     (void);     /*绘制g-gm[][]数组*/
void    show_block      (int b, int x, int y);   /*绘制一个积木,b为积木编号*/
void    show            (void);     /*用于重绘积木取*/
void    show_logo       (void);     /*显示作者信息与按键信息*/
/*=====turn and down=======*/
int     can_left(void);         /*。。。。*/
int     can_right(void);        /*。。。。*/
int     can_down(void);         /*。。。。*/
int     can_turn(void);         /*能否移动*/
void    do_evaluate(void);      /*不能移动时 更改面板数组 (赋值)*/
void    do_del(void);           /*判断能否削行,能则削*/
int     is_over(void);          /*判断是否死了(到顶)*/
void    do_over(void);          /*显示死亡信息*/
/*===========function implement===========*/
void    init_gm_win(){
	int i,j;
	for(j=0;j<12;j++)
		for(i=0;i<22;i++)
			g_gm[i][j]=0;        /*面板底是0*/
	for(i=0;i<22;i++) {
		g_gm[i][0]=FRIME_COLOR;
		g_gm[i][11]=FRIME_COLOR;
	}
	for(i=0;i<12;i++) {
		g_gm[0][i]=FRIME_COLOR;
		g_gm[21][i]=FRIME_COLOR;  /*面板边框色*/
	}
}
void draw_unit(int x,int y,unsigned char color){
	unsigned char t_color;
	int t;
	t=x; x=y; y=t;
	t_color=get_current_color();
	set_current_color(color);
	draw_rectangle(g_gm_left+y*g_unit,g_gm_top+x*g_unit,g_gm_left+(y+1)*g_unit-1,g_gm_top+(x+1)*g_unit-1);
	draw_line(g_gm_left+y*g_unit,g_gm_top+x*g_unit,g_gm_left+(y+1)*g_unit-1,g_gm_top+(x+1)*g_unit-1);
	draw_line(g_gm_left+y*g_unit,g_gm_top+(x+1)*g_unit-1,g_gm_left+(y+1)*g_unit-1,g_gm_top+x*g_unit);
	set_current_color(t_color);
}
void show_gm_win(){
	int i,j,t_color;
	t_color=get_current_color();
	set_current_color(0);
	draw_bar(g_gm_left,g_gm_top,g_gm_right,g_gm_bottom);
	for(i=0;i<12;i++)
		for(j=0;j<22;j++)
			if(g_gm[j][i]!=0)
				draw_unit(i,j,g_gm[j][i]);
	set_current_color(t_color);
}
void show(){
	int i,j,t_color;
	t_color=get_current_color();
	set_current_color(0);
	draw_bar(g_gm_left+g_unit,g_gm_top+g_unit,g_gm_right-g_unit,g_gm_bottom-g_unit);
	for(i=1;i<11;i++)
		for(j=1;j<21;j++)
			if(g_gm[j][i]!=0)
				draw_unit(i,j,g_gm[j][i]);
	set_current_color(t_color);
}
void show_block(int b, int x, int y){
	int i,j;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			if(block[b][j][i]!=0)
				draw_unit(i+x,j+y,block[b][j][i]);
}
void    show_logo(){
    int t_color;
    t_color=get_current_color();
    set_current_color(76);
    out_text_xy(210,110,"空格==暂停");
    out_text_xy(210,110+17,"ESC==退出");
    out_text_xy(210,110+17*2,"UP==转向");
    out_text_xy(210,110+17*3,"方向键==方向");
    out_text_xy(1,110,"程序设计者:");
    out_text_xy(1,110+17,"李广存");
    out_text_xy(1,110+17*2,"QQ:66855618");
    set_current_color(t_color);
}
/*========block function======*/
int can_left(){
	int i,j;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			if(block[block_no][j][i]!=0)
				if(g_gm[block_y+j][block_x+i-1]!=0)
					return 0;
	return 1;
}
int can_right(){
	int i,j;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			if(block[block_no][j][i]!=0)
				if(g_gm[block_y+j][block_x+i+1]!=0)
					return 0;
	return 1;
}
int can_down(){
	int i,j;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			if(block[block_no][j][i]!=0)
				if(g_gm[block_y+j+1][block_x+i]!=0)
					return 0;
	return 1;
}
int can_turn(){
	int t_no,i,j;
	t_no=block_no;
	t_no++;
	if(t_no%4==0) t_no=t_no-4;
	if(t_no<0) t_no=0;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			if(block[t_no][j][i]!=0)
				if(g_gm[block_y+j+1][block_x+i]!=0)
					return 0;
	return 1;
}
void do_evaluate(){
	int i,j;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			if(block[block_no][j][i]!=0)
				g_gm[block_y+j][block_x+i]=block[block_no][j][i];
}
void do_del(){
	int i,j,k,m,flag,t_color;
	char *t_s=NULL;
	for(j=20;j>1;j--){
		flag = -1;
		for(i=1;i<11;i++){
			if(g_gm[j][i]==0)
				flag=0;
		}
		if(flag!=0){
			for(k=j;k>1;k--)
				for(m=0;m<11;m++)
					g_gm[k][m]=g_gm[k-1][m];
			j++;
			t_color=get_current_color();
			set_current_color(6);
			draw_bar(15,45,65,65);
			g_score++; g_level=g_score/30+1;
			set_current_color(0);
			out_text_xy(15,45,itoa(g_score,t_s,10));
			set_current_color(t_color);
		}
	}
}
int is_over(){
	int i,j;
	for(i=1;i<11;i++)
		if(g_gm[4][i]!=0)
				return 1;
	return 0;
}
void do_over(){
    int t_color;
    t_color=get_current_color();
    set_current_color(62);
    out_text_xy(210,60,"您的成绩");
    out_text_xy(230,85,itoa(g_score,NULL,10));
    set_current_color(t_color);
    while(getch()!=27) ;
	close_graph();
	kill_timer();
	exit(0);
}
/*===========END==========================*/

void main(){
	int t_no;                      /*与block_no配合使用(互相记录)*/
	int t_timer;               /*用于计时器 计算时间差*/
	void far *dos_time=MK_FP( 0x40, 0x6C );    /*此地址为dos计时器*/
	init_graph();
	g_score=0;                     /**/
	g_level=1;                     /*初始化得分,等级*/
	set_timer(new_interrupt);      /*设置计时器(见timer.h)*/
	init_gm_win();                 /*初始化面板*/
    show_logo();
/*===================*/
next:       /*下一块积木*/
	show_gm_win();             /*显示边框*/
	if(is_over()) do_over();   /*判断是否结束*/
	block_x=4; block_y=1;      /*积木的初始化位置(4,1)*/
	block_no=4*(random(abs(*((int*)(dos_time))))%8);   /*随机得到block_no*/
	t_no=block_no;     /*记录block_no用在旋转时不会转的别的积木*/
	show_block(block_no,block_x,block_y);    /*显示积木*/
	t_timer=timer_counter;                   /*timer_couter-t_time=时间差*/
res:        /*一个积木下落并接受键盘的过程*/
	for( ; ; ){
		if(abs(timer_counter-t_timer)>(16-g_level*1)){    /*等级影响下落速度*/
			if(!can_down()) {
				do_evaluate();
				do_del();
				goto next;
			}    /*判断是否能下落,不能的话赋值,削行*/
			block_y++;       /*下落*/
			show();          /*重绘面板*/
			show_block(block_no,block_x,block_y);    /*重绘积木*/
			t_timer=timer_counter;       /*为 下次计时准备差量*/
		}             /*并行下落的实现*/
		if(kbhit()!=0)    /*判断是否有键盘按下*/
		switch(bioskey(0)){   /*若有键盘按下,判断按键,做出反映*/
			case UP: {
				if(!can_turn()) goto res;
				block_no++;
				if(block_no-t_no>3) block_no=t_no;
				if(block_x<1) block_x=1;
				if(block_x+3>10) block_x=7;
				show();
				show_block(block_no,block_x,block_y);
				goto res;
			}
			case DOWN: {
				if(!can_down()){
					do_evaluate();
					do_del();
					goto next;
				}
				block_y++;
				show();
				show_block(block_no,block_x,block_y);
				goto res;
			}
			case LEFT: {
				if(!can_left()) goto res;
				block_x--;
				show();
				show_block(block_no,block_x,block_y);
				goto res;
			}
			case RIGHT: {
				if(!can_right()) goto res;
				block_x++;
				show();
				show_block(block_no,block_x,block_y);
				goto res;
			}
			case PAUSE: {
				getch();
				goto res;
			}
			case CLEAR: {
			    init_gm_win();
			    show();
			    goto next;
			}
			case ESC: do_over();
			default : goto res;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -