俄罗斯方块
//这是main函数CPP:
#include <iostream>
#include <windows.h>
#include "fire.h"
#include<time.h>
void main()
{
srand((unsigned)time(NULL));
init();
while (1)
{
MoveCursor(13, 1);std::cout<<"Next ...";
if(lose)
{
MoveCursor(1, 5);
std::cout<<" GAME OVER\n";
MoveCursor(3, 6);
std::cout<<"<1> restart";
MoveCursor(3, 7);
std::cout<<"<2> exit";
if(GetAsyncKeyState('1')&1)init();
if(GetAsyncKeyState('2')&1)return;
MoveCursor(0, 22);
system("pause");
}
//system("cls");
draw();
PD_chongdie();
MoveCursor(14, 10);
std::cout<<"操作指南";
MoveCursor(20, 12);
std::cout<<"★ A左.D右.S快速下 ★";
MoveCursor(20, 14);
std::cout<<"★ .< W >. 变形 ★";
MoveCursor(14, 18);
std::cout<<"游戏得分";
MoveCursor(20, 18);
std::cout<<"★ "<<jifen<<" ★";
switch (level)
{
case 1:MoveCursor(13, 8);std::cout<<"LEVEL ☆ ONE";break;
case 2:MoveCursor(13, 8);std::cout<<"LEVEL ☆ TWO";break;
case 3:MoveCursor(13, 8);std::cout<<"LEVEL ☆ THERE";break;
case 4:MoveCursor(13, 8);std::cout<<"LEVEL ☆ GOLD";break;
}
if(jifen<20)level = 1;
else if(jifen >= 20 &&jifen <30)level = 2;
else if(jifen >= 30 &&jifen <50)level = 3;
else level = 4;
if(mx==false)
{
if(jifen<20)Sleep(300);
else if(jifen >= 20 &&jifen <30)Sleep(200);
else if(jifen >= 30 &&jifen <50)Sleep(100);
else Sleep(50);
}
}
}
//这是一个头文件fire.h
extern int jifen;
extern bool lose;
extern bool mx;
extern int level;
void init();
void draw();
void run();
void MoveCursor(int x, int y);
void PD_chongdie();
//这是另一个头文件quanjubianliang.h
int smap[] =
{
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0
};
int Amap[] =
{
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,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1,0,0,0,// 7
1,0,0,0,
1,0,0,0,
1,0,0,0}
};
int temp1[4][16]={
{
0,1,0,0,
1,1,0,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},
{
1,0,0,0,
1,1,0,0,
1,0,0,0,
0,0,0,0},
{
0,1,0,0,
1,1,1,0,
0,0,0,0,
0,0,0,0}
};
int temp2[4][16]={
{
0,0,0,0,
1,1,1,0,
1,0,0,0,
0,0,0,0},
{
1,0,0,0,
1,0,0,0,
1,1,0,0,
0,0,0,0},
{
0,0,1,0,
1,1,1,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}
};
int temp3[4][16]={
{
1,0,0,0,
1,1,1,0,
0,0,0,0,
0,0,0,0},
{
0,1,0,0,
0,1,0,0,
1,1,0,0,
0,0,0,0},
{
0,0,0,0,
1,1,1,0,
0,0,1,0,
0,0,0,0},
{
1,1,0,0,
1,0,0,0,
1,0,0,0,
0,0,0,0}
};
int temp4[2][16]={
{
1,0,0,0,
1,1,0,0,
0,1,0,0,
0,0,0,0},
{
0,1,1,0,
1,1,0,0,
0,0,0,0,
0,0,0,0}
};
int temp5[2][16]={
{
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}
};
int temp6[2][16]={
{
0,0,0,0,
1,1,1,1,
0,0,0,0,
0,0,0,0},
{
1,0,0,0,
1,0,0,0,
1,0,0,0,
1,0,0,0}
};
//这是zal.cpp
#include <iostream>
#include <windows.h>
#include"quanjubianliang.h"
#define W 10
#define H 20
#define S (W*H)
void SetColor(int color)//---------------------------------设置颜色
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hOut, color);
}
void MoveCursor(int x, int y)//-----------------------------设置光标位置
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);///获取输出流句柄
COORD coord;///定义COORD结构体(包含X和Y两个成员)
coord.X = x * 2; ///设置结构体X轴
coord.Y = y; ///设置结构体Y轴
SetConsoleCursorPosition(hOut, coord);///设置光标位置
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };///修改光标宽带和显示
SetConsoleCursorInfo(hOut, &cursor_info);///设置光标不可见
}
void jiance();
int px = W/2;
int py = 0;
int n=0;
int m=0;
int temp[16]={};
int bx=0;
int jifen=0;
int level=1;
bool lose =false;
bool jian=false;
bool mx=false;
void init()
{
px = W/2-2;
py = 0;
level=1;
lose =false;
bx=0;
jifen=0;
jian=false;
mx=false;
m=rand()%7;
n=rand()%7;
for(int i=0;i<16;++i)
{
smap[i]=ST_X[n][i];
temp[i]=ST_X[m][i];
}
for(int i=0;i<S;++i)
{
bmap[i]=Amap[i];
}
}
void draw()
{
for(int y=0;y<4;++y)
{
MoveCursor(16, 3+y);
for(int x=0;x<4;++x)
{
if(temp[x+4*y]==1)
std::cout<<"■";
else
std::cout<<" ";
}
std::cout<<std::endl;
}
MoveCursor(0, 0);
for (int y = 0;y < H; ++y)
{
std::cout<<"■";
for (int x = 0; x < W; ++x)
{
if (x >= px && x < px + 4 && y >= py && y < py + 4 && smap[(x-px)+(y-py)*4] == 1 )
std::cout<<"□";
else switch(bmap[x+y*W])
{
case 0:SetColor(14);std::cout<<" ";break;
case 1:SetColor(11);std::cout<<"■";break;
case 3:SetColor(14);std::cout<<"■";break;
}
}std::cout<<"■\n";
}
}
void PD_chongdie()
{
if(jian)jiance();
bool chongdie = false;
bool xzc=false;
bool xyc=false;
for (int x = 0; x < 4; ++x)
{
for (int y = 0; y < 4; ++y)
{
if (smap[x+y*4] == 1)
{
if ( ( py+y+2) >= H || bmap[px+x+(py+y+1)*W] == 1 )
{
chongdie = true;
break;
}
}
}
}
if (chongdie) //--------------------------重叠合并
{
for (int y = 0; y < 4; ++y)
{
for (int x = 0; x < 4; ++x)
{
if (smap[x+y*4] == 1)
bmap[px+x+(py+y)*W] = 1;
}
}
n=m;
jian=true;
mx=false;
py = 0;
px = W/2-2;//--------------新方块----
bx=0;
m=rand()%7;
for (int i = 0; i < 4*4; ++i)
{
smap[i]=temp[i];//ST_X[n][i]
}
for (int i = 0; i < 4*4; ++i)
{
temp[i]=ST_X[m][i];
}
}
else
py++;
bool xz=true;
bool xy=true;
if ((GetAsyncKeyState('A')&1)&& px> 0 && xzc == false )
{
for (int x = 0; x < 4; ++x)
{
for (int y = 0; y < 4; ++y)
{
if (smap[x+y*4] == 1)
{
if (bmap[px+x-1+(py+y)*W] == 1 )
{
xzc = true;
break;
}
}
}
}
if(xzc == false) px--;
}
if (GetAsyncKeyState('S')&1)mx=true;
if (GetAsyncKeyState('D')&1)
{
int wx=0;
for(int y=0;y<4;++y)
{
for(int x=0;x<4;++x)
{
if( smap[x+y*4]== 1 )
{
if(x>wx)wx=x;
}
}
}
for (int x = 0; x < 4; ++x)
{
for (int y = 0; y < 4; ++y)
{
if (smap[x+y*4] == 1)
{
if (bmap[px+x+1+(py+y)*W] == 1 )
{
xyc = true;
break;
}
}
}
}
if((px+wx+1<W)&& xyc==false)
px += 1;
}
if (GetAsyncKeyState('W')&1)
{
bx++;
switch (n)
{
case 1:if(bx==5)bx=1;
for(int i=0;i<16;++i)
{smap[i] = temp1[bx-1][i];}break;
case 2:if(bx==5)bx=1;
for(int i=0;i<16;++i)
{smap[i] = temp2[bx-1][i] ;} break;
case 3:if(bx==5)bx=1;
for(int i=0;i<16;++i)
{smap[i] = temp3[bx-1][i] ;} break;
case 4:if(bx==3)bx=1;
for(int i=0;i<16;++i)
{smap[i] = temp4[bx-1][i];} break;
case 5:if(bx==3)bx=1;
for(int i=0;i<16;++i)
{smap[i] = temp5[bx-1][i];} break;
case 6:if(bx==3)bx=1;
for(int i=0;i<16;++i)
{smap[i] = temp6[bx-1][i];} break;
}
}
}
void jiance()
{
int xiao = 0;
int wz_y[4]={};
for(int y=0;y<H;++y)
{
int yi=0;
for(int x=0;x<W;++x)
{
if(bmap[x+y*W]== 0)break;
else if(bmap[x+y*W]== 1)yi++;
}
if(yi==W)wz_y[xiao++]=y;
}
if(xiao)
{
bool xialuo=false;
for(int j=0;j<4;++j)//---消除
{
for(int i=0;i<S;++i)
{
if(bmap[i%W+wz_y[j]*W]==1)
{
bmap[i%W+wz_y[j]*W]=0;
xialuo=true;
}
}
if(xialuo)
{
for(int i=S-1;i>=0;--i)
{
if(i/W < wz_y[j])bmap[i+W]=bmap[i];
}
}
}
switch (xiao)//----记分
{
case 1:jifen++ ;break;
case 2:jifen+= 3 ;break;
case 3:jifen += 5 ;break;
case 4:jifen += 8 ;break;
}
}
jian=false;
for(int i=0;i<W;++i)
{
if(bmap[i] != 0 )lose = true;
}
}