俄罗斯方块

Posted wangmou-233-1024-com

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了俄罗斯方块相关的知识,希望对你有一定的参考价值。

#include<Windows.h>
#include<iostream>
#include<time.h>
using namespace std;
int arr_background[20][10] = { 0 };
int arr_square[2][4] = { 0 };
LRESULT CALLBACK Myluosi(HWND hwnd, UINT nMsg, WPARAM wParam, LPARAM lParam);
void Onpaint(HDC hdc);//画方框
void Oncreat();//初始化
void paintsquare(HDC hmendc);//显示方块
int creatrandomsquare();//产生随机块
void copysquaretoback();//把随即快贴近背景
void Onreturn(HWND hwnd);
void squaredown();//方块下落
void Ontimer(HWND hwnd);//计时器相关函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR lpCmdLine, int nCmdshow)
{

	//初始化窗口类
	WNDCLASSEX wc;
	HWND hwnd;
	MSG msg;//消息结构体

	wc.cbClsExtra = 0;
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
	wc.hCursor = NULL;
	wc.hIcon = NULL;
	wc.hIconSm = NULL;
	wc.hInstance = hInstance;
	wc.lpfnWndProc = Myluosi;
	wc.lpszClassName = "wls";
	wc.lpszMenuName = NULL;
	wc.style = CS_HREDRAW | CS_VREDRAW;
	//注册窗口类对象
	if (RegisterClassEx(&wc) == 0)
	{
		return 0;
	}
	//创建窗口
	hwnd = CreateWindowEx(WS_EX_TOPMOST, "wls", "我罗斯方块", WS_OVERLAPPEDWINDOW, 100, 30, 900, 650, NULL, NULL, hInstance, NULL);
	if (NULL == hwnd)		//窗口句柄
	{
		return 0;
	}
	//显示窗口

	ShowWindow(hwnd, SW_SHOWNORMAL);

	//消息循环

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);//翻译消息

		DispatchMessage(&msg);//分发消息


	}
	return 0;
}
LRESULT CALLBACK Myluosi(HWND hwnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT pt;
	HDC hdc;
	switch (nMsg)
	{
	case WM_CREATE:
		Oncreat();
	break;
	case WM_TIMER:	
		Ontimer(hwnd); 	
		break;
	case WM_PAINT:
		hdc=BeginPaint(hwnd,&pt);
		Onpaint(hdc);
		EndPaint(hwnd,&pt);
		break;
	case WM_KEYDOWN:
	//	GetLastError();
		switch (wParam)
		{
		case VK_RETURN://回车


			GetLastError();
			Onreturn(hwnd);
			break;

		case VK_LEFT://左
			
			break;

		case VK_RIGHT://右
			break;

		case VK_UP://上
			break;

		case VK_DOWN://下
			break;
		}
		break;
	case WM_DESTROY:
		KillTimer(hwnd, 2);
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hwnd, nMsg, wParam, lParam);
}
void Onpaint(HDC hdc) 
{
	HDC hmemdc =CreateCompatibleDC(hdc);
	HBITMAP hbitmapback =CreateCompatibleBitmap(hdc,500,600);
	SelectObject(hmemdc, hbitmapback);
	paintsquare(hmemdc);
	BitBlt(hdc, 0, 0, 300, 600, hmemdc, 0, 0,SRCCOPY);
	DeleteObject(hbitmapback);
	DeleteDC(hmemdc);
}
void Oncreat()
{
	srand((unsigned int )time (NULL));
	creatrandomsquare();
	copysquaretoback();
}
void paintsquare(HDC hmemdc)
{
	int i=0,j=0;
	//画大方框
	Rectangle(hmemdc, 0, 0, 300, 600);
	for (i = 0; i < 20; i++)
	{
		for (j = 0; j < 10; j++)
		{
			if (arr_background[i][j] == 1) 
			{
				Rectangle(hmemdc, 30*j, 30*i, 30+j*30, 30+i*30);
			}
		}
	}

}
int creatrandomsquare()
{
	
	int n = rand() % 7;
	switch (n)
	{
	case 0:
		arr_square[0][0] = 1;arr_square[0][1] = 1;arr_square[0][2] = 0;arr_square[0][3] = 0;
		arr_square[1][0] = 0;arr_square[1][1] = 1;arr_square[1][2] = 1;arr_square[1][3] = 0;
		break;
	case 1:
		arr_square[0][0] = 0; arr_square[0][1] = 1; arr_square[0][2] = 1; arr_square[0][3] = 0;
		arr_square[1][0] = 1; arr_square[1][1] = 1; arr_square[1][2] = 0; arr_square[1][3] = 0;
		break;
	case 2:
		arr_square[0][0] = 1; arr_square[0][1] = 0; arr_square[0][2] = 0; arr_square[0][3] = 0;
		arr_square[1][0] = 1; arr_square[1][1] = 1; arr_square[1][2] = 1; arr_square[1][3] = 0;
		break;
	case 3:
		arr_square[0][0] = 0; arr_square[0][1] = 0; arr_square[0][2] = 1; arr_square[0][3] = 0;
		arr_square[1][0] = 1; arr_square[1][1] = 1; arr_square[1][2] = 1; arr_square[1][3] = 0;
		break;
	case 4:
		arr_square[0][0] = 0; arr_square[0][1] = 1; arr_square[0][2] = 0; arr_square[0][3] = 0;
		arr_square[1][0] = 1; arr_square[1][1] = 1; arr_square[1][2] = 1; arr_square[1][3] = 0;
		break;
	case 5:
		arr_square[0][0] = 1; arr_square[0][1] = 1; arr_square[0][2] = 0; arr_square[0][3] = 0;
		arr_square[1][0] = 1; arr_square[1][1] = 1; arr_square[1][2] = 0; arr_square[1][3] = 0;
		break;
	case 6:
		arr_square[0][0] = 1; arr_square[0][1] = 1; arr_square[0][2] = 1; arr_square[0][3] = 1;
		arr_square[1][0] = 0; arr_square[1][1] = 0; arr_square[1][2] = 0; arr_square[1][3] = 0;
		break;

	}
	return n;
}
void copysquaretoback()
{
	int i, j;
	for (i = 0; i < 2; i++)
	{
		for (j = 0; j < 4; j++)
		{
			arr_background[i][j + 3] = arr_square[i][j];
		}
	}

}
void Onreturn(HWND hwnd)
{
	//打开定时器
	SetTimer(hwnd, 2, 500, NULL);
}
void squaredown()
{
	int i, j;
	for (i = 19; i >= 0; i--)
	{
		for (j = 0; j < 10; j++)
		{
			if (1==arr_background[i][j] )
			{
				arr_background[i + 1][j] =arr_background[i][j];
				arr_background[i][j] = 0;
			}
		}

	}
}
void Ontimer(HWND hwnd)
{
	
	squaredown();
	arr_background;
	HDC hdc = GetDC(hwnd);
	Onpaint(hdc);
	ReleaseDC(hwnd, hdc);
}

注:单人俄罗斯方块做到在方块下落这一步,

以上是关于俄罗斯方块的主要内容,如果未能解决你的问题,请参考以下文章

C# 俄罗斯方块源代码

C++俄罗斯方块

C语言代码俄罗斯方块(yCodeBlocks)?

只会C语言编程还要学哪些才能做俄罗斯方块这样的小游戏?

用Shell编写的俄罗斯方块代码

java写俄罗斯方块啥水平