Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <string>
- #include <time.h>
- #include <list>
- #include <math.h>
- typedef std::list <RECT> snakeList;
- snakeList snake;
- enum Direction {UP,DOWN,LEFT,RIGHT};
- Direction direction;
- RECT rect,rnd={0,0,20,20};
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR plCmdLine, int mShowCMD)
- {
- srand(time(NULL));
- direction = UP;
- LPTSTR className = L"nazwaKlasy";
- WNDCLASSEX wc;
- HWND hwnd;
- MSG msg;
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.style = CS_HREDRAW | CS_VREDRAW;
- 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.lpszClassName = className;
- wc.lpszMenuName = NULL;
- wc.hIconSm= LoadIcon(NULL, IDI_APPLICATION);
- if(RegisterClassEx(&wc))
- {
- if((hwnd=CreateWindowEx(WS_EX_CLIENTEDGE, className, L"Paint",WS_OVERLAPPEDWINDOW,100, 100,900,900,NULL,NULL,hInstance,NULL))!=NULL)
- {
- RECT rect = {400,400,420,420};
- snake.push_front(rect);
- ShowWindow(hwnd, mShowCMD);
- UpdateWindow(hwnd);
- SetTimer(hwnd,1,100,NULL);
- while(GetMessage(&msg,NULL,0,0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- else
- MessageBox(NULL, L"Error", L"Create Window", MB_OK);
- }
- else
- MessageBox(NULL, L"Error", L"Register CLASS", MB_OK);
- KillTimer(hwnd,1);
- DestroyWindow(hwnd);
- return 0;
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lparam)
- {
- GetClientRect(hwnd,&rect);
- switch(msg)
- {
- case WM_SIZE:
- {
- break;
- }
- case WM_PAINT:
- {
- HDC hdc = GetDC(hwnd);
- HPEN oldPen,p1=CreatePen(PS_SOLID,1,RGB(0,0,0));
- HBRUSH oldBrush,b1=CreateSolidBrush(RGB(0,255,0));
- oldPen = (HPEN) SelectObject(hdc,p1);
- oldBrush = (HBRUSH) SelectObject(hdc,b1);
- FillRect(hdc,&rect,oldBrush);
- for(snakeList::iterator iter=snake.begin();iter!=snake.end();iter++)
- {
- FillRect(hdc,&(*iter),b1);
- }
- FillRect(hdc,&rnd,b1);
- SelectObject(hdc,oldPen);
- SelectObject(hdc,oldBrush);
- DeleteObject(p1);
- DeleteObject(b1);
- ReleaseDC(hwnd,hdc);
- break;
- }
- case WM_TIMER:
- {
- switch(direction)
- {
- case UP:
- {
- RECT r=*snake.begin();
- r.top-=20;
- r.bottom-=20;
- if(r.bottom<=rect.top)
- {
- r.bottom=rect.bottom;
- r.top=rect.bottom-20;
- }
- if(r.left==rnd.left && r.top==rnd.top)
- {
- rnd.left=(rand()%rect.right/20)*20;
- rnd.top=(rand()%rect.bottom/20)*20;
- rnd.right=rnd.left+20;
- rnd.bottom=rnd.top+20;
- snake.push_front(r);
- }
- else
- {
- snake.push_front(r);
- snake.pop_back();
- }
- break;
- }
- case DOWN:
- {
- RECT r=*snake.begin();
- r.top+=20;
- r.bottom+=20;
- if(r.top>=rect.bottom)
- {
- r.top=rect.top;
- r.bottom=rect.top+20;
- }
- if(r.left==rnd.left && r.top==rnd.top)
- {
- rnd.left=(rand()%rect.right/20)*20;
- rnd.top=(rand()%rect.bottom/20)*20;
- rnd.right=rnd.left+20;
- rnd.bottom=rnd.top+20;
- snake.push_front(r);
- }
- else
- {
- snake.push_front(r);
- snake.pop_back();
- }
- break;
- }
- case LEFT:
- {
- RECT r=*snake.begin();
- r.right-=20;
- r.left-=20;
- if(r.right<=rect.left)
- {
- r.right=rect.right;
- r.left=rect.right-20;
- }
- if(r.left==rnd.left && r.top==rnd.top)
- {
- rnd.left=(rand()%rect.right/20)*20;
- rnd.top=(rand()%rect.bottom/20)*20;
- rnd.right=rnd.left+20;
- rnd.bottom=rnd.top+20;
- snake.push_front(r);
- }
- else
- {
- snake.push_front(r);
- snake.pop_back();
- }
- break;
- }
- case RIGHT:
- {
- RECT r=*snake.begin();
- r.right+=20;
- r.left+=20;
- if(r.left>=rect.right)
- {
- r.left=rect.left;
- r.right=rect.left+20;
- }
- if(r.left==rnd.left && r.top==rnd.top)
- {
- rnd.left=(rand()%rect.right/20)*20;
- rnd.top=(rand()%rect.bottom/20)*20;
- rnd.right=rnd.left+20;
- rnd.bottom=rnd.top+20;
- snake.push_front(r);
- }
- else
- {
- snake.push_front(r);
- snake.pop_back();
- }
- break;
- }
- }
- InvalidateRect(hwnd,&rect,0);
- break;
- }
- case WM_KEYDOWN:
- {
- switch(wParam)
- {
- case VK_UP:
- direction = UP;
- break;
- case VK_DOWN:
- direction = DOWN;
- break;
- case VK_LEFT:
- direction = LEFT;
- break;
- case VK_RIGHT:
- direction = RIGHT;
- break;
- }
- break;
- }
- case WM_DESTROY:
- {
- KillTimer(hwnd,1);
- PostQuitMessage(0);
- break;
- }
- }
- return DefWindowProc(hwnd,msg,wParam,lparam);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement