Advertisement
alien_fx_fiend

Car Racing Game Win32 (Preliminary)

Jul 26th, 2024
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.47 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <stdio.h>
  5.  
  6. // Global Variables
  7. const int WIDTH = 1366;
  8. const int HEIGHT = 768;
  9. const int ROAD_WIDTH = 200;
  10. const int CAR_WIDTH = 50;
  11. const int CAR_HEIGHT = 100;
  12. const int TYRE_SIZE = 10;
  13. const int FPS = 60;
  14. const int TIMER = 4;
  15.  
  16. int playerX = 100;
  17. int playerY = HEIGHT - CAR_HEIGHT - 50;
  18. int aiX = playerX + CAR_WIDTH + 20;
  19. int aiY = playerY;
  20. int speed = 5;
  21. int aiSpeed = 5;
  22. int timer = TIMER;
  23. int playerTyre1X = playerX + 10;
  24. int playerTyre1Y = playerY + CAR_HEIGHT - TYRE_SIZE;
  25. int playerTyre2X = playerX + CAR_WIDTH - TYRE_SIZE - 10;
  26. int playerTyre2Y = playerY + CAR_HEIGHT - TYRE_SIZE;
  27. int aiTyre1X = aiX + 10;
  28. int aiTyre1Y = aiY + CAR_HEIGHT - TYRE_SIZE;
  29. int aiTyre2X = aiX + CAR_WIDTH - TYRE_SIZE - 10;
  30. int aiTyre2Y = aiY + CAR_HEIGHT - TYRE_SIZE;
  31.  
  32. bool gameStarted = false;
  33. bool gameOver = false;
  34. bool playerWon = false;
  35.  
  36. // Window Procedure
  37. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  38. {
  39.     switch (message)
  40.     {
  41.     case WM_CREATE:
  42.         SetTimer(hWnd, 1, 1000 / FPS, NULL);
  43.         break;
  44.     case WM_TIMER:
  45.         if (timer > 0)
  46.         {
  47.             timer--;
  48.             InvalidateRect(hWnd, NULL, TRUE);
  49.         }
  50.         else if (!gameStarted)
  51.         {
  52.             gameStarted = true;
  53.             srand((unsigned int)time(0));
  54.             aiSpeed = rand() % 5 + 3;
  55.         }
  56.         else if (!gameOver)
  57.         {
  58.             if (GetAsyncKeyState(VK_LEFT))
  59.             {
  60.                 playerX -= speed;
  61.                 playerTyre1X -= speed;
  62.                 playerTyre2X -= speed;
  63.             }
  64.             if (GetAsyncKeyState(VK_RIGHT))
  65.             {
  66.                 playerX += speed;
  67.                 playerTyre1X += speed;
  68.                 playerTyre2X += speed;
  69.             }
  70.             if (GetAsyncKeyState(VK_UP))
  71.             {
  72.                 playerY -= speed;
  73.                 playerTyre1Y -= speed;
  74.                 playerTyre2Y -= speed;
  75.             }
  76.             if (GetAsyncKeyState(VK_DOWN))
  77.             {
  78.                 playerY += speed;
  79.                 playerTyre1Y += speed;
  80.                 playerTyre2Y += speed;
  81.             }
  82.             aiY -= aiSpeed;
  83.             aiTyre1Y -= aiSpeed;
  84.             aiTyre2Y -= aiSpeed;
  85.             if (aiY < 0)
  86.             {
  87.                 gameOver = true;
  88.                 playerWon = false;
  89.             }
  90.             if (playerY < 0)
  91.             {
  92.                 gameOver = true;
  93.                 playerWon = true;
  94.             }
  95.             InvalidateRect(hWnd, NULL, TRUE);
  96.         }
  97.         break;
  98.     case WM_PAINT:
  99.     {
  100.         PAINTSTRUCT ps;
  101.         HDC hdc = BeginPaint(hWnd, &ps);
  102.         HBRUSH hBrush;
  103.         HPEN hPen;
  104.         // Draw road
  105.         hBrush = CreateSolidBrush(RGB(0, 0, 0));
  106.         hPen = CreatePen(PS_NULL, 0, RGB(0, 0, 0));
  107.         SelectObject(hdc, hPen);
  108.         SelectObject(hdc, hBrush);
  109.         Rectangle(hdc, 0, HEIGHT / 2, ROAD_WIDTH, HEIGHT);
  110.         Rectangle(hdc, 0, 0, WIDTH, HEIGHT / 2);
  111.         DeleteObject(hPen);
  112.         DeleteObject(hBrush);
  113.         // Draw player car
  114.         hBrush = CreateSolidBrush(RGB(255, 0, 0));
  115.         hPen = CreatePen(PS_NULL, 0, RGB(255, 0, 0));
  116.         SelectObject(hdc, hPen);
  117.         SelectObject(hdc, hBrush);
  118.         Rectangle(hdc, playerX, playerY, playerX + CAR_WIDTH, playerY + CAR_HEIGHT);
  119.         DeleteObject(hPen);
  120.         DeleteObject(hBrush);
  121.         // Draw player tyres
  122.         hBrush = CreateSolidBrush(RGB(255, 255, 255));
  123.         hPen = CreatePen(PS_NULL, 0, RGB(255, 255, 255));
  124.         SelectObject(hdc, hPen);
  125.         SelectObject(hdc, hBrush);
  126.         Ellipse(hdc, playerTyre1X, playerTyre1Y, playerTyre1X + TYRE_SIZE, playerTyre1Y + TYRE_SIZE);
  127.         Ellipse(hdc, playerTyre2X, playerTyre2Y, playerTyre2X + TYRE_SIZE, playerTyre2Y + TYRE_SIZE);
  128.         DeleteObject(hPen);
  129.         DeleteObject(hBrush);
  130.         // Draw AI car
  131.         hBrush = CreateSolidBrush(RGB(0, 0, 255));
  132.         hPen = CreatePen(PS_NULL, 0, RGB(0, 0, 255));
  133.         SelectObject(hdc, hPen);
  134.         SelectObject(hdc, hBrush);
  135.         Rectangle(hdc, aiX, aiY, aiX + CAR_WIDTH, aiY + CAR_HEIGHT);
  136.         DeleteObject(hPen);
  137.         DeleteObject(hBrush);
  138.         // Draw AI tyres
  139.         hBrush = CreateSolidBrush(RGB(255, 255, 255));
  140.         hPen = CreatePen(PS_NULL, 0, RGB(255, 255, 255));
  141.         SelectObject(hdc, hPen);
  142.         SelectObject(hdc, hBrush);
  143.         Ellipse(hdc, aiTyre1X, aiTyre1Y, aiTyre1X + TYRE_SIZE, aiTyre1Y + TYRE_SIZE);
  144.         Ellipse(hdc, aiTyre2X, aiTyre2Y, aiTyre2X + TYRE_SIZE, aiTyre2Y + TYRE_SIZE);
  145.         DeleteObject(hPen);
  146.         DeleteObject(hBrush);
  147.         // Draw timer
  148.         if (timer > 0)
  149.         {
  150.             wchar_t str[10];
  151.             swprintf(str, 10, L"%d", timer);
  152.             TextOut(hdc, WIDTH / 2 - 10, HEIGHT / 2, str, wcslen(str));
  153.         }
  154.         // Draw game over text
  155.         if (gameOver)
  156.         {
  157.             if (playerWon)
  158.             {
  159.                 TextOut(hdc, WIDTH / 2 - 50, HEIGHT / 2, L"You Win!", 7);
  160.             }
  161.             else
  162.             {
  163.                 TextOut(hdc, WIDTH / 2 - 50, HEIGHT / 2, L"AI Wins!", 7);
  164.             }
  165.         }
  166.         EndPaint(hWnd, &ps);
  167.     }
  168.     break;
  169.     case WM_DESTROY:
  170.         KillTimer(hWnd, 1);
  171.         PostQuitMessage(0);
  172.         break;
  173.     default:
  174.         return DefWindowProc(hWnd, message, wParam, lParam);
  175.     }
  176.     return 0;
  177. }
  178.  
  179. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  180. {
  181.     // Register window class
  182.     WNDCLASSEX wc = { 0 };
  183.     wc.cbSize = sizeof(WNDCLASSEX);
  184.     wc.style = 0;
  185.     wc.lpfnWndProc = WndProc;
  186.     wc.cbClsExtra = 0;
  187.     wc.cbWndExtra = 0;
  188.     wc.hInstance = hInstance;
  189.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  190.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  191.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  192.     wc.lpszMenuName = NULL;
  193.     wc.lpszClassName = L"RacingGame";
  194.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  195.     RegisterClassEx(&wc);
  196.  
  197.     // Create window
  198.     HWND hWnd = CreateWindowEx(0, L"RacingGame", L"Racing Game", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT, NULL, NULL, hInstance, NULL);
  199.  
  200.     // Show window
  201.     ShowWindow(hWnd, nCmdShow);
  202.  
  203.     // Main loop
  204.     MSG msg = { 0 };
  205.     while (GetMessage(&msg, NULL, 0, 0))
  206.     {
  207.         TranslateMessage(&msg);
  208.         DispatchMessage(&msg);
  209.     }
  210.  
  211.     return 0;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement