Advertisement
alien_fx_fiend

2D-Car-Racing *FINAL RELEASE !*

Oct 22nd, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.54 KB | Source Code | 0 0
  1. ==++"RaceCar.cpp" File SourceCode++==::
  2. #include <Windows.h>
  3. #include <ctime>
  4. #include <cstdlib>
  5. #include <math.h>
  6. #include <stdio.h>
  7. #include <string>
  8.  
  9. // Global Variables
  10. const int WIDTH = 1366;
  11. const int HEIGHT = 768;
  12. const int ROAD_WIDTH = 200;
  13. const int CAR_WIDTH = 50;
  14. const int CAR_HEIGHT = 100;
  15. const int TYRE_SIZE = 10;
  16. const int FPS = 60;
  17. const int TIMER = 4;
  18. const int TURN_RADIUS = 5;
  19.  
  20. int playerX = 100;
  21. int playerY = HEIGHT - CAR_HEIGHT - 50;
  22. int playerSpeedX = 0;
  23. int playerSpeedY = 0;
  24. int aiX = playerX + CAR_WIDTH + 20;
  25. int aiY = playerY;
  26. int aiSpeedX = 0;
  27. int aiSpeedY = 0;
  28. int speed = 5;
  29. int aiSpeed = 5;
  30. int timer = TIMER;
  31. int playerTyre1X = playerX + 10;
  32. int playerTyre1Y = playerY + CAR_HEIGHT - TYRE_SIZE;
  33. int playerTyre2X = playerX + CAR_WIDTH - TYRE_SIZE - 10;
  34. int playerTyre2Y = playerY + CAR_HEIGHT - TYRE_SIZE;
  35. int aiTyre1X = aiX + 10;
  36. int aiTyre1Y = aiY + CAR_HEIGHT - TYRE_SIZE;
  37. int aiTyre2X = aiX + CAR_WIDTH - TYRE_SIZE - 10;
  38. int aiTyre2Y = aiY + CAR_HEIGHT - TYRE_SIZE;
  39. float playerAngle = 0.0f;
  40.  
  41. bool gameStarted = false;
  42. bool gameOver = false;
  43. bool playerWon = false;
  44.  
  45. // Window Procedure
  46. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  47. {
  48.     switch (message)
  49.     {
  50.     case WM_CREATE:
  51.         SetTimer(hWnd, 1, 1000 / FPS, NULL);
  52.         break;
  53.     case WM_TIMER:
  54.         if (timer > 0)
  55.         {
  56.             timer--;
  57.             InvalidateRect(hWnd, NULL, TRUE);
  58.         }
  59.         else if (!gameStarted)
  60.         {
  61.             gameStarted = true;
  62.             srand((unsigned int)time(0));
  63.             aiSpeed = rand() % 5 + 3;
  64.         }
  65.         else if (!gameOver)
  66.         {
  67.             if (GetAsyncKeyState(VK_LEFT))
  68.             {
  69.                 if (GetAsyncKeyState(VK_DOWN))
  70.                     playerAngle += 0.1f;
  71.                 else
  72.                     playerAngle -= 0.1f;
  73.             }
  74.             if (GetAsyncKeyState(VK_RIGHT))
  75.             {
  76.                 if (GetAsyncKeyState(VK_DOWN))
  77.                     playerAngle -= 0.1f;
  78.                 else
  79.                     playerAngle += 0.1f;
  80.             }
  81.             if (GetAsyncKeyState(VK_UP))
  82.             {
  83.                 playerX += sin(playerAngle) * speed;
  84.                 playerY -= cos(playerAngle) * speed;
  85.             }
  86.             if (GetAsyncKeyState(VK_DOWN))
  87.             {
  88.                 playerX -= sin(playerAngle) * speed;
  89.                 playerY += cos(playerAngle) * speed;
  90.             }
  91.  
  92.             // Player car boundary check
  93.             if (playerX < 0)
  94.                 playerX = 0;
  95.             if (playerX > WIDTH - CAR_WIDTH)
  96.                 playerX = WIDTH - CAR_WIDTH;
  97.             if (playerY < 0)
  98.                 playerY = 0;
  99.             if (playerY > HEIGHT - CAR_HEIGHT)
  100.                 playerY = HEIGHT - CAR_HEIGHT;
  101.  
  102.             playerTyre1X = playerX + 10;
  103.             playerTyre1Y = playerY + CAR_HEIGHT - TYRE_SIZE;
  104.             playerTyre2X = playerX + CAR_WIDTH - TYRE_SIZE - 10;
  105.             playerTyre2Y = playerY + CAR_HEIGHT - TYRE_SIZE;
  106.  
  107.             if (aiX < WIDTH - ROAD_WIDTH)
  108.             {
  109.                 aiSpeedX = aiSpeed;
  110.                 aiSpeedY = 0;
  111.             }
  112.             else
  113.             {
  114.                 aiSpeedX = 0;
  115.                 aiSpeedY = -aiSpeed;
  116.             }
  117.  
  118.             aiX += aiSpeedX;
  119.             aiY += aiSpeedY;
  120.  
  121.             // AI car boundary check
  122.             if (aiX < 0)
  123.                 aiX = 0;
  124.             if (aiX > WIDTH - CAR_WIDTH)
  125.                 aiX = WIDTH - CAR_WIDTH;
  126.             if (aiY < 0)
  127.                 aiY = 0;
  128.             if (aiY > HEIGHT - CAR_HEIGHT)
  129.                 aiY = HEIGHT - CAR_HEIGHT;
  130.  
  131.             aiTyre1X = aiX + 10;
  132.             aiTyre1Y = aiY + CAR_HEIGHT - TYRE_SIZE;
  133.             aiTyre2X = aiX + CAR_WIDTH - TYRE_SIZE - 10;
  134.             aiTyre2Y = aiY + CAR_HEIGHT - TYRE_SIZE;
  135.  
  136.             // Collision detection between player and AI cars            
  137.             // Define the corners of the player car
  138.             int playerCorner1X = playerX;
  139.             int playerCorner1Y = playerY;
  140.             int playerCorner2X = playerX + CAR_WIDTH;
  141.             int playerCorner2Y = playerY;
  142.             int playerCorner3X = playerX + CAR_WIDTH;
  143.             int playerCorner3Y = playerY + CAR_HEIGHT;
  144.             int playerCorner4X = playerX;
  145.             int playerCorner4Y = playerY + CAR_HEIGHT;
  146.  
  147.             // Define the corners of the AI car
  148.             int aiCorner1X = aiX;
  149.             int aiCorner1Y = aiY;
  150.             int aiCorner2X = aiX + CAR_WIDTH;
  151.             int aiCorner2Y = aiY;
  152.             int aiCorner3X = aiX + CAR_WIDTH;
  153.             int aiCorner3Y = aiY + CAR_HEIGHT;
  154.             int aiCorner4X = aiX;
  155.             int aiCorner4Y = aiY + CAR_HEIGHT;
  156.  
  157.             // Check if the player car is too close to the opponent car from behind
  158.             if (playerY + CAR_HEIGHT > aiY &&
  159.                 playerY < aiY + CAR_HEIGHT &&
  160.                 playerX + CAR_WIDTH > aiX &&
  161.                 playerX < aiX + CAR_WIDTH)
  162.             {
  163.                 // Prevent the player car from moving forward
  164.                 if (GetAsyncKeyState(VK_UP))
  165.                 {
  166.                     playerX -= sin(playerAngle) * speed;
  167.                     playerY += cos(playerAngle) * speed;
  168.                 }
  169.             }
  170.  
  171.             // Check if any of the player car's corners are inside the AI car
  172.             if ((playerCorner1X > aiCorner1X && playerCorner1X < aiCorner3X &&
  173.                 playerCorner1Y > aiCorner1Y && playerCorner1Y < aiCorner3Y) ||
  174.                 (playerCorner2X > aiCorner1X && playerCorner2X < aiCorner3X &&
  175.                     playerCorner2Y > aiCorner1Y && playerCorner2Y < aiCorner3Y) ||
  176.                 (playerCorner3X > aiCorner1X && playerCorner3X < aiCorner3X &&
  177.                     playerCorner3Y > aiCorner1Y && playerCorner3Y < aiCorner3Y) ||
  178.                 (playerCorner4X > aiCorner1X && playerCorner4X < aiCorner3X &&
  179.                     playerCorner4Y > aiCorner1Y && playerCorner4Y < aiCorner3Y) ||
  180.                 // Check if any of the AI car's corners are inside the player car
  181.                 (aiCorner1X > playerCorner1X && aiCorner1X < playerCorner3X &&
  182.                     aiCorner1Y > playerCorner1Y && aiCorner1Y < playerCorner3Y) ||
  183.                 (aiCorner2X > playerCorner1X && aiCorner2X < playerCorner3X &&
  184.                     aiCorner2Y > playerCorner1Y && aiCorner2Y < playerCorner3Y) ||
  185.                 (aiCorner3X > playerCorner1X && aiCorner3X < playerCorner3X &&
  186.                     aiCorner3Y > playerCorner1Y && aiCorner3Y < playerCorner3Y) ||
  187.                 (aiCorner4X > playerCorner1X && aiCorner4X < playerCorner3X &&
  188.                     aiCorner4Y > playerCorner1Y && aiCorner4Y < playerCorner3Y))
  189.             {
  190.                 // Move the player car back to prevent collision
  191.                 if (GetAsyncKeyState(VK_UP))
  192.                 {
  193.                     playerX -= sin(playerAngle) * speed;
  194.                     playerY += cos(playerAngle) * speed;
  195.                 }
  196.                 if (GetAsyncKeyState(VK_DOWN))
  197.                 {
  198.                     playerX += sin(playerAngle) * speed;
  199.                     playerY -= cos(playerAngle) * speed;
  200.                 }
  201.  
  202.                 // Move the AI car back to prevent collision
  203.                 aiX -= aiSpeedX;
  204.                 aiY -= aiSpeedY;
  205.             }
  206.             /*if (playerX < aiX + CAR_WIDTH &&
  207.                 playerX + CAR_WIDTH > aiX &&
  208.                 playerY < aiY + CAR_HEIGHT &&
  209.                 playerY + CAR_HEIGHT > aiY)
  210.             {
  211.                 // Move the player car back to prevent collision
  212.                 if (GetAsyncKeyState(VK_UP))
  213.                 {
  214.                     playerX -= sin(playerAngle) * speed;
  215.                     playerY += cos(playerAngle) * speed;
  216.                 }
  217.                 if (GetAsyncKeyState(VK_DOWN))
  218.                 {
  219.                     playerX += sin(playerAngle) * speed;
  220.                     playerY -= cos(playerAngle) * speed;
  221.                 }
  222.             }*/
  223.             /*if (playerX < aiX + CAR_WIDTH &&
  224.                 playerX + CAR_WIDTH > aiX &&
  225.                 playerY < aiY + CAR_HEIGHT &&
  226.                 playerY + CAR_HEIGHT > aiY)
  227.             {
  228.                 // Handle collision (e.g., game over)
  229.                 //gameOver = true;
  230.             }*/
  231.  
  232.             if (playerX > WIDTH - ROAD_WIDTH - CAR_WIDTH && playerY < 0)
  233.             {
  234.                 gameOver = false;
  235.                 playerWon = true;
  236.             }
  237.             if (aiX > WIDTH - ROAD_WIDTH && aiY < 0)
  238.             {
  239.                 gameOver = false;
  240.                 playerWon = false;
  241.             }
  242.  
  243.             InvalidateRect(hWnd, NULL, TRUE);
  244.         }
  245.         break;
  246.     case WM_PAINT:
  247.     {
  248.         PAINTSTRUCT ps;
  249.         HDC hdc = BeginPaint(hWnd, &ps);
  250.         HBRUSH hBrush;
  251.         HPEN hPen;
  252.         // Draw road
  253.         hBrush = CreateSolidBrush(RGB(0, 0, 0));
  254.         hPen = CreatePen(PS_NULL, 0, RGB(0, 0, 0));
  255.         SelectObject(hdc, hPen);
  256.         SelectObject(hdc, hBrush);
  257.         Rectangle(hdc, 0, HEIGHT / 2, ROAD_WIDTH, HEIGHT);
  258.         Rectangle(hdc, 0, 0, WIDTH, HEIGHT / 2);
  259.         Rectangle(hdc, WIDTH - ROAD_WIDTH, 0, WIDTH, HEIGHT / 2);
  260.         DeleteObject(hPen);
  261.         DeleteObject(hBrush);
  262.         // Draw player car
  263.         hBrush = CreateSolidBrush(RGB(255, 0, 0));
  264.         hPen = CreatePen(PS_NULL, 0, RGB(255, 0, 0));
  265.         SelectObject(hdc, hPen);
  266.         SelectObject(hdc, hBrush);
  267.  
  268.         POINT points[4];
  269.         points[0].x = playerX + cos(playerAngle) * CAR_WIDTH / 2 - sin(playerAngle) * CAR_HEIGHT / 2;
  270.         points[0].y = playerY + sin(playerAngle) * CAR_WIDTH / 2 + cos(playerAngle) * CAR_HEIGHT / 2;
  271.         points[1].x = playerX + cos(playerAngle) * CAR_WIDTH / 2 + sin(playerAngle) * CAR_HEIGHT / 2;
  272.         points[1].y = playerY + sin(playerAngle) * CAR_WIDTH / 2 - cos(playerAngle) * CAR_HEIGHT / 2;
  273.         points[2].x = playerX - cos(playerAngle) * CAR_WIDTH / 2 + sin(playerAngle) * CAR_HEIGHT / 2;
  274.         points[2].y = playerY - sin(playerAngle) * CAR_WIDTH / 2 - cos(playerAngle) * CAR_HEIGHT / 2;
  275.         points[3].x = playerX - cos(playerAngle) * CAR_WIDTH / 2 - sin(playerAngle) * CAR_HEIGHT / 2;
  276.         points[3].y = playerY - sin(playerAngle) * CAR_WIDTH / 2 + cos(playerAngle) * CAR_HEIGHT / 2;
  277.         Polygon(hdc, points, 4);
  278.  
  279.         DeleteObject(hPen);
  280.         DeleteObject(hBrush);
  281.         // Draw player tyres
  282.         hBrush = CreateSolidBrush(RGB(255, 255, 255));
  283.         hPen = CreatePen(PS_NULL, 0, RGB(255, 255, 255));
  284.         SelectObject(hdc, hPen);
  285.         SelectObject(hdc, hBrush);
  286.  
  287.         int tyre1X = playerX + cos(playerAngle) * (-CAR_WIDTH / 4) - sin(playerAngle) * (-CAR_HEIGHT / 2 - TYRE_SIZE / 2);
  288.         int tyre1Y = playerY + sin(playerAngle) * (-CAR_WIDTH / 4) + cos(playerAngle) * (-CAR_HEIGHT / 2 - TYRE_SIZE / 2);
  289.         int tyre2X = playerX + cos(playerAngle) * (CAR_WIDTH / 4) - sin(playerAngle) * (-CAR_HEIGHT / 2 - TYRE_SIZE / 2);
  290.         int tyre2Y = playerY + sin(playerAngle) * (CAR_WIDTH / 4) + cos(playerAngle) * (-CAR_HEIGHT / 2 - TYRE_SIZE / 2);
  291.  
  292.         Ellipse(hdc, tyre1X, tyre1Y, tyre1X + TYRE_SIZE, tyre1Y + TYRE_SIZE);
  293.         Ellipse(hdc, tyre2X, tyre2Y, tyre2X + TYRE_SIZE, tyre2Y + TYRE_SIZE);
  294.         DeleteObject(hPen);
  295.         DeleteObject(hBrush);
  296.         // Draw AI car
  297.         hBrush = CreateSolidBrush(RGB(0, 0, 255));
  298.         hPen = CreatePen(PS_NULL, 0, RGB(0, 0, 255));
  299.         SelectObject(hdc, hPen);
  300.         SelectObject(hdc, hBrush);
  301.         Rectangle(hdc, aiX, aiY, aiX + CAR_WIDTH, aiY + CAR_HEIGHT);
  302.         DeleteObject(hPen);
  303.         DeleteObject(hBrush);
  304.         // Draw AI tyres
  305.         hBrush = CreateSolidBrush(RGB(255, 255, 255));
  306.         hPen = CreatePen(PS_NULL, 0, RGB(255, 255, 255));
  307.         SelectObject(hdc, hPen);
  308.         SelectObject(hdc, hBrush);
  309.         Ellipse(hdc, aiX + 10, aiY, aiX + 10 + TYRE_SIZE, aiY + TYRE_SIZE);
  310.         Ellipse(hdc, aiX + CAR_WIDTH - TYRE_SIZE - 10, aiY, aiX + CAR_WIDTH - 10, aiY + TYRE_SIZE);
  311.         DeleteObject(hPen);
  312.         DeleteObject(hBrush);
  313.         // Draw timer
  314.         if (timer > 0)
  315.         {
  316.             wchar_t str[10];
  317.             swprintf(str, 10, L"%d", timer);
  318.             TextOut(hdc, WIDTH / 2 - 10, HEIGHT / 2, str, wcslen(str));
  319.         }
  320.         // Draw game over text
  321.         if (gameOver)
  322.         {
  323.             if (playerWon)
  324.             {
  325.                 TextOut(hdc, WIDTH / 2 - 50, HEIGHT / 2, L"You Win!", 7);
  326.             }
  327.             else
  328.             {
  329.                 TextOut(hdc, WIDTH / 2 - 50, HEIGHT / 2, L"AI Wins!", 7);
  330.             }
  331.         }
  332.         EndPaint(hWnd, &ps);
  333.     }
  334.     break;
  335.     case WM_DESTROY:
  336.         KillTimer(hWnd, 1);
  337.         PostQuitMessage(0);
  338.         break;
  339.     case WM_KEYDOWN:
  340.         if (wParam == VK_F1)
  341.         {
  342.             MessageBoxW(hWnd, L"2D Racing Game 3.0 Programmed in C++ Win32 API (395 lines of code) by Entisoft Software (c) Evans Thorpemorton", L"About", MB_OK | MB_ICONINFORMATION);
  343.         }
  344.         //break;
  345.         if (wParam == VK_ESCAPE)
  346.         {
  347.             PostQuitMessage(0);
  348.         }
  349.         break;
  350.     default:
  351.         return DefWindowProc(hWnd, message, wParam, lParam);
  352.     }
  353.     return 0;
  354. }
  355.  
  356. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  357. {
  358.     // Register window class
  359.     WNDCLASSEX wc = { 0 };
  360.     wc.cbSize = sizeof(WNDCLASSEX);
  361.     wc.style = 0;
  362.     wc.lpfnWndProc = WndProc;
  363.     wc.cbClsExtra = 0;
  364.     wc.cbWndExtra = 0;
  365.     wc.hInstance = hInstance;
  366.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  367.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  368.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  369.     wc.lpszMenuName = NULL;
  370.     wc.lpszClassName = L"RacingGame";
  371.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  372.     RegisterClassEx(&wc);
  373.  
  374.     // Calculate the position to center the window
  375.     int screenWidth = GetSystemMetrics(SM_CXSCREEN);
  376.     int screenHeight = GetSystemMetrics(SM_CYSCREEN);
  377.     int windowX = (screenWidth - WIDTH) / 2;
  378.     int windowY = (screenHeight - HEIGHT) / 2;
  379.  
  380.     // Create window
  381.     HWND hWnd = CreateWindowEx(0, L"RacingGame", L"Racing Game (ArrowKeys=Move)", WS_OVERLAPPEDWINDOW, windowX, windowY, WIDTH, HEIGHT, NULL, NULL, hInstance, NULL);
  382.  
  383.     // Show window
  384.     //ShowWindow(hWnd, nCmdShow);
  385.     ShowWindow(hWnd, SW_SHOWMAXIMIZED);
  386.  
  387.     // Main loop
  388.     MSG msg = { 0 };
  389.     while (GetMessage(&msg, NULL, 0, 0))
  390.     {
  391.         TranslateMessage(&msg);
  392.         DispatchMessage(&msg);
  393.     }
  394.  
  395.     return 0;
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement