看来论坛里的俄罗斯方块,因为没有注释和运行不了,自己用自己的想法写了一个
看来论坛里的俄罗斯方块,因为没有注释和运行不了,自己用自己的想法写了一个。写的很糙,加了注释,多多谅解#include <windows.h> //第一次写没有优化 vs2012编写
#include <windowsx.h>
#include "strsafe.h"
#define BLOCKX 30
#define BLOCKY 30
#define RED SetDCBrushColor(hdc, RGB(255, 0, 0)) //预定义是为了下面少写代码
#define YELLOW SetDCBrushColor(hdc, RGB(232, 232, 0))
#define GREEN SetDCBrushColor(hdc, RGB(0, 255, 0))
#define BLUE SetDCBrushColor(hdc, RGB(0, 0, 255))
#define ORANGE SetDCBrushColor(hdc, RGB(255, 128, 0))
#define PURPLE SetDCBrushColor(hdc, RGB(255, 0, 255))
#define CYAN SetDCBrushColor(hdc, RGB(0, 255, 255))
typedef struct Blockall //定义了两个结构:上面的是方块的两个坐标, BOOL ShowBlock用于确定画不画方块
{
short Topx; //Color 存放方块的颜色
short Topy;
short Bottomx;
short Bottomy;
BOOLShowBlock;
int Color;
}WDWCOORD;
typedef struct ShapeStruct//Color 是为了方便索引颜色, shape 是变形式索引数组位置 A,B,C,D,四个是方块的位置
{
int color;
short Shape;
short InitialA;
short InitialB;
short InitialC;
short InitialD;
}SHAPE;
int ChangeBlock(int, int);
int AllBlock(HDC, WDWCOORD *, SHAPE *, int, int, int);
void DrawBlock(HDC, WDWCOORD *);
void EraseBlock();
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("MyWindows");
MSG msg;
HWND hwnd;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("该程序需要在 Windows NT 下才能运行"),szAppName,MB_ICONERROR);
}
hwnd = CreateWindow(szAppName,TEXT("Russia.block"),
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
720,
620,
NULL,
NULL,
hInstance,
NULL);
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)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static WDWCOORD Wdw;
int i, j, x, y;
static int MoveLR = 0, MoveUD = 0, bType;
static SHAPE Shape = {0, 0, {4, 0},{4, 1},{4, 2},{4, 3},
0, 1, {3, 0},{4, 0},{5, 0},{6, 0}, //条形 red
1, 2, {3, 0},{4, 0},{4, 1},{5, 1},
1, 3, {4, 1},{5, 1},{4, 2},{5, 0},//左折 yellow
2, 4, {3, 1},{4, 0},{4, 1},{5, 0},
2, 5, {4, 0},{4, 1},{5, 1},{5, 2},//右折 greed
3, 6, {4, 2},{5, 0},{5, 1},{5, 2},
3, 6, {3, 0},{4, 0},{5, 0},{5, 1},
3, 6, {4, 0},{4, 1},{4, 2},{5, 0},
3, 6, {3, 0},{3, 1},{4, 1},{5, 1},//左L形 blue
4, 7, {4, 0},{4, 1},{4, 2},{5, 2},
4, 7, {3, 1},{4, 1},{5, 0},{5, 1},
4, 7, {4, 0},{5, 0},{5, 1},{5, 2},
4, 7, {3, 0},{3, 1},{4, 0},{5, 0},//右L形 purple
5, 8, {4, 0},{4, 1},{5, 0},{5, 1}, //正方形 orange
6, 9, {3, 1},{4, 0},{4, 1},{5, 1},//上凸形 cyan
6, 9, {4, 1},{5, 0},{5, 1},{5, 2},
6, 9, {3, 0},{4, 0},{4, 1},{5, 0},
6, 9, {4, 0},{4, 1},{4, 2},{5, 1}};
switch (message)
{
case WM_CREATE:
SetTimer(hwnd,1,1000,NULL);
GetClientRect(hwnd, &rect);
bType = rand() % 7;
x = (rect.right - rect.left) * 2 / 9 + 8;
y = rect.top + 8;
for (j = 0; j < 19; j++) //给二维数组结构体赋值, 分别是方块的两个坐标和初始化showblock的值
{
for (i = 0; i < 10; i++)
{
Wdw.Topx = x + BLOCKX * i;
Wdw.Topy =y + BLOCKY * j;
Wdw.Bottomx =x + BLOCKX * (i + 1);
Wdw.Bottomy = y + BLOCKY * (j + 1);
Wdw.ShowBlock = 0;
}
}
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
SetDCPenColor(hdc, RGB(205, 205, 205));
SelectObject(hdc, GetStockObject(DC_PEN));
MoveToEx(hdc, (rect.right - rect.left) * 2 / 9, rect.top + 1, NULL);// 画出游戏区域的长方形区域
LineTo(hdc, (rect.right - rect.left) * 6 / 9 , rect.top + 1);
LineTo(hdc,(rect.right - rect.left) * 6 / 9 , rect.bottom - 1);
LineTo(hdc,(rect.right - rect.left) * 2 / 9 , rect.bottom - 1);
LineTo(hdc,(rect.right - rect.left) * 2 / 9 , rect.top + 1);
SetDCPenColor(hdc, RGB(250, 250, 250));
SelectObject(hdc, GetStockObject(DC_PEN));
MoveToEx(hdc, (rect.right - rect.left) * 2 / 9 + 1, rect.top + 2, NULL);
LineTo(hdc, (rect.right - rect.left) * 6 / 9 -1 , rect.top + 2);
LineTo(hdc,(rect.right - rect.left) * 6 / 9 - 1, rect.bottom - 2);
LineTo(hdc,(rect.right - rect.left) * 2 / 9 + 1, rect.bottom - 2);
LineTo(hdc,(rect.right - rect.left) * 2 / 9 + 1, rect.top + 2);
SetDCPenColor(hdc, RGB(205, 205, 205));
SelectObject(hdc, GetStockObject(DC_PEN));
MoveToEx(hdc, (rect.right - rect.left) * 2 / 9 + 2, rect.top + 3, NULL);
LineTo(hdc, (rect.right - rect.left) * 6 / 9 - 2, rect.top + 3);
LineTo(hdc,(rect.right - rect.left) * 6 / 9 - 2, rect.bottom - 3);
LineTo(hdc,(rect.right - rect.left) * 2 / 9 + 2, rect.bottom - 3);
LineTo(hdc,(rect.right - rect.left) * 2 / 9 + 2, rect.top + 3);
SetDCPenColor(hdc, RGB(255, 255, 255));
SelectObject(hdc, GetStockObject(DC_PEN));
switch (Shape.color) //开始画方块
{
case 0:
RED;SelectObject(hdc, GetStockObject(DC_BRUSH));
break;
case 1:
YELLOW;SelectObject(hdc, GetStockObject(DC_BRUSH));
break;
case 2:
GREEN;SelectObject(hdc, GetStockObject(DC_BRUSH));
break;
case 3:
BLUE;SelectObject(hdc, GetStockObject(DC_BRUSH));
break;
case 4:
PURPLE;SelectObject(hdc, GetStockObject(DC_BRUSH));
break;
case 5:
ORANGE;SelectObject(hdc, GetStockObject(DC_BRUSH));
break;
case 6:
CYAN;SelectObject(hdc, GetStockObject(DC_BRUSH));
break;
}
Rectangle(hdc, Wdw.InitialA + MoveLR].InitialA + MoveUD].Topx,
Wdw.InitialA + MoveLR].InitialA + MoveUD].Topy,
Wdw.InitialA + MoveLR].InitialA + MoveUD].Bottomx,
Wdw.InitialA + MoveLR].InitialA + MoveUD].Bottomy);
Rectangle(hdc, Wdw.InitialB + MoveLR].InitialB + MoveUD].Topx,
Wdw.InitialB + MoveLR].InitialB + MoveUD].Topy,
Wdw.InitialB + MoveLR].InitialB + MoveUD].Bottomx,
Wdw.InitialB + MoveLR].InitialB + MoveUD].Bottomy);
Rectangle(hdc, Wdw.InitialC + MoveLR].InitialC + MoveUD].Topx,
Wdw.InitialC + MoveLR].InitialC + MoveUD].Topy,
Wdw.InitialC + MoveLR].InitialC + MoveUD].Bottomx,
Wdw.InitialC + MoveLR].InitialC + MoveUD].Bottomy);
Rectangle(hdc, Wdw.InitialD + MoveLR].InitialD + MoveUD].Topx,
Wdw.InitialD + MoveLR].InitialD + MoveUD].Topy,
Wdw.InitialD + MoveLR].InitialD + MoveUD].Bottomx,
Wdw.InitialD + MoveLR].InitialD + MoveUD].Bottomy);
if (AllBlock(hdc, Wdw, Shape, bType, MoveUD, MoveLR)) //根据返回值确定是否消除和堆叠方块
{
EraseBlock(Wdw, Shape, bType, MoveUD);
bType = rand() % 7; //取随机数 0 - 6 (这里是伪随机数,要取随机是用函数srand()取种子)
switch (bType)
{
case 0:
bType += 0;
break;
case 1:
bType += 1;
break;
case 2:
bType += 2;
break;
case 3:
bType += 3;
break;
case 4:
bType += 6;
break;
case 5:
bType += 9; //5 + 9 = 14
break;
case 6:
bType += 9; //6 + 9 = 15
}
MoveLR = 0; MoveUD = 0; // 置0 在开始的地方重绘一个方块
}
DrawBlock(hdc, Wdw);
EndPaint(hwnd,&ps);
return 0;
case WM_TIMER:
MoveUD += 1;
InvalidateRect(hwnd, NULL, TRUE);
return 0;
case WM_KEYDOWN:
switch (LOWORD(wParam))
{
case VK_UP:
bType = ChangeBlock(Shape.Shape, bType);
break;
case VK_DOWN:
MoveUD += 1;
;
break;
case VK_LEFT:
MoveLR -= 1;
break;
case VK_RIGHT:
MoveLR += 1;
break;
default:
break;
}
MoveLR = max(-(int)Shape.InitialA, min(MoveLR, 9 - (int)Shape.InitialD)); // 根据方块的位置来确定方块不会越界
InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
int ChangeBlock(int a, int b)
{
switch (a) //改变方块是根据改变结构体SHAPE Shape short InitialABCD 来索引数组 static WDWCOORD Wdw
{
case 0:
return 1;
break;
case 1:
return 0;
break;
case 2:
return 3;
break;
case 3:
return 2;
break;
case 4:
return 5;
break;
case 5:
return 4;
break;
case 6:
if (b < 9)
{
return b + 1;
break;
}
else
{
return 6;
break;
}
case 7:
if (b < 13)
{
return b + 1;
break;
}
else
{
return 10;
break;
}
case 8:
return 14;
break;
case 9:
if (b < 18)
{
return b + 1;
break;
}
else
{
return 15;
break;
}
}
return 0;
};
int AllBlock(HDC hdc, WDWCOORD *a, SHAPE *b, int c, int d, int e) //根据WDWCOORD Wdw里面的showblock值 和 color 值来绘制屏幕上的方块
{
if ((((b + c) ->InitialA + d) == 18 || ((b + c) ->InitialB + d) == 18 //判断是不是到达底部
|| ((b + c) ->InitialC + d) == 18 || ((b + c) ->InitialD + d) == 18)||
(((a + (((b + c) -> InitialA + e) * 19)) + ((b + c) -> InitialA + (d + 1)))->ShowBlock== TRUE) || //判断是不是堆叠方块
(((a + (((b + c) -> InitialB + e) * 19)) + ((b + c) -> InitialB + (d + 1)))->ShowBlock== TRUE) ||
(((a + (((b + c) -> InitialC + e) * 19)) + ((b + c) -> InitialC + (d + 1)))->ShowBlock== TRUE) ||
(((a + (((b + c) -> InitialD + e) * 19)) + ((b + c) -> InitialD + (d + 1)))->ShowBlock== TRUE))
{
((a + (((b + c) -> InitialA + e) * 19)) + ((b + c) -> InitialA + d))->ShowBlock = TRUE; //如果是将坐标的showblock值 置 TRUE
((a + (((b + c) -> InitialB + e) * 19)) + ((b + c) -> InitialB + d))->ShowBlock = TRUE;
((a + (((b + c) -> InitialC + e) * 19)) + ((b + c) -> InitialC + d))->ShowBlock = TRUE;
((a + (((b + c) -> InitialD + e) * 19)) + ((b + c) -> InitialD + d))->ShowBlock = TRUE;
switch((b + c) -> color)
{
case 0:
((a + (((b + c) -> InitialA + e) * 19)) + ((b + c) -> InitialA + d))->Color = 0; //根据结构体Shape.color的值 填入Wdw[][].color
((a + (((b + c) -> InitialB + e) * 19)) + ((b + c) -> InitialB + d))->Color = 0;
((a + (((b + c) -> InitialC + e) * 19)) + ((b + c) -> InitialC + d))->Color = 0;
((a + (((b + c) -> InitialD + e) * 19)) + ((b + c) -> InitialD + d))->Color = 0;
break;
case 1:
((a + (((b + c) -> InitialA + e) * 19)) + ((b + c) -> InitialA + d))->Color = 1;
((a + (((b + c) -> InitialB + e) * 19)) + ((b + c) -> InitialB + d))->Color = 1;
((a + (((b + c) -> InitialC + e) * 19)) + ((b + c) -> InitialC + d))->Color = 1;
((a + (((b + c) -> InitialD + e) * 19)) + ((b + c) -> InitialD + d))->Color = 1;
break;
case 2:
((a + (((b + c) -> InitialA + e) * 19)) + ((b + c) -> InitialA + d))->Color = 2;
((a + (((b + c) -> InitialB + e) * 19)) + ((b + c) -> InitialB + d))->Color = 2;
((a + (((b + c) -> InitialC + e) * 19)) + ((b + c) -> InitialC + d))->Color = 2;
((a + (((b + c) -> InitialD + e) * 19)) + ((b + c) -> InitialD + d))->Color = 2;
break;
case 3:
((a + (((b + c) -> InitialA + e) * 19)) + ((b + c) -> InitialA + d))->Color = 3;
((a + (((b + c) -> InitialB + e) * 19)) + ((b + c) -> InitialB + d))->Color = 3;
((a + (((b + c) -> InitialC + e) * 19)) + ((b + c) -> InitialC + d))->Color = 3;
((a + (((b + c) -> InitialD + e) * 19)) + ((b + c) -> InitialD + d))->Color = 3;
break;
case 4:
((a + (((b + c) -> InitialA + e) * 19)) + ((b + c) -> InitialA + d))->Color = 4;
((a + (((b + c) -> InitialB + e) * 19)) + ((b + c) -> InitialB + d))->Color = 4;
((a + (((b + c) -> InitialC + e) * 19)) + ((b + c) -> InitialC + d))->Color = 4;
((a + (((b + c) -> InitialD + e) * 19)) + ((b + c) -> InitialD + d))->Color = 4;
break;
case 5:
((a + (((b + c) -> InitialA + e) * 19)) + ((b + c) -> InitialA + d))->Color = 5;
((a + (((b + c) -> InitialB + e) * 19)) + ((b + c) -> InitialB + d))->Color = 5;
((a + (((b + c) -> InitialC + e) * 19)) + ((b + c) -> InitialC + d))->Color = 5;
((a + (((b + c) -> InitialD + e) * 19)) + ((b + c) -> InitialD + d))->Color = 5;
break;
case 6:
((a + (((b + c) -> InitialA + e) * 19)) + ((b + c) -> InitialA + d))->Color = 6;
((a + (((b + c) -> InitialB + e) * 19)) + ((b + c) -> InitialB + d))->Color = 6;
((a + (((b + c) -> InitialC + e) * 19)) + ((b + c) -> InitialC + d))->Color = 6;
((a + (((b + c) -> InitialD + e) * 19)) + ((b + c) -> InitialD + d))->Color = 6;
break;
}
return 1; //堆叠了返回 1 否则返回 0
}
else
{
return 0;
}
}
void DrawBlock(HDC hdc, WDWCOORD *a) //画框里的方块根据Wdw[][]里的showblock和color来绘制
{
int i, j;
for (i = 0; i < 19; i++)
{
for (j = 0; j < 10; j++)
{
if ((a + j * 19).ShowBlock == TRUE)
{
switch ((a + j * 19).Color)
{
case 0:
RED;SelectObject(hdc, GetStockObject(DC_BRUSH));
break;
case 1:
YELLOW;SelectObject(hdc, GetStockObject(DC_BRUSH));
break;
case 2:
GREEN;SelectObject(hdc, GetStockObject(DC_BRUSH));
break;
case 3:
BLUE;SelectObject(hdc, GetStockObject(DC_BRUSH));
break;
case 4:
PURPLE;SelectObject(hdc, GetStockObject(DC_BRUSH));
break;
case 5:
ORANGE;SelectObject(hdc, GetStockObject(DC_BRUSH));
break;
case 6:
CYAN;SelectObject(hdc, GetStockObject(DC_BRUSH));
break;
}
SetDCPenColor(hdc, RGB(255, 255, 255));
SelectObject(hdc, GetStockObject(DC_PEN));
Rectangle(hdc,(a + j * 19).Topx, (a + j * 19).Topy,
(a + j * 19).Bottomx, (a + j * 19).Bottomy);
}
}
}
}
void EraseBlock(WDWCOORD *a, SHAPE *b, int c, int d) //消除方块
{
int i, j = 0, k, Blocka = TRUE, Blockb = TRUE, Blockc = TRUE, Blockd = TRUE, Same;
for(i = 0; i < 10; i++)
{
if((((a + (i * 19)) + ((b + c) -> InitialA + d))->ShowBlock) == FALSE)
{
Blocka--;
}
if((((a + (i * 19)) + ((b + c) -> InitialB + d))->ShowBlock) == FALSE)
{
Blockb--;
}
if((((a + (i * 19)) + ((b + c) -> InitialC + d))->ShowBlock) == FALSE)
{
Blockc--;
}
if((((a + (i * 19)) + ((b + c) -> InitialD + d))->ShowBlock) == FALSE)
{
Blockd--;
}
}
if (Blocka == TRUE)
{
for(i = 0; i < 10; i++)
{
((a + (i * 19)) + ((b + c) -> InitialA + d))->ShowBlock = FALSE;
}
for(j = 0; j < ((b + c) -> InitialA + d); j++)
{
for(k = 0; k < 10; k++)
{
((a + (k * 19)) + ((b + c) -> InitialA + (d - j)))->ShowBlock ^= ((a + (k * 19)) + ((b + c) -> InitialA + (d - j - 1)))->ShowBlock;
((a + (k * 19)) + ((b + c) -> InitialA + (d - j)))->Color = ((a + (k * 19)) + ((b + c) -> InitialA + (d - j - 1)))->Color;
((a + (k * 19)) + ((b + c) -> InitialA + (d - j - 1)))->ShowBlock = FALSE;
}
}
}
if (Blockb == TRUE)
{
if(((b + c) -> InitialA + d) != ((b + c) -> InitialB + d))
{
for(i = 0; i < 10; i++)
{
((a + (i * 19)) + ((b + c) -> InitialB + d))->ShowBlock = FALSE;
}
for(j = 0; j < ((b + c) -> InitialB + d); j++)
{
for(k = 0; k < 10; k++)
{
((a + (k * 19)) + ((b + c) -> InitialB + (d - j)))->ShowBlock ^= ((a + (k * 19)) + ((b + c) -> InitialB + (d - j - 1)))->ShowBlock;
((a + (k * 19)) + ((b + c) -> InitialB + (d - j)))->Color = ((a + (k * 19)) + ((b + c) -> InitialB + (d - j - 1)))->Color;
((a + (k * 19)) + ((b + c) -> InitialB + (d - j - 1)))->ShowBlock = FALSE;
}
}
}
}
if (Blockc == TRUE)
{
if(((b + c) -> InitialC + d) != ((b + c) -> InitialA + d) && ((b + c) -> InitialC + d) != ((b + c) -> InitialB + d))
{
for(i = 0; i < 10; i++)
{
((a + (i * 19)) + ((b + c) -> InitialC + d))->ShowBlock = FALSE;
}
for(j = 0; j < ((b + c) -> InitialC + d); j++)
{
for(k = 0; k < 10; k++)
{
((a + (k * 19)) + ((b + c) -> InitialC + (d - j)))->ShowBlock ^=((a + (k * 19)) + ((b + c) -> InitialC + (d - j - 1)))->ShowBlock;
((a + (k * 19)) + ((b + c) -> InitialC + (d - j)))->Color = ((a + (k * 19)) + ((b + c) -> InitialC + (d - j - 1)))->Color;
((a + (k * 19)) + ((b + c) -> InitialC + (d - j - 1)))->ShowBlock = FALSE;
}
}
}
}
if (Blockd == TRUE)
{
if(((b + c) -> InitialD + d) != ((b + c) -> InitialA + d) && ((b + c) -> InitialD + d) != ((b + c) -> InitialB + d)
&& ((b + c) -> InitialD + d) != ((b + c) -> InitialC + d))
{
for(i = 0; i < 10; i++)
{
((a + (i * 19)) + ((b + c) -> InitialD + d))->ShowBlock = FALSE;
}
for(j = 0; j < ((b + c) -> InitialD + d); j++)
{
for(k = 0; k < 10; k++)
{
((a + (k * 19)) + ((b + c) -> InitialD + (d - j)))->ShowBlock ^= ((a + (k * 19)) + ((b + c) -> InitialD + (d - j - 1)))->ShowBlock;
((a + (k * 19)) + ((b + c) -> InitialD + (d - j)))->Color = ((a + (k * 19)) + ((b + c) -> InitialD + (d - j - 1)))->Color;
((a + (k * 19)) + ((b + c) -> InitialD + (d - j - 1)))->ShowBlock = FALSE;
}
}
}
}
}
很厉害啊
页:
[1]