Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <time.h>
- #include <malloc.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define TIMER_TIME 70
- #define TIMER_ID 100
- #define SIZE 30
- enum Vector { Left, Right, Up, Down };
- struct Coin {
- int PosX, PosY;
- int Width, Height;
- COLORREF BorderColor, FillColor;
- };
- struct Snake {
- int PosX, PosY;
- int Width, Height;
- COLORREF BorderColor, FillColor;
- Vector vector, PrevVector;
- };
- HWND hwnd;
- int Score = 1;
- PAINTSTRUCT PS;
- RECT WndRect;
- Snake snake[255];
- Coin coin;
- void SetNewCoinPos() {
- coin.PosX = (rand() % WndRect.right - 100) + 100;
- coin.PosY = (rand() % WndRect.bottom - 100) + 100;
- }
- void PrintScore(HDC &hdc) {
- char buff[255];
- sprintf_s(buff, "Wynik: %d", Score-1);
- TextOut(hdc, 0, 0, buff, strlen(buff));
- }
- void PrintSnake(Snake &snak, HDC &hdc) {
- HBRUSH Brush = CreateSolidBrush(snak.FillColor);
- HPEN Pen = CreatePen(PS_SOLID, 2, snak.BorderColor);
- HBRUSH OldBrush = (HBRUSH)SelectObject(hdc, Brush);
- HPEN OldPen = (HPEN)SelectObject(hdc, Pen);
- Rectangle(hdc, snak.PosX, snak.PosY, snak.PosX + snak.Width, snak.PosY + snak.Height);
- SelectObject(hdc, OldBrush);
- SelectObject(hdc, OldPen);
- DeleteObject(Brush);
- DeleteObject(Pen);
- }
- void PrintCoin(HDC &hdc) {
- HBRUSH Brush = CreateSolidBrush(coin.FillColor);
- HPEN Pen = CreatePen(PS_SOLID, 2, coin.BorderColor);
- HBRUSH OldBrush = (HBRUSH)SelectObject(hdc, Brush);
- HPEN OldPen = (HPEN)SelectObject(hdc, Pen);
- Ellipse(hdc, coin.PosX, coin.PosY, coin.PosX + coin.Width, coin.PosY + coin.Height);
- SelectObject(hdc, OldBrush);
- SelectObject(hdc, OldPen);
- DeleteObject(Brush);
- DeleteObject(Pen);
- }
- void SetTail(int i) {
- snake[i].BorderColor = RGB(0, 0, 0);
- snake[i].FillColor = RGB(0, 255, 255);
- snake[i].Width = SIZE;
- snake[i].Height = SIZE;
- if (snake[i - 1].vector == Up) {
- snake[i].PosX = snake[i - 1].PosX;
- snake[i].PosY = snake[i - 1].PosY + SIZE;
- }
- else if (snake[i - 1].vector == Down) {
- snake[i].PosX = snake[i - 1].PosX;
- snake[i].PosY = snake[i - 1].PosY - SIZE;
- }
- else if (snake[i - 1].vector == Left) {
- snake[i].PosX = snake[i - 1].PosX + SIZE;
- snake[i].PosY = snake[i - 1].PosY;
- }
- else if (snake[i - 1].vector == Right) {
- snake[i].PosX = snake[i - 1].PosX - SIZE;
- snake[i].PosY = snake[i - 1].PosY;
- }
- snake[i].vector = snake[i - 1].vector;//
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
- switch (msg) {
- case WM_TIMER: {
- GetClientRect(hwnd, &WndRect);
- switch (snake[0].vector) {
- case Up:
- snake[0].PosY -= SIZE;
- if (snake[0].PosY < WndRect.top) snake[0].PosY = WndRect.bottom - snake[0].Height;
- break;
- case Down:
- snake[0].PosY += SIZE;
- if (snake[0].PosY + snake[0].Height > WndRect.bottom) snake[0].PosY = 0;
- break;
- case Left:
- snake[0].PosX -= SIZE;
- if (snake[0].PosX < WndRect.left) snake[0].PosX = WndRect.right - snake[0].Width;
- break;
- case Right:
- snake[0].PosX += SIZE;
- if (snake[0].PosX + snake[0].Width > WndRect.right) snake[0].PosX = 0;
- break;
- }
- for (int i = Score; i > 0; i--) {
- snake[i].PosX = snake[i - 1].PosX;
- snake[i].PosY = snake[i - 1].PosY;
- if (snake[0].PosX == snake[i].PosX && snake[0].PosY == snake[i].PosY &&
- snake[0].PosX + snake[0].Width == snake[i].PosX + snake[i].Width &&
- snake[0].PosY + snake[0].Height == snake[i].PosY + snake[i].Height) {
- KillTimer(hwnd, TIMER_ID);
- MessageBox(NULL, TEXT("Przegrales!"), TEXT("Przegrales!"), MB_ICONEXCLAMATION);
- }
- }
- if (snake[0].PosX >= coin.PosX - SIZE && snake[0].PosY >= coin.PosY - SIZE &&
- snake[0].PosX + snake[0].Width <= (coin.PosX + coin.Width) + SIZE &&
- snake[0].PosY + snake[0].Height <= (coin.PosY + coin.Height) + SIZE) {
- SetNewCoinPos();
- Score++;
- SetTail(Score);
- }
- InvalidateRect(hwnd, &WndRect, 1);
- }break;
- case WM_PAINT: {
- HDC hdc = BeginPaint(hwnd, &PS);
- PrintCoin(hdc);
- for (int i = Score; i >= 0; i--) {
- PrintSnake(snake[i], hdc);
- }
- PrintScore(hdc);
- EndPaint(hwnd, &PS);;
- }break;
- case WM_KEYDOWN: {
- switch ((int)wParam) {
- case VK_UP:
- snake[0].PrevVector = snake[0].vector;
- if (snake[0].PrevVector != Down) {
- snake[0].vector = Up;
- }
- break;
- case VK_DOWN:
- snake[0].PrevVector = snake[0].vector;
- if (snake[0].PrevVector != Up) {
- snake[0].vector = Down;
- }
- break;
- case VK_RIGHT:
- snake[0].PrevVector = snake[0].vector;
- if (snake[0].PrevVector != Left) {
- snake[0].vector = Right;
- }
- break;
- case VK_LEFT:
- snake[0].PrevVector = snake[0].vector;
- if (snake[0].PrevVector != Right) {
- snake[0].vector = Left;
- }
- break;
- }
- }break;
- case WM_CLOSE: DestroyWindow(hwnd); break;
- case WM_DESTROY:PostQuitMessage(0); break;
- default: return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR ilCmdLine, int nCmdShow) {
- WNDCLASSEX window;
- MSG msg;
- TCHAR Class_Name[] = TEXT("OKNO_TEST"), Title[] = TEXT("Tytul");
- window.cbClsExtra = NULL;
- window.cbSize = sizeof(WNDCLASSEX);
- window.cbWndExtra = NULL;
- window.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- window.hCursor = LoadCursor(NULL, IDC_ARROW);
- window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- window.hInstance = hInstance;
- window.lpfnWndProc = WndProc;
- window.lpszClassName = Class_Name;
- window.lpszMenuName = 0;
- window.style = CS_VREDRAW | CS_HREDRAW;
- RegisterClassEx(&window);
- hwnd = CreateWindowEx(WS_EX_WINDOWEDGE, Class_Name, Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
- snake[0].BorderColor = RGB(0, 0, 0);
- snake[0].FillColor = RGB(255, 0, 255);
- snake[0].Width = SIZE;
- snake[0].Height = SIZE;
- snake[0].PosX = 100;
- snake[0].PosY = 100;
- snake[0].vector = Down;
- GetClientRect(hwnd, &WndRect);
- srand(time(NULL));
- coin.BorderColor = RGB(0, 0, 0);
- coin.FillColor = RGB(240, 240, 90);
- coin.Width = SIZE;
- coin.Height = SIZE;
- SetNewCoinPos();
- ShowWindow(hwnd, nCmdShow);
- UpdateWindow(hwnd);
- SetTimer(hwnd, TIMER_ID, TIMER_TIME, NULL);
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- if (!UnregisterClass(Class_Name, hInstance)) {
- MessageBox(NULL, TEXT("Błąd niszczenia okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
- return 0;
- }
- return msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement