WinAPI 과제로 Inversus라는 게임을 만들게 되었다. 이 게임은 내가 학교 다니면서 맨 처음으로 만들어 봤던 게임이다.
자료구조 수업을 함께 들으면서 연결리스트와 배열같은 자료구조들에 대한 이해를 높임과 동시에 진행했던 프로젝트라서 퀄리티가 크게 높지는 않지만 나름 열심히 했었던 프로젝트이다. 참고로 그래픽리소스는 하나도 쓰지 않고 그냥 기본으로 제공되는 사각형만 사용하여 만들었다.
[코드]
#include <Windows.h>
#include <math.h>
#define PIE 3.141592
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hinst;
LPCTSTR lpszClass = TEXT("First");
float CheckDistance(int x, int y, int mx, int my);
BOOL CheckCir(int x, int y, int mx, int my, int size);
BOOL CheckMap(int x, int y, int pr, int mx, int my, int mr);
struct map {
int x, y;
int r;
BOOL bow;//black or white
};
struct Player
{
int x, y;
int r;
};
struct Roundbullet
{
double x, y;
double r;
double rad;
double angle;
};
struct Bullet
{
int x, y;
int dir; //방향 1. 오른쪽 2. 왼쪽 3.위쪽 4.아래쪽
int r;
Bullet * next;
};
struct Enemy
{
int x, y;
int r;
Enemy * next;
};
struct Bomb
{
int x, y;
int r;
Bomb * next;
};
void Createappear(int x, int y);//생성위치를 받아옴
void DeleteAppear(Bomb * delAppear);
void CreateBullet(int px, int py, int dir);
Enemy * CreateEnemy();
void DeleteBullet(Bullet * delbullet);
void DeleteEnemy(Enemy * delenemy);
void CreateBomb(int x, int y);//폭발위치를 받아옴;
void DeleteBomb(Bomb * delBomb);
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow) //h의 의미? = 핸들
{
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; //프로시저함수의 이름
WndClass.lpszClassName = lpszClass; //윈도우 클래스 내용의 이름
WndClass.lpszMenuName = NULL; // 메뉴바 이름
WndClass.style = CS_HREDRAW | CS_VREDRAW; //윈도우 출력 스타일
// 여기까지 WNDCLASS구조체의 변수들에 값을 대입
RegisterClass(&WndClass); // 윈도우 클래스를 운영체제에 등록
hWnd = CreateWindow(lpszClass, "Windows Program 1-1", WS_OVERLAPPEDWINDOW, 0, 0, 1100, 1100, NULL, (HMENU)NULL, hinstance, NULL);
//CreateWindow(클래스 이름,타이틀 이름,윈도우 스타일, 윈도우 위치좌표x , y,윈도우 가로크기, 윈도우 세로크기, 부모 윈도우 핸들, 메뉴 핸들, 응용프로그램 인스턴스, 생성 윈도우 정보(NULL))
ShowWindow(hWnd, nCmdShow); //윈도우의 화면 출력
UpdateWindow(hWnd); //OS에 WM_PAINT메시지 전송
while (GetMessage(&Message, NULL, 0, 0))
{
//윈도우 프로시저에서 PostQuitMessage()를 호출할때 종료됨.
TranslateMessage(&Message);
DispatchMessage(&Message);
}//메시지루프를 돌림.
return (int)Message.wParam;
}
Bullet * bulletHead = NULL;
Bullet * temp = NULL;
Enemy * enemyHead = NULL;
Enemy * etemp = NULL;
Bomb * bombHead = NULL;
Bomb * btemp = NULL;
Bomb * appearHead = NULL;
Bomb * appeartemp = NULL;
void Reset();
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam) //CALLBACK
//(윈도우 핸들, 처리해야 할 메시지의 값,입력장치의 값1,2)
{
PAINTSTRUCT ps;
HDC hdc, hmemdc;
HBRUSH hbrush, oldbrush;
HPEN hpen, oldpen;
static int dir;
static Roundbullet roundbullet[6];
static int appearcount;
static map map[20][20];
static int deathCount = 3;
static int Reload = 0;
static int makeenemy = 0;
static int movecount;
static char buf[20];
static int bomb = 0;
static int combo = 0;
static int makeSpeed = 0;
static Enemy * tempenemy;
static Player player;
static RECT rect;
static int bulletcount;
static int score = 0;
HBITMAP hBitmap, OldBitmap; // HBITMAP은 대략 종이를 의미한다. 종이 2장 선언
switch (iMessage) //메시지의 번호
{
case WM_CREATE:
GetClientRect(hWnd, &rect);
player.x = 400;
player.y = 400;
player.r = 30;
bulletcount = 6;
SetTimer(hWnd, 1, 5, NULL);
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
map[i][j].x = 25 + 50 * j;
map[i][j].y = 25 + i * 50;
map[i][j].r = 25;
map[i][j].bow = TRUE;
}
}//맵초기화
for (int i = 5; i < 15; i++)
{
for (int j = 5; j < 15; j++)
{
map[i][j].bow = FALSE;
}
}
for (int i = 0; i < 6; i++)
{
roundbullet[i].x = (player.r - 5)*cos(PIE*(i + 1) / 3) + player.x;
roundbullet[i].y = (player.r - 5)*sin(PIE*(i + 1) / 3) + player.y;
roundbullet[i].r = 10;
}
InvalidateRect(hWnd, NULL, FALSE);
break;
case WM_KEYDOWN:
if (wParam == VK_LEFT)
{
if (bulletcount > 0) {
CreateBullet(player.x, player.y, 1);
bulletcount--;
}
}
else if (wParam == VK_RIGHT)
{
if (bulletcount > 0) {
CreateBullet(player.x, player.y, 2);
bulletcount--;
}
}
else if (wParam == VK_UP)
{
if (bulletcount > 0) {
CreateBullet(player.x, player.y, 3);
bulletcount--;
}
}
else if (wParam == VK_DOWN)
{
if (bulletcount > 0) {
CreateBullet(player.x, player.y, 4);
bulletcount--;
}
}
else if (wParam == VK_SPACE)
{
if (score > 100) {
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
map[i][j].bow = FALSE;
}
}
score -= 100;
}
}
break;
case WM_TIMER:
switch (wParam)
{
case 1:
makeSpeed++;
makeenemy++;
if (makeSpeed > 10000)
{
makeenemy++;
}
if (makeSpeed > 20000)
{
makeenemy++;
}
if (makeSpeed > 30000)
{
makeenemy++;
}
Reload++;
if (GetAsyncKeyState('W') & 0x8000)
{
player.y -= 5;
}
if (GetAsyncKeyState('S') & 0x8000)
{
player.y += 5;
}
if (GetAsyncKeyState('A') & 0x8000)
{
player.x -= 5;
}
if (GetAsyncKeyState('D') & 0x8000)
{
player.x += 5;
}
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
if (CheckMap(player.x, player.y, player.r, map[i][j].x, map[i][j].y, map[i][j].r) == TRUE && map[i][j].bow == TRUE)
{
if (GetAsyncKeyState('W') & 0x8000)
{
player.y += 5;
}
if (GetAsyncKeyState('S') & 0x8000)
{
player.y -= 5;
}
if (GetAsyncKeyState('A') & 0x8000)
{
player.x += 5;
}
if (GetAsyncKeyState('D') & 0x8000)
{
player.x -= 5;
}
}
}
}
for (int i = 0; i < 6; i++)
{
roundbullet[i].angle += 2;
roundbullet[i].rad = 3.14 * roundbullet[i].angle / 180.0;
}
if (Reload == 80)
{
if (bulletcount < 6) {
bulletcount++;
}
Reload = 0;
}
//몬스터 생성
if (makeenemy == 100)
{
tempenemy = CreateEnemy();
Createappear(tempenemy->x, tempenemy->y);
SetTimer(hWnd, 3, 5, NULL);
makeenemy = 0;
}
for (etemp = enemyHead; etemp != NULL; etemp = etemp->next) {
if (player.x > etemp->x)
{
etemp->x++;
}
else
{
etemp->x--;
}
if (player.y > etemp->y)
{
etemp->y++;
}
else
{
etemp->y--;
}
}
//총알지워짐.
for (temp = bulletHead; temp != NULL; temp = temp->next)
{
if (temp->dir == 1) {
temp->x -= 10;
if (temp->x < rect.left)
{
DeleteBullet(temp);
}
for (etemp = enemyHead; etemp != NULL; etemp = etemp->next)
{
if (CheckCir(temp->x, temp->y, etemp->x, etemp->y, 2 * temp->r) == TRUE)
{
combo += 10;
score += combo;
SetTimer(hWnd, 2, 1, NULL);
CreateBomb(temp->x, temp->y);
DeleteBullet(temp);
}
}
}
else if (temp->dir == 2)
{
temp->x += 10;
if (temp->x > rect.right)
{
DeleteBullet(temp);
}
for (etemp = enemyHead; etemp != NULL; etemp = etemp->next)
{
if (CheckCir(temp->x, temp->y, etemp->x, etemp->y, 2 * temp->r) == TRUE)
{
combo += 10;
score += combo;
SetTimer(hWnd, 2, 1, NULL);
CreateBomb(temp->x, temp->y);
DeleteBullet(temp);
}
}
}
else if (temp->dir == 3)
{
temp->y -= 10;
if (temp->x < rect.top)
{
DeleteBullet(temp);
}
for (etemp = enemyHead; etemp != NULL; etemp = etemp->next)
{
if (CheckCir(temp->x, temp->y, etemp->x, etemp->y, 2 * temp->r) == TRUE)
{
combo += 10;
score += combo;
SetTimer(hWnd, 2, 1, NULL);
CreateBomb(temp->x, temp->y);
DeleteBullet(temp);
}
}
}
else if (temp->dir == 4)
{
temp->y += 10;
if (temp->x > rect.bottom)
{
DeleteBullet(temp);
}
for (etemp = enemyHead; etemp != NULL; etemp = etemp->next)
{
if (CheckCir(temp->x, temp->y, etemp->x, etemp->y, 2 * temp->r) == TRUE)
{
combo += 10;
score += combo;
SetTimer(hWnd, 2, 1, NULL);
CreateBomb(temp->x, temp->y);
DeleteBullet(temp);
}
}
}
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
if (CheckCir(temp->x, temp->y, map[i][j].x, map[i][j].y, temp->r * 3) && map[i][j].bow == TRUE)
{
map[i][j].bow = FALSE;
}
}
}
}
for (etemp = enemyHead; etemp != NULL; etemp = etemp->next)
{
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
if (CheckCir(etemp->x, etemp->y, map[i][j].x, map[i][j].y, map[i][j].r - 10) && map[i][j].bow == FALSE)
{
map[i][j].bow = TRUE;
}
if (CheckCir(etemp->x, etemp->y, player.x, player.y, etemp->r))
{
player.x = rand() % 1000;
player.y = rand() % 1000;
player.r = 30;
bulletcount = 6;
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
map[i][j].bow = FALSE;
}
}
combo = 0;
deathCount--;
CreateBomb(player.x, player.y);
SetTimer(hWnd, 2, 1, NULL);
}
}
}
}
if (deathCount < 0)
{
KillTimer(hWnd, 1);
KillTimer(hWnd, 2);
MessageBox(hWnd, "사망", "끝!", MB_OK);
PostQuitMessage(0);
}
break;
case 2:
bomb++;
if (bomb < 10)
{
for (btemp = bombHead; btemp != NULL; btemp = btemp->next)
{
btemp->r += 5;
}
}
else if (bomb < 15 && bomb>9)
{
for (btemp = bombHead; btemp != NULL; btemp = btemp->next)
{
btemp->r -= 10;
}
}
else
{
for (btemp = bombHead; btemp != NULL; btemp = btemp->next)//폭발반경내의 타일을 모두 하얀색으로 바꿈
{
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
if (CheckCir(map[i][j].x, map[i][j].y, btemp->x, btemp->y, map[i][j].r * 2) == TRUE && map[i][j].bow == TRUE)
{
map[i][j].bow = FALSE;
}
}
}
//폭발반경내에 있는 몹들을 모두 죽임
for (etemp = enemyHead; etemp != NULL; etemp = etemp->next)
{
if (CheckCir(btemp->x, btemp->y, etemp->x, etemp->y, btemp->r))
{
DeleteEnemy(etemp);
}
}
bomb = 0;
KillTimer(hWnd, 2);
DeleteBomb(btemp);
}
}
break;
case 3:
appearcount++;
if (appearcount < 10)
{
for (btemp = appearHead; btemp != NULL; btemp = btemp->next)
{
btemp->r -= 10;
}
}
if (appearcount == 10)
{
for (btemp = appearHead; btemp != NULL; btemp = btemp->next)
{
appearcount = 0;
KillTimer(hWnd, 3);
DeleteAppear(btemp);
}
}
break;
}
InvalidateRect(hWnd, NULL, FALSE);
break;
case WM_PAINT: //메시지의 처리
hdc = BeginPaint(hWnd, &ps);
hmemdc = CreateCompatibleDC(hdc); // hMemDC 에 기존 DC (hdc)에 맞게 새 DC 생성
hBitmap = CreateCompatibleBitmap(hdc, rect.right, rect.bottom); // crt 규격대로 종이 생성
OldBitmap = (HBITMAP)SelectObject(hmemdc, hBitmap); // 종이 교체
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
if (map[i][j].bow == TRUE) {
hpen = CreatePen(PS_SOLID, 1, RGB(125, 125, 125));
oldpen = (HPEN)SelectObject(hmemdc, hpen);
hbrush = CreateSolidBrush(RGB(0, 0, 0));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, map[i][j].x - map[i][j].r, map[i][j].y - map[i][j].r, map[i][j].x + map[i][j].r, map[i][j].y + map[i][j].r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
DeleteObject(hpen);
SelectObject(hmemdc, oldpen);
}
else
{
hpen = CreatePen(PS_SOLID, 1, RGB(125, 125, 125));
oldpen = (HPEN)SelectObject(hmemdc, hpen);
hbrush = CreateSolidBrush(RGB(255, 255, 255));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, map[i][j].x - map[i][j].r, map[i][j].y - map[i][j].r, map[i][j].x + map[i][j].r, map[i][j].y + map[i][j].r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
DeleteObject(hpen);
SelectObject(hmemdc, oldpen);
}
}
}//맵
if (bulletHead != NULL) {
for (temp = bulletHead; temp != NULL; temp = temp->next)
{
if (temp != NULL) {
if (temp->dir == 1)
{
hbrush = CreateSolidBrush(RGB(125, 125, 125));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r + 40, temp->y - temp->r, temp->x + 40 + temp->r, temp->y + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
hbrush = CreateSolidBrush(RGB(100, 100, 100));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r + 30, temp->y - temp->r, temp->x + 30 + temp->r, temp->y + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
hbrush = CreateSolidBrush(RGB(50, 50, 50));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r + 20, temp->y - temp->r, temp->x + 20 + temp->r, temp->y + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
hbrush = CreateSolidBrush(RGB(30, 30, 30));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x + 10 - temp->r, temp->y - temp->r, temp->x + 10 + temp->r, temp->y + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
hbrush = CreateSolidBrush(RGB(0, 0, 0));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r, temp->y - temp->r, temp->x + temp->r, temp->y + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
}
else if (temp->dir == 2)
{
hbrush = CreateSolidBrush(RGB(125, 125, 125));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r - 40, temp->y - temp->r, temp->x - 40 + temp->r, temp->y + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
hbrush = CreateSolidBrush(RGB(100, 100, 100));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r - 30, temp->y - temp->r, temp->x - 30 + temp->r, temp->y + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
hbrush = CreateSolidBrush(RGB(50, 50, 50));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r - 20, temp->y - temp->r, temp->x - 20 + temp->r, temp->y + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
hbrush = CreateSolidBrush(RGB(30, 30, 30));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - 10 - temp->r, temp->y - temp->r, temp->x - 10 + temp->r, temp->y + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
hbrush = CreateSolidBrush(RGB(0, 0, 0));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r, temp->y - temp->r, temp->x + temp->r, temp->y + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
}
else if (temp->dir == 3)
{
hbrush = CreateSolidBrush(RGB(125, 125, 125));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r, temp->y + 40 - temp->r, temp->x + temp->r, temp->y + 40 + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
hbrush = CreateSolidBrush(RGB(100, 100, 100));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r, temp->y + 30 - temp->r, temp->x + temp->r, temp->y + 30 + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
hbrush = CreateSolidBrush(RGB(50, 50, 50));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r, temp->y + 20 - temp->r, temp->x + temp->r, temp->y + 20 + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
hbrush = CreateSolidBrush(RGB(30, 30, 30));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r, temp->y + 10 - temp->r, temp->x + temp->r, temp->y + 10 + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
hbrush = CreateSolidBrush(RGB(0, 0, 0));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r, temp->y - temp->r, temp->x + temp->r, temp->y + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
}
else if (temp->dir == 4)
{
hbrush = CreateSolidBrush(RGB(125, 125, 125));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r, temp->y - 40 - temp->r, temp->x + temp->r, temp->y - 40 + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
hbrush = CreateSolidBrush(RGB(100, 100, 100));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r, temp->y - 30 - temp->r, temp->x + temp->r, temp->y - 30 + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
hbrush = CreateSolidBrush(RGB(50, 50, 50));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r, temp->y - 20 - temp->r, temp->x + temp->r, temp->y - 20 + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
hbrush = CreateSolidBrush(RGB(30, 30, 30));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r, temp->y - 10 - temp->r, temp->x + temp->r, temp->y - 10 + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
hbrush = CreateSolidBrush(RGB(0, 0, 0));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, temp->x - temp->r, temp->y - temp->r, temp->x + temp->r, temp->y + temp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
}
}
}
}
//빵야빵야
if (bulletcount != 6)
{
hbrush = CreateSolidBrush(RGB(0, 155, 0));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, player.x - player.r - 5, player.y - player.r - 30, player.x - 5 - player.r + Reload, player.y - player.r - 20);//게이지
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
}
hbrush = CreateSolidBrush(RGB(0, 0, 0));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
RoundRect(hmemdc, player.x - player.r, player.y - player.r, player.x + player.r, player.y + player.r, 40, 40);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
//주인공
for (int i = 0; i < bulletcount; i++)
{
hbrush = CreateSolidBrush(RGB(255, 255, 255));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
roundbullet[i].x = (player.r - 5)*cos(PIE*(i + 1) / 3 + roundbullet[i].rad) + player.x;
roundbullet[i].y = (player.r - 5)*sin(PIE*(i + 1) / 3 + roundbullet[i].rad) + player.y;
roundbullet[i].r = 5;
Ellipse(hmemdc, roundbullet[i].x - roundbullet[i].r, roundbullet[i].y - roundbullet[i].r, roundbullet[i].x + roundbullet[i].r, roundbullet[i].y + roundbullet[i].r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
}
//총알돌아가는거
for (int i = 0; i < deathCount; i++)
{
hbrush = CreateSolidBrush(RGB(40, 231, 255));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, 1050, i * 20, 1100, i * 20 + 20);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
}//death count
if (enemyHead != NULL) {
for (etemp = enemyHead; etemp != NULL; etemp = etemp->next)
{
hbrush = CreateSolidBrush(RGB(40, 231, 255));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
RoundRect(hmemdc, etemp->x - etemp->r, etemp->y - etemp->r, etemp->x + etemp->r, etemp->y + etemp->r, 40, 40);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
}
}
if (bombHead != NULL)
{
for (btemp = bombHead; btemp != NULL; btemp = btemp->next)
{
hbrush = CreateSolidBrush(RGB(200, 0, 0));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Ellipse(hmemdc, btemp->x - btemp->r, btemp->y - btemp->r, btemp->x + btemp->r, btemp->y + btemp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
}
}//bomb
if (appearHead != NULL)
{
for (btemp = appearHead; btemp != NULL; btemp = btemp->next)
{
hbrush = CreateSolidBrush(RGB(40, 231, 255));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, btemp->x - btemp->r, btemp->y - btemp->r, btemp->x + btemp->r, btemp->y + btemp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
}
}//Appear
if (enemyHead != NULL) {
for (etemp = enemyHead; etemp != NULL; etemp = etemp->next)
{
SetROP2(hmemdc, R2_MASKPEN);
hbrush = CreateHatchBrush(HS_BDIAGONAL, RGB(0, 0, 0));
oldbrush = (HBRUSH)SelectObject(hmemdc, hbrush);
Rectangle(hmemdc, etemp->x - 60 - etemp->r, etemp->y - etemp->r, etemp->x - 60 + etemp->r, etemp->y + etemp->r);
Rectangle(hmemdc, etemp->x + 60 - etemp->r, etemp->y - etemp->r, etemp->x + 60 + etemp->r, etemp->y + etemp->r);
Rectangle(hmemdc, etemp->x - etemp->r, etemp->y - 60 - etemp->r, etemp->x + etemp->r, etemp->y - 60 + etemp->r);
Rectangle(hmemdc, etemp->x - etemp->r, etemp->y + 60 - etemp->r, etemp->x + etemp->r, etemp->y + 60 + etemp->r);
Rectangle(hmemdc, etemp->x - 60 - etemp->r, etemp->y + 60 - etemp->r, etemp->x - 60 + etemp->r, etemp->y + 60 + etemp->r);
Rectangle(hmemdc, etemp->x + 60 - etemp->r, etemp->y - 60 - etemp->r, etemp->x + 60 + etemp->r, etemp->y - 60 + etemp->r);
Rectangle(hmemdc, etemp->x - 60 - etemp->r, etemp->y - 60 - etemp->r, etemp->x - 60 + etemp->r, etemp->y - 60 + etemp->r);
Rectangle(hmemdc, etemp->x + 60 - etemp->r, etemp->y + 60 - etemp->r, etemp->x + 60 + etemp->r, etemp->y + 60 + etemp->r);
DeleteObject(hbrush);
SelectObject(hmemdc, oldbrush);
}
}
//적
wsprintf(buf, "Score: %d", score);
TextOut(hmemdc, 1000, 300, buf, strlen(buf));
wsprintf(buf, "Combo: %d", combo / 10);
TextOut(hmemdc, 1000, 320, buf, strlen(buf));
BitBlt(hdc, 0, 0, rect.right, rect.bottom, hmemdc, 0, 0, SRCCOPY); // 배껴그리기
// hdc 의 0,0 위치에 hMemDC의 0,0위치부터 crt.right,crt.bottom까지의 영역, 즉 crt범위를 그린다 라는 설정인듯.
DeleteObject(SelectObject(hmemdc, OldBitmap)); // 종이 원래대로 한 후 제거
DeleteDC(hmemdc); // hMemDC 제거
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return(DefWindowProc(hWnd, iMessage, wParam, lParam));
//case에서 정의되지 않은 메시지는 커널이 처리하도록 메시지 전달
}
float CheckDistance(int x, int y, int mx, int my)
{
return sqrt((float)(mx - x)*(mx - x) + (my - y)*(my - y));
}
BOOL CheckCir(int x, int y, int mx, int my, int size)
{
if (CheckDistance(x, y, mx, my) < size * 2)
{
return TRUE;
}
else
{
return FALSE;
}
}
BOOL CheckMap(int x, int y, int pr, int mx, int my, int mr)
{
if ((x - 55 - pr < mx - 55 + mr && x - 55 + pr > mx - 55 - mr) && (y - 55 - pr < my - 55 + mr && y - 55 + pr > my - 55 - mr)) //맵반지름+플레이어반지름 = 55
{
return TRUE;
}
else {
return FALSE;
}
}
void CreateBullet(int px, int py, int dir)
{
Bullet * newBullet = (Bullet*)malloc(sizeof(Bullet));
newBullet->x = px;
newBullet->y = py;
newBullet->dir = dir;
newBullet->r = 5;
if (bulletHead == NULL || bulletHead->next == NULL)
{
if (bulletHead == NULL)
{
bulletHead = newBullet;
newBullet->next = NULL;
}
if (bulletHead->next == NULL)
{
bulletHead->next = newBullet;
newBullet->next = NULL;
}
}
else
{
for (temp = bulletHead; temp->next != NULL; temp = temp->next)
{
}
temp->next = newBullet;
newBullet->next = NULL;
}
}
void DeleteBullet(Bullet * delbullet)
{
if (delbullet == bulletHead)
{
if (delbullet->next == NULL)
{
bulletHead = NULL;
}
else
{
bulletHead = bulletHead->next;
}
}
else
{
for (temp = bulletHead; temp->next != delbullet; temp = temp->next)
{
}
temp->next = delbullet->next;
free(delbullet);
}
}
Enemy * CreateEnemy()
{
Enemy * newEnemy = (Enemy*)malloc(sizeof(Enemy));
newEnemy->x = rand() % 1000;
newEnemy->y = rand() % 1000;
newEnemy->r = 30;
if (enemyHead == NULL || enemyHead->next == NULL)
{
if (enemyHead == NULL)
{
enemyHead = newEnemy;
newEnemy->next = NULL;
}
if (enemyHead->next == NULL)
{
enemyHead->next = newEnemy;
newEnemy->next = NULL;
}
}
else
{
for (etemp = enemyHead; etemp->next != NULL; etemp = etemp->next)
{
}
etemp->next = newEnemy;
newEnemy->next = NULL;
}
return newEnemy;
}
void DeleteEnemy(Enemy * delenemy)
{
if (delenemy == enemyHead)
{
if (delenemy->next == NULL)
{
enemyHead = NULL;
}
else
{
enemyHead = enemyHead->next;
}
}
else
{
for (etemp = enemyHead; etemp->next != delenemy; etemp = etemp->next)
{
}
etemp->next = delenemy->next;
free(delenemy);
}
}
void CreateBomb(int x, int y)//폭발위치를 받아옴
{
Bomb * newBomb = (Bomb*)malloc(sizeof(Bomb));
newBomb->x = x;
newBomb->y = y;
newBomb->r = 80;
if (bombHead == NULL || bombHead->next == NULL)
{
if (bombHead == NULL)
{
bombHead = newBomb;
newBomb->next = NULL;
}
if (bombHead->next == NULL)
{
bombHead->next = newBomb;
newBomb->next = NULL;
}
}
else
{
for (btemp = bombHead; btemp->next != NULL; btemp = btemp->next)
{
}
btemp->next = newBomb;
newBomb->next = NULL;
}
}
void DeleteBomb(Bomb * delBomb)
{
if (delBomb == bombHead)
{
if (delBomb->next == NULL)
{
bombHead = NULL;
}
else
{
bombHead = bombHead->next;
}
}
else
{
for (btemp = bombHead; btemp->next != delBomb; btemp = btemp->next)
{
}
btemp->next = delBomb->next;
free(delBomb);
}
}
void Createappear(int x, int y)//생성위치를 받아옴
{
Bomb * newAppear = (Bomb*)malloc(sizeof(Bomb));
newAppear->x = x;
newAppear->y = y;
newAppear->r = 150;
if (appearHead == NULL || appearHead->next == NULL)
{
if (appearHead == NULL)
{
appearHead = newAppear;
newAppear->next = NULL;
}
if (appearHead->next == NULL)
{
appearHead->next = newAppear;
newAppear->next = NULL;
}
}
else
{
for (btemp = appearHead; btemp->next != NULL; btemp = btemp->next)
{
}
btemp->next = newAppear;
newAppear->next = NULL;
}
}
void DeleteAppear(Bomb * delAppear)
{
if (delAppear == appearHead)
{
if (delAppear->next == NULL)
{
appearHead = NULL;
}
else
{
appearHead = appearHead->next;
}
}
else
{
for (btemp = appearHead; appeartemp->next != delAppear; btemp = btemp->next)
{
}
btemp->next = delAppear->next;
free(delAppear);
}
}
'WinAPI' 카테고리의 다른 글
[WinAPI] 2D 게임 현황 <3주차> (0) | 2021.08.08 |
---|---|
[WinAPI] 2D 게임 현황 <2주차> (0) | 2021.08.01 |
[WinAPI] 2D 게임 현황 <1주차> (0) | 2021.07.25 |
[WinAPI] 개발계획 - 2D Game (0) | 2021.07.24 |
[WinAPI] 학기 텀프로젝트 (0) | 2019.11.01 |