• پایان فعالیت بخشهای انجمن: امکان ایجاد موضوع یا نوشته جدید برای عموم کاربران غیرفعال شده است

یه تتریس به زبان سی در وجه غیر گرافیکی

sha19425

Registered User
تاریخ عضویت
10 ژانویه 2006
نوشته‌ها
281
لایک‌ها
21
کد:
#include <time.h>
#include <dos.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>

void menu();
void kadr();
void left();
void right();
void rotate();
void down();
void clean();
void draw();
void refresh();
void fix();
void checkscore();
void delrow(int );
void checkend();

int checklimit();
int checkscreen();


int score,LEVEL;

int X = 40 , Y = 8 ,LIMIT_RIGHT=51, LIMIT_LEFT=30 ,LIMIT_DOWN=40;
int POLY , FORM ;

int  x_poly[6][4][6]={
 { {0,1,1,1,88},{-1,0,-1,1,88},{-1,-1,-1,0,88},{1,1,0,-1,88}}
,{ {0,1,0,-1,88},{0,1,1,1,88},{0,-1,0,1,88},{0,-1,-1,-1,88} }
,{ {1,1,1,1,88},{-2,-1,0,1,88},{1,1,1,1,88},{-2,-1,0,1,88}}
,{ {0,0,-1,88},{-1,0,0,88},{-1,0,-1,88},{-1,0,-1,88} }
,{ {0,1,0,-1,0,88},{0,1,0,-1,0,88},{0,1,0,-1,0,88}, {0,1,0,-1,0,88}}
,{ {0,-1,0,-1,88},{0,-1,-1,-2,88},{0,-1,0,-1,88},{0,-1,-1,-2,88} }
		      };
int  y_poly[6][4][6]={
 { {-1,0,-1,1,88},{-1,-1,0,-1,88},{-1,0,1,1,88},{0,1,1,1,88} }
,{ {0,1,1,1,88},{0,-1,0,1,88},{0,-1,-1,-1,88},{0,-1,0,1,88} }
,{ {-1,0,1,2,88},{0,0,0,0,88},{-1,0,1,2,88},{0,0,0,0,88} }
,{ {0,-1,0,88},{-1,0,-1,88},{-1,-1,0,88},{-1,0,0,88} }
,{ {0,0,1,0,-1,88},{0,0,1,0,-1,88},{0,0,1,0,-1,88},{0,0,1,0,-1,88} }
,{ {0,0,-1,1,88},{0,0,-1,-1,88},{0,0,-1,1,88},{0,0,-1,-1,88} }
		   };

int screen[80][50];

main()
{
textmode(C4350);
clrscr();

menu();

clrscr();

clock_t start=0, end;


randomize();
textcolor(4);
gotoxy(65,3);cprintf("SCORE : %d",score);


  POLY = random(6);
  FORM = random(4);

  textcolor(10);
  kadr();
  textcolor(9);
  draw();



while(1){



 if(kbhit()){
     char ch = getch();
     switch(ch){
	     case 72 : rotate();break;
	     case 75 : left()  ;break;
	     case 77 : right() ;break;
	     case 80 : down()  ;break;
	     case 27 : main();
	       }//switch()
	     }//if

end = clock();


if((end - start)/CLK_TCK >= 0.5/float(LEVEL) ) { refresh(); start = end; }

	}//while()

} // main()

void left(){
	clean();
	X--;
	if( !checklimit() || !checkscreen() )X++;
	draw();
	   }

void right(){
	clean();
	X++;
	if( !checklimit() || !checkscreen() )X--;
	draw();
	   }

void rotate(){
	if(POLY==1&&FORM==3&&X==50)return;// halate estesna baray shekle 2 va forme 4
	clean();
	FORM = ++FORM%4 ;
	if( !checklimit()  || !checkscreen() )FORM--;
	draw();
	   }

void down(){

	clean();
	Y++;
	int step=1;
	while(step<=5){
	if(!checkscreen()){Y--;break;}
	else { Y++; step++;continue ;}

		 }//while
	if(!checkscreen())Y--;
	draw();
	   }

int checklimit(){


for(int i=0;x_poly[POLY][FORM][i]!=88;i++)
if( X+x_poly[POLY][FORM][i] <= LIMIT_LEFT  || X+x_poly[POLY][FORM][i] >= LIMIT_RIGHT)return 0 ;


return 1;

		}


void clean(){

	for(int i=0;x_poly[POLY][FORM][i]!=88;i++){

		gotoxy(X+x_poly[POLY][FORM][i],Y+y_poly[POLY][FORM][i]);
		printf(" ");

						   }//for

	    }

void draw(){

	for(int i=0;x_poly[POLY][FORM][i]!=88;i++){
		textcolor(9);
		gotoxy(X+x_poly[POLY][FORM][i],Y+y_poly[POLY][FORM][i]);
		cprintf("%c",219);
		gotoxy(79,45);
						   }//for

	    }

void refresh(){

	clean();
	Y++;
	if(!checkscreen()){Y--;draw();fix();}
	draw();

	      }

int checkscreen(){

for( int i = 0 ; y_poly[POLY][FORM][i] != 88 ; i++ )
	if( Y + y_poly[POLY][FORM][i]  >= LIMIT_DOWN ) return 0;

for( i = 0 ; x_poly[POLY][FORM][i] != 88 ; i++ )
	if( screen [ X+x_poly[POLY][FORM][i] ] [Y + y_poly[POLY][FORM][i]] == -1)return 0;

return 1;

		 }//checkscreen()

void fix(){

       for(int i = 0 ; x_poly[POLY][FORM][i] != 88 ; i++ )
		screen[X+x_poly[POLY][FORM][i]][Y+y_poly[POLY][FORM][i]] = -1;

  X = 40 , Y = 8;
  POLY = random(6);
  FORM = random(4);

  checkscore();
  checkend();

	  }//fix()




void kadr(){

	for(int i=8 ; i<=LIMIT_DOWN ; i++){
gotoxy(LIMIT_LEFT , i);
cprintf("%c",219);
gotoxy(LIMIT_RIGHT, i);
cprintf("%c",219);
			  }//for

for( i=LIMIT_LEFT ; i<=LIMIT_RIGHT ; i++){
gotoxy(i , LIMIT_DOWN);
cprintf("%c" , 219);

			  }//for
textcolor(7);
gotoxy(2,45);cprintf("ESC = quite");
	   }//kadr()

void checkscore(){
int count=0;
for( int j=LIMIT_DOWN ; j>=1; j-- ){
for( int i = LIMIT_LEFT+1 ; i < LIMIT_RIGHT ; i++ ){
	if(screen[i][j]!=-1) break ;
	if(i==LIMIT_RIGHT-1) {delrow(j); count++ ; j=LIMIT_DOWN;}

					}//for



			   }//for

score=score+100*count;
textcolor(4);
gotoxy(73,3);cprintf("%d",score);
}

void delrow(int j){

for(int i=LIMIT_LEFT+1;i<LIMIT_RIGHT;i++){
      screen[i][j]=0;
      gotoxy(i,j);printf(" ");

					 }//for
delay(200);

for(int k=j-1;k>=1;k--){
for( i=LIMIT_LEFT+1 ; i<LIMIT_RIGHT ; i++ ){

	if( screen[i][k] == -1 ){ screen[i][k] = 0;screen[i][k+1]=-1;
				  gotoxy(i,k);printf(" ");
				  gotoxy(i,k+1);cprintf("%c",219);
				}//if
					    }//for
		}//for

		  }//delrow()

void checkend(){

      for(int i = LIMIT_LEFT+1 ; i<LIMIT_RIGHT ; i++ )
	   if(screen[i][8] == -1) {textcolor(6);gotoxy(60,10);cprintf("GAME OVER");getch();main();}

	       }//checkend()

void menu(){

for(int i=0;i<=79;i++){
for(int j=0;j<=49;j++){
screen[i][j]=0;
		      }}
score = 0;
X = 40,Y = 8;

clrscr();

textcolor(14);
gotoxy(2,45);cprintf("ESC = quite");
gotoxy(50,45);cprintf("WRITTEN BY HOSSEIN");
LEVEL = 1;

 for( i=1 ; i<=10 ; i++) {
 gotoxy(20,i*2+10);cprintf(" LEVEL %d " ,i);
     //getch();
     }

textcolor(9);
gotoxy(18,12);cprintf("%c",219);

   i = 12;

while(1){
  char ch = getch();

switch(ch){

  case 72: if(LEVEL >1) LEVEL--  ; else continue;gotoxy(18,i);cprintf(" "); gotoxy(18,i=i-2);cprintf("%c",219);break;

  case 80: if(LEVEL <10) LEVEL++ ; else continue;gotoxy(18,i);cprintf(" ");gotoxy(18,i=i+2);cprintf("%c",219);break;

  case 13: return;

  case 27: exit(0);
		      }//switch

		}//while

	   }//menu()
 

فایل های ضمیمه

  • TETRIS.rar
    21.8 KB · نمایش ها: 14
بالا