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 1
- HWND hwnd;
- 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;
- };
- int Score =1;
- PAINTSTRUCT PS;
- RECT WndRect;
- Snake snake;
- Coin coin;
- void SetNewCoinPos() {
- coin.PosX = (rand() % WndRect.right-50)+50;
- coin.PosY =( rand() % WndRect.bottom-50)+50;
- }
- 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);
- }
- 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 255 -> 0
- switch (snake.vector) {
- case Up:
- snake.PosY -= SPEED*Score;
- if (snake.PosY < WndRect.top) snake.PosY = WndRect.bottom - snake.Height;
- break;
- case Down:
- snake.PosY += SPEED*Score;
- if (snake.PosY + snake.Height > WndRect.bottom) snake.PosY = 0;
- break;
- case Left:
- snake.PosX -= SPEED*Score;
- if (snake.PosX < WndRect.left) snake.PosX = WndRect.right - snake.Width;
- break;
- case Right:
- snake.PosX += SPEED*Score;
- if (snake.PosX + snake.Width > WndRect.right) snake.PosX = 0;
- break;
- }
- if (snake.PosX >= coin.PosX-50 &&
- snake.PosY >= coin.PosY-50 &&
- snake.PosX + snake.Width <= (coin.PosX + coin.Width)+50 &&
- snake.PosY + snake.Height <= (coin.PosY + coin.Height)+50)
- {
- SetNewCoinPos();
- Score++;
- }
- InvalidateRect(hwnd, &WndRect, 1);
- }break;
- case WM_PAINT: {
- HDC hdc = BeginPaint(hwnd, &PS);
- PrintCoin(hdc);//[255]
- PrintSnake(snake,hdc);//[255]
- PrintScore(hdc);
- EndPaint(hwnd, &PS);;
- }break;
- case WM_KEYDOWN: {
- switch ((int)wParam) {
- case VK_UP:
- snake.PrevVector = snake.vector;
- if (snake.PrevVector != Down) {
- snake.vector = Up;
- }
- break;
- case VK_DOWN:
- snake.PrevVector = snake.vector;
- if (snake.PrevVector != Up) {
- snake.vector = Down;
- }
- break;
- case VK_RIGHT:
- snake.PrevVector = snake.vector;
- if (snake.PrevVector != Left) {
- snake.vector = Right;
- }
- break;
- case VK_LEFT:
- snake.PrevVector = snake.vector;
- if (snake.PrevVector != Right) {
- snake.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.BorderColor = RGB(0, 0, 0);
- snake.FillColor = RGB(255, 0, 255);
- snake.Width = 50;
- snake.Height = 50;
- snake.PosX = 100;
- snake.PosY = 100;
- snake.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