Advertisement
alien_fx_fiend

Car Racing Game [*FINAL*] Llama AI Generated

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