2008-5-8 13:28
530761333
sdk下的俄罗斯方块源码
[size=5]windows程序设计的实习练手之物[/size]
[attach]7075[/attach]
代码文件主要有两个,一个是主程序,一个是方块阵列.
[b]MonyerRect.cpp[/b]
[size=2][color=#ff6600]#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "rect.h"[/color][/size]
[size=2][color=#ff6600]#define RectRow 10//列数
#define RectLine 20//行数
#define RectWidth 30//每方格宽度
#define RectLeft 100//距左边距离
#define RectTop 50//距右边距离[/color][/size]
[size=2][color=#ff6600]int BoxEnd = 0;//本次移动是否结束1
int BoxTimes = 0;//下落次数
int BoxMove = 3;//左右距离
int RectRandom = rand()%7;//小方格随机
int RectOldRandom = rand()%7;//主方格随机
int RectArray[RectLine][RectRow];//显示数组
int RectStatic[RectLine][RectRow];//静态方格数组
int RectSmall[4][4];//小方格数组
int Line,Row,i,j;
int OldBoxMove;//移动前左右距离
int BoxTrack;//最后操作方向,0为横向,1为纵向
int RectKill=0;//已满格的行
int sharp=0;//变换形状
int RectKillTimes = 0;//一次消除方块行数
int Score=0;//分数
POINT p1,p2;//绘制时用到
char ScoreShow[] = "您目前的分数为:" ;[/color][/size]
[size=2][color=#ff6600]LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int MoveRectBox(HWND);
int doRectKill();[/color][/size]
[size=2][color=#ff6600]LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hDC;
HBRUSH hBrush;
HPEN hPen;
PAINTSTRUCT PtStr;
char str[10];[/color][/size]
[size=2][color=#ff6600]switch(message)
{
case WM_TIMER:
if(BoxEnd==1)//若本次方块已落下
{
BoxEnd = 0;
BoxTimes = 0;//下落距离清零
sharp = 0;
BoxMove = 3;
RectOldRandom = RectRandom;
RectRandom = rand()%7;//随机方块
doRectKill();
}
else//本次方块下落中
{
BoxTimes++;//增加下落距离
BoxTrack = 1;//纵向
}
InvalidateRect(hWnd,NULL,0);
break;
case WM_PAINT:
hDC = BeginPaint(hWnd,&PtStr);
//初始化主面板数据为0
for(Line=0;Line<RectLine;Line++)
{
for(int Row=0;Row<RectRow;Row++)
{
RectArray[Line][Row] = 0;
}
}[/color][/size]
[size=2][color=#ff6600] //初始化小面板数据为0
for(Line=0;Line<4;Line++)
{
for(int Row=0;Row<4;Row++)
{
RectSmall[Line][Row] = 0;
}
}[/color][/size]
[size=2][color=#ff6600] //显示分数
TextOut(hDC,0,0,ScoreShow,lstrlen(ScoreShow) );
itoa(Score,str,10);
TextOut(hDC,150,0,str,lstrlen(str) );
//**************************************************************************************
//为RectArray赋值
for(Line=0;Line<RectLine;Line++)//将RectStatic赋给RectArray
{
for(int Row=0;Row<RectRow;Row++)
{
RectArray[Line][Row] = RectStatic[Line][Row];
}
}[/color][/size]
[size=2][color=#ff6600] for(i=0;i<4;i++)//左右边界限定
{
Line = Rect_Box[RectOldRandom][sharp][i][0]+BoxTimes;//获取Box行位置
Row = Rect_Box[RectOldRandom][sharp][i][1]+BoxMove;//获取Box列位置[/color][/size]
[size=2][color=#ff6600] if(Row<0)//左边界限制
{
BoxMove++;
Row++;
break;
}
if(Row>RectRow-1)//右边界限制
{
BoxMove--;
Row--;
break;
}[/color][/size]
[size=2][color=#ff6600] if(RectArray[Line][Row]==1)//发生碰撞?
{
if(!BoxTrack)//横向
{
BoxMove = OldBoxMove;
break;
}
}[/color][/size]
[size=2][color=#ff6600] }[/color][/size]
[size=2][color=#ff6600] for(i=0;i<4;i++)//下边界限定
{
Line = Rect_Box[RectOldRandom][sharp][i][0]+BoxTimes;//获取Box行位置
Row = Rect_Box[RectOldRandom][sharp][i][1]+BoxMove;//获取Box列位置[/color][/size]
[size=2][color=#ff6600] if(Line>RectLine-1)//如果下落超过底
{
BoxTimes--;//BoxTimes回滚
Line--;
BoxEnd = 1;
break;
}[/color][/size]
[size=2][color=#ff6600] if(RectArray[Line][Row]==1)//如果即将下落位置已经拥有
{
if(BoxTrack)//纵向
{
BoxTimes--;
BoxEnd = 1;//本次方块移动结束[/color][/size]
[size=2][color=#ff6600] if(BoxTimes==-1)//Game Over本次游戏结束
{
for(Line=0;Line<RectLine;Line++)//重新开始
{
for(int Row=0;Row<RectRow;Row++)
{
RectStatic[Line][Row] = 0;
}
}
Score = 0;
BoxEnd = 1;
return 0;
}
break;
}
}
}[/color][/size]
[size=2][color=#ff6600] if(BoxEnd)//如果方块停止,将Box存入RectStatic
{
for(j=0;j<4;j++)
{
RectStatic[
Rect_Box[RectOldRandom][sharp][j][0]+BoxTimes
][
Rect_Box[RectOldRandom][sharp][j][1]+BoxMove
] = 1;
}
return 0;
}
for(i=0;i<4;i++)//将Box赋给RectArray
{
Line = Rect_Box[RectOldRandom][sharp][i][0]+BoxTimes;//获取Box行位置
Row = Rect_Box[RectOldRandom][sharp][i][1]+BoxMove;//获取Box列位置
RectArray[Line][Row]=1;
}[/color][/size]
[size=2][color=#ff6600]//*************************************************************************************
//为RectSmall赋值
for(i=0;i<4;i++)
{
RectSmall[Rect_Box[RectRandom][0][i][0]][Rect_Box[RectRandom][0][i][1]]=1;
}[/color][/size]
[size=2][color=#ff6600] /*通过RectArray数组显示主面板*/
for(Line=0;Line<RectLine;Line++)
{
for(Row=0;Row<RectRow;Row++)
{
p1.x = RectLeft + Row * RectWidth;
p1.y = RectTop + Line * RectWidth;
p2.x = p1.x + RectWidth;
p2.y = p1.y + RectWidth;
if(RectArray[Line][Row]==0)
{
hBrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
hPen = (HPEN)GetStockObject(BLACK_PEN);
}
else
{
hBrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
hPen = (HPEN)GetStockObject(WHITE_PEN);
}
SelectObject(hDC,hPen);
SelectObject(hDC,hBrush);
Rectangle(hDC,p1.x,p1.y,p2.x,p2.y);
}
}[/color][/size]
[size=2][color=#ff6600] //通过RectSmall数据显示小面板
for(Line=0;Line<4;Line++)
{
for(Row=0;Row<4;Row++)
{
if(RectSmall[Line][Row]==0)
{
hBrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
hPen = (HPEN)GetStockObject(BLACK_PEN);
}
else
{
hBrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
hPen = (HPEN)GetStockObject(WHITE_PEN);
}
SelectObject(hDC,hPen);
SelectObject(hDC,hBrush);
Rectangle(
hDC,
RectRow * RectWidth + 4*RectWidth + Row * RectWidth/3,
RectTop + Line * RectWidth/3,
RectRow * RectWidth + 4*RectWidth + Row * RectWidth/3 + RectWidth/3,
RectTop + Line * RectWidth/3 + RectWidth/3
);
}
}[/color][/size]
[size=2][color=#ff6600] EndPaint(hWnd,&PtStr);
break;[/color][/size]
[size=2][color=#ff6600]case WM_KEYDOWN:
{
switch(wParam)
{
case VK_UP:
if(sharp<3) sharp++;
else sharp=0;
InvalidateRect(hWnd,NULL,0);
break;
case VK_DOWN:
BoxTimes++;
BoxTrack = 1;//纵向
InvalidateRect(hWnd,NULL,0);
break;
case VK_LEFT:
OldBoxMove = BoxMove;
BoxMove--;
BoxTrack = 0;//横向
InvalidateRect(hWnd,NULL,0);
break;
case VK_RIGHT:
OldBoxMove = BoxMove;
BoxMove++;
BoxTrack = 0;//横向
InvalidateRect(hWnd,NULL,0);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
}[/color][/size]
[size=2][color=#ff6600]case WM_CREATE:
break;[/color][/size]
[size=2][color=#ff6600]case WM_DESTROY:
PostQuitMessage(0);
return 0;[/color][/size]
[size=2][color=#ff6600]default:
return DefWindowProc(hWnd,message,wParam,lParam);[/color][/size]
[size=2][color=#ff6600]}
return 0;
}[/color][/size]
[size=2][color=#ff6600]int MoveRectBox(HWND hWnd)
{
InvalidateRect(hWnd,NULL,0);
return 0;
}[/color][/size]
[size=2][color=#ff6600]int doRectKill()
{
RectKillTimes = 0;
for(Line=0;Line<RectLine;Line++)//检查RectStatic,消去行
{
for(int Row=0;Row<RectRow;Row++)
{
if(RectStatic[Line][Row]==0)//如果存在空块,则退出
{
RectKill = 0;
break;
}
else
{
RectKill = Line;
}
}
if(RectKill!=0)//发现满行,消除
{
for(i=RectKill;i>1;i--)
{
for(j=0;j<RectRow;j++)
{
RectStatic[i][j] = RectStatic[i-1][j];
}
}
Score += 100 + RectKillTimes * 25;//一次消除多行,加分
RectKillTimes++;
}
}
return 0;
}[/color][/size]
[size=2][color=#ff6600]/*******************************************WinMain*******************************************/
int WINAPI WinMain
(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
HWND hWnd;
WNDCLASS WndCls;
MSG msg;[/color][/size]
[size=2][color=#ff6600]WndCls.cbClsExtra = 0;
WndCls.cbWndExtra = 0;
WndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndCls.hCursor = LoadCursor(NULL,IDC_ARROW);
WndCls.hIcon = LoadIcon(NULL,IDI_APPLICATION);
WndCls.hInstance = hInstance;
WndCls.lpfnWndProc = WndProc;
WndCls.lpszClassName = "Monyer Rect";
WndCls.lpszMenuName = NULL;
WndCls.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&WndCls);[/color][/size]
[size=2][color=#ff6600]hWnd = CreateWindow
(
"Monyer Rect",
"Monyer Rect",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
800,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);[/color][/size]
[size=2][color=#ff6600]srand((unsigned)time(NULL));
SetTimer(hWnd,1,500,NULL);
for(Line=0;Line<RectLine;Line++)
{
for(int Row=0;Row<RectRow;Row++)
{
RectStatic[Line][Row] = 0;
}
}[/color][/size]
[size=2][color=#ff6600]while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}[/color][/size]
[b]rect.h[/b]
[size=2][color=#ff6600]int Rect_Box[7][4][4][2] =
{
{//A//Rect_Box[0]
{//A0//Rect_Box[0][0]
{0,1},{1,1},{2,1},{3,1}
},
{//A1//Rect_Box[0][1]
{1,0},{1,1},{1,2},{1,3}
},
{//A2//Rect_Box[0][2]
{0,1},{1,1},{2,1},{3,1}
},
{//A3//Rect_Box[0][3]
{1,0},{1,1},{1,2},{1,3}
}
},
{//B//Rect_Box[1]
{//B0//Rect_Box[1][0]
{0,1},{1,1},{2,1},{1,2}
},
{//B1//Rect_Box[1][1]
{1,0},{0,1},{1,1},{2,1}
},
{//B2//Rect_Box[1][2]
{1,0},{1,1},{1,2},{0,1}
},
{//B3//Rect_Box[1][3]
{1,0},{1,1},{1,2},{2,1}
}
},
{//C//Rect_Box[2]
{//C0//Rect_Box[2][0]
{0,0},{0,1},{1,0},{1,1}
},
{//C1//Rect_Box[2][1]
{0,0},{0,1},{1,0},{1,1}
},
{//C2//Rect_Box[2][2]
{0,0},{0,1},{1,0},{1,1}
},
{//C3//Rect_Box[2][3]
{0,0},{0,1},{1,0},{1,1}
}
},
{//D//Rect_Box[3]
{//D0//Rect_Box[3][0]
{0,1},{1,1},{2,1},{2,2}
},
{//D1//Rect_Box[3][1]
{0,0},{0,1},{1,1},{2,1}
},
{//D2//Rect_Box[3][2]
{2,0},{2,1},{2,2},{1,2}
},
{//D3//Rect_Box[3][3]
{1,0},{1,1},{1,2},{2,0}
}
},
{//E//Rect_Box[4]
{//E0//Rect_Box[4][0]
{0,2},{1,2},{2,2},{2,1}
},
{//E1//Rect_Box[4][1]
{1,1},{1,2},{1,3},{2,3}
},
{//E2//Rect_Box[4][2]
{0,2},{0,3},{1,2},{2,2}
},
{//E3//Rect_Box[4][3]
{0,1},{1,1},{1,2},{1,3}
}
},
{//F//Rect_Box[5]
{//F0//Rect_Box[5][0]
{0,1},{1,1},{1,2},{2,2}
},
{//F1//Rect_Box[5][1]
{1,2},{1,3},{2,1},{2,2}
},
{//F2//Rect_Box[5][2]
{0,1},{1,1},{1,2},{2,2}
},
{//F3//Rect_Box[5][3]
{1,2},{1,3},{2,1},{2,2}
}
},
{//G//Rect_Box[6]
{//G0//Rect_Box[6][0]
{0,2},{1,1},{1,2},{2,1}
},
{//G1//Rect_Box[6][1]
{1,0},{1,1},{2,1},{2,2}
},
{//G2//Rect_Box[6][2]
{0,2},{1,1},{1,2},{2,1}
},
{//G3//Rect_Box[6][3]
{1,0},{1,1},{2,1},{2,2}
}
}
};[/color][/size]
vc6.0下编译通过。