본문 바로가기
코스웨어/15년 스마트컨트롤러

20151119- 강동조 개인업무일지 PushPush

by 알 수 없는 사용자 2015. 11. 19.
728x90
반응형

 

소스 코드(작성중)

#include <windows.h>

#include "smart.h"

#include "resource.h"


LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

HINSTANCE g_hInst;

LPSTR lpszClass = L"1119 - PushPush";


LRESULT Msg_OnDESTROY(HWND, WPARAM, LPARAM);

LRESULT Msg_OnCREATE(HWND, WPARAM, LPARAM);

LRESULT Msg_OnPAINT(HWND, WPARAM, LPARAM);

LRESULT Msg_OnKEYDOWN(HWND, WPARAM, LPARAM);

void loadMap();


stMsgMap MSGMAP[] = {   // 자주 쓰는 것을 위로 올린다.

{ WM_CREATE, Msg_OnCREATE },

{ WM_KEYDOWN, Msg_OnKEYDOWN },

{ WM_PAINT, Msg_OnPAINT },

{ WM_DESTROY, Msg_OnDESTROY },

{ WM_NULL, 0 }

};


UINT uiStage;   // 게임 스테이지


static int ixPos;   // 캐릭터 이동 좌표

static int iyPos;


static HDC MemDC;


static HBITMAP GroundBitmap;// 배경

static HBITMAP myBitmap;

static HBITMAP DownBitmap;   // 1 아래

static HBITMAP LeftBitmap;   // 2 왼쪽

static HBITMAP RightBitmap;   // 3 오른쪽

static HBITMAP UpBitmap;   // 4 위

static HBITMAP HERO;      // 5 정면

static HBITMAP Load;      // 8 길

static HBITMAP Dot; //7번

static HBITMAP Box;         // 9 공

UINT uiDotNum; //점의 개수 

UINT uiScore;

WCHAR cTitle[200];


UCHAR ucStageMap[ALLSTAGE][yFRAME][xFRAME + 1] = {

{

"                ",

"                ",

"##  @         ###",

"##    ##   #  ###",

"##    ##   #  ###",

"##    ##   #  ###",

"##    ##   #  ###",

"##    ##   #  ###",

"##               ",

"   .  ######     ",

}

,

{

"#########   ###",

"##     @B   ###",

"##    ###   ###",

"####  ####  ###",

"#  #  #. #  ###",

"#  #        ###",

"#  #           ",

"#  #######   ##",

"#   .    B     ",

"###############",

}

};


UCHAR ucMap[yFRAME][xFRAME + 1];


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)GetStockObject(WHITE_BRUSH);

WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);

WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);

WndClass.hInstance = hInstance;

WndClass.lpfnWndProc = (WNDPROC)WndProc;

WndClass.lpszClassName = lpszClass;

WndClass.lpszMenuName = NULL;

WndClass.style = CS_HREDRAW | CS_VREDRAW;

RegisterClass(&WndClass);


hWnd = CreateWindow(lpszClass,

lpszClass,

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT,

CW_USEDEFAULT,

xWINSIZE,

yWINSIZE,

NULL,

(HMENU)NULL,

hInstance,

NULL);

ShowWindow(hWnd, nCmdShow);


while (GetMessage(&Message, 0, 0, 0))

{

TranslateMessage(&Message);

DispatchMessage(&Message);

}

return Message.wParam;

}


LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)

{

stMsgMap * stpMap = MSGMAP;


ixPos;


while (WM_NULL != (*stpMap).uiMsg)

{

if ((*stpMap).uiMsg == iMessage)

{

return (((*stpMap).fp)(hWnd, wParam, lParam));

}

// MessageMap의 위치 이동

++stpMap;

}


return(DefWindowProc(hWnd, iMessage, wParam, lParam));

}


LRESULT Msg_OnCREATE(HWND hWnd, WPARAM wParam, LPARAM lParam)

{

HDC hdc;


loadMap();


hdc = GetDC(hWnd);   // 화면출력 함수

MemDC = CreateCompatibleDC(hdc);   // 메모리 DC에서 비트맵을 읽어온 후 화면 DC로 복사

// 비트맵을 읽어온 후 이 비트맵을 메모리 DC에 선택해 준다

DownBitmap = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP1));   // 아래 캐릭터

RightBitmap = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP2));   // 오른쪽 캐릭터

LeftBitmap = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP3));   // 왼쪽 캐릭터

UpBitmap = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP4));   // 위 캐릭터

GroundBitmap = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP6));   // 밖 배경 

Load = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP5));   // 길

Dot = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP7));   // 공

Box = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP8));   // 공

SelectObject(MemDC, HERO);   // 복구

ReleaseDC(hWnd, hdc);   // 화면출력 종료 함수

myBitmap = DownBitmap;


return 0;

}


LRESULT Msg_OnPAINT(HWND hWnd, WPARAM wParam, LPARAM lParam)

{

HDC hdc;

PAINTSTRUCT ps;

int iCntX;   // 배경 카운트

int iCntY;


hdc = BeginPaint(hWnd, &ps);


for (iCntX = 0; iCntX < xFRAME; ++iCntX)   // 배경 가로

{

for (iCntY = 0; iCntY < yFRAME; ++iCntY)   // 배경 세로

{

if ('#' == ucMap[iCntY][iCntX])

{

SelectObject(MemDC, GroundBitmap);

}


else if (' ' == ucMap[iCntY][iCntX])

{

SelectObject(MemDC, Load);

}


else if ('@' == ucMap[iCntY][iCntX])

{

SelectObject(MemDC, myBitmap);   // 이동방향 캐릭터 준비

}


else if ('.' == ucMap[iCntY][iCntX])

{

SelectObject(MemDC, Dot);   // 홀

}

else if ('B' == ucMap[iCntY][iCntX])

{

SelectObject(MemDC, Box);   // 박스

}

BitBlt(hdc, (iCntX * xTILE), (iCntY * yTILE), xTILE, yTILE, MemDC, 0, 0, SRCCOPY);

}   // 배경 복사 , 화면 DC로 출력

}


EndPaint(hWnd, &ps);

wsprintf(cTitle, L"stage: [%d]    KeyCount: [%d]", (uiStage + 1), uiScore);

SetWindowText(hWnd, cTitle);

return 0;

}


LRESULT Msg_OnKEYDOWN(HWND hWnd, WPARAM wParam, LPARAM lParam)

{


int iCntX;

int iCntY;

int iRet;

UINT uiDotCount;

RECT stArea;


stArea.left = ixPos * xTILE;

stArea.right = (ixPos + 2) * xTILE;

stArea.bottom = iyPos * yTILE;

stArea.top = (iyPos + 2) * yTILE;


switch (wParam)

{

case VK_LEFT:   //   좌

uiScore++;

myBitmap = LeftBitmap;   // 이동 모션 삽입

if (' ' == ucMap[iyPos][ixPos - 1])   // 경계검사

{

ucMap[iyPos][ixPos - 1] = '@';   // @ 삽입 , 캐릭터 이동

ucMap[iyPos][ixPos] = ucStageMap[uiStage][iyPos][ixPos];   // 배경유지

ucMap[iyPos][ixPos] = ' ';   // ' '삽입 , 이동전 캐릭터 삭제

--ixPos;   // 배열에 +1 해서 48 픽셀 이동

stArea.left = (ixPos - 1)* xTILE;

}

else if (('B' == ucMap[iyPos][ixPos - 1]) & (' ' == ucMap[iyPos][ixPos - 2] | '.' == ucMap[iyPos][ixPos - 2]))   //이동방향 박스 검사

{

ucMap[iyPos][ixPos - 1] = '@';   // @ 삽입 , 캐릭터 이동

ucMap[iyPos][ixPos] = ucStageMap[uiStage][iyPos][ixPos];   // 배경유지

ucMap[iyPos][ixPos] = ' ';   // ' '삽입 , 이동전 캐릭터 삭제

--ixPos;   // 배열에 +1 해서 48 픽셀 이동

stArea.left = (ixPos - 1)* xTILE;

ucMap[iyPos][ixPos - 1] = 'B';


if ('B' == ucMap[iyPos][ixPos - 1] & '.' == ucMap[iyPos][ixPos - 1])

{

break;

}

}

break;


case VK_RIGHT:   // 우

uiScore++;

myBitmap = RightBitmap;

if (' ' == ucMap[iyPos][ixPos + 1])

{

ucMap[iyPos][ixPos + 1] = '@';

ucMap[iyPos][ixPos] = ucStageMap[uiStage][iyPos][ixPos];

ucMap[iyPos][ixPos] = ' ';

++ixPos;

stArea.right = (ixPos + 2) * xTILE;

}

else if ('B' == ucMap[iyPos][ixPos + 1] & (' ' == ucMap[iyPos][ixPos + 2] | '.' == ucMap[iyPos][ixPos + 2]))   //이동방향 박스 검사

{

ucMap[iyPos][ixPos + 1] = '@';   // @ 삽입 , 캐릭터 이동

ucMap[iyPos][ixPos] = ucStageMap[uiStage][iyPos][ixPos];   // 배경유지

ucMap[iyPos][ixPos] = ' ';   // ' '삽입 , 이동전 캐릭터 삭제

++ixPos;   // 배열에 +1 해서 48 픽셀 이동

stArea.right = (ixPos + 2) * xTILE;

ucMap[iyPos][ixPos + 1] = 'B';

}

break;


case VK_UP:   // 위

uiScore++;

myBitmap = UpBitmap;   // 이동 모션 삽입

if (' ' == ucMap[iyPos - 1][ixPos])

{

ucMap[iyPos - 1][ixPos] = '@';

ucMap[iyPos-1][ixPos] = ucMap[iyPos][ixPos];

ucMap[iyPos][ixPos] = ' ';

--iyPos;

stArea.bottom = (iyPos-2) * yTILE;

}

else if ('B' == ucMap[iyPos - 1][ixPos] & (' ' == ucMap[iyPos - 2][ixPos] | '.' == ucMap[iyPos - 2][ixPos]))   //이동방향 박스 검사

{

ucMap[iyPos - 1][ixPos] = '@';   // @ 삽입 , 캐릭터 이동

ucMap[iyPos][ixPos] = ucStageMap[uiStage][iyPos][ixPos];   // 배경유지

ucMap[iyPos][ixPos] = ' ';   // ' '삽입 , 이동전 캐릭터 삭제

--iyPos;   // 배열에 +1 해서 48 픽셀 이동

stArea.bottom = (iyPos - 2) * yTILE;

ucMap[iyPos - 1][ixPos] = 'B';

}

break;


case VK_DOWN:   // 아래

uiScore++;

myBitmap = DownBitmap;

if (' ' == ucMap[iyPos + 1][ixPos]) //캐릭터 이동

{

ucMap[iyPos + 1][ixPos] = '@';

ucMap[iyPos][ixPos] = ucStageMap[uiStage][iyPos][ixPos];

ucMap[iyPos][ixPos] = ' ';

++iyPos;

stArea.top = (iyPos + 2) * yTILE;

}

else if ('B' == ucMap[iyPos + 1][ixPos] & (' ' == ucMap[iyPos + 2][ixPos] | '.' == ucMap[iyPos + 2][ixPos]))   //이동방향 박스 검사

{

ucMap[iyPos + 1][ixPos] = '@';   // @ 삽입 , 캐릭터 이동

ucMap[iyPos][ixPos] = ucStageMap[uiStage][iyPos][ixPos];   // 배경유지

ucMap[iyPos][ixPos] = ' ';   // ' '삽입 , 이동전 캐릭터 삭제

++iyPos;   // 배열에 +1 해서 48 픽셀 이동

stArea.top = (iyPos + 2) * yTILE;

ucMap[iyPos + 1][ixPos] = 'B';

}

break;


case VK_HOME: //이판이 다시하고 싶을때 누르는 키

iRet = MessageBox(hWnd, L"다시 시작하겠습니까?", "Push", MB_YESNO);

if (IDOK)

{

loadMap();

InvalidateRect(hWnd, NULL, TRUE);

return 0;

}

}

uiDotCount = 0;

for (iCntX = 0; iCntX < xFRAME; ++iCntX)

{

for (iCntY = 0; iCntY < yFRAME; ++iCntY)

{

if ('B' == ucMap[iCntY][iCntX])

{

if ('.' == ucStageMap[uiStage][iCntY][iCntX])

{

uiDotCount++;

}

}


}

}


InvalidateRect(hWnd, &stArea, FALSE);   // WM_PAINT 호출

if (uiDotCount == uiDotNum)

{

MessageBox(hWnd, L"성공 하였습니다", L"PUSH", MB_YESNOCANCEL);

uiStage++;

if (ALLSTAGE <= uiStage)

{

MessageBox(hWnd, L"끝판을 꺠셨습니다", L"PUSH", MB_OK);

SendMessage(hWnd, WM_DESTROY, 0, 0);

return 0;

}

loadMap();

InvalidateRect(hWnd, NULL, TRUE);

}

return 0;

}


LRESULT Msg_OnDESTROY(HWND hWnd, WPARAM wParam, LPARAM lParam)

{

DeleteObject(HERO);   // 메모리 종료

DeleteObject(LeftBitmap);

DeleteObject(RightBitmap);

DeleteObject(UpBitmap);

DeleteObject(DownBitmap);

DeleteObject(GroundBitmap);

DeleteObject(Load);

DeleteObject(Box);

DeleteDC(MemDC);


PostQuitMessage(0);

return 0;

}


void loadMap()

{

int iCntX;

int iCntY;


uiDotNum = 0;

uiScore = 0;


for (iCntX = 0; iCntX < xFRAME; ++iCntX)

{

for (iCntY = 0; iCntY < yFRAME; ++iCntY)

{

ucMap[iCntY][iCntX] = ucStageMap[uiStage][iCntY][iCntX];

if ('.' == ucMap[iCntY][iCntX])

{

uiDotNum++;

}

if ('@' == ucMap[iCntY][iCntX])

{

ixPos = iCntX;

iyPos = iCntY;

}

}

}


return;

}

 

 

 

 

 

 

제일 위에 보게되면 몇스테이지인지 나오며 방향키를 누를떄마다 KeyCount가 +가됩니다 높을수록 점수가 낮습니다

 

 

728x90