Advertisement
alien_fx_fiend

Car Racing Game (Almost Complete)

Jul 26th, 2024
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.54 KB | None | 0 0
  1. /*
  2. #Write Win32 API C++ code for a simple car racing game where player car is positioned below left (a red rectangle) and AI car is blue (rectangle next t9 player car.) The road (a black filled path is from bottom left and extends to top and then right. Timer 1, 2, 3, Go! before game starts. Controls are arrow keys. Using only GDI and C++ logic. Show full sourcecode omitting code comments. Window dimensions should be 1366px by 768px. AI car alongside the player car that moves automatically that completes with human player to the finish (end of road track.) Also it shouldn't be a race against time. Timer is to signal start of game only. Also if possible draw tyre's for cars too (simple will suffice.)
  3.  
  4. #The cars should rotate when pressing left or right arrowkeys the up arrow/ down arrow should move forward, and reverse, respectively. Also winning exit should be the far top rightmost (not right above it). Here's the full source code, please make the changes:
  5. */
  6.  
  7. #include <Windows.h>
  8. #include <ctime>
  9. #include <cstdlib>
  10. #include <math.h>
  11. #include <stdio.h>
  12.  
  13. // Global Variables
  14. const int WIDTH = 1366;
  15. const int HEIGHT = 768;
  16. const int ROAD_WIDTH = 200;
  17. const int CAR_WIDTH = 50;
  18. const int CAR_HEIGHT = 100;
  19. const int TYRE_SIZE = 10;
  20. const int FPS = 60;
  21. const int TIMER = 4;
  22. const int TURN_RADIUS = 5;
  23.  
  24. int playerX = 100;
  25. int playerY = HEIGHT - CAR_HEIGHT - 50;
  26. int playerSpeedX = 0;
  27. int playerSpeedY = 0;
  28. int aiX = playerX + CAR_WIDTH + 20;
  29. int aiY = playerY;
  30. int aiSpeedX = 0;
  31. int aiSpeedY = 0;
  32. int speed = 5;
  33. int aiSpeed = 5;
  34. int timer = TIMER;
  35. int playerTyre1X = playerX + 10;
  36. int playerTyre1Y = playerY + CAR_HEIGHT - TYRE_SIZE;
  37. int playerTyre2X = playerX + CAR_WIDTH - TYRE_SIZE - 10;
  38. int playerTyre2Y = playerY + CAR_HEIGHT - TYRE_SIZE;
  39. int aiTyre1X = aiX + 10;
  40. int aiTyre1Y = aiY + CAR_HEIGHT - TYRE_SIZE;
  41. int aiTyre2X = aiX + CAR_WIDTH - TYRE_SIZE - 10;
  42. int aiTyre2Y = aiY + CAR_HEIGHT - TYRE_SIZE;
  43. float playerAngle = 0.0f;
  44.  
  45. bool gameStarted = false;
  46. bool gameOver = false;
  47. bool playerWon = false;
  48.  
  49. // Window Procedure
  50. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  51. {
  52.     switch (message)
  53.     {
  54.     case WM_CREATE:
  55.         SetTimer(hWnd, 1, 1000 / FPS, NULL);
  56.         break;
  57.     case WM_TIMER:
  58.         if (timer > 0)
  59.         {
  60.             timer--;
  61.             InvalidateRect(hWnd, NULL, TRUE);
  62.         }
  63.         else if (!gameStarted)
  64.         {
  65.             gameStarted = true;
  66.             srand((unsigned int)time(0));
  67.             aiSpeed = rand() % 5 + 3;
  68.         }
  69.         else if (!gameOver)
  70.         {
  71.             if (GetAsyncKeyState(VK_LEFT))
  72.             {
  73.                 playerAngle -= 0.1f;
  74.             }
  75.             if (GetAsyncKeyState(VK_RIGHT))
  76.             {
  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.             playerTyre1X = playerX + 10;
  91.             playerTyre1Y = playerY + CAR_HEIGHT - TYRE_SIZE;
  92.             playerTyre2X = playerX + CAR_WIDTH - TYRE_SIZE - 10;
  93.             playerTyre2Y = playerY + CAR_HEIGHT - TYRE_SIZE;
  94.  
  95.             if (aiX < WIDTH - ROAD_WIDTH)
  96.             {
  97.                 aiSpeedX = aiSpeed;
  98.                 aiSpeedY = 0;
  99.             }
  100.             else if (aiY > 0)
  101.             {
  102.                 aiSpeedX = 0;
  103.                 aiSpeedY = -aiSpeed;
  104.             }
  105.             else
  106.             {
  107.                 aiSpeedX = 0;
  108.                 aiSpeedY = 0;
  109.             }
  110.  
  111.             aiX += aiSpeedX;
  112.             aiY += aiSpeedY;
  113.  
  114.             aiTyre1X = aiX + 10;
  115.             aiTyre1Y = aiY + CAR_HEIGHT - TYRE_SIZE;
  116.             aiTyre2X = aiX + CAR_WIDTH - TYRE_SIZE - 10;
  117.             aiTyre2Y = aiY + CAR_HEIGHT - TYRE_SIZE;
  118.  
  119.             if (playerX > WIDTH - ROAD_WIDTH - CAR_WIDTH && playerY < 0)
  120.             {
  121.                 gameOver = true;
  122.                 playerWon = true;
  123.             }
  124.             if (aiX > WIDTH - ROAD_WIDTH && aiY < 0)
  125.             {
  126.                 gameOver = true;
  127.                 playerWon = false;
  128.             }
  129.  
  130.             InvalidateRect(hWnd, NULL, TRUE);
  131.         }
  132.         break;
  133.     case WM_PAINT:
  134.     {
  135.         PAINTSTRUCT ps;
  136.         HDC hdc = BeginPaint(hWnd, &ps);
  137.         HBRUSH hBrush;
  138.         HPEN hPen;
  139.         // Draw road
  140.         hBrush = CreateSolidBrush(RGB(0, 0, 0));
  141.         hPen = CreatePen(PS_NULL, 0, RGB(0, 0, 0));
  142.         SelectObject(hdc, hPen);
  143.         SelectObject(hdc, hBrush);
  144.         Rectangle(hdc, 0, HEIGHT / 2, ROAD_WIDTH, HEIGHT);
  145.         Rectangle(hdc, 0, 0, WIDTH, HEIGHT / 2);
  146.         Rectangle(hdc, WIDTH - ROAD_WIDTH, 0, WIDTH, HEIGHT / 2);
  147.         DeleteObject(hPen);
  148.         DeleteObject(hBrush);
  149.         // Draw player car
  150.         hBrush = CreateSolidBrush(RGB(255, 0, 0));
  151.         hPen = CreatePen(PS_NULL, 0, RGB(255, 0, 0));
  152.         SelectObject(hdc, hPen);
  153.         SelectObject(hdc, hBrush);
  154.  
  155.         POINT points[4];
  156.         points[0].x = playerX + cos(playerAngle) * CAR_WIDTH / 2 - sin(playerAngle) * CAR_HEIGHT / 2;
  157.         points[0].y = playerY + sin(playerAngle) * CAR_WIDTH / 2 + cos(playerAngle) * CAR_HEIGHT / 2;
  158.         points[1].x = playerX + cos(playerAngle) * CAR_WIDTH / 2 + sin(playerAngle) * CAR_HEIGHT / 2;
  159.         points[1].y = playerY + sin(playerAngle) * CAR_WIDTH / 2 - cos(playerAngle) * CAR_HEIGHT / 2;
  160.         points[2].x = playerX - cos(playerAngle) * CAR_WIDTH / 2 + sin(playerAngle) * CAR_HEIGHT / 2;
  161.         points[2].y = playerY - sin(playerAngle) * CAR_WIDTH / 2 - cos(playerAngle) * CAR_HEIGHT / 2;
  162.         points[3].x = playerX - cos(playerAngle) * CAR_WIDTH / 2 - sin(playerAngle) * CAR_HEIGHT / 2;
  163.         points[3].y = playerY - sin(playerAngle) * CAR_WIDTH / 2 + cos(playerAngle) * CAR_HEIGHT / 2;
  164.         Polygon(hdc, points, 4);
  165.  
  166.         DeleteObject(hPen);
  167.         DeleteObject(hBrush);
  168.         // Draw player tyres
  169.         hBrush = CreateSolidBrush(RGB(255, 255, 255));
  170.         hPen = CreatePen(PS_NULL, 0, RGB(255, 255, 255));
  171.         SelectObject(hdc, hPen);
  172.         SelectObject(hdc, hBrush);
  173.  
  174.         int tyre1X = playerX + cos(playerAngle) * (-CAR_WIDTH / 4) - sin(playerAngle) * (CAR_HEIGHT / 2 + TYRE_SIZE / 2);
  175.         int tyre1Y = playerY + sin(playerAngle) * (-CAR_WIDTH / 4) + cos(playerAngle) * (CAR_HEIGHT / 2 + TYRE_SIZE / 2);
  176.         int tyre2X = playerX + cos(playerAngle) * (CAR_WIDTH / 4) - sin(playerAngle) * (CAR_HEIGHT / 2 + TYRE_SIZE / 2);
  177.         int tyre2Y = playerY + sin(playerAngle) * (CAR_WIDTH / 4) + cos(playerAngle) * (CAR_HEIGHT / 2 + TYRE_SIZE / 2);
  178.  
  179.         Ellipse(hdc, tyre1X, tyre1Y, tyre1X + TYRE_SIZE, tyre1Y + TYRE_SIZE);
  180.         Ellipse(hdc, tyre2X, tyre2Y, tyre2X + TYRE_SIZE, tyre2Y + TYRE_SIZE);
  181.         DeleteObject(hPen);
  182.         DeleteObject(hBrush);
  183.         // Draw AI car
  184.         hBrush = CreateSolidBrush(RGB(0, 0, 255));
  185.         hPen = CreatePen(PS_NULL, 0, RGB(0, 0, 255));
  186.         SelectObject(hdc, hPen);
  187.         SelectObject(hdc, hBrush);
  188.         Rectangle(hdc, aiX, aiY, aiX + CAR_WIDTH, aiY + CAR_HEIGHT);
  189.         DeleteObject(hPen);
  190.         DeleteObject(hBrush);
  191.         // Draw AI tyres
  192.         hBrush = CreateSolidBrush(RGB(255, 255, 255));
  193.         hPen = CreatePen(PS_NULL, 0, RGB(255, 255, 255));
  194.         SelectObject(hdc, hPen);
  195.         SelectObject(hdc, hBrush);
  196.         Ellipse(hdc, aiTyre1X, aiTyre1Y, aiTyre1X + TYRE_SIZE, aiTyre1Y + TYRE_SIZE);
  197.         Ellipse(hdc, aiTyre2X, aiTyre2Y, aiTyre2X + TYRE_SIZE, aiTyre2Y + TYRE_SIZE);
  198.         DeleteObject(hPen);
  199.         DeleteObject(hBrush);
  200.         // Draw timer
  201.         if (timer > 0)
  202.         {
  203.             wchar_t str[10];
  204.             swprintf(str, 10, L"%d", timer);
  205.             TextOut(hdc, WIDTH / 2 - 10, HEIGHT / 2, str, wcslen(str));
  206.         }
  207.         // Draw game over text
  208.         if (gameOver)
  209.         {
  210.             if (playerWon)
  211.             {
  212.                 TextOut(hdc, WIDTH / 2 - 50, HEIGHT / 2, L"You Win!", 7);
  213.             }
  214.             else
  215.             {
  216.                 TextOut(hdc, WIDTH / 2 - 50, HEIGHT / 2, L"AI Wins!", 7);
  217.             }
  218.         }
  219.         EndPaint(hWnd, &ps);
  220.     }
  221.     break;
  222.     case WM_DESTROY:
  223.         KillTimer(hWnd, 1);
  224.         PostQuitMessage(0);
  225.         break;
  226.     default:
  227.         return DefWindowProc(hWnd, message, wParam, lParam);
  228.     }
  229.     return 0;
  230. }
  231.  
  232. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  233. {
  234.     // Register window class
  235.     WNDCLASSEX wc = { 0 };
  236.     wc.cbSize = sizeof(WNDCLASSEX);
  237.     wc.style = 0;
  238.     wc.lpfnWndProc = WndProc;
  239.     wc.cbClsExtra = 0;
  240.     wc.cbWndExtra = 0;
  241.     wc.hInstance = hInstance;
  242.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  243.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  244.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  245.     wc.lpszMenuName = NULL;
  246.     wc.lpszClassName = L"RacingGame";
  247.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  248.     RegisterClassEx(&wc);
  249.  
  250.     // Create window
  251.     HWND hWnd = CreateWindowEx(0, L"RacingGame", L"Racing Game", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT, NULL, NULL, hInstance, NULL);
  252.  
  253.     // Show window
  254.     ShowWindow(hWnd, nCmdShow);
  255.  
  256.     // Main loop
  257.     MSG msg = { 0 };
  258.     while (GetMessage(&msg, NULL, 0, 0))
  259.     {
  260.         TranslateMessage(&msg);
  261.         DispatchMessage(&msg);
  262.     }
  263.  
  264.     return 0;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement