Advertisement
alien_fx_fiend

2D-Car-Racing *FINAL RELEASE !*

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