#include<windows.h>
struct Boarder
{
bool Have[15][10];
int Per_Width,Per_Height;
int Width_Ex,Height_Ex;
};
struct Boarder boarder;
struct Shape
{
bool Have[4][4];
int x,y;
};
struct Shape shapes[19];
struct Shape shape;
HDC hdc;
HWND hwnd;
RECT wndrect;
char title[10]="Game";
int direct;
int shape_num;
float last_time;
float now_time;
void Init();
void clearscreen();
void Draw();
void TestLeft();
void TestBottom();
void TestDisappear();
void TestChange();
LRESULT Windowproc(HWND,UINT,WPARAM,LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpcmdLine,int nCmdShow)
{
MSG msg;
WNDCLASS wc;
wc.style =CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)Windowproc;
wc.cbWndExtra =0;
wc.cbClsExtra =0;
wc.hInstance = hInstance;
wc.hIcon =NULL;
wc.hCursor =NULL;
wc.hbrBackground =(HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszClassName =title;
wc.lpszMenuName =NULL;
if (!hInstance)
{
return 0;
}
RegisterClass(&wc);
hwnd=CreateWindow(title,title,WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL);
if(!hwnd)
{
return 0;
}
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
Init();
while (1)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message ==WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
now_time=(float)timeGetTime();
if(now_time>last_time+400)
{
last_time=now_time;
clearscreen();
Draw();
TestDisappear();
TestBottom();
TestLeft();
TestChange();
}
}
}
return 0;
}
LRESULT Windowproc(HWND hwnd ,UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 1;
case WM_KEYDOWN:
{
switch (wParam)
{
case VK_ESCAPE:
PostMessage(hwnd,WM_CLOSE,0,0);
break;
case VK_LEFT:
direct= 1;
break;
case VK_RIGHT:
direct= 2;
break;
case VK_UP:
direct= 3;
break;
}
break;
}
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
void TestBottom()
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(shape.Have[i][j] && shape.y+i ==14)
{
for(int p =0;p<4;p++)
{
for(int q=0;q<4;q++)
{
if(shape.Have[p][q])
{
boarder.Have[shape.y +p][shape.x +q]=true;
}
}
}
shape_num=rand()%19;
shape=shapes[shape_num];//换shape,记录
return;
}
if(shape.Have[i][j] && boarder.Have[shape.y +i+1][shape.x+j])
{
for(int p =0;p<4;p++)
{
for(int q=0;q<4;q++)
{
if(shape.Have[p][q])
{
boarder.Have[shape.y +p][shape.x +q]=true;
}
}
}
shape_num=rand()%19;
shape=shapes[shape_num];
return;
}
}
}
shape.y +=1;
}
void Init()
{
int i,j;
hdc=GetDC(hwnd);
GetWindowRect(hwnd,&wndrect);
direct=0;
last_time=(float)timeGetTime();
for (int k=0;k<19;k++)
{
for(i=0;i<4;i++)
{
for (j=0;j<4;j++)
{
shapes[k].Have[i][j]=false;
shapes[k].x=4;
shapes[k].y=0;
}
}
}
shapes[0].Have[0][0]=true;
shapes[0].Have[1][0]=true;
shapes[0].Have[2][0]=true;
shapes[0].Have[2][1]=true;
shapes[1].Have[0][0]=true;
shapes[1].Have[0][1]=true;
shapes[1].Have[0][2]=true;
shapes[1].Have[1][0]=true;
shapes[2].Have[0][0]=true;
shapes[2].Have[0][1]=true;
shapes[2].Have[1][1]=true;
shapes[2].Have[2][1]=true;
shapes[3].Have[2][0]=true;
shapes[3].Have[2][1]=true;
shapes[3].Have[2][2]=true;
shapes[3].Have[1][2]=true;
shapes[4].Have[0][1]=true;
shapes[4].Have[1][1]=true;
shapes[4].Have[2][1]=true;
shapes[4].Have[2][0]=true;
shapes[5].Have[0][0]=true;
shapes[5].Have[1][0]=true;
shapes[5].Have[1][1]=true;
shapes[5].Have[1][2]=true;
shapes[6].Have[0][0]=true;
shapes[6].Have[0][1]=true;
shapes[6].Have[1][0]=true;
shapes[6].Have[2][0]=true;
shapes[7].Have[0][0]=true;
shapes[7].Have[0][1]=true;
shapes[7].Have[0][2]=true;
shapes[7].Have[1][2]=true;
shapes[8].Have[0][0]=true;
shapes[8].Have[1][0]=true;
shapes[8].Have[1][1]=true;
shapes[8].Have[2][1]=true;
shapes[9].Have[1][1]=true;
shapes[9].Have[1][0]=true;
shapes[9].Have[0][1]=true;
shapes[9].Have[0][2]=true;
shapes[10].Have[2][0]=true;
shapes[10].Have[1][0]=true;
shapes[10].Have[1][1]=true;
shapes[10].Have[0][1]=true;
shapes[11].Have[0][0]=true;
shapes[11].Have[0][1]=true;
shapes[11].Have[1][1]=true;
shapes[11].Have[1][2]=true;
shapes[12].Have[1][0]=true;
shapes[12].Have[1][1]=true;
shapes[12].Have[1][2]=true;
shapes[12].Have[0][1]=true;
shapes[13].Have[0][0]=true;
shapes[13].Have[1][0]=true;
shapes[13].Have[2][0]=true;
shapes[13].Have[1][1]=true;
shapes[14].Have[0][0]=true;
shapes[14].Have[0][1]=true;
shapes[14].Have[0][2]=true;
shapes[14].Have[1][1]=true;
shapes[15].Have[0][1]=true;
shapes[15].Have[1][0]=true;
shapes[15].Have[1][1]=true;
shapes[15].Have[2][1]=true;
shapes[16].Have[0][0]=true;
shapes[16].Have[1][0]=true;
shapes[16].Have[2][0]=true;
shapes[16].Have[3][0]=true;
shapes[17].Have[0][0]=true;
shapes[17].Have[0][1]=true;
shapes[17].Have[0][2]=true;
shapes[17].Have[0][3]=true;
shapes[18].Have[0][0]=true;
shapes[18].Have[0][1]=true;
shapes[18].Have[1][0]=true;
shapes[18].Have[1][1]=true;
boarder.Per_Width=(int)(( wndrect.right-wndrect.left )*0.66/14);
boarder.Per_Height=(int)((wndrect.bottom-wndrect.top)/19.0);
boarder.Width_Ex=2*boarder.Per_Width;
boarder.Height_Ex=1*boarder.Per_Height;
for(i=0;i<15;i++)
{
for(j=0;j<10;j++)
{
boarder.Have[i][j]=false;
}
}
shape_num=rand()%19;
shape=shapes[shape_num];
}
void clearscreen()
{
HBRUSH hbrush=(HBRUSH)CreateSolidBrush(RGB(255,255,255));
SelectObject(hdc,hbrush);
Rectangle(hdc,wndrect.left ,wndrect.top ,wndrect.right ,wndrect.bottom );
SelectObject(hdc,hbrush);
DeleteObject(hbrush);
}
void Draw()
{
HBRUSH h_old_brush;
HBRUSH hbrush1=(HBRUSH)CreateSolidBrush(RGB(150,100,50));
HBRUSH hbrush2=(HBRUSH)CreateSolidBrush(RGB(10,150,200));
HBRUSH hbrush3=(HBRUSH)CreateSolidBrush(RGB(10,250,250));
int i,j;
h_old_brush=(HBRUSH)SelectObject(hdc,hbrush3);
//划界面
for(i=0;i<15;i++)
{
for(j=0;j<10;j++)
{
Rectangle(hdc,boarder.Width_Ex +j*boarder.Per_Width,boarder.Height_Ex + i*boarder.Per_Height ,boarder.Width_Ex +(j+1)*boarder.Per_Width,boarder.Height_Ex + (i+1)*boarder.Per_Height );
}
}
SelectObject(hdc,hbrush1);
for(i=0;i<15;i++)
{
for(j=0;j<10;j++)
{
if(boarder.Have[i][j])
{
Rectangle(hdc,boarder.Width_Ex +j*boarder.Per_Width,boarder.Height_Ex + i*boarder.Per_Height ,boarder.Width_Ex +(j+1)*boarder.Per_Width,boarder.Height_Ex + (i+1)*boarder.Per_Height );
}
}
}
SelectObject(hdc,hbrush2);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(shape.Have[i][j])
{
Rectangle(hdc,boarder.Width_Ex +(j+shape.x) *boarder.Per_Width,boarder.Height_Ex + (i+shape.y )*boarder.Per_Height ,boarder.Width_Ex +(shape.x +1+j)*boarder.Per_Width,boarder.Height_Ex + (shape.y +1+i)*boarder.Per_Height );
}
}
}
SelectObject(hdc,h_old_brush);
DeleteObject(hbrush1);
DeleteObject(hbrush2);
DeleteObject(hbrush3);
}
void TestLeft()
{
int i,j;
bool canLeft=true;
bool canRight=true;
if(shape.x <1)
canLeft=false;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(shape.Have[i][j]&&boarder.Have[shape.y +i-1][shape.x +j-1])
{
canLeft=false;
}
}
}
if( canLeft &&(direct==1))
{
shape.x -=1;
direct=0;
}
//////////right
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(shape.Have[i][j]&&boarder.Have[shape.y +i+1][shape.x+j+1])
{
canRight=false;
}
/*if(shape.x +j>10)
{
canRight=false;
}