1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//Michael Ervin - Windows based graphical snake game - 7/29/2011
//////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <Windows.h>
#include "resource.h"
#include "SnakeObj.h"
#define screen_width 800
#define screen_height 600
HWND hWnd;
HDC hDC;
char _direction;
int _level, _score, _highscore;
const RECT _rect = {0,0,screen_width,screen_height};
snake _snake1(2); //Needs to not be global
LRESULT CALLBACK MessageProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEXA wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = MessageProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
wc.lpszClassName = "SNAKE";
RegisterClassExA(&wc);
hWnd = CreateWindowExA(NULL,
"SNAKE", "Snake",
WS_SYSMENU,
0, 45,
screen_width, screen_height,
NULL, NULL,
hInstance,
NULL);
ShowWindow(hWnd, nCmdShow);
hDC = GetDC(hWnd);
HBRUSH _bkbrush = CreateSolidBrush(_white);
_level = 1;
_score = 0;
//snake _snake1(1);
_snake1.init_snake(295);
MSG msg;
_snake1.show_head(hDC);
while(TRUE)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
/*if(msg.wParam == _up || msg.wParam == _down || msg.wParam == _left || msg.wParam == _right)
_snake1._direction = msg.wParam;*/
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
_snake1.animate_snake(hDC);
_snake1.next_pos();
Sleep(75);
}
}
return msg.wParam;
}
LRESULT CALLBACK MessageProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
case WM_KEYDOWN:
{
switch (wParam)
{
case _up:
_snake1._direction = _up;
break;
case _down:
_snake1._direction = _down;
break;
case _left:
_snake1._direction = _left;
break;
case _right:
_snake1._direction = _right;
break;
}
return 0;
} break;
}
return DefWindowProcA(hWnd, message, wParam, lParam);
}
|