Advertisement
Guest User

Untitled

a guest
Jan 11th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <windows.h>
  5. #include <time.h>
  6.  
  7. #define height 15
  8. #define width 30
  9. #define lewo 75
  10. #define prawo 77
  11. #define gora 72
  12. #define dol 80
  13.  
  14. typedef struct {
  15. char a;
  16. int x;
  17. int y;
  18. int wynik;
  19. }Snake;
  20. typedef struct{
  21. char field[height][width];
  22. }map;
  23.  
  24. int koniec = 1;
  25.  
  26.  
  27. void initScreen();
  28. void initMap(map *f, Snake *g, Snake *h, Snake *e);
  29. void ruch(Snake *g);
  30. void ruch2(Snake *h);
  31. void putcharXY(int x, int y, unsigned char z);
  32. void rysuj(const map *f);
  33. void zryj(map *f, Snake *g, Snake *h, Snake *e);
  34.  
  35.  
  36. void putcharXY(int x, int y, unsigned char z){
  37. COORD a;
  38. a.Y = x;
  39. a.X = y;
  40. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), a);
  41. printf("%c", z);
  42. }
  43.  
  44. void initScreen(void){
  45. // Wymaga: windows.h
  46. // wyłączenie mrugania kursora - działa od windowsa XP
  47. CONSOLE_CURSOR_INFO cciInfo;
  48. cciInfo.dwSize = 1;
  49. cciInfo.bVisible = 0;
  50. SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cciInfo);
  51. system("chcp 852");
  52. system("cls");
  53. }
  54.  
  55.  
  56. void ruch(Snake *g){
  57. while (_kbhit()){
  58. switch (_getch()){
  59. case lewo:
  60. if (g->y > 1){
  61. while (1){
  62. putcharXY(g->x, g->y, 32);
  63. g->y--;
  64. putcharXY(g->x, g->y, 176);
  65. Sleep(500);
  66. if (_getch()){
  67. ruch(g);
  68. }
  69. }
  70. }
  71. break;
  72. case prawo:
  73. if (g->y < width - 2){
  74. while (1){
  75. putcharXY(g->x, g->y, 32);
  76. g->y++;
  77. putcharXY(g->x, g->y, 176);
  78. Sleep(500);
  79. if (_getch()){
  80. ruch(g);
  81. }
  82. }
  83. }
  84. break;
  85. case gora:
  86. if (g->x > 1){
  87. while (1){
  88. putcharXY(g->x, g->y, 32);
  89. g->x--;
  90. putcharXY(g->x, g->y, 176);
  91. Sleep(500);
  92. if (_getch()){
  93. ruch(g);
  94. }
  95. }
  96. }
  97. break;
  98. case dol:
  99. if (g->x < height - 2){
  100. while (1){
  101. putcharXY(g->x, g->y, 32);
  102. g->x++;
  103. putcharXY(g->x, g->y, 176);
  104. Sleep(500);
  105. if (_getch()){
  106. ruch(g);
  107. }
  108. }
  109. }
  110. break;
  111. }
  112. }
  113. }
  114. void ruch2(Snake *h){
  115. while (_kbhit()){
  116. switch (_getch()){
  117. case 'a':
  118. if (h->y>1){
  119. while (1){
  120. putcharXY(h->x, h->y, 32);
  121. h->y--;
  122. putcharXY(h->x, h->y, 178);
  123. Sleep(500);
  124. if (_getch()){
  125. ruch2(h);
  126. }
  127. }
  128. }
  129. break;
  130. case 'd':
  131. if (h->y<width - 2){
  132. while (1){
  133. putcharXY(h->x, h->y, 32);
  134. h->y++;
  135. putcharXY(h->x, h->y, 178);
  136. Sleep(500);
  137. if (_getch()){
  138. ruch2(h);
  139. }
  140. }
  141. }
  142. break;
  143. case 'w':
  144. if (h->x>1){
  145. while (1){
  146. putcharXY(h->x, h->y, 32);
  147. h->x--;
  148. putcharXY(h->x, h->y, 178);
  149. Sleep(500);
  150. if (_getch()){
  151. ruch2(h);
  152. }
  153. }
  154. }
  155. break;
  156. case 's':
  157. if (h->x<height - 2){
  158. while (1){
  159. putcharXY(h->x, h->y, 32);
  160. h->x++;
  161. putcharXY(h->x, h->y, 178);
  162. Sleep(500);
  163. if (_getch()){
  164. ruch2(h);
  165. }
  166. }
  167. }
  168. break;
  169. }
  170. }
  171. }
  172.  
  173.  
  174. void initMap(map *f, Snake *g, Snake *h, Snake *e){
  175. int i, j;
  176. for (i = 0; i<height; i++){
  177. for (j = 0; j<width; j++){
  178. if (i == 0 || i == height - 1){
  179. f->field[i][j] = 219;
  180. // printf("%c", 219);
  181. }
  182. else if (j == 0 || j == width - 1){
  183. // printf("%c", 219);
  184. f->field[i][j] = 219;
  185. }
  186. else if (i == g->x && j == g->y){
  187. // printf("%c", 176);
  188. f->field[i][j] = 176;
  189. }
  190. else if (i == h->x && j == h->y){
  191. // printf("%c", 178);
  192. f->field[i][j] = 178;
  193. }
  194. else if (i == e->x && j == e->y){
  195. f->field[i][j] = 177;
  196. }
  197. else
  198. // printf(" ");
  199. f->field[i][j] = 32;
  200. }
  201. }
  202. }
  203.  
  204. void rysuj(const map *f){
  205. int i, j, tmp;
  206. static int pirszyroz = 1;
  207. static char kasze[height][width];
  208. for (i = 0; i<height; i++){
  209. for (j = 0; j<width; j++){
  210. tmp = f->field[i][j];
  211. if (pirszyroz){
  212. putcharXY(i, j, tmp);
  213. kasze[i][j] = tmp;
  214. }
  215. else{
  216. if (kasze[i][j] != tmp){
  217. putcharXY(i, j, tmp);
  218. kasze[i][j] = tmp;
  219. }
  220. }
  221. }
  222. }
  223. pirszyroz = 0;
  224. }
  225.  
  226. void zryj(map *f, Snake *g, Snake *h, Snake *e){
  227. if (g->x == e->x && g->y == e->y){
  228. e->x = rand() % 13 + 1;
  229. e->y = rand() % 28 + 1;
  230. putcharXY(e->x, e->y, e->a);
  231. g->wynik++;
  232. putcharXY(6, 35, (int)g->wynik);
  233. }
  234. else if (h->x == e->x && h->y == e->y){
  235. e->x = rand() % 13 + 1;
  236. e->y = rand() % 28 + 1;
  237. putcharXY(e->x, e->y, e->a);
  238. h->wynik++;
  239. putcharXY(11, 35, (int)h->wynik);
  240. }
  241.  
  242. }
  243.  
  244.  
  245. main(){
  246. initScreen();
  247. srand(time(NULL));
  248. Snake s1 = { 176, 7, 10, 49 };
  249. Snake s2 = { 178, 7, 20, 49 };
  250. Snake japko = { 177, rand() % 13 + 1, rand() % 28 + 1 };
  251. map mapa;
  252. char field[height][width];
  253. int h = 0;
  254. initMap(&mapa, &s1, &s2, &japko);
  255.  
  256. DWORD WINAPI ThreadId1, ThreadId2;
  257. HANDLE hHandles1, hHandles2;
  258. hHandles1 = CreateThread(NULL, 0, ruch(&s1),
  259. 0, 0, &ThreadId1);
  260. hHandles2 = CreateThread(NULL, 0, ruch2(&s2),
  261. 0, 0, &ThreadId2);
  262. if (hHandles1 == NULL || hHandles2 == NULL)
  263. {
  264. printf("Nieudane tworzenie watkow\n");
  265. exit(0);
  266. }
  267. //Oczekiwanie na zakończenie funkcji
  268. WaitForSingleObject(hHandles1, INFINITE);
  269. WaitForSingleObject(hHandles2, INFINITE);
  270.  
  271.  
  272. while (1){
  273. rysuj(&mapa);
  274. ruch(&s1);
  275. ruch2(&s2);
  276. zryj(&mapa, &s1, &s2, &japko);
  277. }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement