Advertisement
Alan468

Snake V0.00001

Nov 15th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.92 KB | None | 0 0
  1. #include <Windows.h>
  2.  
  3. #define TIMER_TIME 250
  4. #define TIMER_ID 100
  5. #define SPEED 50
  6.  
  7. HWND hwnd;
  8.  
  9. enum Vector { Left, Right, Up, Down };
  10.  
  11. struct Snake {
  12.     int PosX, PosY;
  13.     int Width, Height;
  14.     COLORREF BorderColor, FillColor;
  15.     Vector vector, PrevVector;
  16. };
  17.  
  18. Snake snake;
  19.  
  20. void PrintSnake(Snake &snak) {
  21.     PAINTSTRUCT PS;
  22.     HDC hdc = BeginPaint(hwnd, &PS);
  23.  
  24.     HBRUSH Brush = CreateSolidBrush(snak.FillColor);
  25.     HPEN Pen = CreatePen(PS_SOLID, 2, snak.BorderColor);
  26.  
  27.     HBRUSH OldBrush = (HBRUSH)SelectObject(hdc, Brush);
  28.     HPEN OldPen = (HPEN)SelectObject(hdc, Pen);
  29.  
  30.     Rectangle(hdc, snak.PosX, snak.PosY, snak.PosX + snak.Width, snak.PosY + snak.Height);
  31.  
  32.     SelectObject(hdc, OldBrush);
  33.     SelectObject(hdc, OldPen);
  34.  
  35.     DeleteObject(Brush);
  36.     DeleteObject(Pen);
  37.  
  38.     EndPaint(hwnd, &PS);
  39. }
  40.  
  41. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  42.     switch (msg) {
  43.     case WM_CREATE:break;
  44.     case WM_COMMAND:break;
  45.     case WM_TIMER: {
  46.         RECT WndRect;
  47.         GetClientRect(hwnd, &WndRect);
  48.  
  49.         // for 255 -> 0
  50.         switch (snake.vector) {
  51.         case Up:
  52.  
  53.             snake.PosY -= SPEED;
  54.             if (snake.PosY < WndRect.top) snake.PosY = WndRect.bottom - snake.Height;
  55.  
  56.             break;
  57.         case Down:
  58.  
  59.             snake.PosY += SPEED;
  60.             if (snake.PosY + snake.Height > WndRect.bottom) snake.PosY = 0;
  61.  
  62.             break;
  63.         case Left:
  64.  
  65.             snake.PosX -= SPEED;
  66.             if (snake.PosX < WndRect.left) snake.PosX = WndRect.right - snake.Width;
  67.  
  68.             break;
  69.         case Right:
  70.  
  71.             snake.PosX += SPEED;
  72.  
  73.             if (snake.PosX + snake.Width > WndRect.right) snake.PosX = 0;
  74.  
  75.             break;
  76.         }
  77.  
  78.         InvalidateRect(hwnd, &WndRect, 1);
  79.         PrintSnake(snake);//[255]
  80.  
  81.     }break;
  82.     case WM_PAINT: {
  83.         PrintSnake(snake);//[255]
  84.  
  85.     }break;
  86.  
  87.     case WM_KEYDOWN: {
  88.  
  89.         switch ((int)wParam) {
  90.         case VK_UP:
  91.             snake.PrevVector = snake.vector;
  92.             if (snake.PrevVector != Down) {
  93.                 snake.vector = Up;
  94.             }
  95.             break;
  96.  
  97.         case VK_DOWN:
  98.             snake.PrevVector = snake.vector;
  99.             if (snake.PrevVector != Down) {
  100.                 snake.vector = Down;
  101.             }
  102.             break;
  103.  
  104.         case VK_RIGHT:
  105.             snake.vector = Right;
  106.             break;
  107.  
  108.         case VK_LEFT:
  109.             snake.vector = Left;
  110.             break;
  111.         }
  112.     }break;
  113.  
  114.     case WM_CLOSE:  DestroyWindow(hwnd);    break;
  115.     case WM_DESTROY:PostQuitMessage(0);     break;
  116.     default:    return DefWindowProc(hwnd, msg, wParam, lParam);
  117.     }
  118.     return 0;
  119. }
  120.  
  121. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR ilCmdLine, int nCmdShow) {
  122.     WNDCLASSEX window;
  123.     MSG msg;
  124.     TCHAR Class_Name[] = TEXT("OKNO_TEST"), Title[] = TEXT("Tytul");
  125.  
  126.     window.cbClsExtra = NULL;
  127.     window.cbSize = sizeof(WNDCLASSEX);
  128.     window.cbWndExtra = NULL;
  129.     window.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  130.     window.hCursor = LoadCursor(NULL, IDC_ARROW);
  131.     window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  132.     window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  133.     window.hInstance = hInstance;
  134.     window.lpfnWndProc = WndProc;
  135.     window.lpszClassName = Class_Name;
  136.     window.lpszMenuName = 0;
  137.     window.style = CS_VREDRAW | CS_HREDRAW;
  138.  
  139.     if (!RegisterClassEx(&window)) {
  140.         MessageBox(NULL, TEXT("Błąd rejestracji okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
  141.         return 0;
  142.     }
  143.  
  144.     hwnd = CreateWindowEx(WS_EX_WINDOWEDGE, Class_Name, Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
  145.  
  146.     if (hwnd == NULL) {
  147.         MessageBox(NULL, TEXT("Błąd tworzenia okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
  148.         return 0;
  149.     }
  150.  
  151.     snake.BorderColor = RGB(0, 0, 0);
  152.     snake.FillColor = RGB(255, 0, 255);
  153.     snake.Width = 50;
  154.     snake.Height = 50;
  155.     snake.PosX = 100;
  156.     snake.PosY = 100;
  157.     snake.vector = Down;
  158.  
  159.     ShowWindow(hwnd, nCmdShow);
  160.     UpdateWindow(hwnd);
  161.     SetTimer(hwnd, TIMER_ID, TIMER_TIME, NULL);
  162.  
  163.     while (GetMessage(&msg, NULL, 0, 0)) {
  164.         TranslateMessage(&msg);
  165.         DispatchMessage(&msg);
  166.     }
  167.  
  168.     if (!UnregisterClass(Class_Name, hInstance)) {
  169.         MessageBox(NULL, TEXT("Błąd niszczenia okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
  170.         return 0;
  171.     }
  172.     return msg.wParam;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement