Advertisement
Alan468

Wonsz v1

Nov 15th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.45 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <time.h>
  3. #include <malloc.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #define TIMER_TIME 70
  8. #define TIMER_ID 100
  9. #define SIZE 30
  10.  
  11. enum Vector { Left, Right, Up, Down };
  12.  
  13. struct Coin {
  14.     int PosX, PosY;
  15.     int Width, Height;
  16.     COLORREF BorderColor, FillColor;
  17. };
  18.  
  19. struct Snake {
  20.     int PosX, PosY;
  21.     int Width, Height;
  22.     COLORREF BorderColor, FillColor;
  23.     Vector vector, PrevVector;
  24. };
  25.  
  26. HWND hwnd;
  27. int Score = 1;
  28. PAINTSTRUCT PS;
  29. RECT WndRect;
  30. Snake snake[255];
  31. Coin coin;
  32.  
  33. void SetNewCoinPos() {
  34.     coin.PosX = (rand() % WndRect.right - 100) + 100;
  35.     coin.PosY = (rand() % WndRect.bottom - 100) + 100;
  36. }
  37.  
  38. void PrintScore(HDC &hdc) {
  39.     char buff[255];
  40.     sprintf_s(buff, "Wynik: %d", Score-1);
  41.     TextOut(hdc, 0, 0, buff, strlen(buff));
  42. }
  43.  
  44. void PrintSnake(Snake &snak, HDC &hdc) {
  45.     HBRUSH Brush = CreateSolidBrush(snak.FillColor);
  46.     HPEN Pen = CreatePen(PS_SOLID, 2, snak.BorderColor);
  47.  
  48.     HBRUSH OldBrush = (HBRUSH)SelectObject(hdc, Brush);
  49.     HPEN OldPen = (HPEN)SelectObject(hdc, Pen);
  50.  
  51.     Rectangle(hdc, snak.PosX, snak.PosY, snak.PosX + snak.Width, snak.PosY + snak.Height);
  52.  
  53.     SelectObject(hdc, OldBrush);
  54.     SelectObject(hdc, OldPen);
  55.  
  56.     DeleteObject(Brush);
  57.     DeleteObject(Pen);
  58. }
  59.  
  60. void PrintCoin(HDC &hdc) {
  61.     HBRUSH Brush = CreateSolidBrush(coin.FillColor);
  62.     HPEN Pen = CreatePen(PS_SOLID, 2, coin.BorderColor);
  63.  
  64.     HBRUSH OldBrush = (HBRUSH)SelectObject(hdc, Brush);
  65.     HPEN OldPen = (HPEN)SelectObject(hdc, Pen);
  66.  
  67.     Ellipse(hdc, coin.PosX, coin.PosY, coin.PosX + coin.Width, coin.PosY + coin.Height);
  68.  
  69.     SelectObject(hdc, OldBrush);
  70.     SelectObject(hdc, OldPen);
  71.  
  72.     DeleteObject(Brush);
  73.     DeleteObject(Pen);
  74. }
  75.  
  76. void SetTail(int i) {
  77.     snake[i].BorderColor = RGB(0, 0, 0);
  78.     snake[i].FillColor = RGB(0, 255, 255);
  79.     snake[i].Width = SIZE;
  80.     snake[i].Height = SIZE;
  81.  
  82.     if (snake[i - 1].vector == Up) {
  83.         snake[i].PosX = snake[i - 1].PosX;
  84.         snake[i].PosY = snake[i - 1].PosY + SIZE;
  85.     }
  86.     else if (snake[i - 1].vector == Down) {
  87.         snake[i].PosX = snake[i - 1].PosX;
  88.         snake[i].PosY = snake[i - 1].PosY - SIZE;
  89.     }
  90.     else if (snake[i - 1].vector == Left) {
  91.         snake[i].PosX = snake[i - 1].PosX + SIZE;
  92.         snake[i].PosY = snake[i - 1].PosY;
  93.     }
  94.     else if (snake[i - 1].vector == Right) {
  95.         snake[i].PosX = snake[i - 1].PosX - SIZE;
  96.         snake[i].PosY = snake[i - 1].PosY;
  97.     }
  98.  
  99.  
  100.     snake[i].vector = snake[i - 1].vector;//
  101. }
  102.  
  103. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  104.     switch (msg) {
  105.     case WM_TIMER: {
  106.         GetClientRect(hwnd, &WndRect);
  107.  
  108.         switch (snake[0].vector) {
  109.         case Up:
  110.             snake[0].PosY -= SIZE;
  111.             if (snake[0].PosY < WndRect.top) snake[0].PosY = WndRect.bottom - snake[0].Height;
  112.             break;
  113.         case Down:
  114.             snake[0].PosY += SIZE;
  115.             if (snake[0].PosY + snake[0].Height > WndRect.bottom) snake[0].PosY = 0;
  116.             break;
  117.         case Left:
  118.             snake[0].PosX -= SIZE;
  119.             if (snake[0].PosX < WndRect.left) snake[0].PosX = WndRect.right - snake[0].Width;
  120.             break;
  121.         case Right:
  122.             snake[0].PosX += SIZE;
  123.             if (snake[0].PosX + snake[0].Width > WndRect.right) snake[0].PosX = 0;
  124.             break;
  125.         }
  126.  
  127.         for (int i = Score; i > 0; i--) {
  128.                 snake[i].PosX = snake[i - 1].PosX;
  129.                 snake[i].PosY = snake[i - 1].PosY;
  130.  
  131.                 if (snake[0].PosX == snake[i].PosX && snake[0].PosY == snake[i].PosY &&
  132.                     snake[0].PosX + snake[0].Width == snake[i].PosX + snake[i].Width &&
  133.                     snake[0].PosY + snake[0].Height == snake[i].PosY + snake[i].Height) {
  134.                     KillTimer(hwnd, TIMER_ID);
  135.                     MessageBox(NULL, TEXT("Przegrales!"), TEXT("Przegrales!"), MB_ICONEXCLAMATION);
  136.                 }
  137.         }
  138.  
  139.         if (snake[0].PosX >= coin.PosX - SIZE && snake[0].PosY >= coin.PosY - SIZE &&
  140.             snake[0].PosX + snake[0].Width <= (coin.PosX + coin.Width) + SIZE &&
  141.             snake[0].PosY + snake[0].Height <= (coin.PosY + coin.Height) + SIZE) {
  142.             SetNewCoinPos();
  143.             Score++;
  144.             SetTail(Score);
  145.         }
  146.         InvalidateRect(hwnd, &WndRect, 1);
  147.     }break;
  148.  
  149.     case WM_PAINT: {
  150.         HDC hdc = BeginPaint(hwnd, &PS);
  151.         PrintCoin(hdc);
  152.         for (int i = Score; i >= 0; i--) {
  153.             PrintSnake(snake[i], hdc);
  154.         }
  155.         PrintScore(hdc);
  156.         EndPaint(hwnd, &PS);;
  157.     }break;
  158.  
  159.     case WM_KEYDOWN: {
  160.         switch ((int)wParam) {
  161.         case VK_UP:
  162.             snake[0].PrevVector = snake[0].vector;
  163.             if (snake[0].PrevVector != Down) {
  164.                 snake[0].vector = Up;
  165.             }
  166.             break;
  167.         case VK_DOWN:
  168.             snake[0].PrevVector = snake[0].vector;
  169.             if (snake[0].PrevVector != Up) {
  170.                 snake[0].vector = Down;
  171.             }
  172.             break;
  173.         case VK_RIGHT:
  174.             snake[0].PrevVector = snake[0].vector;
  175.             if (snake[0].PrevVector != Left) {
  176.                 snake[0].vector = Right;
  177.             }
  178.             break;
  179.         case VK_LEFT:
  180.             snake[0].PrevVector = snake[0].vector;
  181.             if (snake[0].PrevVector != Right) {
  182.                 snake[0].vector = Left;
  183.             }
  184.             break;
  185.         }
  186.     }break;
  187.  
  188.     case WM_CLOSE:  DestroyWindow(hwnd);    break;
  189.     case WM_DESTROY:PostQuitMessage(0);     break;
  190.     default:    return DefWindowProc(hwnd, msg, wParam, lParam);
  191.     }
  192.     return 0;
  193. }
  194.  
  195. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR ilCmdLine, int nCmdShow) {
  196.     WNDCLASSEX window;
  197.     MSG msg;
  198.     TCHAR Class_Name[] = TEXT("OKNO_TEST"), Title[] = TEXT("Tytul");
  199.  
  200.     window.cbClsExtra = NULL;
  201.     window.cbSize = sizeof(WNDCLASSEX);
  202.     window.cbWndExtra = NULL;
  203.     window.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  204.     window.hCursor = LoadCursor(NULL, IDC_ARROW);
  205.     window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  206.     window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  207.     window.hInstance = hInstance;
  208.     window.lpfnWndProc = WndProc;
  209.     window.lpszClassName = Class_Name;
  210.     window.lpszMenuName = 0;
  211.     window.style = CS_VREDRAW | CS_HREDRAW;
  212.  
  213.     RegisterClassEx(&window);
  214.  
  215.     hwnd = CreateWindowEx(WS_EX_WINDOWEDGE, Class_Name, Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
  216.  
  217.     snake[0].BorderColor = RGB(0, 0, 0);
  218.     snake[0].FillColor = RGB(255, 0, 255);
  219.     snake[0].Width = SIZE;
  220.     snake[0].Height = SIZE;
  221.     snake[0].PosX = 100;
  222.     snake[0].PosY = 100;
  223.     snake[0].vector = Down;
  224.  
  225.     GetClientRect(hwnd, &WndRect);
  226.  
  227.     srand(time(NULL));
  228.     coin.BorderColor = RGB(0, 0, 0);
  229.     coin.FillColor = RGB(240, 240, 90);
  230.     coin.Width = SIZE;
  231.     coin.Height = SIZE;
  232.     SetNewCoinPos();
  233.  
  234.     ShowWindow(hwnd, nCmdShow);
  235.     UpdateWindow(hwnd);
  236.     SetTimer(hwnd, TIMER_ID, TIMER_TIME, NULL);
  237.  
  238.     while (GetMessage(&msg, NULL, 0, 0)) {
  239.         TranslateMessage(&msg);
  240.         DispatchMessage(&msg);
  241.     }
  242.  
  243.     if (!UnregisterClass(Class_Name, hInstance)) {
  244.         MessageBox(NULL, TEXT("Błąd niszczenia okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
  245.         return 0;
  246.     }
  247.     return msg.wParam;
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement