码迷,mamicode.com
首页 > 其他好文 > 详细

俄罗斯方块

时间:2020-05-06 19:54:34      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:reg   计时器   随机   signed   tla   定时器   main   amp   led   

#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);
}

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

俄罗斯方块

标签:reg   计时器   随机   signed   tla   定时器   main   amp   led   

原文地址:https://www.cnblogs.com/wangmou-233-1024-com/p/12838137.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!