Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #define TIMER_TIME 250
- #define TIMER_ID 100
- #define SPEED 50
- HWND hwnd;
- enum Vector { Left, Right, Up, Down };
- struct Snake {
- int PosX, PosY;
- int Width, Height;
- COLORREF BorderColor, FillColor;
- Vector vector, PrevVector;
- };
- Snake snake;
- void PrintSnake(Snake &snak) {
- PAINTSTRUCT PS;
- HDC hdc = BeginPaint(hwnd, &PS);
- HBRUSH Brush = CreateSolidBrush(snak.FillColor);
- HPEN Pen = CreatePen(PS_SOLID, 2, snak.BorderColor);
- HBRUSH OldBrush = (HBRUSH)SelectObject(hdc, Brush);
- HPEN OldPen = (HPEN)SelectObject(hdc, Pen);
- Rectangle(hdc, snak.PosX, snak.PosY, snak.PosX + snak.Width, snak.PosY + snak.Height);
- SelectObject(hdc, OldBrush);
- SelectObject(hdc, OldPen);
- DeleteObject(Brush);
- DeleteObject(Pen);
- EndPaint(hwnd, &PS);
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
- switch (msg) {
- case WM_CREATE:break;
- case WM_COMMAND:break;
- case WM_TIMER: {
- RECT WndRect;
- GetClientRect(hwnd, &WndRect);
- // for 255 -> 0
- switch (snake.vector) {
- case Up:
- snake.PosY -= SPEED;
- if (snake.PosY < WndRect.top) snake.PosY = WndRect.bottom - snake.Height;
- break;
- case Down:
- snake.PosY += SPEED;
- if (snake.PosY + snake.Height > WndRect.bottom) snake.PosY = 0;
- break;
- case Left:
- snake.PosX -= SPEED;
- if (snake.PosX < WndRect.left) snake.PosX = WndRect.right - snake.Width;
- break;
- case Right:
- snake.PosX += SPEED;
- if (snake.PosX + snake.Width > WndRect.right) snake.PosX = 0;
- break;
- }
- InvalidateRect(hwnd, &WndRect, 1);
- PrintSnake(snake);//[255]
- }break;
- case WM_PAINT: {
- PrintSnake(snake);//[255]
- }break;
- case WM_KEYDOWN: {
- switch ((int)wParam) {
- case VK_UP:
- snake.PrevVector = snake.vector;
- if (snake.PrevVector != Down) {
- snake.vector = Up;
- }
- break;
- case VK_DOWN:
- snake.PrevVector = snake.vector;
- if (snake.PrevVector != Down) {
- snake.vector = Down;
- }
- break;
- case VK_RIGHT:
- snake.vector = Right;
- break;
- case VK_LEFT:
- snake.vector = Left;
- break;
- }
- }break;
- case WM_CLOSE: DestroyWindow(hwnd); break;
- case WM_DESTROY:PostQuitMessage(0); break;
- default: return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR ilCmdLine, int nCmdShow) {
- WNDCLASSEX window;
- MSG msg;
- TCHAR Class_Name[] = TEXT("OKNO_TEST"), Title[] = TEXT("Tytul");
- window.cbClsExtra = NULL;
- window.cbSize = sizeof(WNDCLASSEX);
- window.cbWndExtra = NULL;
- window.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- window.hCursor = LoadCursor(NULL, IDC_ARROW);
- window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- window.hInstance = hInstance;
- window.lpfnWndProc = WndProc;
- window.lpszClassName = Class_Name;
- window.lpszMenuName = 0;
- window.style = CS_VREDRAW | CS_HREDRAW;
- if (!RegisterClassEx(&window)) {
- MessageBox(NULL, TEXT("Błąd rejestracji okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
- return 0;
- }
- hwnd = CreateWindowEx(WS_EX_WINDOWEDGE, Class_Name, Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
- if (hwnd == NULL) {
- MessageBox(NULL, TEXT("Błąd tworzenia okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
- return 0;
- }
- snake.BorderColor = RGB(0, 0, 0);
- snake.FillColor = RGB(255, 0, 255);
- snake.Width = 50;
- snake.Height = 50;
- snake.PosX = 100;
- snake.PosY = 100;
- snake.vector = Down;
- ShowWindow(hwnd, nCmdShow);
- UpdateWindow(hwnd);
- SetTimer(hwnd, TIMER_ID, TIMER_TIME, NULL);
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- if (!UnregisterClass(Class_Name, hInstance)) {
- MessageBox(NULL, TEXT("Błąd niszczenia okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
- return 0;
- }
- return msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement