Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ==++ Here's the full source code for (file 1/1) "RaceCar.cpp"::++==
- #include <Windows.h>
- #include <ctime>
- #include <cstdlib>
- #include <math.h>
- #include <stdio.h>
- #include <string>
- #include "resource.h" // Add this with your other includes
- // 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;
- const double PI = 3.14159265358979323846;
- int playerX = 100;
- int playerY = HEIGHT - CAR_HEIGHT - 50;
- int playerSpeedX = 0;
- int playerSpeedY = 0;
- int aiX = playerX + CAR_WIDTH + 20;
- int aiY = playerY;
- float aiAngle = -PI / 2; // Add this line
- 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 = -PI / 2; // Initialize to face North by default
- //float playerAngle = 0.0f;
- bool gameStarted = false;
- bool gameOver = false;
- bool playerWon = false;
- bool godMode = false;
- //int timer = 30 * 10; // 30 seconds * 10 (timer resolution)
- // 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, FALSE);
- }
- else if (!gameStarted)
- {
- gameStarted = true;
- srand((unsigned int)time(0));
- aiSpeed = rand() % 5 + 3;
- // Set initial positions for both cars on the lower left road, facing up
- playerX = ROAD_WIDTH / 2 - CAR_WIDTH / 2;
- playerY = HEIGHT - CAR_HEIGHT - 20;
- playerAngle = -PI / 2; // -90 degrees, pointing straight up
- aiX = ROAD_WIDTH / 2 - CAR_WIDTH / 2;
- aiY = HEIGHT - CAR_HEIGHT - 100;
- aiAngle = -PI / 2; // -90 degrees, pointing north
- }
- else if (!gameOver)
- {
- // Add God Mode toggle
- if (GetAsyncKeyState('G') & 1) // Check if G key was just pressed
- {
- godMode = !godMode;
- }
- // Store previous position for collision recovery
- float prevPlayerX = playerX;
- float prevPlayerY = playerY;
- float prevPlayerAngle = playerAngle;
- // Player car controls
- if (GetAsyncKeyState(VK_LEFT))
- {
- if (GetAsyncKeyState(VK_DOWN))
- playerAngle += 0.05f; // Reverse turning
- else
- playerAngle -= 0.05f; // Forward turning
- }
- if (GetAsyncKeyState(VK_RIGHT))
- {
- if (GetAsyncKeyState(VK_DOWN))
- playerAngle -= 0.05f; // Reverse turning
- else
- playerAngle += 0.05f; // Forward turning
- }
- // Forward/Backward movement in the direction the car is facing
- if (GetAsyncKeyState(VK_UP))
- {
- // Move forward in the direction of playerAngle
- playerX += sin(playerAngle) * speed;
- playerY -= cos(playerAngle) * speed;
- }
- if (GetAsyncKeyState(VK_DOWN))
- {
- // Move backward in the opposite direction of playerAngle
- playerX -= sin(playerAngle) * speed;
- playerY += cos(playerAngle) * speed;
- }
- // Update player headlights position based on car angle
- playerTyre1X = playerX + 10;
- playerTyre1Y = playerY + 5;
- playerTyre2X = playerX + CAR_WIDTH - TYRE_SIZE - 10;
- playerTyre2Y = playerY + 5;
- // Road collision detection for player
- bool onRoad = false;
- // Vertical road
- if (playerX >= 0 && playerX <= ROAD_WIDTH - CAR_WIDTH)
- onRoad = true;
- // Horizontal road at top (twice as tall)
- if (playerY >= 0 && playerY <= (ROAD_WIDTH * 2) &&
- playerX >= 0 && playerX <= WIDTH - CAR_WIDTH)
- onRoad = true;
- if (!onRoad && !godMode) // Only restrict movement if god mode is off
- {
- // Return to previous position if off road
- playerX = prevPlayerX;
- playerY = prevPlayerY;
- playerAngle = prevPlayerAngle;
- }
- // AI car movement logic
- if (aiY > ROAD_WIDTH * 2 && aiX < ROAD_WIDTH / 2)
- {
- // Move up on vertical road
- aiSpeedX = 0;
- aiSpeedY = -aiSpeed;
- aiAngle = -PI / 2; // facing up
- }
- else if (aiY <= ROAD_WIDTH * 2)
- {
- // Turn right on horizontal road
- aiSpeedX = aiSpeed;
- aiSpeedY = 0;
- aiAngle = 0; // facing right
- }
- aiX += aiSpeedX;
- aiY += aiSpeedY;
- // Update AI headlights position
- aiTyre1X = aiX + 10;
- aiTyre1Y = aiY + 5;
- aiTyre2X = aiX + CAR_WIDTH - TYRE_SIZE - 10;
- aiTyre2Y = aiY + 5;
- // 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 (!godMode && 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 (!godMode && ((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;
- }*/
- // Win conditions
- // Victory conditions commented out
- /*if (playerX > WIDTH - ROAD_WIDTH - CAR_WIDTH && playerY < ROAD_WIDTH)
- {
- gameOver = true;
- playerWon = true;
- }
- if (aiX > WIDTH - ROAD_WIDTH - CAR_WIDTH && aiY < ROAD_WIDTH)
- {
- gameOver = true;
- playerWon = false;
- }*/
- InvalidateRect(hWnd, NULL, FALSE);
- }
- break;
- case WM_PAINT:
- {
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint(hWnd, &ps);
- // Create memory DC and bitmap for double buffering
- HDC memDC = CreateCompatibleDC(hdc);
- HBITMAP memBitmap = CreateCompatibleBitmap(hdc, WIDTH, HEIGHT);
- HBITMAP oldBitmap = (HBITMAP)SelectObject(memDC, memBitmap);
- // Clear background
- //HBRUSH whiteBrush = CreateSolidBrush(RGB(255, 255, 255));
- HBRUSH lightGreenBrush = CreateSolidBrush(RGB(144, 238, 144)); // Light green color
- RECT rect = { 0, 0, WIDTH, HEIGHT };
- FillRect(memDC, &rect, lightGreenBrush);
- //FillRect(memDC, &rect, whiteBrush);
- DeleteObject(lightGreenBrush);
- //DeleteObject(whiteBrush);
- // Draw roads (black rectangles)
- HBRUSH blackBrush = CreateSolidBrush(RGB(0, 0, 0));
- // Vertical road
- RECT verticalRoad = { 0, 0, ROAD_WIDTH, HEIGHT };
- FillRect(memDC, &verticalRoad, blackBrush);
- // Horizontal road (twice as tall)
- RECT horizontalRoad = { 0, 0, WIDTH, ROAD_WIDTH * 2 };
- FillRect(memDC, &horizontalRoad, blackBrush);
- DeleteObject(blackBrush);
- // Draw yellow road strips
- HBRUSH yellowBrush = CreateSolidBrush(RGB(255, 255, 0));
- SelectObject(memDC, yellowBrush);
- // Vertical road strips
- for (int y = 0; y < HEIGHT; y += 80) {
- Rectangle(memDC, ROAD_WIDTH / 2 - 5, y, ROAD_WIDTH / 2 + 5, y + 40);
- }
- // Horizontal road strips
- for (int x = 0; x < WIDTH; x += 80) {
- Rectangle(memDC, x, ROAD_WIDTH - 5, x + 40, ROAD_WIDTH + 5);
- }
- DeleteObject(yellowBrush);
- // Draw player car (red rectangle)
- HBRUSH redBrush = CreateSolidBrush(RGB(255, 0, 0));
- SelectObject(memDC, redBrush);
- // Save the current graphics state
- int savedDC = SaveDC(memDC);
- // Set up the transformation matrix for rotation
- XFORM xform;
- SetGraphicsMode(memDC, GM_ADVANCED);
- xform.eM11 = (FLOAT)cos(playerAngle);
- xform.eM12 = (FLOAT)sin(playerAngle);
- xform.eM21 = (FLOAT)-sin(playerAngle);
- xform.eM22 = (FLOAT)cos(playerAngle);
- xform.eDx = (FLOAT)playerX + CAR_WIDTH / 2;
- xform.eDy = (FLOAT)playerY + CAR_HEIGHT / 2;
- SetWorldTransform(memDC, &xform);
- // Draw the rotated rectangle
- Rectangle(memDC, -CAR_WIDTH / 2, -CAR_HEIGHT / 2, CAR_WIDTH / 2, CAR_HEIGHT / 2);
- // Draw headlights (white circles)
- HBRUSH headlightBrush = CreateSolidBrush(RGB(255, 255, 255));
- SelectObject(memDC, headlightBrush);
- Ellipse(memDC, -CAR_WIDTH / 2 + 10, -CAR_HEIGHT / 2 + 5,
- -CAR_WIDTH / 2 + 20, -CAR_HEIGHT / 2 + 15);
- Ellipse(memDC, CAR_WIDTH / 2 - 20, -CAR_HEIGHT / 2 + 5,
- CAR_WIDTH / 2 - 10, -CAR_HEIGHT / 2 + 15);
- DeleteObject(headlightBrush);
- // Restore the original graphics state
- RestoreDC(memDC, savedDC);
- DeleteObject(redBrush);
- // Draw AI car (blue rectangle)
- HBRUSH blueBrush = CreateSolidBrush(RGB(0, 0, 255));
- SelectObject(memDC, blueBrush);
- // Save the current graphics state for AI car
- savedDC = SaveDC(memDC);
- // Set up the transformation matrix for AI car rotation
- SetGraphicsMode(memDC, GM_ADVANCED);
- xform.eM11 = (FLOAT)cos(aiAngle);
- xform.eM12 = (FLOAT)sin(aiAngle);
- xform.eM21 = (FLOAT)-sin(aiAngle);
- xform.eM22 = (FLOAT)cos(aiAngle);
- xform.eDx = (FLOAT)aiX + CAR_WIDTH / 2;
- xform.eDy = (FLOAT)aiY + CAR_HEIGHT / 2;
- SetWorldTransform(memDC, &xform);
- // Draw the rotated AI car
- Rectangle(memDC, -CAR_WIDTH / 2, -CAR_HEIGHT / 2, CAR_WIDTH / 2, CAR_HEIGHT / 2);
- // Draw AI headlights
- headlightBrush = CreateSolidBrush(RGB(255, 255, 255));
- SelectObject(memDC, headlightBrush);
- Ellipse(memDC, -CAR_WIDTH / 2 + 10, -CAR_HEIGHT / 2 + 5,
- -CAR_WIDTH / 2 + 20, -CAR_HEIGHT / 2 + 15);
- Ellipse(memDC, CAR_WIDTH / 2 - 20, -CAR_HEIGHT / 2 + 5,
- CAR_WIDTH / 2 - 10, -CAR_HEIGHT / 2 + 15);
- DeleteObject(headlightBrush);
- // Restore the original graphics state
- RestoreDC(memDC, savedDC);
- DeleteObject(blueBrush);
- // Draw countdown timer if game hasn't started
- if (!gameStarted)
- {
- char timerText[10];
- sprintf_s(timerText, "%d", timer / 10);
- SetTextColor(memDC, RGB(255, 0, 0));
- SetBkMode(memDC, TRANSPARENT);
- TextOutA(memDC, WIDTH / 2 - 10, HEIGHT / 2 - 10, timerText, strlen(timerText));
- }
- // Draw God Mode text indicator
- if (godMode)
- {
- SetTextColor(memDC, RGB(255, 0, 0));
- SetBkMode(memDC, TRANSPARENT);
- TextOutA(memDC, 10, 10, "God Mode ON", 10);
- }
- // Copy from memory DC to screen
- BitBlt(hdc, 0, 0, WIDTH, HEIGHT, memDC, 0, 0, SRCCOPY);
- // Clean up
- SelectObject(memDC, oldBitmap);
- DeleteObject(memBitmap);
- DeleteDC(memDC);
- EndPaint(hWnd, &ps);
- break;
- }
- case WM_DESTROY:
- KillTimer(hWnd, 1);
- PostQuitMessage(0);
- break;
- case WM_KEYDOWN:
- if (wParam == VK_F1)
- {
- MessageBoxW(hWnd, L"2D Racing Game 3.0 Programmed in C++ Win32 API (491 lines of code) by Entisoft Software (c) Evans Thorpemorton", L"About", MB_OK | MB_ICONINFORMATION); // orig 395 lines
- }
- //break;
- if (wParam == VK_ESCAPE)
- {
- 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 = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = WndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)); // Modified line
- //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 = (HICON)LoadImage(hInstance, // Modified line
- MAKEINTRESOURCE(IDI_ICON1), // Modified line
- IMAGE_ICON, // Modified line
- 16, // Modified line
- 16, // Modified line
- LR_DEFAULTCOLOR); // Modified line
- //wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- RegisterClassEx(&wc);
- // Calculate the position to center the window
- int screenWidth = GetSystemMetrics(SM_CXSCREEN);
- int screenHeight = GetSystemMetrics(SM_CYSCREEN);
- int windowX = (screenWidth - WIDTH) / 2;
- int windowY = (screenHeight - HEIGHT) / 2;
- // Create window
- HWND hWnd = CreateWindowEx(0, L"RacingGame", L"Racing Game (ArrowKeys=Move G=GodMode)", WS_OVERLAPPEDWINDOW, windowX, windowY, WIDTH, HEIGHT, NULL, NULL, hInstance, NULL);
- // Show window
- //ShowWindow(hWnd, nCmdShow);
- ShowWindow(hWnd, SW_SHOWMAXIMIZED);
- // 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