Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <time.h>
- #include <math.h>
- #include <cstdlib>
- #include <algorithm>
- #define ID_TIMER 1
- HWND okno;
- RECT rect;
- HBRUSH brush;
- HPEN pen;
- HDC hdc;
- PAINTSTRUCT ps;
- LPCWSTR klasa = L"Klasa";
- LPCWSTR title = L"Sznejk.";
- wchar_t title2[128];
- int width = 600, height = 600;
- int randx, randy;
- int sx = 24, sy = 24;
- int kra = 10, krb = 10;
- int punkty = 0;
- bool up = true, down = false, right = false, left = false;
- bool koniec = false;
- POINT tab[2500];
- void plansza(){
- brush = CreateSolidBrush(RGB(255,255,255));
- SelectObject(hdc, brush);
- for(int i=0; i<50; i++){
- for(int j=0; j<50; j++){
- Rectangle(hdc, kra*j-1+10, kra*i-1+10, kra*j+kra+10, kra*i+kra+10);
- }
- }
- DeleteObject(brush);
- }
- void food(){
- brush = CreateSolidBrush(RGB(0,255,0));
- SelectObject(hdc, brush);
- Rectangle(hdc, kra*randx-1+10, kra*randy-1+10, kra*randx+kra+10, kra*randy+kra+10);
- DeleteObject(brush);
- }
- bool gameovercheck(){
- bool gameover = false;
- if(tab[0].x < 0 || tab[0].x > 49 || tab[0].y < 0 || tab[0].y > 49) gameover = true;
- else{
- //to nie dzialo przy zmianie rozmiaru okna
- for(int i=0; i<3+punkty-1; i++){
- for(int j=i+1; j<3+punkty; j++){
- if(tab[i].x == tab[j].x && tab[i].y == tab[j].y){
- gameover = true;
- wsprintf(title2, L"%d %d - %d %d %d %d", i, j, tab[i].x, tab[j].x,
- tab[i].y, tab[j].y);
- SetWindowText(okno, title2);
- break;
- }
- }
- }
- }
- if(gameover){
- KillTimer(okno, ID_TIMER);
- wsprintf(title2, L"Game Over! KONIEC GRY! CO?! NAJWYZSZY WYNIK!\nTak trzymaj
- zolnierzu! Zdobyles %d punktow!", punkty);
- MessageBox(0, title2, L"Game Over!", MB_ICONWARNING | MB_OK);
- return true;
- }else return false;
- }
- void snake(int x, int y){
- tab[0].x = x;
- tab[0].y = y;
- if(x == randx && y == randy){
- SetWindowText(okno, L"Mmmm ale dobre! Sca tego wiyncy!");
- randx = rand()%50;
- randy = rand()%50;
- punkty++;
- tab[2+punkty].x = tab[1+punkty].x;
- tab[2+punkty].y = tab[1+punkty].y;
- }
- brush = CreateSolidBrush(RGB(255,0,0));
- SelectObject(hdc, brush);
- for(int i=0; i<3+punkty; i++) Rectangle(hdc, kra*tab[i].x-1+10, kra*tab[i].y-1+10,
- kra*tab[i].x+kra+10, kra*tab[i].y+kra+10);
- DeleteObject(brush);
- for(int i=2+punkty; i>0; i--){
- tab[i].x = tab[i-1].x;
- tab[i].y = tab[i-1].y;
- }
- }
- LRESULT CALLBACK WndProc(HWND hWnda, UINT uMsg, WPARAM wParam, LPARAM lParam){
- GetClientRect(hWnda, &rect);
- switch(uMsg){
- case WM_CREATE:
- SetTimer(hWnda, ID_TIMER, 100, NULL);
- break;
- case WM_ERASEBKGND:
- return true;
- case WM_KEYDOWN:
- switch(wParam){
- case VK_UP:
- SetWindowText(okno, L"Sznejk idzie do gory.");
- up = true;
- down = false;
- right = false;
- left = false;
- break;
- case VK_DOWN:
- SetWindowText(okno, L"Sznejk idzie w dol.");
- up = false;
- down = true;
- right = false;
- left = false;
- break;
- case VK_LEFT:
- SetWindowText(okno, L"Sznejk idzie w lewo.");
- up = false;
- down = false;
- right = false;
- left = true;
- break;
- case VK_RIGHT:
- SetWindowText(okno, L"Sznejk idzie w prawo.");
- up = false;
- down = false;
- right = true;
- left = false;
- break;
- }
- break;
- case WM_TIMER:
- switch(wParam){
- case ID_TIMER:
- if(up) tab[0].y--;
- else if(down) tab[0].y++;
- else if(right) tab[0].x++;
- else if(left) tab[0].x--;
- koniec = gameovercheck();
- InvalidateRect(hWnda, NULL, false);
- break;
- }
- break;
- case WM_PAINT:
- hdc = BeginPaint(okno, &ps);
- kra = (rect.right - rect.left - 20)/50;
- krb = (rect.bottom - rect.top - 20)/50;
- if(krb<kra) kra = krb;
- plansza();
- food();
- snake(tab[0].x,tab[0].y);
- EndPaint(okno, &ps);
- break;
- case WM_SIZE:
- InvalidateRect(hWnda, NULL, false);
- break;
- case WM_CLOSE:
- DestroyWindow(okno);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnda, uMsg, wParam, lParam);
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int
- nCmdShow){
- srand(time(NULL));
- randx = rand()%50;
- randy = rand()%50;
- tab[0].x = sx;
- tab[0].y = sy;
- tab[1].x = sx;
- tab[1].y = sy+1;
- tab[2].x = sx;
- tab[2].y = sy+2;
- WNDCLASS a;
- a.style = CS_HREDRAW | CS_VREDRAW;
- a.lpfnWndProc = WndProc;
- a.cbClsExtra = sizeof(WNDCLASS);
- a.cbWndExtra = sizeof(WNDCLASS);
- a.hInstance = hInstance;
- a.hIcon = LoadIcon(0, IDI_APPLICATION);
- a.hCursor = LoadCursor(0, IDC_ARROW);
- a.hbrBackground = (HBRUSH)(COLOR_WINDOW);
- a.lpszMenuName = 0;
- a.lpszClassName = klasa;
- if(RegisterClass(&a) == NULL){
- MessageBox(0, L"Blad przy rejestrowaniu okna.", L"Blad!", MB_ICONWARNING |
- MB_OK);
- return 0;
- }
- okno = CreateWindowEx(0, klasa, title, WS_OVERLAPPEDWINDOW, 100, 100, width, height,
- 0, 0, hInstance, 0);
- ShowWindow(okno, nCmdShow);
- UpdateWindow(okno);
- MSG msg;
- while(GetMessage(&msg, 0, 0, 0) > 0){
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- if(koniec){
- UnregisterClass(klasa, hInstance);
- return msg.wParam;
- }
- }
- UnregisterClass(klasa, hInstance);
- return msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement