您的位置:首页 > 编程语言 > C语言/C++

Windows API 俄罗斯方块源码 -- 俄罗斯方块.cpp

2012-04-07 09:29 399 查看
// 俄罗斯方块.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "俄罗斯方块A.h"
#include<windows.h>
#include <mmsystem.h>
#pragma comment(lib, "WINMM.LIB")
#define LEFT VK_LEFT
#define RIGHT VK_RIGHT
#define DOWN VK_DOWN
#define UP VK_UP
#define LEVEL1 1000
#define LEVEL2 500
#define LEVEL3 200
#define LEVEL4 100
RUS rus;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
void time1(UINT);
void PUSEGAME();
void GoOn();
void ReStartGame();
HINSTANCE   hInst;
UINT Time=1000;
int num=1;//控制消行时的闪烁次数
HWND hWnd;
int LINE=0;//用于gameover后刷屏
bool Mm=0;//当难度更改后重画难度的显示区域
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevinstance,PSTR szCmdLine,int iCmdShow)
{

static TCHAR szAppName[]=TEXT("hello windows 7");
HWND hwnd;
MSG msg;
HACCEL hAccelTable;
WNDCLASS wndclass;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=MAKEINTRESOURCE(IDC_A);
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_A));
wndclass.lpszClassName=szAppName;
RegisterClassW(&wndclass);
hInst=hInstance;
hwnd=CreateWindow(szAppName,TEXT("Display"),WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX,CW_USEDEFAULT,CW_USEDEFAULT,365,465,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
rus.Initialize_MAP();
rus.Initialize_OBJECT();
rus.InitializeXY();
rus.CheckDownWhite();
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
bool IsMoveRight=false,IsMoveLeft=false,IsMoveVertical=true,IsDeformation=false,puse=false,IsShining=false;int mm=1;

LRESULT CALLBACK WndProc(HWND hwnd,UINT message, WPARAM wParam,LPARAM lParam)
{
int wmId, wmEvent;//与菜单有关
TCHAR szBuffer [50] ;
int iLength;
static int cxChar, cxCaps, cyChar ;
hWnd=hwnd;
HDC hdc;
PAINTSTRUCT ps;
static HPEN hpen=CreatePen(PS_SOLID,2,RGB(255,0,0)),hpenw=CreatePen(PS_SOLID,2,RGB(255,255,0)),hpen1=CreatePen(PS_SOLID,10,RGB(220,72,41));
static int *PointX=rus.GetX(),*PointY=rus.GetY();//与红色方块的位置有关
TEXTMETRIC tm ;
switch(message)
{
case WM_CREATE:
hdc= GetDC (hwnd) ;
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cyChar = tm.tmHeight + tm.tmExternalLeading ;
ReleaseDC (hwnd, hdc) ;
PlaySound (TEXT ("Begin.wav"), NULL, SND_FILENAME | SND_ASYNC|SND_LOOP );

SetTimer(hwnd,1,Time,NULL);
return 0;
/////////////////////////////////////////////////////////////////////////////////////////
case WM_COMMAND:
wmId    = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case ID_32772://第一级
time1(LEVEL1);
break;
case ID_32773://第二级
time1(LEVEL2);
break;
case ID_32774://第三级
time1(LEVEL3);
break;
case ID_32775://第四级
time1(LEVEL4);
break;
case ID_32777://暂停
PUSEGAME();
break;
case ID_32778://继续
GoOn();
break;
case ID_32779://新游戏
ReStartGame();
InvalidateRect(hwnd,NULL,FALSE);//重画窗口
break;
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hwnd, About);          //菜单
break;
case IDM_EXIT:
DestroyWindow(hwnd);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
break;

/////////////////////////////////////////////////////////////////////////////////////////
case    WM_SIZE:
MM=true;                                                       //窗口改变大小,重画;
return 0;
/////////////////////////////////////////////////////////////////////////////////////////
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
if(MM||mm||Mm)
{
if(Mm)
{
iLength = wsprintf (szBuffer, TEXT ("   0"));
SetTextAlign (hdc, TA_RIGHT | TA_TOP);
TextOut(hdc,300,220,szBuffer,iLength);
}
else
{
iLength = wsprintf (szBuffer, TEXT ("%i"),rus.SCORE);
SetTextAlign (hdc, TA_RIGHT | TA_TOP);
TextOut(hdc,300,220,szBuffer,iLength);
}
SetTextAlign (hdc, TA_LEFT | TA_TOP);
iLength = wsprintf (szBuffer, TEXT ("SCORE: "));
TextOut(hdc,250,200,szBuffer,iLength);

iLength = wsprintf (szBuffer, TEXT ("难度:"));
TextOut(hdc,250,280,szBuffer,iLength);
switch(Time)
{
case LEVEL1:
iLength = wsprintf (szBuffer, TEXT ("Level 1"));            //画文字
TextOut(hdc,250,310,szBuffer,iLength);
break;
case LEVEL2:
iLength = wsprintf (szBuffer, TEXT ("Level 2"));
TextOut(hdc,250,310,szBuffer,iLength);
break;
case LEVEL3:
iLength = wsprintf (szBuffer, TEXT ("Level 3"));
TextOut(hdc,250,310,szBuffer,iLength);
break;
case LEVEL4:
iLength = wsprintf (szBuffer, TEXT ("Level 4"));
TextOut(hdc,250,310,szBuffer,iLength);
break;
default:break;
}
Mm=0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////

SelectObject(hdc,hpen1);
MoveToEx(hdc,10,0,NULL);
LineTo(hdc,10,410);
MoveToEx(hdc,10,410,NULL);                                     //画外围的直线
LineTo(hdc,350,410);
MoveToEx(hdc,350,410,NULL);
LineTo(hdc,350,0);
MoveToEx(hdc,227,0,NULL);
LineTo(hdc,227,410);
MoveToEx(hdc,243,125,NULL);
LineTo(hdc,243,43);
MoveToEx(hdc,243,125,NULL);
LineTo(hdc,320,125);
MoveToEx(hdc,240,330,NULL);
LineTo(hdc,240,270);
MoveToEx(hdc,240,270,NULL);
LineTo(hdc,320,270);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(rus.NUM)
{
PUSEGAME();
IsShining=true;
SetTimer(hwnd,3,200,NULL);
for(int m=0;m<rus.NUM;m++)
for(int i=1;i<11;i++)
{
if(num%2)
{
SelectObject(hdc,hpenw);
Rectangle(hdc,(i)*20,(WhichLineNeedDelete[m]-1)*20,(i)*20+18,(WhichLineNeedDelete[m]-1)*20+18);  //让被消掉的行闪烁
}
else
{
SelectObject(hdc,hpen);
Rectangle(hdc,(i)*20,(WhichLineNeedDelete[m]-1)*20,(i)*20+18,(WhichLineNeedDelete[m]-1)*20+18);
}
}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
if(rus.NEXTOBJECT[i][j])
{
SelectObject(hdc,hpen);
Rectangle(hdc,(j+12)*20+10,(i+2)*20,(j+12)*20+18+10,(i+2)*20+18);////  画出下一个OBJECT的图形
}
else
{
SelectObject(hdc,hpenw);
Rectangle(hdc,(j+12)*20+10,(i+2)*20,(j+12)*20+18+10,(i+2)*20+18);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
if((MM||mm)&&!rus.GameOver&&!IsShining)
{
for(int i=1;i<11;i++)
for(int j=1;j<21;j++)
if(rus.MAP[i][j])
{
SelectObject(hdc,hpen);
Rectangle(hdc,i*20,(j-1)*20,i*20+18,(j-1)*20+18);
}                                                               //画出当前MAP的状态
else
{
SelectObject(hdc,hpenw);
Rectangle(hdc,i*20,(j-1)*20,i*20+18,(j-1)*20+18);
}
mm=0;
MM=false;

}
///////////////////////////////////////////////////////////////////////////////////////////////////////////

if(IsDeformation)
{
SelectObject(hdc,hpenw);
for(int i=0;i<4;i++)
Rectangle(hdc,rus.TempPoint[i].x*20,(rus.TempPoint[i].y-1)*20,rus.TempPoint[i].x*20+18,(rus.TempPoint[i].y-1)*20+18);
IsDeformation=false;
}//变形后画方块覆盖红色方块

if(IsMoveRight)
{
SelectObject(hdc,hpenw);
IsMoveRight=false;
IsMoveLeft=false;
IsMoveVertical=false;
for(int i=0;i<rus.GetHeight();i++)
Rectangle(hdc,rus.rPoint[i].x*20,(rus.rPoint[i].y-1)*20,rus.rPoint[i].x*20+18,(rus.rPoint[i].y-1)*20+18);
//向右移动后画方块覆盖红色方块
}
if(IsMoveLeft)
{
SelectObject(hdc,hpenw);
IsMoveRight=false;
IsMoveLeft=false;
IsMoveVertical=false;
for(int i=0;i<rus.GetHeight();i++)
Rectangle(hdc,rus.lPoint[i].x*20,(rus.lPoint[i].y-1)*20,rus.lPoint[i].x*20+18,(rus.lPoint[i].y-1)*20+18);
//向左移动后画方块覆盖红色方块
}
if(!rus.GameOver)
{
SelectObject(hdc,hpen);
for(int i=0;i<4;i++)
Rectangle(hdc,PointX[i]*20,(PointY[i]-1)*20,PointX[i]*20+18,(PointY[i]-1)*20+18);//画红色方块
}
if(IsMoveVertical)
{
SelectObject(hdc,hpenw);
for(int i=0;i<rus.GetWideth();i++)
Rectangle(hdc,rus.wPoint[i].x*20,(rus.wPoint[i].y-1)*20,rus.wPoint[i].x*20+18,(rus.wPoint[i].y-1)*20+18);
}//向下移动后画方块覆盖红色方块
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(rus.GameOver)
{
rus.DrawGameOver();
SetTimer(hWnd,2,100,NULL);
for(int i=1;i<11;i++)
{
SelectObject(hdc,hpenw);
Rectangle(hdc,i*20,(LINE)*20,i*20+18,(LINE)*20+18);                     //画出GAME OVER
SelectObject(hdc,hpen);
if(rus.MAP[i][LINE+1])
Rectangle(hdc,i*20,(LINE)*20,i*20+18,(LINE)*20+18);
}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
EndPaint(hwnd,&ps);
return 0;
case WM_TIMER:
switch (wParam)
{
case 1:
if(!rus.GameOver&&!puse)//判断游戏是否结束
{
rus.VerticalMove(rus.JudgeBottom());//使OBJECT的x,y坐标向下移动
IsMoveVertical=true;

InvalidateRect(hwnd,NULL,FALSE);//重画窗口
}
else
KillTimer(hwnd,1);
break ;
case 2:
if(rus.GameOver)
if(LINE<19)
{LINE++;InvalidateRect(hwnd,NULL,FALSE);}//重画窗口
else
{KillTimer(hwnd,2);LINE=0;}

break ;
case 3:
if(rus.NUM)
{
if(num<4)
{
num++;
InvalidateRect(hwnd,NULL,FALSE);//重画窗口
}
else
{
GoOn();
InvalidateRect(hwnd,NULL,FALSE);//重画窗口
KillTimer(hwnd,3);
num=1;
IsShining=false;
rus.NUM=0;
}
}

}
return 0;

case WM_KEYDOWN:

switch(wParam)
{
case VK_UP:
if(!puse&&!rus.GameOver)
{IsDeformation=rus.Deformation();//判断是否变形
MM=1;}
break;

case VK_DOWN:
if(!rus.GameOver&&!puse)
{
IsMoveVertical=true;
rus.VerticalMove(rus.JudgeBottom());
}
break;
case VK_RIGHT:
if(!rus.GameOver&&!puse)
{
if(!rus.JudgeRight())//如果到达右边界判断OBJECT的左边的MAP中是否有方块
IsMoveRight=true;
rus.RightMove(!rus.JudgeRight());
}
break;
case VK_LEFT:
if(!rus.GameOver&&!puse)
{
if(!rus.JudgeLeft())//如果到达右边界判断OBJECT的左边的MAP中是否有方块
IsMoveLeft=true;
rus.LeftMove(!rus.JudgeLeft());
}
break;

}

InvalidateRect(hwnd,NULL,FALSE);

return 0;
case WM_DESTROY:
KillTimer(hwnd,1);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
void time1(UINT TIMe)
{
Time=TIMe;
KillTimer(hWnd,1);
SetTimer(hWnd,1,Time,NULL);
Mm=1;
}
void PUSEGAME()
{
if(!rus.GameOver)
puse=true;
}
void GoOn()
{
if(!rus.GameOver)
{
SetTimer(hWnd,1,Time,NULL);
puse=false;
}
}
void ReStartGame()
{
rus.Initialize_MAP();
rus.Initialize_OBJECT();
rus.InitializeXY();
rus.CheckDownWhite();
IsMoveRight=false;
IsMoveLeft=false;
IsMoveVertical=true;
IsDeformation=false;
puse=false;
rus.SCORE=0;
MM=0;
mm=1;
Mm=1;
rus.GameOver=0;
SetTimer(hWnd,1,Time,NULL);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息