Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <ctime>
- #include <cstdlib>
- #include <math.h>
- #include <stdio.h>
- // Global Variables
- const int WIDTH = 1366;
- const int HEIGHT = 768;
- const int ROAD_WIDTH = 200;
- const int CAR_WIDTH = 50;
- const int CAR_HEIGHT = 100;
- const int TYRE_SIZE = 10;
- const int FPS = 60;
- const int TIMER = 4;
- const int TURN_RADIUS = 5;
- int playerX = 100;
- int playerY = HEIGHT - CAR_HEIGHT - 50;
- int playerSpeedX = 0;
- int playerSpeedY = 0;
- int aiX = playerX + CAR_WIDTH + 20;
- int aiY = playerY;
- int aiSpeedX = 0;
- int aiSpeedY = 0;
- int speed = 5;
- int aiSpeed = 5;
- int timer = TIMER;
- int playerTyre1X = playerX + 10;
- int playerTyre1Y = playerY + CAR_HEIGHT - TYRE_SIZE;
- int playerTyre2X = playerX + CAR_WIDTH - TYRE_SIZE - 10;
- int playerTyre2Y = playerY + CAR_HEIGHT - TYRE_SIZE;
- int aiTyre1X = aiX + 10;
- int aiTyre1Y = aiY + CAR_HEIGHT - TYRE_SIZE;
- int aiTyre2X = aiX + CAR_WIDTH - TYRE_SIZE - 10;
- int aiTyre2Y = aiY + CAR_HEIGHT - TYRE_SIZE;
- float playerAngle = 0.0f;
- bool gameStarted = false;
- bool gameOver = false;
- bool playerWon = false;
- // Window Procedure
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_CREATE:
- SetTimer(hWnd, 1, 1000 / FPS, NULL);
- break;
- case WM_TIMER:
- if (timer > 0)
- {
- timer--;
- InvalidateRect(hWnd, NULL, TRUE);
- }
- else if (!gameStarted)
- {
- gameStarted = true;
- srand((unsigned int)time(0));
- aiSpeed = rand() % 5 + 3;
- }
- else if (!gameOver)
- {
- if (GetAsyncKeyState(VK_LEFT))
- {
- if (GetAsyncKeyState(VK_DOWN))
- playerAngle += 0.1f;
- else
- playerAngle -= 0.1f;
- }
- if (GetAsyncKeyState(VK_RIGHT))
- {
- if (GetAsyncKeyState(VK_DOWN))
- playerAngle -= 0.1f;
- else
- playerAngle += 0.1f;
- }
- if (GetAsyncKeyState(VK_UP))
- {
- playerX += sin(playerAngle) * speed;
- playerY -= cos(playerAngle) * speed;
- }
- if (GetAsyncKeyState(VK_DOWN))
- {
- playerX -= sin(playerAngle) * speed;
- playerY += cos(playerAngle) * speed;
- }
- // Player car boundary check
- if (playerX < 0)
- playerX = 0;
- if (playerX > WIDTH - CAR_WIDTH)
- playerX = WIDTH - CAR_WIDTH;
- if (playerY < 0)
- playerY = 0;
- if (playerY > HEIGHT - CAR_HEIGHT)
- playerY = HEIGHT - CAR_HEIGHT;
- playerTyre1X = playerX + 10;
- playerTyre1Y = playerY + CAR_HEIGHT - TYRE_SIZE;
- playerTyre2X = playerX + CAR_WIDTH - TYRE_SIZE - 10;
- playerTyre2Y = playerY + CAR_HEIGHT - TYRE_SIZE;
- if (aiX < WIDTH - ROAD_WIDTH)
- {
- aiSpeedX = aiSpeed;
- aiSpeedY = 0;
- }
- else
- {
- aiSpeedX = 0;
- aiSpeedY = -aiSpeed;
- }
- aiX += aiSpeedX;
- aiY += aiSpeedY;
- // AI car boundary check
- if (aiX < 0)
- aiX = 0;
- if (aiX > WIDTH - CAR_WIDTH)
- aiX = WIDTH - CAR_WIDTH;
- if (aiY < 0)
- aiY = 0;
- if (aiY > HEIGHT - CAR_HEIGHT)
- aiY = HEIGHT - CAR_HEIGHT;
- aiTyre1X = aiX + 10;
- aiTyre1Y = aiY + CAR_HEIGHT - TYRE_SIZE;
- aiTyre2X = aiX + CAR_WIDTH - TYRE_SIZE - 10;
- aiTyre2Y = aiY + CAR_HEIGHT - TYRE_SIZE;
- // Collision detection between player and AI cars
- // Define the corners of the player car
- int playerCorner1X = playerX;
- int playerCorner1Y = playerY;
- int playerCorner2X = playerX + CAR_WIDTH;
- int playerCorner2Y = playerY;
- int playerCorner3X = playerX + CAR_WIDTH;
- int playerCorner3Y = playerY + CAR_HEIGHT;
- int playerCorner4X = playerX;
- int playerCorner4Y = playerY + CAR_HEIGHT;
- // Define the corners of the AI car
- int aiCorner1X = aiX;
- int aiCorner1Y = aiY;
- int aiCorner2X = aiX + CAR_WIDTH;
- int aiCorner2Y = aiY;
- int aiCorner3X = aiX + CAR_WIDTH;
- int aiCorner3Y = aiY + CAR_HEIGHT;
- int aiCorner4X = aiX;
- int aiCorner4Y = aiY + CAR_HEIGHT;
- // Check if the player car is too close to the opponent car from behind
- if (playerY + CAR_HEIGHT > aiY &&
- playerY < aiY + CAR_HEIGHT &&
- playerX + CAR_WIDTH > aiX &&
- playerX < aiX + CAR_WIDTH)
- {
- // Prevent the player car from moving forward
- if (GetAsyncKeyState(VK_UP))
- {
- playerX -= sin(playerAngle) * speed;
- playerY += cos(playerAngle) * speed;
- }
- }
- // Check if any of the player car's corners are inside the AI car
- if ((playerCorner1X > aiCorner1X && playerCorner1X < aiCorner3X &&
- playerCorner1Y > aiCorner1Y && playerCorner1Y < aiCorner3Y) ||
- (playerCorner2X > aiCorner1X && playerCorner2X < aiCorner3X &&
- playerCorner2Y > aiCorner1Y && playerCorner2Y < aiCorner3Y) ||
- (playerCorner3X > aiCorner1X && playerCorner3X < aiCorner3X &&
- playerCorner3Y > aiCorner1Y && playerCorner3Y < aiCorner3Y) ||
- (playerCorner4X > aiCorner1X && playerCorner4X < aiCorner3X &&
- playerCorner4Y > aiCorner1Y && playerCorner4Y < aiCorner3Y) ||
- // Check if any of the AI car's corners are inside the player car
- (aiCorner1X > playerCorner1X && aiCorner1X < playerCorner3X &&
- aiCorner1Y > playerCorner1Y && aiCorner1Y < playerCorner3Y) ||
- (aiCorner2X > playerCorner1X && aiCorner2X < playerCorner3X &&
- aiCorner2Y > playerCorner1Y && aiCorner2Y < playerCorner3Y) ||
- (aiCorner3X > playerCorner1X && aiCorner3X < playerCorner3X &&
- aiCorner3Y > playerCorner1Y && aiCorner3Y < playerCorner3Y) ||
- (aiCorner4X > playerCorner1X && aiCorner4X < playerCorner3X &&
- aiCorner4Y > playerCorner1Y && aiCorner4Y < playerCorner3Y))
- {
- // Move the player car back to prevent collision
- if (GetAsyncKeyState(VK_UP))
- {
- playerX -= sin(playerAngle) * speed;
- playerY += cos(playerAngle) * speed;
- }
- if (GetAsyncKeyState(VK_DOWN))
- {
- playerX += sin(playerAngle) * speed;
- playerY -= cos(playerAngle) * speed;
- }
- // Move the AI car back to prevent collision
- aiX -= aiSpeedX;
- aiY -= aiSpeedY;
- }
- /*if (playerX < aiX + CAR_WIDTH &&
- playerX + CAR_WIDTH > aiX &&
- playerY < aiY + CAR_HEIGHT &&
- playerY + CAR_HEIGHT > aiY)
- {
- // Move the player car back to prevent collision
- if (GetAsyncKeyState(VK_UP))
- {
- playerX -= sin(playerAngle) * speed;
- playerY += cos(playerAngle) * speed;
- }
- if (GetAsyncKeyState(VK_DOWN))
- {
- playerX += sin(playerAngle) * speed;
- playerY -= cos(playerAngle) * speed;
- }
- }*/
- /*if (playerX < aiX + CAR_WIDTH &&
- playerX + CAR_WIDTH > aiX &&
- playerY < aiY + CAR_HEIGHT &&
- playerY + CAR_HEIGHT > aiY)
- {
- // Handle collision (e.g., game over)
- //gameOver = true;
- }*/
- if (playerX > WIDTH - ROAD_WIDTH - CAR_WIDTH && playerY < 0)
- {
- gameOver = false;
- playerWon = true;
- }
- if (aiX > WIDTH - ROAD_WIDTH && aiY < 0)
- {
- gameOver = false;
- playerWon = false;
- }
- InvalidateRect(hWnd, NULL, TRUE);
- }
- break;
- case WM_PAINT:
- {
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint(hWnd, &ps);
- HBRUSH hBrush;
- HPEN hPen;
- // Draw road
- hBrush = CreateSolidBrush(RGB(0, 0, 0));
- hPen = CreatePen(PS_NULL, 0, RGB(0, 0, 0));
- SelectObject(hdc, hPen);
- SelectObject(hdc, hBrush);
- Rectangle(hdc, 0, HEIGHT / 2, ROAD_WIDTH, HEIGHT);
- Rectangle(hdc, 0, 0, WIDTH, HEIGHT / 2);
- Rectangle(hdc, WIDTH - ROAD_WIDTH, 0, WIDTH, HEIGHT / 2);
- DeleteObject(hPen);
- DeleteObject(hBrush);
- // Draw player car
- hBrush = CreateSolidBrush(RGB(255, 0, 0));
- hPen = CreatePen(PS_NULL, 0, RGB(255, 0, 0));
- SelectObject(hdc, hPen);
- SelectObject(hdc, hBrush);
- POINT points[4];
- points[0].x = playerX + cos(playerAngle) * CAR_WIDTH / 2 - sin(playerAngle) * CAR_HEIGHT / 2;
- points[0].y = playerY + sin(playerAngle) * CAR_WIDTH / 2 + cos(playerAngle) * CAR_HEIGHT / 2;
- points[1].x = playerX + cos(playerAngle) * CAR_WIDTH / 2 + sin(playerAngle) * CAR_HEIGHT / 2;
- points[1].y = playerY + sin(playerAngle) * CAR_WIDTH / 2 - cos(playerAngle) * CAR_HEIGHT / 2;
- points[2].x = playerX - cos(playerAngle) * CAR_WIDTH / 2 + sin(playerAngle) * CAR_HEIGHT / 2;
- points[2].y = playerY - sin(playerAngle) * CAR_WIDTH / 2 - cos(playerAngle) * CAR_HEIGHT / 2;
- points[3].x = playerX - cos(playerAngle) * CAR_WIDTH / 2 - sin(playerAngle) * CAR_HEIGHT / 2;
- points[3].y = playerY - sin(playerAngle) * CAR_WIDTH / 2 + cos(playerAngle) * CAR_HEIGHT / 2;
- Polygon(hdc, points, 4);
- DeleteObject(hPen);
- DeleteObject(hBrush);
- // Draw player tyres
- hBrush = CreateSolidBrush(RGB(255, 255, 255));
- hPen = CreatePen(PS_NULL, 0, RGB(255, 255, 255));
- SelectObject(hdc, hPen);
- SelectObject(hdc, hBrush);
- int tyre1X = playerX + cos(playerAngle) * (-CAR_WIDTH / 4) - sin(playerAngle) * (CAR_HEIGHT / 2 + TYRE_SIZE / 2);
- int tyre1Y = playerY + sin(playerAngle) * (-CAR_WIDTH / 4) + cos(playerAngle) * (CAR_HEIGHT / 2 + TYRE_SIZE / 2);
- int tyre2X = playerX + cos(playerAngle) * (CAR_WIDTH / 4) - sin(playerAngle) * (CAR_HEIGHT / 2 + TYRE_SIZE / 2);
- int tyre2Y = playerY + sin(playerAngle) * (CAR_WIDTH / 4) + cos(playerAngle) * (CAR_HEIGHT / 2 + TYRE_SIZE / 2);
- Ellipse(hdc, tyre1X, tyre1Y, tyre1X + TYRE_SIZE, tyre1Y + TYRE_SIZE);
- Ellipse(hdc, tyre2X, tyre2Y, tyre2X + TYRE_SIZE, tyre2Y + TYRE_SIZE);
- DeleteObject(hPen);
- DeleteObject(hBrush);
- // Draw AI car
- hBrush = CreateSolidBrush(RGB(0, 0, 255));
- hPen = CreatePen(PS_NULL, 0, RGB(0, 0, 255));
- SelectObject(hdc, hPen);
- SelectObject(hdc, hBrush);
- Rectangle(hdc, aiX, aiY, aiX + CAR_WIDTH, aiY + CAR_HEIGHT);
- DeleteObject(hPen);
- DeleteObject(hBrush);
- // Draw AI tyres
- hBrush = CreateSolidBrush(RGB(255, 255, 255));
- hPen = CreatePen(PS_NULL, 0, RGB(255, 255, 255));
- SelectObject(hdc, hPen);
- SelectObject(hdc, hBrush);
- Ellipse(hdc, aiTyre1X, aiTyre1Y, aiTyre1X + TYRE_SIZE, aiTyre1Y + TYRE_SIZE);
- Ellipse(hdc, aiTyre2X, aiTyre2Y, aiTyre2X + TYRE_SIZE, aiTyre2Y + TYRE_SIZE);
- DeleteObject(hPen);
- DeleteObject(hBrush);
- // Draw timer
- if (timer > 0)
- {
- wchar_t str[10];
- swprintf(str, 10, L"%d", timer);
- TextOut(hdc, WIDTH / 2 - 10, HEIGHT / 2, str, wcslen(str));
- }
- // Draw game over text
- if (gameOver)
- {
- if (playerWon)
- {
- TextOut(hdc, WIDTH / 2 - 50, HEIGHT / 2, L"You Win!", 7);
- }
- else
- {
- TextOut(hdc, WIDTH / 2 - 50, HEIGHT / 2, L"AI Wins!", 7);
- }
- }
- EndPaint(hWnd, &ps);
- }
- break;
- case WM_DESTROY:
- KillTimer(hWnd, 1);
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- // Register window class
- WNDCLASSEX wc = { 0 };
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.style = 0;
- wc.lpfnWndProc = WndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- wc.lpszMenuName = NULL;
- wc.lpszClassName = L"RacingGame";
- wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- RegisterClassEx(&wc);
- // Create window
- HWND hWnd = CreateWindowEx(0, L"RacingGame", L"Racing Game", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT, NULL, NULL, hInstance, NULL);
- // Show window
- ShowWindow(hWnd, nCmdShow);
- // Main loop
- MSG msg = { 0 };
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement