⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eluosi.cpp

📁 俄罗斯方块小游戏的源码 只实现了基本的功能
💻 CPP
字号:
// eluosi.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include "SmallBox.h"
#include "Game.h"
#include <crtdbg.h>
#include <stdio.h>
#if _DEBUG
#define DEBUG_NEW _CRTIMP new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif
// Global Variables:
HINSTANCE hInst;
HICON hdisable = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(ICON_DISMOVE));
HICON hmove = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(ICON_ENABLE));
HICON hbg = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(ICON_BG));									// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text
CSmallBox g_bgbox[g_GameBgCol][g_GameBgRow];
BOOL g_gamestate = FALSE;
CGame game;


// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
void OnKeyDown(HWND hwnd, int nVKey, LPARAM lParam);
// VOID CALLBACK TimerProc(
//   HWND hwnd,         // handle to window
//   UINT uMsg,         // WM_TIMER message
//   UINT_PTR idEvent,  // timer identifier
//   DWORD dwTime       // current system time
//);


int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	// TODO: Place code here.
	MSG msg;
	//HACCEL hAccelTable;
	// Initia   lize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_ELUOSI, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);
	
	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}
	
	//hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_ELUOSI);
	
	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		//if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		//{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
		//	}
	}
	
	return msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;
	
	wcex.cbSize = sizeof(WNDCLASSEX); 
	
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_ELUOSI);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_ELUOSI;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
	
	return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;
	
	hInst = hInstance; // Store instance handle in our global variable
	
	hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
	
	if (!hWnd)
	{
		return FALSE;
	}
	
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
	
	return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int x = 0;
    int y = 0;
    HICON hicon;
	int wmId, wmEvent;
	PAINTSTRUCT ps;
    RECT rc;
	HDC hdc = NULL;
	char szscore[10];
	//TCHAR szHello[MAX_LOADSTRING];
	//LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
	
	switch (message) 
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam); 
		wmEvent = HIWORD(wParam); 
		switch (wmId)
		{
		case IDM_EXIT:
			DestroyWindow(hWnd);
			ReleaseDC(hWnd,hdc);
			break;
		case GAMESTART:
			game.InitGame(g_bgbox);
            game.score = 0;
			GetClientRect(hWnd, &rc);
			InvalidateRect(hWnd, &rc, FALSE);
			game.SetOver(FALSE);
			g_gamestate = TRUE;
			
			
			
			SetTimer(hWnd,1,300,(TIMERPROC) NULL);
			
			
			ReleaseDC(hWnd,hdc);
			break;
		default:
			ReleaseDC(hWnd,hdc);
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
        case WM_CREATE:
            game.WriteShape();
			//             game.InitGame(g_bgbox);
            ReleaseDC(hWnd,hdc);
			break;
        case WM_KEYDOWN:
            OnKeyDown(hWnd,wParam,lParam);
            ReleaseDC(hWnd,hdc);
            break;
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			RECT rt;
			GetClientRect(hWnd, &rt);
            if(g_gamestate == TRUE)
            {
				game.GamePaint(g_bgbox,hdc);
                //ICONINFO incoinfo;
				for(int x = 0;x < g_GameBgCol-1; x++)
				{
					for(int y = 0;y < g_GameBgRow-1;y++)
					{
						switch(g_bgbox[x][y].GetState())
						{
						case MOVE:
							hicon = hmove;
							break;
						case DISABLEMOVE:
							hicon = hdisable;
							break;
						case BACKGROUND:
							hicon = hbg;
							break;
						default:
							break;
						}
						//GetIconInfo(hicon,&incoinfo);
						DrawIconEx(hdc,x*16,
							y*16,
							hicon,16,
							16,0,NULL,DI_NORMAL);
					}
				}
				DeleteObject(hicon);
                
				// DrawTextEx(hdc,"nihao",strlen("nihao"),&rt,DT_CENTER,NULL);
            }
			EndPaint(hWnd, &ps);
            ReleaseDC(hWnd,hdc);
            hdc = NULL;
			break;
		case WM_DESTROY:
            KillTimer(hWnd,1);
            DeleteObject(hbg);
            DeleteObject(hmove);
            DeleteObject(hdisable);
            ReleaseDC(hWnd,hdc);
			PostQuitMessage(0);
			break;
        case WM_TIMER:
			if(game.Disabletag == TRUE)
			{
				game.GreateBox(hdc,hmove);
				game.Disabletag = FALSE;
			}
			
            if(game.GetOver() == TRUE)
            {KillTimer(hWnd,1);
			MessageBox(NULL,"游戏结束","GAMEOVER",MB_OK);
			
			break;
			
            }
            GetClientRect(hWnd,&rc);
            hdc = GetDC(hWnd);
			sprintf(szscore,"游戏分数:%d",game.GetScore());
			DrawText(hdc,szscore,strlen(szscore),&rc,DT_CENTER);
			if(game.IsCanDisable(g_bgbox,game.GetCurX(),game.GetCurY()) == TRUE)
            {
                game.DisableBox(hWnd,hdc,g_bgbox,game.GetCurX(),game.GetCurY(),hmove);
				
				
                game.DrawBox(hdc,hdisable);
                game.ReleaseBox(hWnd,g_bgbox,game.GetCurX(),game.GetCurY());
                game.SetIndex(0);
                game.Disabletag = TRUE;
                //game.GreateBox(hdc,hmove);
                InvalidateRect(hWnd,&rc,FALSE);
				
                
            }
            if(game.IsCanMOVE(g_bgbox,game.GetCurX(),game.GetCurY() + 1) == TRUE)
            {
                game.DrawBox(hdc,hbg);
                game.SetCurY(game.GetCurY() + 1);
                game.DrawBox(hdc,hmove);
            }
            
            ReleaseDC(hWnd,hdc);
            
			break;
		default:
            ReleaseDC(hWnd,hdc);
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for about box.
// LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
// {
// 	switch (message)
// 	{
// 		case WM_INITDIALOG:
// 				return TRUE;
// 
// 		case WM_COMMAND:
// 			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
// 			{
// 				EndDialog(hDlg, LOWORD(wParam));
// 				return TRUE;
// 			}
// 			break;
// 	}
//     return FALSE;
// }
void OnKeyDown(HWND hwnd, int nVKey, LPARAM lParam)
{
    HDC hdc = NULL;
    hdc = GetDC(hwnd);
    switch(nVKey)
    {
    case VK_UP:
        if(game.IsCanChange(g_bgbox,game.GetCurX(),game.GetCurY()) == TRUE)
        {
            game.DrawBox(hdc,hbg);
            game.ChangeBox(g_bgbox,game.GetCurX(),game.GetCurY());
            game.DrawBox(hdc,hmove);
        }
        else
        {
            //MessageBox(NULL,NULL,NULL,MB_OK);
        }
        ReleaseDC(hwnd,hdc);
        hdc  = NULL;
		break;
    case VK_RIGHT:
        if(game.IsCanMOVE(g_bgbox,game.GetCurX()+1,game.GetCurY()) == TRUE)
        {
            game.DrawBox(hdc,hbg);
            game.SetCurX(game.GetCurX() + 1);
            game.DrawBox(hdc,hmove);
        }
        ReleaseDC(hwnd,hdc);
        hdc = NULL;
		break;
    case VK_LEFT:
        if(game.IsCanMOVE(g_bgbox,game.GetCurX()-1,game.GetCurY()) == TRUE)
        {
            game.DrawBox(hdc,hbg);
            game.SetCurX(game.GetCurX() - 1);
            game.DrawBox(hdc,hmove);
        }
        ReleaseDC(hwnd,hdc);
        hdc = NULL;
        break;
    case VK_DOWN:
        if(game.IsCanMOVE(g_bgbox,game.GetCurX(),game.GetCurY() + 1) == TRUE)
        {
            game.DrawBox(hdc,hbg);
            game.SetCurY(game.GetCurY() + 1);
            game.DrawBox(hdc,hmove);
        }
        ReleaseDC(hwnd,hdc);
        hdc = NULL;
        break;
    default:
        ReleaseDC(hwnd,hdc);
        hdc = NULL;
    }
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -