Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <ctime>
- #include <cstdlib>
- #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;
- int playerX = 100;
- int playerY = HEIGHT - CAR_HEIGHT - 50;
- int aiX = playerX + CAR_WIDTH + 20;
- int aiY = playerY;
- 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;
- 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))
- {
- playerX -= speed;
- playerTyre1X -= speed;
- playerTyre2X -= speed;
- }
- if (GetAsyncKeyState(VK_RIGHT))
- {
- playerX += speed;
- playerTyre1X += speed;
- playerTyre2X += speed;
- }
- if (GetAsyncKeyState(VK_UP))
- {
- playerY -= speed;
- playerTyre1Y -= speed;
- playerTyre2Y -= speed;
- }
- if (GetAsyncKeyState(VK_DOWN))
- {
- playerY += speed;
- playerTyre1Y += speed;
- playerTyre2Y += speed;
- }
- aiY -= aiSpeed;
- aiTyre1Y -= aiSpeed;
- aiTyre2Y -= aiSpeed;
- if (aiY < 0)
- {
- gameOver = true;
- playerWon = false;
- }
- if (playerY < 0)
- {
- gameOver = true;
- playerWon = true;
- }
- 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);
- 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);
- Rectangle(hdc, playerX, playerY, playerX + CAR_WIDTH, playerY + CAR_HEIGHT);
- 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);
- Ellipse(hdc, playerTyre1X, playerTyre1Y, playerTyre1X + TYRE_SIZE, playerTyre1Y + TYRE_SIZE);
- Ellipse(hdc, playerTyre2X, playerTyre2Y, playerTyre2X + TYRE_SIZE, playerTyre2Y + 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