Advertisement
Alan468

Snake V0.1

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