/*------------------------------------------------------------------
****************我的俄罗斯方块******************8
------------------------------------------------------------------*/
#include <windows.h>
#include <stdio.h>
#include "resource.h"
#define CLOSE 1
#define iCell 18
#define iNextcell 20
#define uColor RGB(255,0, 0 )
//------------------------------全局变量-----------------------------------
HMENU hMenu;
static bool bPause=1,begin=0, //为了不让没开始,点“加速”或“减速”就能开始
bEnd=1;
static int iNow,iNext,iSorce=0,time=870;
static POINT pt[5]; //运送方块的车,pt[4]用来保存中心位置
//---------------------------函数申明---------------------------------
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK AboutDlgProc1(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK AboutDlgProc2(HWND,UINT,WPARAM,LPARAM);
//一级函数
void BuildButton(HWND,LPARAM);
void DefinedSquare();
void DefinedWork();
void DefinedNext();
int GetNumber();
void OpenPlay(HWND);
void FallSquare(HWND);
void FlipSquare(HWND);
void LeftSquare(HWND);
void RightSquare(HWND);
void DownSquare(HWND);
void PaintDivide(HWND);
void PaintWork(HWND);
void PaintText(HWND);
bool CopySquare(HWND);
void CopyNext(HWND);
void PaintNext(HWND);
//二级函数
bool CanDown();
bool CanLeft();
bool CanRight();
void CanFlip();
void SquareEnter();
void KillLine(HWND);
//三级函数
void UpSquare();
//-----------------------------结构体定义区---------------------------------------
struct
{
POINT pCenter;
bool bSquare[4][4];
}SQUARE[28]={0}; //方块结构
struct
{
bool bWorksquare;
RECT rWorksquare;
}WORKSQUARE[20][12]={0}; //工作区结构
struct
{
bool bNextsquare;
RECT rNextsquare;
}NEXTSQUARE[4][4]={0}; //下一个方块结构
/*-------------------------------------------------------------------------------
***************************函数入口***************************
-------------------------------------------------------------------------------*/
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
PSTR szCmdLine,int iCmdShow)
{
static TCHAR szAppName[]=TEXT("俄罗斯方块");
WNDCLASS wndclass;
HWND hwnd;
MSG msg;
wndclass.style =CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc =WndProc;
wndclass.cbClsExtra =0;
wndclass.cbWndExtra =0;
wndclass.hInstance =hInstance;
wndclass.hIcon =LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
wndclass.hCursor =LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground =CreateSolidBrush(RGB(127,127,127)) ;;
wndclass.lpszMenuName =NULL;
wndclass.lpszClassName =szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("Program requires Windows NT!"),
szAppName,MB_ICONERROR);
return 0;
}
hMenu=LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU1));
hwnd=CreateWindow(szAppName, //window class name
TEXT("俄罗斯方块"), //window caption
WS_OVERLAPPEDWINDOW&~WS_SIZEBOX&~WS_MAXIMIZEBOX, //window style
250, //initial x position
140, //initial y position
400, //initial x size
450, //initial y size
NULL, //parent window handle
hMenu, //window minu handle
hInstance, //program instance handle
NULL); //creation parameters
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/*-------------------------------------------------------------------------------
***************************窗口过程函数*********************
-------------------------------------------------------------------------------*/
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
static HINSTANCE hInstance;
switch(message)
{
//-------WM_CREATE----------------------------------------------------------
case WM_CREATE:
hInstance=((LPCREATESTRUCT)lParam)->hInstance;
BuildButton(hwnd,lParam); //建立按扭
DefinedSquare(); //定义方块
DefinedWork();
DefinedNext();
iNow=GetNumber();
iNext=GetNumber();
return 0;
//-------WM_COMMAND---------------------------------------------------------
case WM_COMMAND:
hMenu=GetMenu(hwnd);
switch (wParam)
{
case OPEN:
begin=1;
bPause=0;
bEnd=0;
iSorce=0;
time=870;
OpenPlay(hwnd);
PaintWork(hwnd);
SquareEnter();
CopySquare(hwnd); //将方块从方块定义中放入车
CopyNext(hwnd);
PaintNext(hwnd);
PaintText(hwnd);
SetTimer(hwnd,1,time,NULL); //计时器
return 0;
case PAUSE:
bPause=!bPause;
PaintText(hwnd);
return 0;
case EXIT:
PostQuitMessage(0);
return 0;
case QUICK:
if(begin==1)
{
if(time==90)
return 0;
time-=130;
SetTimer(hwnd,1,time,NULL);
}
return 0;
case LOW:
if(begin==1)
{
if(time==1000)
return 0;
time+=130;
SetTimer(hwnd,1,time,NULL);
}
return 0;
case ABOUT1:
DialogBox(hInstance,TEXT("AboutBox1"),hwnd,AboutDlgProc1);
break ;
case ABOUT2:
DialogBox(hInstance,TEXT("AboutBox2"),hwnd,AboutDlgProc2);
break ;
case CLOSE:
PostQuitMessage(0);
break;
}
break;
//-------WM_TIMER-----------------------------------------------------------
case WM_TIMER:
if(!bPause&&!bEnd)
FallSquare(hwnd);
return 0;
//-------WM_KEYDOWN-------------------------------------------------------
case WM_KEYDOWN:
switch(wParam)
{
case VK_UP:
if(!bPause&&!bEnd)
FlipSquare(hwnd);
break;
case VK_LEFT:
if(!bPause&&!bEnd)
LeftSquare(hwnd);
break;
case VK_RIGHT:
if(!bPause&&!bEnd)
RightSquare(hwnd);
break;
case VK_DOWN:
if(!bPause&&!bEnd)
DownSquare(hwnd);
break;
case VK_CONTROL:
if(!bPause&&!bEnd)
{
FallSquare(hwnd);
FallSquare(hwnd);
}
break;
case VK_F9:
SendMessage(hwnd,WM_COMMAND,OPEN,0);
break;
}
break;
//-------WM_CHAR----------------------------------------------------------
case WM_CHAR:
switch(wParam)
{
case 'w':
case 'W':
if(!bPause&&!bEnd)
FlipSquare(hwnd);
break;
case 'a':
case 'A':
if(!bPause&&!bEnd)
LeftSquare(hwnd);
break;
case 'd':
case 'D':
if(!bPause&&!bEnd)
RightSquare(hwnd);
break;
case 's':
case 'S':
if(!bPause&&!bEnd)
DownSquare(hwnd);
break;
}
break;
//-------WM_PAINT---------------------------------------------------------
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
PaintDivide(hwnd); //画工作区与分数区的分界线
PaintWork(hwnd); //画工作区
PaintText(hwnd); //显示文本
PaintNext(hwnd); //显示下一个方块
EndPaint(hwnd,&ps);
return 0;
//-------WM_DESTROY--------------------------------------------------------------
case WM_DESTROY:
KillTimer(hwnd,1);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
/*---------------------------