Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ==++ Here's the full code for (file 1/1) "RaceCar.cpp"::: ++==
- ```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;
- const double M_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 = true;
- //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
- playerAngle = 0; // 0 degrees, pointing straight up (north)
- aiX = ROAD_WIDTH / 2 - CAR_WIDTH / 2;
- aiY = HEIGHT - CAR_HEIGHT - 100;
- //aiAngle = -PI / 2; // -90 degrees, pointing north
- aiAngle = 0; // 0 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)
- {
- aiSpeedX = 0;
- aiSpeedY = -aiSpeed;
- aiAngle = 0; // Facing upward
- }
- else if (aiY <= ROAD_WIDTH * 2 && aiX < WIDTH - ROAD_WIDTH)
- {
- aiSpeedX = aiSpeed;
- aiSpeedY = 0;
- aiAngle = -PI / 2; // Facing right
- }
- else if (aiX >= WIDTH - ROAD_WIDTH && aiY <= HEIGHT - ROAD_WIDTH)
- {
- aiSpeedX = 0;
- aiSpeedY = aiSpeed;
- aiAngle = PI; // Facing downward
- }
- else if (aiX > ROAD_WIDTH && aiY >= HEIGHT - ROAD_WIDTH)
- {
- aiSpeedX = -aiSpeed;
- aiSpeedY = 0;
- aiAngle = PI / 2; // Facing left
- }
- // AI Car Movement and Headlight Update
- aiX += aiSpeedX;
- aiY += aiSpeedY;
- // Calculate the front of the car using its angle
- float headlightDistance = CAR_HEIGHT / 2 - 10;
- // Calculate headlight positions based on the car's angle
- float headlightOffsetX = cos(aiAngle) * headlightDistance;
- float headlightOffsetY = sin(aiAngle) * headlightDistance;
- // Set AI headlights at the front of the car
- //aiTyre1X = aiX + CAR_WIDTH / 4 + headlightOffsetX;
- //aiTyre1Y = aiY + headlightOffsetY;
- //aiTyre2X = aiX - CAR_WIDTH / 4 + headlightOffsetX;
- //aiTyre2Y = aiY + headlightOffsetY;
- // 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;
- }
- /* 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 lightGreenBrush = CreateSolidBrush(RGB(144, 238, 144)); // Light green color
- RECT rect = { 0, 0, WIDTH, HEIGHT };
- FillRect(memDC, &rect, lightGreenBrush);
- DeleteObject(lightGreenBrush);
- // 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);
- // Drawing Player's Car and Headlights
- HBRUSH redBrush = CreateSolidBrush(RGB(255, 0, 0));
- SelectObject(memDC, redBrush);
- int savedDC = SaveDC(memDC);
- 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);
- Rectangle(memDC, -CAR_WIDTH / 2, -CAR_HEIGHT / 2, CAR_WIDTH / 2, CAR_HEIGHT / 2);
- HBRUSH headlightBrush = CreateSolidBrush(RGB(255, 255, 255));
- SelectObject(memDC, headlightBrush);
- // Consistent headlight offset, relative to the car's dimensions
- int headlightSize = 10;
- int playerheadlightOffsetX = 10;
- int playerheadlightOffsetY = -CAR_HEIGHT / 2 + 15; // Adjusted vertical offset
- Ellipse(memDC, -CAR_WIDTH / 2 + playerheadlightOffsetX, playerheadlightOffsetY,
- -CAR_WIDTH / 2 + playerheadlightOffsetX + headlightSize, playerheadlightOffsetY + headlightSize);
- Ellipse(memDC, CAR_WIDTH / 2 - playerheadlightOffsetX - headlightSize, playerheadlightOffsetY,
- CAR_WIDTH / 2 - playerheadlightOffsetX, playerheadlightOffsetY + headlightSize);
- DeleteObject(headlightBrush);
- RestoreDC(memDC, savedDC);
- DeleteObject(redBrush);
- // Draw AI car (blue rectangle)
- // AI Car Rendering
- // Drawing AI's Car and Headlights
- // Draw AI car (blue rectangle)
- // AI Car Rendering
- // AI Car Rendering
- //O3's fix working best inconsistent padded positioning tho
- // Draw AI car (blue rectangle)
- /* // ----- Draw AI Car (blue rectangle) with rotated transform -----
- HBRUSH blueBrush = CreateSolidBrush(RGB(0, 0, 255));
- SelectObject(memDC, blueBrush);
- int savedDC2 = SaveDC(memDC); // Save state using a unique variable name
- SetGraphicsMode(memDC, GM_ADVANCED);
- XFORM xform2;
- xform2.eM11 = (FLOAT)cos(aiAngle);
- xform2.eM12 = (FLOAT)sin(aiAngle);
- xform2.eM21 = (FLOAT)-sin(aiAngle);
- xform2.eM22 = (FLOAT)cos(aiAngle);
- xform2.eDx = (FLOAT)aiX + CAR_WIDTH / 2;
- xform2.eDy = (FLOAT)aiY + CAR_HEIGHT / 2;
- SetWorldTransform(memDC, &xform2);
- Rectangle(memDC, -CAR_WIDTH / 2, -CAR_HEIGHT / 2, CAR_WIDTH / 2, CAR_HEIGHT / 2);
- RestoreDC(memDC, savedDC2);
- DeleteObject(blueBrush);
- // ----- Dynamic AI Headlight Positioning -----
- //
- // This solution computes the AI car's nose (front) using its center (cx,cy)
- // and a front vector derived from aiAngle. Note that the "front distance" is
- // CAR_HEIGHT/2 when facing up/down (aiAngle == 0 or PI) and CAR_WIDTH/2 when
- // facing right/left (aiAngle == -PI/2 or PI/2). We then move slightly back from
- // the extreme nose by frontMargin, and offset perpendicular (by aiheadlightSeparation)
- // to get the left and right headlight positions.
- //
- // Define parameters (adjust as needed):
- float cx = aiX + CAR_WIDTH / 2.0f;
- float cy = aiY + CAR_HEIGHT / 2.0f;
- float frontMargin = 5.0f; // Moves headlights slightly inward from the nose
- float aiheadlightSeparation = 10.0f; // Lateral offset from the nose for each headlight
- int aiheadlightSize = 8; // Size of the headlight circle
- // Determine the front distance depending on orientation:
- float frontDistance;
- if (aiAngle == 0 || aiAngle == PI)
- frontDistance = CAR_HEIGHT / 2.0f;
- else // aiAngle == -PI/2 or PI/2
- frontDistance = CAR_WIDTH / 2.0f;
- // The default car drawing uses a local coordinate system where the car's nose is at (0, -1)
- // i.e. at (0, -frontDistance). Thus the front vector (rotated) is:
- float noseX = cx + (-sin(aiAngle)) * frontDistance;
- float noseY = cy + (-cos(aiAngle)) * frontDistance;
- // Pull the headlight positions slightly back from the extreme nose using frontMargin:
- float effectiveX = cx + (-sin(aiAngle)) * (frontDistance - frontMargin);
- float effectiveY = cy + (-cos(aiAngle)) * (frontDistance - frontMargin);
- // Compute a perpendicular vector to the front. For a front vector f = (-sin(aiAngle), -cos(aiAngle)),
- // a perpendicular is p = (cos(aiAngle), -sin(aiAngle)). (We use -p for the left headlight.)
- float px = cos(aiAngle);
- float py = -sin(aiAngle);
- // Compute left and right headlight positions by offsetting perpendicular to the front:
- float leftHeadlightX = effectiveX - px * aiheadlightSeparation;
- float leftHeadlightY = effectiveY - py * aiheadlightSeparation;
- float rightHeadlightX = effectiveX + px * aiheadlightSeparation;
- float rightHeadlightY = effectiveY + py * aiheadlightSeparation;
- // Draw the AI headlights using the computed world coordinates:
- HBRUSH aiHeadlightBrush = CreateSolidBrush(RGB(255, 255, 255));
- SelectObject(memDC, aiHeadlightBrush);
- Ellipse(memDC, (int)leftHeadlightX, (int)leftHeadlightY,
- (int)(leftHeadlightX + aiheadlightSize), (int)(leftHeadlightY + aiheadlightSize));
- Ellipse(memDC, (int)rightHeadlightX, (int)rightHeadlightY,
- (int)(rightHeadlightX + aiheadlightSize), (int)(rightHeadlightY + aiheadlightSize));
- DeleteObject(aiHeadlightBrush); */
- //voila! It's working perfectly /w the following dynamic code!!!
- // ----- Draw AI Car (blue rectangle) with rotated transform -----
- {
- HBRUSH blueBrush = CreateSolidBrush(RGB(0, 0, 255));
- SelectObject(memDC, blueBrush);
- int savedDC2 = SaveDC(memDC); // Save current DC state
- SetGraphicsMode(memDC, GM_ADVANCED);
- XFORM xform2;
- xform2.eM11 = (FLOAT)cos(aiAngle);
- xform2.eM12 = (FLOAT)sin(aiAngle);
- xform2.eM21 = (FLOAT)-sin(aiAngle);
- xform2.eM22 = (FLOAT)cos(aiAngle);
- xform2.eDx = (FLOAT)aiX + CAR_WIDTH / 2;
- xform2.eDy = (FLOAT)aiY + CAR_HEIGHT / 2;
- SetWorldTransform(memDC, &xform2);
- Rectangle(memDC, -CAR_WIDTH / 2, -CAR_HEIGHT / 2, CAR_WIDTH / 2, CAR_HEIGHT / 2);
- RestoreDC(memDC, savedDC2);
- DeleteObject(blueBrush);
- }
- // ----- Dynamically Compute and Draw AI Headlights -----
- //
- // This solution calculates the headlight positions based on the car�s center,
- // a �front� vector (pointing toward the nose) and a perpendicular vector for lateral offset.
- // The idea is that the headlights are placed at a fixed distance (headlightDistance)
- // from the center along the front vector (which is always the nose direction)
- // and then offset left/right by aiheadlightSeparation.
- {
- // Car center
- float cx = aiX + CAR_WIDTH / 2.0f;
- float cy = aiY + CAR_HEIGHT / 2.0f;
- // Parameters � adjust these to fine-tune the look:
- // We use CAR_HEIGHT/2 as the full distance from center to nose when facing up.
- // To mimic the player car�s appearance (headlights slightly inset from the very front),
- // we subtract 15 pixels.
- float headlightDistance = CAR_HEIGHT / 2.0f - 15.0f;
- float aiheadlightSeparation = 10.0f; // Lateral offset from the center of the nose
- int aiheadlightSize = 8; // Headlight circle size
- // Compute the "front" direction vector.
- // Since the car�s default (unrotated) orientation has its nose at the top,
- // the front vector in local coordinates is (0, -1). Rotating that by aiAngle:
- float frontDirX = -sin(aiAngle);
- float frontDirY = -cos(aiAngle);
- // Determine the headlight �center� position � a point along the nose,
- // but moved slightly inward by headlightDistance:
- float headlightCenterX = cx + frontDirX * headlightDistance;
- float headlightCenterY = cy + frontDirY * headlightDistance;
- // Compute a perpendicular vector to the front.
- // A vector perpendicular to (frontDirX, frontDirY) is given by (cos(aiAngle), -sin(aiAngle)).
- float perpX = cos(aiAngle);
- float perpY = -sin(aiAngle);
- // Compute left and right headlight positions by offsetting the headlight center along the perpendicular.
- float leftHeadlightX = headlightCenterX - perpX * aiheadlightSeparation;
- float leftHeadlightY = headlightCenterY - perpY * aiheadlightSeparation;
- float rightHeadlightX = headlightCenterX + perpX * aiheadlightSeparation;
- float rightHeadlightY = headlightCenterY + perpY * aiheadlightSeparation;
- // Draw the AI headlights using the computed world coordinates:
- HBRUSH aiHeadlightBrush = CreateSolidBrush(RGB(255, 255, 255));
- SelectObject(memDC, aiHeadlightBrush);
- Ellipse(memDC, (int)leftHeadlightX, (int)leftHeadlightY,
- (int)(leftHeadlightX + aiheadlightSize), (int)(leftHeadlightY + aiheadlightSize));
- Ellipse(memDC, (int)rightHeadlightX, (int)rightHeadlightY,
- (int)(rightHeadlightX + aiheadlightSize), (int)(rightHeadlightY + aiheadlightSize));
- DeleteObject(aiHeadlightBrush);
- }
- //original AI code
- /* HBRUSH blueBrush = CreateSolidBrush(RGB(0, 0, 255));
- SelectObject(memDC, blueBrush);
- savedDC = SaveDC(memDC);
- SetGraphicsMode(memDC, GM_ADVANCED);
- XFORM xform2;
- xform2.eM11 = (FLOAT)cos(aiAngle);
- xform2.eM12 = (FLOAT)sin(aiAngle);
- xform2.eM21 = (FLOAT)-sin(aiAngle);
- xform2.eM22 = (FLOAT)cos(aiAngle);
- xform2.eDx = (FLOAT)aiX + CAR_WIDTH / 2;
- xform2.eDy = (FLOAT)aiY + CAR_HEIGHT / 2;
- SetWorldTransform(memDC, &xform2);
- Rectangle(memDC, -CAR_WIDTH / 2, -CAR_HEIGHT / 2, CAR_WIDTH / 2, CAR_HEIGHT / 2);
- HBRUSH aiheadlightBrush = CreateSolidBrush(RGB(255, 255, 255));
- SelectObject(memDC, aiheadlightBrush);
- int aiheadlightSize = 8;
- // Define AI headlight positions based on calculated offsets
- Ellipse(memDC, -CAR_WIDTH / 4 - aiheadlightSize / 2, -CAR_HEIGHT / 2 + 5,
- -CAR_WIDTH / 4 + aiheadlightSize / 2, -CAR_HEIGHT / 2 + 5 + aiheadlightSize);
- Ellipse(memDC, CAR_WIDTH / 4 - aiheadlightSize / 2, -CAR_HEIGHT / 2 + 5,
- CAR_WIDTH / 4 + aiheadlightSize / 2, -CAR_HEIGHT / 2 + 5 + aiheadlightSize);
- DeleteObject(aiheadlightBrush);
- 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