Advertisement
alien_fx_fiend

GPT Racing Car Game (Broken)

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