Advertisement
Mihao

Snake2

Jan 11th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. #include <windows.h>
  2. #include <time.h>
  3. #include <math.h>
  4. #include <cstdlib>
  5. #include <algorithm>
  6. #define ID_TIMER 1
  7. HWND okno;
  8. RECT rect;
  9. HBRUSH brush;
  10. HPEN pen;
  11. HDC hdc;
  12. PAINTSTRUCT ps;
  13. LPCWSTR klasa = L"Klasa";
  14. LPCWSTR title = L"Sznejk.";
  15. wchar_t title2[128];
  16. int width = 600, height = 600;
  17. int randx, randy;
  18. int sx = 24, sy = 24;
  19. int kra = 10, krb = 10;
  20. int punkty = 0;
  21. bool up = true, down = false, right = false, left = false;
  22. bool koniec = false;
  23. POINT tab[2500];
  24. void plansza(){
  25. brush = CreateSolidBrush(RGB(255,255,255));
  26. SelectObject(hdc, brush);
  27. for(int i=0; i<50; i++){
  28. for(int j=0; j<50; j++){
  29. Rectangle(hdc, kra*j-1+10, kra*i-1+10, kra*j+kra+10, kra*i+kra+10);
  30. }
  31. }
  32. DeleteObject(brush);
  33. }
  34. void food(){
  35. brush = CreateSolidBrush(RGB(0,255,0));
  36. SelectObject(hdc, brush);
  37. Rectangle(hdc, kra*randx-1+10, kra*randy-1+10, kra*randx+kra+10, kra*randy+kra+10);
  38. DeleteObject(brush);
  39. }
  40. bool gameovercheck(){
  41. bool gameover = false;
  42. if(tab[0].x < 0 || tab[0].x > 49 || tab[0].y < 0 || tab[0].y > 49) gameover = true;
  43. else{
  44. //to nie dzialo przy zmianie rozmiaru okna
  45. for(int i=0; i<3+punkty-1; i++){
  46. for(int j=i+1; j<3+punkty; j++){
  47. if(tab[i].x == tab[j].x && tab[i].y == tab[j].y){
  48. gameover = true;
  49. wsprintf(title2, L"%d %d - %d %d %d %d", i, j, tab[i].x, tab[j].x,
  50. tab[i].y, tab[j].y);
  51. SetWindowText(okno, title2);
  52. break;
  53. }
  54. }
  55. }
  56. }
  57. if(gameover){
  58. KillTimer(okno, ID_TIMER);
  59. wsprintf(title2, L"Game Over! KONIEC GRY! CO?! NAJWYZSZY WYNIK!\nTak trzymaj
  60. zolnierzu! Zdobyles %d punktow!", punkty);
  61. MessageBox(0, title2, L"Game Over!", MB_ICONWARNING | MB_OK);
  62. return true;
  63. }else return false;
  64. }
  65. void snake(int x, int y){
  66. tab[0].x = x;
  67. tab[0].y = y;
  68. if(x == randx && y == randy){
  69. SetWindowText(okno, L"Mmmm ale dobre! Sca tego wiyncy!");
  70. randx = rand()%50;
  71. randy = rand()%50;
  72. punkty++;
  73. tab[2+punkty].x = tab[1+punkty].x;
  74. tab[2+punkty].y = tab[1+punkty].y;
  75. }
  76. brush = CreateSolidBrush(RGB(255,0,0));
  77. SelectObject(hdc, brush);
  78. for(int i=0; i<3+punkty; i++) Rectangle(hdc, kra*tab[i].x-1+10, kra*tab[i].y-1+10,
  79. kra*tab[i].x+kra+10, kra*tab[i].y+kra+10);
  80. DeleteObject(brush);
  81. for(int i=2+punkty; i>0; i--){
  82. tab[i].x = tab[i-1].x;
  83. tab[i].y = tab[i-1].y;
  84. }
  85. }
  86. LRESULT CALLBACK WndProc(HWND hWnda, UINT uMsg, WPARAM wParam, LPARAM lParam){
  87. GetClientRect(hWnda, &rect);
  88. switch(uMsg){
  89. case WM_CREATE:
  90. SetTimer(hWnda, ID_TIMER, 100, NULL);
  91. break;
  92. case WM_ERASEBKGND:
  93. return true;
  94. case WM_KEYDOWN:
  95. switch(wParam){
  96. case VK_UP:
  97. SetWindowText(okno, L"Sznejk idzie do gory.");
  98. up = true;
  99. down = false;
  100. right = false;
  101. left = false;
  102. break;
  103. case VK_DOWN:
  104. SetWindowText(okno, L"Sznejk idzie w dol.");
  105. up = false;
  106. down = true;
  107. right = false;
  108. left = false;
  109. break;
  110. case VK_LEFT:
  111. SetWindowText(okno, L"Sznejk idzie w lewo.");
  112. up = false;
  113. down = false;
  114. right = false;
  115. left = true;
  116. break;
  117. case VK_RIGHT:
  118. SetWindowText(okno, L"Sznejk idzie w prawo.");
  119. up = false;
  120. down = false;
  121. right = true;
  122. left = false;
  123. break;
  124. }
  125. break;
  126. case WM_TIMER:
  127. switch(wParam){
  128. case ID_TIMER:
  129. if(up) tab[0].y--;
  130. else if(down) tab[0].y++;
  131. else if(right) tab[0].x++;
  132. else if(left) tab[0].x--;
  133. koniec = gameovercheck();
  134. InvalidateRect(hWnda, NULL, false);
  135. break;
  136. }
  137. break;
  138. case WM_PAINT:
  139. hdc = BeginPaint(okno, &ps);
  140. kra = (rect.right - rect.left - 20)/50;
  141. krb = (rect.bottom - rect.top - 20)/50;
  142. if(krb<kra) kra = krb;
  143. plansza();
  144. food();
  145. snake(tab[0].x,tab[0].y);
  146. EndPaint(okno, &ps);
  147. break;
  148. case WM_SIZE:
  149. InvalidateRect(hWnda, NULL, false);
  150. break;
  151. case WM_CLOSE:
  152. DestroyWindow(okno);
  153. break;
  154. case WM_DESTROY:
  155. PostQuitMessage(0);
  156. break;
  157. default:
  158. return DefWindowProc(hWnda, uMsg, wParam, lParam);
  159. }
  160. return 0;
  161. }
  162. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int
  163. nCmdShow){
  164. srand(time(NULL));
  165. randx = rand()%50;
  166. randy = rand()%50;
  167. tab[0].x = sx;
  168. tab[0].y = sy;
  169. tab[1].x = sx;
  170. tab[1].y = sy+1;
  171. tab[2].x = sx;
  172. tab[2].y = sy+2;
  173. WNDCLASS a;
  174. a.style = CS_HREDRAW | CS_VREDRAW;
  175. a.lpfnWndProc = WndProc;
  176. a.cbClsExtra = sizeof(WNDCLASS);
  177. a.cbWndExtra = sizeof(WNDCLASS);
  178. a.hInstance = hInstance;
  179. a.hIcon = LoadIcon(0, IDI_APPLICATION);
  180. a.hCursor = LoadCursor(0, IDC_ARROW);
  181. a.hbrBackground = (HBRUSH)(COLOR_WINDOW);
  182. a.lpszMenuName = 0;
  183. a.lpszClassName = klasa;
  184. if(RegisterClass(&a) == NULL){
  185. MessageBox(0, L"Blad przy rejestrowaniu okna.", L"Blad!", MB_ICONWARNING |
  186. MB_OK);
  187. return 0;
  188. }
  189. okno = CreateWindowEx(0, klasa, title, WS_OVERLAPPEDWINDOW, 100, 100, width, height,
  190. 0, 0, hInstance, 0);
  191. ShowWindow(okno, nCmdShow);
  192. UpdateWindow(okno);
  193. MSG msg;
  194. while(GetMessage(&msg, 0, 0, 0) > 0){
  195. TranslateMessage(&msg);
  196. DispatchMessage(&msg);
  197. if(koniec){
  198. UnregisterClass(klasa, hInstance);
  199. return msg.wParam;
  200. }
  201. }
  202. UnregisterClass(klasa, hInstance);
  203. return msg.wParam;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement