Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- #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.)
- #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:
- */
- #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))
- {
- playerAngle -= 0.1f;
- }
- if (GetAsyncKeyState(VK_RIGHT))
- {
- 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;
- }
- 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 if (aiY > 0)
- {
- aiSpeedX = 0;
- aiSpeedY = -aiSpeed;
- }
- else
- {
- aiSpeedX = 0;
- aiSpeedY = 0;
- }
- aiX += aiSpeedX;
- aiY += aiSpeedY;
- aiTyre1X = aiX + 10;
- aiTyre1Y = aiY + CAR_HEIGHT - TYRE_SIZE;
- aiTyre2X = aiX + CAR_WIDTH - TYRE_SIZE - 10;
- aiTyre2Y = aiY + CAR_HEIGHT - TYRE_SIZE;
- if (playerX > WIDTH - ROAD_WIDTH - CAR_WIDTH && playerY < 0)
- {
- gameOver = true;
- playerWon = true;
- }
- if (aiX > WIDTH - ROAD_WIDTH && aiY < 0)
- {
- gameOver = true;
- 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