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

📄 square.c

📁 俄罗斯方块(tc编程的源代码和可执行文件)运行环境TC 2.0
💻 C
📖 第 1 页 / 共 2 页
字号:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#include <graphics.h>
/*----------------------------my-h-files-----------------------------------*/
#include "d:\tcc\source\square.h"
#include "d:\tcc\source\wboard.h"
/*--------------------some variables used in this program.-----------------*/
#define ROWS 20
#define COLS 10
#define TRUE 1
#define FALSE 0
#define DOWN	0X50
#define RIGHT	0x4d
#define LEFT	0x4b
#define SPCBAR	0x39
#define ESC		0x01
#define P		0x19
/*------------------------Declaring the variables--------------------------*/
int score=0,speed=1,level=1,lines=0;
int new=TRUE;
int over=FALSE;
int desk[ROWS][COLS];
char *buf[4];
/*------------------------Declaring the funtions---------------------------*/
void init(void);
	void init_structdata(void);
	void init_data(void);
	void init_graphic(void);
void main_draw(void);
int getkey();
struct _square *select_sqr(void);
int move_arrow(int dirt);
void wait(void);
void begin(void);
void display_sqr();
void display_psqr();
void del();
void update(int s,int l);
void frozen();
void turnover();
void openbuf();
/*------------------------functions in detail------------------------------*/
void init_structdata(void)
{
	char a;
	/*speed && line*/
	printf("\nWould you like to select the speed and line to begin?[Y]/[N]");
	while((a=getch(),a)!='Y' || a!='y' || a!='N' || a!='n')
	{
		if(a=='y' || a=='Y')
		{
			printf("\nWhat is the speed do you want to begin with?...from 1 to 9:");
			scanf("%d",&speed);
			printf("About the rows to start...from 0 to 15:");
			scanf("%d",&line);
			puts("press any key to continue...");
			getch();
			break;
		}
		else
		{
			printf("\nThe default value of speed and line is 1 and 0.");
			puts("\npress any key to continue...");
			getch();
			break;
		}
	}
}
void init(void)
{
	clrscr();
	init_structdata();
	init_data();
	init_graphic();
}
void init_data(void)
{
	register int y=0,x=0;
	int *cnt;
	for(;y<ROWS;y++)
		for(;x<COLS;x++)
			desk[y][x]=0;
	for(y=0;y<4;y++)
		for(x=0;x<4;x++)
		{
			psqr->matrix[y][x]=0;/*printf("%d",psqr->matrix[y][x]);*/
			pnxtsqr->matrix[y][x]=0;
		}
	/*other arguments*/
	brd.spc=20;
	score=0;
	over=FALSE;
	init_wboard();
}
	void init_graphic(void)
{
	int Driver=DETECT,Mode;
  /*	registerbgidriver(EGAVGA_driver);*/
	initgraph(&Driver,&Mode,"..//..//bgi");
	directvideo=0;
}
void main_draw(void)
{
	int spc=5;
	setcolor(RED);
	outtextxy(145,10,"Russian Block!");
	outtextxy(120,30,"---Programmed by Dzbjet");
	outtextxy(410,30,"Preview Window:");
	outtextxy(350,150,"Speed:");
	outtextxy(350,180,"Level:");
	outtextxy(350,210,"Score:");
	outtextxy(350,240,"Lines:");
	setcolor(GREEN);
	outtextxy(350,310,"Press \"ESC\" to exit...");
	outtextxy(380,330,"or \"p\" to pause.");
	setcolor(RED);
	outtextxy(330,380,"    *  *   * * # #  # #    ");
	outtextxy(330,390,"   *     *  # *    #    #   ");
	outtextxy(330,400,"   *       #  *         #   ");
	outtextxy(330,410,"    *        *         #    ");
	outtextxy(330,420,"      *    *   #     #       ");
	outtextxy(330,430,"        *         #          ");
	setcolor(GREEN);
	setfillstyle(7,9);
	/*draw main board*/
	bar(100-spc,50-spc,300+spc,50);
	bar(420,50-spc,500+2*spc,50);
	bar(98-spc,50,98,450);
	bar(302,50,302+spc,450);
	bar(420,50,420+spc,130);
	bar(500+1*spc,50,500+2*spc,130);
	bar(100-spc,450,300+spc,450+spc);
	bar(420,130,500+2*spc,130+spc);
}
struct _square *select_sqr(void)
{
	   struct _square *p=(struct _square*)malloc(sizeof(struct _square));
       int cnty,cntx;/*faint!! How do I get the value ranging from 0 to 6*/
	   int num;
	   num=rand()%7; 	/*num ranged 0 to 6*/
	   p->kind=num;
	   num=random(4);
	   p->num=num;
       for(cnty=0;cnty<4;cnty++)
			for(cntx=0;cntx<4;cntx++)
			p->matrix[cnty][cntx]=wboard[p->kind][p->num][cnty][cntx];
	   p->intx=3;
	   p->inty=0;
	   p->color=RED;
	   return p;
}
int getkey()
{
	union REGS r;
	r.h.ah=0;
	int86(0x16,&r,&r);
	return r.h.ah;
}
void wait(void)
{
	getch();
}
void begin(void)
{
	int i,t=1;
	static int c=0;
	/*draw the game board!*/
	main_draw();
	update(score,lines);
	randomize();
	psqr=select_sqr();
	display_sqr(draw);
	while(!over)
	{
	epp:
		if(new)
		{
			c=0;
			if(!t)
			{
				setviewport(brd.startx+200+225,brd.starty+50,brd.startx+425+80,brd.starty+130,1);
				clearviewport();
				pnxtsqr=select_sqr();
				display_psqr();
			}
		else
		{
			pnxtsqr=select_sqr();
			display_psqr();t=0;
		}
			new=FALSE;
			display_sqr(draw);
			delay(500);
			i=move_arrow(down);
			if(i)
			{
				over=TRUE;
				goto e;
			}
		}
		while(!kbhit())
			{
				c++;
				delay(10-speed);
				if(c==70){c=0; i=move_arrow(down);}
				if(i){new=TRUE;i=0;psqr=pnxtsqr;goto epp;}
			}
		switch(getkey())
		{
			case DOWN:
				i=move_arrow(down);break;
			case LEFT:
				move_arrow(left);break;
			case RIGHT:
				move_arrow(right);break;
			case ESC:
				over=TRUE;break;
			case SPCBAR:
				turnover();break;
			case P:
				wait();
		}
		if(i){new=TRUE;i=0;psqr=pnxtsqr;}
		e:;
	}
outtext("GameOver!");getch();
}
void display_sqr(int optr)
{
	register int x,y;
	setviewport(100,50,200,250,1);
	for(y=0;y<4;y++)
		for(x=0;x<4;x++)
		if(psqr->matrix[y][x]==1)
			putimage((psqr->intx+x)*brd.spc,(y+psqr->inty)*brd.spc,buf[optr],COPY_PUT);
}
int move_arrow(int dirt)
{
	int move=0;
	switch(dirt)
	{
	case	right:
		switch(psqr->kind)
		{
		case 0:
			move=psqr->intx<8 && desk[psqr->inty][psqr->intx+2]==0 &&
				 desk[psqr->inty+1][psqr->intx+2]==0;
			break;
		case 1:
			switch(psqr->num)
			{
			case 0:
				move=psqr->intx<7 && desk[psqr->inty][psqr->intx+1]==0 &&
					 desk[psqr->inty+1][psqr->intx+3]==0;
				break;
			case 1:
				move=psqr->intx<8 && desk[psqr->inty][psqr->intx+2]==0 &&
					 desk[psqr->inty+1][psqr->intx+2]==0 && desk[psqr->inty+2][psqr->intx+2]==0;
				break;
			case 2:
				move=psqr->intx<7 && desk[psqr->inty][psqr->intx+3]==0 &&
					 desk[psqr->inty+1][psqr->intx+3]==0;
				break;
			case 3:
				move=psqr->intx<8 && desk[psqr->inty][psqr->intx+2]==0 &&
					 desk[psqr->inty+1][psqr->intx+1]==0 && desk[psqr->inty+2][psqr->intx+1]==0;
			}
			break;
		case 2:
			switch(psqr->num)
			{
			case 0:
				move=psqr->intx<7 && desk[psqr->inty][psqr->intx+3]==0 &&
					 desk[psqr->inty+1][psqr->intx+1]==0;
				break;
			case 1:
				move=psqr->intx<8 && desk[psqr->inty][psqr->intx+1]==0 &&
					 desk[psqr->inty+1][psqr->intx+1]==0 && desk[psqr->inty+2][psqr->intx+2]==0;
				break;
			case 2:
				move=psqr->intx<7 && desk[psqr->inty][psqr->intx+3]==0 &&
					 desk[psqr->inty+1][psqr->intx+3]==0;
				break;
			case 3:
				move=psqr->intx<8 && desk[psqr->inty][psqr->intx+2]==0 &&
					 desk[psqr->inty+1][psqr->intx+2]==0 && desk[psqr->inty+2][psqr->intx+2]==0;
			}
			break;
		case 3:
			switch(psqr->num)
			{
			case 0:
				move=psqr->intx<7 && desk[psqr->inty][psqr->intx+2]==0 &&
					 desk[psqr->inty+1][psqr->intx+3]==0;
				break;
			case 1:
				move=psqr->intx<8 && desk[psqr->inty][psqr->intx+2]==0 &&
					 desk[psqr->inty+1][psqr->intx+2]==0 && desk[psqr->inty+2][psqr->intx+2]==0;
				break;
			case 2:
				move=psqr->intx<7 && desk[psqr->inty][psqr->intx+3]==0 &&
					 desk[psqr->inty+1][psqr->intx+2]==0;
				break;
			case 3:
				move=psqr->intx<8 && desk[psqr->inty][psqr->intx+1]==0 &&
					 desk[psqr->inty+1][psqr->intx+2]==0 && desk[psqr->inty+2][psqr->intx+1]==0;
			}
			break;
		case 4:
			switch(psqr->num)
			{
			case 0:
			case 2:
				move=psqr->intx<8 && desk[psqr->inty][psqr->intx+1]==0 &&
					 desk[psqr->inty+1][psqr->intx+2]==0 && desk[psqr->inty+2][psqr->intx+2]==0;
				break;
			case 1:
			case 3:
				move=psqr->intx<7 && desk[psqr->inty][psqr->intx+3]==0 &&
					 desk[psqr->inty+1][psqr->intx+2]==0;
			}
			break;
		case 5:
			switch(psqr->num)
			{
			case 0:
			case 2:
				move=psqr->intx<8 && desk[psqr->inty][psqr->intx+2]==0 &&
					 desk[psqr->inty+1][psqr->intx+2]==0 && desk[psqr->inty+2][psqr->intx+1]==0;
				break;
			case 1:
			case 3:
				move=psqr->intx<7 && desk[psqr->inty][psqr->intx+2]==0 &&
					 desk[psqr->inty+1][psqr->intx+3]==0;
			}
			break;
		case 6:
			switch(psqr->num)
			{
			case 0:
			case 2:
				move=psqr->intx<9 && desk[psqr->inty][psqr->intx+1]==0 &&
					 desk[psqr->inty+1][psqr->intx+1]==0 && desk[psqr->inty+2][psqr->intx+1]==0 &&
					 desk[psqr->inty+3][psqr->intx+1]==0;
				break;
			case 1:
			case 3:
				move=psqr->intx<6 && desk[psqr->inty][psqr->intx+4]==0;
			}
		}
		if(move)
			{
			display_sqr(erase);
			psqr->intx++;
			display_sqr(draw);
			}
	return 0;
	case left:
		switch(psqr->kind)
		{
		case 0:
			move=psqr->intx>0 && desk[psqr->inty][psqr->intx-1]==0 &&
				 desk[psqr->inty+1][psqr->intx-1]==0;
			break;
		case 1:
			switch(psqr->num)
			{
			case 0:
				move=psqr->intx>0 && desk[psqr->inty][psqr->intx+1]==0 &&
					 desk[psqr->inty+1][psqr->intx+3]==0;
				break;
			case 1:
				move=psqr->intx>0 && desk[psqr->inty][psqr->intx]==0 &&
					 desk[psqr->inty+1][psqr->intx]==0 && desk[psqr->inty+2][psqr->intx-1]==0;
				break;
			case 2:
				move=psqr->intx>0 && desk[psqr->inty][psqr->intx-1]==0 &&
					 desk[psqr->inty+1][psqr->intx+1]==0;
				break;
			case 3:
            	move=psqr->intx>0 && desk[psqr->inty][psqr->intx-1]==0 &&
					 desk[psqr->inty+1][psqr->intx-1]==0 && desk[psqr->inty+2][psqr->intx-1]==0;
			}
			break;
		case 2:
			switch(psqr->num)
			{
			case 0:
				move=psqr->intx>0 && desk[psqr->inty][psqr->intx-1]==0 &&
					 desk[psqr->inty+1][psqr->intx-1]==0;
				break;
			case 1:
				move=psqr->intx>0 && desk[psqr->inty][psqr->intx-1]==0 &&
					 desk[psqr->inty+1][psqr->intx-1]==0 && desk[psqr->inty+2][psqr->intx-1]==0;
				break;
			case 2:
				move=psqr->intx>0 && desk[psqr->inty][psqr->intx+1]==0 &&
					 desk[psqr->inty+1][psqr->intx-1]==0;
				break;
			case 3:
				move=psqr->intx>0 && desk[psqr->inty][psqr->intx-1]==0 &&
					 desk[psqr->inty+1][psqr->intx]==0 && desk[psqr->inty+2][psqr->intx]==0;
			}
			break;
		case 3:
			switch(psqr->num)
			{
			case 0:
				move=psqr->intx>0 && desk[psqr->inty][psqr->intx]==0 &&
					 desk[psqr->inty+1][psqr->intx-1]==0;
				break;
			case 1:
				move=psqr->intx>0 && desk[psqr->inty][psqr->intx]==0 &&
					 desk[psqr->inty+1][psqr->intx-1]==0 && desk[psqr->inty+2][psqr->intx]==0;
				break;

⌨️ 快捷键说明

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