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 10
- #define TIMER_ID 100
- #define SPEED 8
- 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 = 0;
- 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);
- 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 = 50;
- snake[i].Height = 50;
- if (snake[i - 1].vector == Up) {
- snake[i].PosX = snake[i - 1].PosX;
- snake[i].PosY = snake[i - 1].PosY + 50;
- }
- else if (snake[i - 1].vector == Down) {
- snake[i].PosX = snake[i - 1].PosX;
- snake[i].PosY = snake[i - 1].PosY - 50;
- }
- else if (snake[i - 1].vector == Left) {
- snake[i].PosX = snake[i - 1].PosX + 50;
- snake[i].PosY = snake[i - 1].PosY;
- }
- else if (snake[i - 1].vector == Right) {
- snake[i].PosX = snake[i - 1].PosX - 50;
- 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_CREATE:break;
- case WM_COMMAND:break;
- case WM_TIMER: {
- GetClientRect(hwnd, &WndRect);
- for (int i = Score; i >= 0; i--) {
- switch (snake[i].vector) {
- case Up:
- snake[i].PosY -= SPEED;
- if (snake[i].PosY < WndRect.top) snake[i].PosY = WndRect.bottom - snake[i].Height;
- break;
- case Down:
- snake[i].PosY += SPEED;
- if (snake[i].PosY + snake[i].Height > WndRect.bottom) snake[i].PosY = i;
- break;
- case Left:
- snake[i].PosX -= SPEED;
- if (snake[i].PosX < WndRect.left) snake[i].PosX = WndRect.right - snake[i].Width;
- break;
- case Right:
- snake[i].PosX += SPEED;
- if (snake[i].PosX + snake[i].Width > WndRect.right) snake[i].PosX = i;
- break;
- }
- if (i > 0) {
- snake[i].PosX = snake[i-1 ].PosX;
- snake[i].PosY = snake[i-1].PosY ;
- snake[i].vector = snake[i-1].vector;
- }
- }
- if (snake[0].PosX >= coin.PosX - 50 && snake[0].PosY >= coin.PosY - 50 &&
- snake[0].PosX + snake[0].Width <= (coin.PosX + coin.Width) + 50 &&
- snake[0].PosY + snake[0].Height <= (coin.PosY + coin.Height) + 50) {
- 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;
- if (!RegisterClassEx(&window)) {
- MessageBox(NULL, TEXT("Błąd rejestracji okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
- return 0;
- }
- hwnd = CreateWindowEx(WS_EX_WINDOWEDGE, Class_Name, Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
- if (hwnd == NULL) {
- MessageBox(NULL, TEXT("Błąd tworzenia okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
- return 0;
- }
- snake[0].BorderColor = RGB(0, 0, 0);
- snake[0].FillColor = RGB(255, 0, 255);
- snake[0].Width = 50;
- snake[0].Height = 50;
- 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 = 50;
- coin.Height = 50;
- 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