new
#include <string.h>
#include <conio.h>
#include <stdlib.h>
//#include <dos.h>
#include <stdio.h>
//#include <string.h>
//#define FALSE 0
//#define TRUE !FALSE
//#define Chr(X) (X & 0xFF)
int I,J,HomeX,HomeY,X,Y,x,y;
int oldX=0;
int flag_X_plus=0;
int flag_X_minus=0;
int flag_Rotation=0;
int line_Del_number=0;
int line_Del_flag=0;
int gameStart=0;
char attribute=0x00;
char symbol=0;
char pn=0;
int Score=200;
int FlagStep=0;
int Level=0;
int Line=0;
int FlagPosition=0;
long int counter=100000;
int oldY;
int Mode=1;
int FlagRotation=0;
//int FlagX=0;
int StepGame=0;
//int Xstart;
//int GameStart=0;
int FigureNumber=6;
int NoSleep=0;
char Key;
int HomeX=15;
int HomeY=2;
int Rotation=0;
//int CurrentFigure[5][5];
int CurentGame[21][12];
int CurentGameOld[21][12];
int gameTemp[21][12];
int smGameX=3;
int smGameY=0;
//int smFigureX=0;
//int smFigureY=4;
int DataGame=0;
int DataFigure=0;
int game[21][12]= {
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,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0, 0, 0},
{0, 0, 1, 1, 0},
{0, 1, 1, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}
}
},
// T
{
{
{0, 0, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 1, 1, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0}
},
{
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 1, 1, 1, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0}
},
{
{0, 0, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 1, 1, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0}
},
{
{0, 0, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 1, 1, 1, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}
}
}
};
void flush (){ //Очистить буфер клавиатуры
asm { cli; sub ax,ax; mov es,ax; mov al,es:[41ah]; mov es:[41ch],al; sti; }
}
void gameOver ()
{
gotoxy(16,12);
printf("GAME OVER.");
sleep(5);
exit(0);
// getch();
}
void printText()
{
gotoxy(5,3);
printf("Level:");
gotoxy(5,4);
printf("%i",Level);
gotoxy(5,6);
printf("Score:");
gotoxy(5,7);
printf("%i",pn);
gotoxy(5,9);
printf("Line:");
gotoxy(5,10);
printf("%i",Line);
}
void Cls()
{
asm{
mov ax,1h
int 10h
}
}
void page(){
asm { mov ah,05h; mov al,pn ; int 10h; }
}
void PutChar ()
{
int sm=((((y-1)*40)+x)*2)-2;
asm {
// mov ax,1
// int 10h
//
// mov al,40
// mov ah,25
mov dl,symbol
mov dh,attribute
mov cl,pn
// mov ch,ah
// mul ch
// shl ax,1
// shl ax,1
// sub ax,2
//
push es
push di // 1
mov di,sm
push dx //2
mov ax,2048
xor ch,ch
mul cx
pop dx // 1
add di,ax
// ret
// push es //3
// push di
mov ax,0B800h
mov es,ax
// pop es
// jmp CharAddr
// mov di,2100;
mov es:[di],dx
pop di
// pop dx
pop es
// ret
}
// return ;
// CharAddr:
// asm {
// }
}
void figureRand ()
{
srand(time(NULL));
FigureNumber=rand()%7;
}
void updateScore ()
{
if(Line>10){
Score=450;
Level=1;
}
if(Line>20){
Score=400;
Level=2;
}
if(Line>30){
Score=350;
Level=3;
}
if(Line>40){
Score=300;
Level=4;
}
}
void TextAttr(char Attr) {
textcolor(Attr & 0xF);
textbackground(Attr >> 4);
}
void WaitRetrace() {
delay(Score);
}
void PrintStakan();
void GameFigure()
{
if (Mode==1) {
memcpy(CurentGame,game,sizeof(game));
for(J=0;J<=4;J++) for(I=0;I<=4;I++){
DataGame=CurentGame[J+StepGame][smGameX+I+X];
DataFigure=Figure[FigureNumber][Rotation][J][I];
if((DataGame+DataFigure)==2)
{
// memcpy(game,CurentGameOld,sizeof(CurentGameOld));
// for(J=0;J<=4;J++) for(I=0;I<=4;I++){
//DataGame=CurentGame[J+StepGame-1][smGameX+I+oldX];
// DataFigure=Figure[FigureNumber][Rotation][J][I];
// game[J+StepGame-1][smGameX+I+oldX]=DataFigure;
// }
Mode=0;
// printText();
if(StepGame<3)
{
//exit(0);
gameOver();
}
else
//Y=HomeY;
StepGame=0;
//exit(0);
break;
}
if (Mode==1)
CurentGame[J+StepGame][smGameX+I+X]=DataGame+DataFigure;
}
if (Mode==1){
memcpy(CurentGameOld,CurentGame,sizeof(CurentGame));
PrintStakan();
}
if(NoSleep==0){
flush();
// sleep(1);
WaitRetrace();
}
//StepGame++;
if(Mode==1){
// if(StepGame==23){
// StepGame=0;
// X=0;
// StepGame++;
// }
if(FlagRotation==0){
if(FlagPosition==0){
StepGame++;
FlagStep=0;
}
else {
FlagPosition--;
}
// counter=100000;
// } else counter--;
}
else {
FlagRotation--;
}
}
else
memcpy(game,CurentGameOld,sizeof(CurentGameOld));
if(Mode==0){
StepGame=0;
X=0;
NoSleep=0;
updateScore();
} }
}
void PrintStakan()
{
// Cls();
if(pn==0)pn=1;
else pn=0;
// if(pn==1)pn=0;
// textmode(1);
// clrscr();
// Cls();
// if(pn==1)
// printText();
symbol=0;
attribute=0x44;
for(J=0;J<=20;J++) for(I=0;I<=11;I++) {
// if(CurentGame[J][I]!=0)
// {
// clrscr();
// gotoxy(HomeX+I,HomeY+J);printf("%i", game[J][I]);
// gotoxy(HomeX+I,HomeY+J);puts("ю");
x=HomeX+I;
y=HomeY+J;
// if(pn==0)pn=1;
// else pn=0;
// symbol=0;
PutChar();
}
symbol=0x09;
attribute=0x120;
for(J=0;J<=20;J++) for(I=0;I<=11;I++) {
if(CurentGame[J][I]!=0)
{
// clrscr();
// gotoxy(HomeX+I,HomeY+J);printf("%i", game[J][I]);
// gotoxy(HomeX+I,HomeY+J);puts("ю");
x=HomeX+I;
y=HomeY+J;
// if(pn==0)pn=1;
// else pn=0;
PutChar();
}
// else
// {
// gotoxy(HomeX+I,HomeY+J);puts(".");
// }
}
//Cls();
page();
}
void lineDelete()
{
Line++;
memcpy(gameTemp,game,sizeof(game));
for(J=line_Del_number;J>=1;J--) for(I=0;I<=11;I++){
gameTemp[J][I]=game[J-1][I];
}
memcpy(game,gameTemp,sizeof(gameTemp));
}
void lineDetectDel()
{
int SumLine=0;
for(J=0;J<=19;J++) {
for(I=0;I<=11;I++) {
SumLine=SumLine+game[J][I];
}
if(SumLine==12){
// exit(0);
line_Del_number=J;
lineDelete();
}
else SumLine=0;
}
}
void main()
{
while(1)
{
// if(gameStart==0){
// Cls();
// gameStart=1;
// }
// PrintStakan();
// if(Mode==0)
// figureRand();
// Key=getch();
// SwitchKey();
if(gameStart==0)
{
figureRand();
gameStart=1;
Cls();
}
GameFigure();
// if(kbhit()&&getch()==SPACE)
if(Mode==0)
figureRand();
Mode=1;
lineDetectDel();
if (kbhit()) {
Key = getch();
switch (Key)
{
case 0x71: // press ESC to exit
exit(0);
break;
case 0x20: // press SPACE to clear screen
if(Rotation<3){
Rotation++;
memcpy(CurentGame,game,sizeof(game));
for(J=0;J<=4;J++) for(I=0;I<=4;I++){
DataGame=CurentGame[J+StepGame][smGameX+I+X];
DataFigure=Figure[FigureNumber][Rotation][J][I];
if((DataGame+DataFigure)==2)
{
flag_Rotation=1;
break;
}
}
if(flag_Rotation==1)
{Rotation--;flag_Rotation=0;}
//Rotation++;
}
else
Rotation=0;
if(FlagRotation==0){
FlagRotation=2;
}
break;
case 0x4d:
oldX=X;
X++;
if(FlagStep==0){
FlagPosition=1;
FlagStep=1;
}
memcpy(CurentGame,game,sizeof(game));
for(J=0;J<=4;J++) for(I=0;I<=4;I++){
DataGame=CurentGame[J+StepGame][smGameX+I+X];
DataFigure=Figure[FigureNumber][Rotation][J][I];
if((DataGame+DataFigure)==2)
{
flag_X_plus=1;
break;
}
}
if(flag_X_plus==1)
{X--;flag_X_plus=0;}
// FlagX=3;
break;
case 0x4b:
oldX=X;
X--;
if (FlagStep==0){
FlagPosition=1;
FlagStep=1;
}
memcpy(CurentGame,game,sizeof(game));
for(J=0;J<=4;J++) for(I=0;I<=4;I++){
DataGame=CurentGame[J+StepGame][smGameX+I+X];
DataFigure=Figure[FigureNumber][Rotation][J][I];
if((DataGame+DataFigure)==2)
{
flag_X_minus=1;
break;
}
}
if(flag_X_minus==1)
{X++;flag_X_minus=0;}
// FlagX=3;
break;
case 0x50:
NoSleep=1;
break;
}
}
}
}