Windows编程实现俄罗斯方块

Windows编程实现俄罗斯方块_第1张图片

Windows编程实现俄罗斯方块_第2张图片

主框架

#include "paint.h"
#include "play.h"

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR lpCmdLine,int nCmdShow)
{
	//1.初始化窗口类 
	WNDCLASSEX wndClass = { 0 };	
	wndClass.cbSize = sizeof(WNDCLASSEX);
	wndClass.cbClsExtra = 0;
	wndClass.cbWndExtra = 0;
	wndClass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
	wndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
	wndClass.hIcon = (HICON)::LoadImage(NULL,L"icon.ico",IMAGE_ICON,0,0,LR_LOADFROMFILE |LR_DEFAULTCOLOR);
	wndClass.hIconSm = NULL;
	wndClass.hInstance = hInstance;
	wndClass.lpfnWndProc = WndProc;
	wndClass.lpszClassName = L"Tetris Game";	//窗口类名
	wndClass.lpszMenuName = 0;
	wndClass.style = CS_HREDRAW | CS_VREDRAW;

	//2.注册窗口
	if (!RegisterClassEx(&wndClass))
	{
		return -1;
	}

	//3.创建窗口
	HWND hwnd = CreateWindow(L"Tetris Game", WINDOWS_TITLE, WS_OVERLAPPEDWINDOW, 100, 30, WINDOWS_HEIGHT, WINDOWS_WIDTH, NULL, NULL, hInstance, NULL);

	//4.窗口的显示 移动 更新
	//MoveWindow(hwnd,250,80,WINDOWS_HEIGHT,WINDOWS_WIDTH,true);
	ShowWindow(hwnd, nCmdShow);
	//UpdateWindow(hwnd);

	PlaySound(L"城之内ミサ-雪花",NULL,SND_ASYNC | SND_FILENAME |SND_LOOP);

	//5.消息循环
	MSG msg = { 0 };
	while (GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	//6. 窗口注销
	UnregisterClass(L"Tetris Game", wndClass.hInstance);    //程序准备结束 注销窗口类
	return 0;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int id[2] = { 0, 1 };
	PAINTSTRUCT paintStruct;
	switch(message)
	{
	case WM_CREATE:				//只产生一次
		//初始化数据
		OnCreate();
		break;
	case WM_TIMER:				//定时器 
		OnTimer(hwnd);
		break;
	case WM_KEYDOWN:
		switch (wParam)			//按键消息
		{
		case VK_RETURN:
			OnReturn(hwnd);
			break;
		case VK_UP:
			OnChangeSqare(hwnd);
			break;
		case VK_DOWN:
			OnDown(hwnd);
			break;
		case VK_LEFT:
			OnLeft(hwnd);
			break;
		case VK_RIGHT:
			OnRight(hwnd);
			break;
		}
		break;
	case WM_PAINT:				//绘画
		BeginPaint(hwnd, &paintStruct);
		OnPaint(hwnd);
		EndPaint(hwnd, &paintStruct);
		break;
	case WM_CLOSE:
		if (6 == MessageBox(hwnd, L"确实要退出?", L"Tetris Game", MB_YESNO))		   //	IDYES == 6  IDNO == 7
		{
			DestroyWindow(hwnd);
		}
		break;
	case WM_DESTROY:
		KillTimer(hwnd, DEF_TIMER1);
		Game_CleanUp(hwnd);		//资源清理
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd, message, wParam, lParam);
	}
}
完整源代码和资源
http://download.csdn.net/detail/zhangxiaoyu_sy/9889256


你可能感兴趣的:(游戏编程)