Hwan'Story › @kim-hwan

API로 만들어본 소코반 게임

· (수정 ) · 조회 79

API로 만들어본 소코반 게임

API로 만들어본 소코반 게임

소스코드

//Written by akon47.
#include <Windows.h>
#include "resource.h"
#define MAXSTAGE 5
#define BH 32
#define BW 32
#define REGAME 0

int nx, ny;
int dx = 0, dy = 0;
int nsg = 0;
int nmove = 0;
int tmove = 0;
int time = 0;
int totime = 0;
int ManBit = 4;

HBITMAP hBit[9];
void DrawScreen(HDC hdc);
void DrawBitmap(HDC hdc, int x, int y, HBITMAP hBit);
void move();
void scansg(int s);
BOOL End(void);
void Rstage(void);
void Clear(HWND hWnd);

char ns[18][21];
char Stage[MAXSTAGE][18][21] = {
    {
    "####################",
    "####################",
    "####################",
    "####################",
    "####################",
    "####################",
    "#######.############",
    "####### ############",
    "#######O O.#########",
    "#####. O@ ##########",
    "#########O##########",
    "#########.##########",
    "####################",
    "####################",
    "####################",
    "####################",
    "####################",
    "####################"
    },
    {
    "####################",
    "####################",
    "####################",
    "#####   ############",
    "#####O  ############",
    "#####  O############",
    "###  O O ###########",
    "### # ## ###########",
    "#   # ## #####  ..##",
    "# O  O   @      ..##",
    "##### ### # ##  ..##",
    "#####     ##########",
    "####################",
    "####################",
    "####################",
    "####################",
    "####################",
    "####################"
    },
    {
    "####################",
    "####################",
    "####################",
    "####################",
    "####################",
    "####################",
    "######  #  #########",
    "#####   #@ #########",
    "#####O O O #########",
    "##### O##  #########",
    "##### O # ##########",
    "###.....  ##########",
    "####################",
    "####################",
    "####################",
    "####################",
    "####################",
    "####################"
    },
    {
    "####################",
    "####################",
    "####################",
    "####################",
    "####..  #     ######",
    "####..  # O  O  ####",
    "####..  #O####  ####",
    "####..    @ ##  ####",
    "####..  # #  O #####",
    "######### ##O O ####",
    "###### O  O O O ####",
    "######    #     ####",
    "####################",
    "####################",
    "####################",
    "####################",
    "####################",
    "####################"
    },
    {
    "####################",
    "####################",
    "####################",
    "####################",
    "##########     @####",
    "########## O#O #####",
    "########## O  O#####",
    "###########O O #####",
    "########## O # #####",
    "##....  ## O  O  ###",
    "###...    O  O   ###",
    "##....  ############",
    "####################",
    "####################",
    "####################",
    "####################",
    "####################",
    "####################"
    },
};

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hInst;
HWND hWndMain;
LPCTSTR lpszClass = TEXT("소코반");

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
{
    HWND hWnd;
    MSG Message;
    WNDCLASS WndClass;
    g_hInst = hInstance;

    WndClass.cbClsExtra = 0;
    WndClass.cbWndExtra = 0;
    WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    WndClass.hInstance = hInstance;
    WndClass.lpfnWndProc = WndProc;
    WndClass.lpszClassName = lpszClass;
    WndClass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
    WndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
    WndClass.style = CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&WndClass);

    hWnd = CreateWindow(lpszClass, lpszClass, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 0, 0,
        NULL, (HMENU)NULL, hInstance, NULL);
    ShowWindow(hWnd, nCmdShow);

    while (GetMessage(&Message, NULL, 0, 0))
    {
        TranslateMessage(&Message);
        DispatchMessage(&Message);
    }
    return (int)Message.wParam;
}
BOOL CALLBACK AboutDlgProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
    switch (iMessage)
    {
    case WM_INITDIALOG:
        return TRUE;
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case IDOK:
            EndDialog(hDlg, IDOK);
            return TRUE;
        case IDCANCEL:
            EndDialog(hDlg, IDCANCEL);
            return TRUE;
        }
        break;
    }
    return FALSE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    RECT lcd;
    static RECT rt = { 785, 120, 900, 160 };

    switch (iMessage)
    {
    case WM_CREATE:
        hWndMain = hWnd;
        SetRect(&lcd, 0, 0, 960, BH * 18);
        AdjustWindowRect(&lcd, WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, FALSE);
        SetWindowPos(hWnd, NULL, 0, 0, lcd.right - lcd.left, lcd.bottom - lcd.top,
            SWP_NOMOVE | SWP_NOZORDER);
        hBit[0] = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_WALL));
        hBit[1] = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_GOAL));
        hBit[2] = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_PACK));
        hBit[3] = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_EMPTY));
        hBit[4] = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_MANDOWN));
        hBit[6] = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_MANUP));
        hBit[7] = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_MANLEFT));
        hBit[8] = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_MANRIGHT));
        hBit[5] = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_MAP));
        SetTimer(hWnd, 1, 1000, NULL);
        SetTimer(hWnd, 2, 1000, NULL);
        scansg(nsg);
        return 0;
    case WM_TIMER:
        switch (wParam)
        {
        case 1:
            InvalidateRect(hWndMain, &rt, FALSE);
            time++;
            break;
        case 2:
            InvalidateRect(hWndMain, &rt, FALSE);
            totime++;
            break;
        }
        return 0;
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case ID_NEWGAME:
            nsg = 0;
            Rstage();
            tmove = 0;
            totime = 0;
            time = 0;
            KillTimer(hWnd, 1);
            KillTimer(hWnd, 2);
            SetTimer(hWnd, 1, 1000, NULL);
            SetTimer(hWnd, 2, 1000, NULL);
            break;
        case ID_EXIT:
            DestroyWindow(hWnd);
            break;
        case ID_ABOUT:
            DialogBox(g_hInst, MAKEINTRESOURCE(IDD_ABOUT), hWnd, AboutDlgProc);
            break;
        }
        return 0;
    case WM_KEYDOWN:
        switch (wParam)
        {
        case VK_LEFT:
            dx -= 1;
            ManBit = 7;
            move();
            Clear(hWnd);
            break;
        case VK_RIGHT:
            dx += 1;
            ManBit = 8;
            move();
            Clear(hWnd);
            break;
        case VK_UP:
            dy -= 1;
            ManBit = 6;
            move();
            Clear(hWnd);
            break;
        case VK_DOWN:
            dy += 1;
            ManBit = 4;
            move();
            Clear(hWnd);
            break;
        }
        return 0;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        DrawScreen(hdc);
        EndPaint(hWnd, &ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return (DefWindowProc(hWnd, iMessage, wParam, lParam));
}
void DrawScreen(HDC hdc)
{
    int x, y;
    int iBit;
    TCHAR Message[128];

    for (y = 0; y < 18; y++)
    {
        for (x = 0; x < 20; x++)
        {
            switch (ns[y][x])
            {
            case '#':
                iBit = 0;
                break;
            case '.':
                iBit = 1;
                break;
            case 'O':
                iBit = 2;
                break;
            case ' ':
                iBit = 3;
                break;
            case '@':
                iBit = ManBit;
                nx = x;
                ny = y;
                break;
            }
            DrawBitmap(hdc, x * BW, y * BH, hBit[iBit]);
        }
    }
    DrawBitmap(hdc, 20 * BW, 0, hBit[5]);	// Draw MAP

    wsprintf(Message, TEXT("스 테 이 지 : %d"), nsg + 1);
    TextOut(hdc, 700, 60, Message, lstrlen(Message));
    wsprintf(Message, TEXT("이 동 회 수 : %d"), nmove);
    TextOut(hdc, 700, 80, Message, lstrlen(Message));
    wsprintf(Message, TEXT("총 이동회수 : %d"), tmove);
    TextOut(hdc, 700, 100, Message, lstrlen(Message));
    wsprintf(Message, TEXT("경 과 시 간 : %d 초"), time);
    TextOut(hdc, 700, 120, Message, lstrlen(Message));
    wsprintf(Message, TEXT("총 경과시간 : %d 초"), totime);
    TextOut(hdc, 700, 140, Message, lstrlen(Message));
}
void DrawBitmap(HDC hdc, int x, int y, HBITMAP hBit)
{
    HDC MemDC;
    HBITMAP OldBitmap;
    int bx, by;
    BITMAP bit;

    MemDC = CreateCompatibleDC(hdc);
    OldBitmap = (HBITMAP)SelectObject(MemDC, hBit);

    GetObject(hBit, sizeof(BITMAP), &bit);
    bx = bit.bmWidth;
    by = bit.bmHeight;

    BitBlt(hdc, x, y, bx, by, MemDC, 0, 0, SRCCOPY);

    SelectObject(MemDC, OldBitmap);
    DeleteDC(MemDC);
}
void scansg(int s)
{
    int my, nx;

    for (my = 0; my < 18; my++)
    {
        for (nx = 0; nx < 20; nx++)
            ns[my][nx] = Stage[s][my][nx];
    }
}

void move(void)
{
    InvalidateRect(hWndMain, NULL, FALSE);

    if (ns[ny + dy][nx + dx] != '#')
    {
        if (ns[ny + dy][nx + dx] != 'O' || ns[ny + dy][nx + dx] == '.')
        {
            if (Stage[nsg][ny][nx] != '.')
            {
                ns[ny][nx] = ' ';
                ns[ny + dy][nx + dx] = '@';
                dx = 0;
                dy = 0;
                nmove++;
                tmove++;
            }
            else
            {
                ns[ny][nx] = '.';
                ns[ny + dy][nx + dx] = '@';
                dx = 0;
                dy = 0;
                nmove++;
                tmove++;
            }
        }
        else
        {
            if (ns[ny + 2 * dy][nx + 2 * dx] == ' ' || ns[ny + 2 * dy][nx + 2 * dx] == '.')
            {
                if (Stage[nsg][ny][nx] == '.')
                {
                    ns[ny][nx] = '.';
                    ns[ny + dy][nx + dx] = '@';
                    ns[ny + 2 * dy][nx + 2 * dx] = 'O';
                    dx = 0;
                    dy = 0;
                    nmove++;
                    tmove++;
                    return;
                }
                else
                {
                    ns[ny][nx] = ' ';
                    ns[ny + dy][nx + dx] = '@';
                    ns[ny + 2 * dy][nx + 2 * dx] = 'O';
                    dx = 0;
                    dy = 0;
                    nmove++;
                    tmove++;
                    return;
                }
            }
            dx = 0;
            dy = 0;
            MessageBeep(0);
            return;
        }
    }
    else
    {
        dx = 0;
        dy = 0;
        MessageBeep(0);
        return;
    }
}
BOOL End(void)
{
    int x, y;

    for (y = 0; y < 18; y++)
    {
        for (x = 0; x < 20; x++)
        {
            if (Stage[nsg][y][x] == '.' && ns[y][x] != 'O')
            {
                return FALSE;
            }
        }
    }
    return TRUE;
}
void Clear(HWND hWnd)
{
    TCHAR Message[128];
    if (End())
    {
        wsprintf(Message, TEXT("스테이지 %d 클리어!!") TEXT("다음 스테이지로!"), nsg + 1);
        MessageBox(hWnd, Message, TEXT("클리어!"), MB_OK);
        if (nsg < MAXSTAGE - 1)
            nsg++;
        Rstage();
        KillTimer(hWnd, 1);
        KillTimer(hWnd, 2);
        SetTimer(hWnd, 1, 1000, NULL);
        SetTimer(hWnd, 2, 1000, NULL);
        time = 0;
    }
}
void Rstage(void)
{
    nmove = 0;
    scansg(nsg);
    InvalidateRect(hWndMain, NULL, TRUE);
}

실행결과

관련 게시글