Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <ctime>
- #include <cstdlib>
- #include <cmath>
- #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 playerSpeed = 5;
- float playerAngle = 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;
- bool gameStarted = false;
- bool gameOver = false;
- bool playerWon = false;
- // Function to draw a rotated rectangle
- void DrawRotatedRect(HDC hdc, int x, int y, int width, int height, float angle)
- {
- POINT points[4];
- float radians = angle * (3.14159f / 180.0f);
- points[0] = { x + static_cast<int>(cos(radians) * 0 - sin(radians) * 0), y + static_cast<int>(sin(radians) * 0 + cos(radians) * 0) };
- points[1] = { x + static_cast<int>(cos(radians) * width - sin(radians) * 0), y + static_cast<int>(sin(radians) * width + cos(radians) * 0) };
- points[2] = { x + static_cast<int>(cos(radians) * width - sin(radians) * height), y + static_cast<int>(sin(radians) * width + cos(radians) * height) };
- points[3] = { x + static_cast<int>(cos(radians) * 0 - sin(radians) * height), y + static_cast<int>(sin(radians) * 0 + cos(radians) * height) };
- Polygon(hdc, points, 4);
- }
- // 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(static_cast<unsigned int>(time(0)));
- aiSpeed = rand() % 5 + 3;
- }
- else if (!gameOver)
- {
- if (GetAsyncKeyState(VK_LEFT))
- {
- playerAngle -= TURN_RADIUS;
- }
- else if (GetAsyncKeyState(VK_RIGHT))
- {
- playerAngle += TURN_RADIUS;
- }
- if (GetAsyncKeyState(VK_UP))
- {
- playerX += static_cast<int>(playerSpeed * cos(playerAngle * (3.14159f / 180.0f)));
- playerY += static_cast<int>(playerSpeed * sin(playerAngle * (3.14159f / 180.0f)));
- }
- else if (GetAsyncKeyState(VK_DOWN))
- {
- playerX -= static_cast<int>(playerSpeed * cos(playerAngle * (3.14159f / 180.0f)));
- playerY -= static_cast<int>(playerSpeed * sin(playerAngle * (3.14159f / 180.0f)));
- }
- 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;
- if (playerX > WIDTH - ROAD_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);
- DrawRotatedRect(hdc, playerX, playerY, CAR_WIDTH, CAR_HEIGHT, playerAngle);
- 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 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