Advertisement
Alan468

Snake V0.5

Nov 15th, 2016
119
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 10
  8. #define TIMER_ID 100
  9. #define SPEED 8
  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 = 0;
  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);
  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 = 50;
  80.     snake[i].Height = 50;
  81.  
  82.     if (snake[i - 1].vector == Up) {
  83.         snake[i].PosX = snake[i - 1].PosX;
  84.         snake[i].PosY = snake[i - 1].PosY + 50;
  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 - 50;
  89.     }
  90.     else if (snake[i - 1].vector == Left) {
  91.         snake[i].PosX = snake[i - 1].PosX + 50;
  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 - 50;
  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_CREATE:break;
  106.     case WM_COMMAND:break;
  107.     case WM_TIMER: {
  108.         GetClientRect(hwnd, &WndRect);
  109.  
  110.         for (int i = Score; i >= 0; i--) {
  111.  
  112.             switch (snake[i].vector) {
  113.             case Up:
  114.                 snake[i].PosY -= SPEED;
  115.                 if (snake[i].PosY < WndRect.top) snake[i].PosY = WndRect.bottom - snake[i].Height;
  116.                 break;
  117.             case Down:
  118.                 snake[i].PosY += SPEED;
  119.                 if (snake[i].PosY + snake[i].Height > WndRect.bottom) snake[i].PosY = i;
  120.                 break;
  121.             case Left:
  122.                 snake[i].PosX -= SPEED;
  123.                 if (snake[i].PosX < WndRect.left) snake[i].PosX = WndRect.right - snake[i].Width;
  124.                 break;
  125.             case Right:
  126.                 snake[i].PosX += SPEED;
  127.                 if (snake[i].PosX + snake[i].Width > WndRect.right) snake[i].PosX = i;
  128.                 break;
  129.             }
  130.             if (i > 0) {
  131.                 snake[i].PosX = snake[i-1 ].PosX;
  132.                 snake[i].PosY = snake[i-1].PosY ;
  133.                 snake[i].vector = snake[i-1].vector;
  134.  
  135.             }
  136.  
  137.         }
  138.  
  139.  
  140.  
  141.         if (snake[0].PosX >= coin.PosX - 50 && snake[0].PosY >= coin.PosY - 50 &&
  142.             snake[0].PosX + snake[0].Width <= (coin.PosX + coin.Width) + 50 &&
  143.             snake[0].PosY + snake[0].Height <= (coin.PosY + coin.Height) + 50) {
  144.             SetNewCoinPos();
  145.             Score++;
  146.             SetTail(Score);
  147.         }
  148.         InvalidateRect(hwnd, &WndRect, 1);
  149.     }break;
  150.  
  151.     case WM_PAINT: {
  152.         HDC hdc = BeginPaint(hwnd, &PS);
  153.         PrintCoin(hdc);
  154.         for (int i = Score; i >= 0; i--) {
  155.             PrintSnake(snake[i], hdc);
  156.         }
  157.         PrintScore(hdc);
  158.         EndPaint(hwnd, &PS);;
  159.     }break;
  160.  
  161.     case WM_KEYDOWN: {
  162.  
  163.         switch ((int)wParam) {
  164.         case VK_UP:
  165.             snake[0].PrevVector = snake[0].vector;
  166.             if (snake[0].PrevVector != Down) {
  167.                 snake[0].vector = Up;
  168.             }
  169.             break;
  170.         case VK_DOWN:
  171.             snake[0].PrevVector = snake[0].vector;
  172.             if (snake[0].PrevVector != Up) {
  173.                 snake[0].vector = Down;
  174.             }
  175.             break;
  176.         case VK_RIGHT:
  177.             snake[0].PrevVector = snake[0].vector;
  178.             if (snake[0].PrevVector != Left) {
  179.                 snake[0].vector = Right;
  180.             }
  181.             break;
  182.         case VK_LEFT:
  183.             snake[0].PrevVector = snake[0].vector;
  184.             if (snake[0].PrevVector != Right) {
  185.                 snake[0].vector = Left;
  186.             }
  187.             break;
  188.         }
  189.     }break;
  190.  
  191.     case WM_CLOSE:  DestroyWindow(hwnd);    break;
  192.     case WM_DESTROY:PostQuitMessage(0);     break;
  193.     default:    return DefWindowProc(hwnd, msg, wParam, lParam);
  194.     }
  195.     return 0;
  196. }
  197.  
  198. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR ilCmdLine, int nCmdShow) {
  199.  
  200.     WNDCLASSEX window;
  201.     MSG msg;
  202.     TCHAR Class_Name[] = TEXT("OKNO_TEST"), Title[] = TEXT("Tytul");
  203.  
  204.     window.cbClsExtra = NULL;
  205.     window.cbSize = sizeof(WNDCLASSEX);
  206.     window.cbWndExtra = NULL;
  207.     window.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  208.     window.hCursor = LoadCursor(NULL, IDC_ARROW);
  209.     window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  210.     window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  211.     window.hInstance = hInstance;
  212.     window.lpfnWndProc = WndProc;
  213.     window.lpszClassName = Class_Name;
  214.     window.lpszMenuName = 0;
  215.     window.style = CS_VREDRAW | CS_HREDRAW;
  216.  
  217.     if (!RegisterClassEx(&window)) {
  218.         MessageBox(NULL, TEXT("Błąd rejestracji okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
  219.         return 0;
  220.     }
  221.  
  222.     hwnd = CreateWindowEx(WS_EX_WINDOWEDGE, Class_Name, Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
  223.  
  224.     if (hwnd == NULL) {
  225.         MessageBox(NULL, TEXT("Błąd tworzenia okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
  226.         return 0;
  227.     }
  228.  
  229.     snake[0].BorderColor = RGB(0, 0, 0);
  230.     snake[0].FillColor = RGB(255, 0, 255);
  231.     snake[0].Width = 50;
  232.     snake[0].Height = 50;
  233.     snake[0].PosX = 100;
  234.     snake[0].PosY = 100;
  235.     snake[0].vector = Down;
  236.  
  237.     GetClientRect(hwnd, &WndRect);
  238.  
  239.     srand(time(NULL));
  240.     coin.BorderColor = RGB(0, 0, 0);
  241.     coin.FillColor = RGB(240, 240, 90);
  242.     coin.Width = 50;
  243.     coin.Height = 50;
  244.     SetNewCoinPos();
  245.  
  246.     ShowWindow(hwnd, nCmdShow);
  247.     UpdateWindow(hwnd);
  248.     SetTimer(hwnd, TIMER_ID, TIMER_TIME, NULL);
  249.  
  250.     while (GetMessage(&msg, NULL, 0, 0)) {
  251.         TranslateMessage(&msg);
  252.         DispatchMessage(&msg);
  253.     }
  254.  
  255.     if (!UnregisterClass(Class_Name, hInstance)) {
  256.         MessageBox(NULL, TEXT("Błąd niszczenia okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
  257.         return 0;
  258.     }
  259.     return msg.wParam;
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement