Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 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 25
  8. #define width 60
  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, Snake *h);
  30. void move(Snake *g, Snake *h);
  31. int move1(Snake *g);
  32. int move2(Snake *h);
  33. void logic(Snake *g, Snake *h);
  34. void logic1(Snake *g);
  35. void logic2(Snake *h);
  36. void putcharXY(int x, int y, unsigned char z);
  37. void rysuj(const map *f);
  38. void food(map *f, Snake *g, Snake *h, Snake *e);
  39.  
  40.  
  41. void putcharXY(int x, int y, unsigned char z){
  42. COORD a;
  43. a.Y = x;
  44. a.X = y;
  45. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), a);
  46. printf("%c", z);
  47. }
  48.  
  49. void initScreen(void){
  50. // Wymaga: windows.h
  51. // wyłączenie mrugania kursora - działa od windowsa XP
  52. CONSOLE_CURSOR_INFO cciInfo;
  53. cciInfo.dwSize = 1;
  54. cciInfo.bVisible = 0;
  55. SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cciInfo);
  56. system("chcp 852");
  57. system("cls");
  58. }
  59.  
  60. int move1(Snake *g){
  61. static int dir1 = 3;
  62. while (_kbhit()){
  63. switch (_getch()){
  64. case lewo:
  65. if (dir1 != 2 && g->y>1){
  66. /* putcharXY(g->x, g->y, 32);
  67. g->y--;
  68. putcharXY(g->x, g->y, g->a);*/
  69. dir1 = 1;
  70. }
  71. break;
  72. case prawo:
  73. if (dir1 != 1 && g->y<width - 2){
  74. /*putcharXY(g->x, g->y, 32);
  75. g->y++;
  76. putcharXY(g->x, g->y, g->a);*/
  77. dir1 = 2;
  78. }
  79. break;
  80. case gora:
  81. if (dir1 != 4 && g->x>1){
  82. /*putcharXY(g->x, g->y, 32);
  83. g->x--;
  84. putcharXY(g->x, g->y, g->a);*/
  85. dir1 = 3;
  86. }
  87. break;
  88. case dol:
  89. if (dir1 != 3 && g->x<height - 2){
  90. /*putcharXY(g->x, g->y, 32);
  91. g->x++;
  92. putcharXY(g->x, g->y, g->a);*/
  93. dir1 = 4;
  94. }
  95. break;
  96. }
  97. }
  98. return dir1;
  99. }
  100.  
  101. int move2(Snake *h){
  102. static int dir2 = 3;
  103. while (_kbhit()){
  104. switch (_getch()){
  105. case 'a':
  106. if (dir2 != 2 && h->y>1){
  107. /* putcharXY(g->x, g->y, 32);
  108. g->y--;
  109. putcharXY(g->x, g->y, g->a);*/
  110. dir2 = 1;
  111. }
  112. break;
  113. case 'd':
  114. if (dir2 != 1 && h->y<width - 2){
  115. /*putcharXY(g->x, g->y, 32);
  116. g->y++;
  117. putcharXY(g->x, g->y, g->a);*/
  118. dir2 = 2;
  119. }
  120. break;
  121. case 'w':
  122. if (dir2 != 4 && h->x>1){
  123. /*putcharXY(g->x, g->y, 32);
  124. g->x--;
  125. putcharXY(g->x, g->y, g->a);*/
  126. dir2 = 3;
  127. }
  128. break;
  129. case 's':
  130. if (dir2 != 3 && h->x<height - 2){
  131. /*putcharXY(g->x, g->y, 32);
  132. g->x++;
  133. putcharXY(g->x, g->y, g->a);*/
  134. dir2 = 4;
  135. }
  136. break;
  137. }
  138. }
  139. return dir2;
  140. }
  141.  
  142.  
  143.  
  144.  
  145. void move(Snake *g, Snake *h){
  146. move1(g);
  147. move2(h);
  148. }
  149.  
  150. void logic1(Snake *g){
  151. if (move1(g) == 1){
  152. putcharXY(g->x, g->y, 32);
  153. g->y--;
  154. putcharXY(g->x, g->y, g->a);
  155. }
  156. else if (move1(g) == 2){
  157. putcharXY(g->x, g->y, 32);
  158. g->y++;
  159. putcharXY(g->x, g->y, g->a);
  160. }
  161. else if (move1(g) == 3){
  162. putcharXY(g->x, g->y, 32);
  163. g->x--;
  164. putcharXY(g->x, g->y, g->a);
  165. }
  166. else if (move1(g) == 4){
  167. putcharXY(g->x, g->y, 32);
  168. g->x++;
  169. putcharXY(g->x, g->y, g->a);
  170. }
  171. }
  172.  
  173. void logic2(Snake *h){
  174. if (move2(h) == 1){
  175. putcharXY(h->x, h->y, 32);
  176. h->y--;
  177. putcharXY(h->x, h->y, h->a);
  178. }
  179. else if (move2(h) == 2){
  180. putcharXY(h->x, h->y, 32);
  181. h->y++;
  182. putcharXY(h->x, h->y, h->a);
  183. }
  184. else if (move2(h) == 3){
  185. putcharXY(h->x, h->y, 32);
  186. h->x--;
  187. putcharXY(h->x, h->y, h->a);
  188. }
  189. else if (move2(h) == 4){
  190. putcharXY(h->x, h->y, 32);
  191. h->x++;
  192. putcharXY(h->x, h->y, h->a);
  193. }
  194. }
  195.  
  196. void logic(Snake*g, Snake *h){
  197. move1(g);
  198. move2(h);
  199. logic1(g);
  200. logic2(h);
  201. }
  202.  
  203. /*void ruch(Snake *g, Snake *h){
  204. while (_kbhit()){
  205. switch (_getch()){
  206. case lewo:
  207. if (g->y>1){
  208. putcharXY(g->x, g->y, 32);
  209. g->y--;
  210. putcharXY(g->x, g->y, 176);
  211. }
  212. break;
  213. case prawo:
  214. if (g->y<width - 2){
  215. putcharXY(g->x, g->y, 32);
  216. g->y++;
  217. putcharXY(g->x, g->y, 176);
  218. }
  219. break;
  220. case gora:
  221. if (g->x>1){
  222. putcharXY(g->x, g->y, 32);
  223. g->x--;
  224. putcharXY(g->x, g->y, 176);
  225. }
  226. break;
  227. case dol:
  228. if (g->x<height - 2){
  229. putcharXY(g->x, g->y, 32);
  230. g->x++;
  231. putcharXY(g->x, g->y, 176);
  232. }
  233. break;
  234. case 'a':
  235. if (h->y>1){
  236. putcharXY(h->x, h->y, 32);
  237. h->y--;
  238. putcharXY(h->x, h->y, 178);
  239. }
  240. break;
  241. case 'd':
  242. if (h->y<width - 2){
  243. putcharXY(h->x, h->y, 32);
  244. h->y++;
  245. putcharXY(h->x, h->y, 178);
  246. }
  247. break;
  248. case 'w':
  249. if (h->x>1){
  250. putcharXY(h->x, h->y, 32);
  251. h->x--;
  252. putcharXY(h->x, h->y, 178);
  253. }
  254. break;
  255. case 's':
  256. if (h->x<height - 2){
  257. putcharXY(h->x, h->y, 32);
  258. h->x++;
  259. putcharXY(h->x, h->y, 178);
  260. }
  261. break;
  262. }
  263. }
  264. }
  265. */
  266.  
  267. void initMap(map *f, Snake *g, Snake *h, Snake *e){
  268. int i, j;
  269. for (i = 0; i<height; i++){
  270. for (j = 0; j<width; j++){
  271. if (i == 0 || i == height - 1){
  272. f->field[i][j] = 219;
  273. // printf("%c", 219);
  274. }
  275. else if (j == 0 || j == width - 1){
  276. // printf("%c", 219);
  277. f->field[i][j] = 219;
  278. }
  279. else if (i == g->x && j == g->y){
  280. // printf("%c", 176);
  281. f->field[i][j] = 176;
  282. }
  283. else if (i == h->x && j == h->y){
  284. // printf("%c", 178);
  285. f->field[i][j] = 178;
  286. }
  287. else if (i == e->x && j == e->y){
  288. f->field[i][j] = 177;
  289. }
  290. else
  291. // printf(" ");
  292. f->field[i][j] = 32;
  293. }
  294. }
  295. }
  296.  
  297. void rysuj(const map *f){
  298. int i, j, tmp;
  299. static int pirszyroz = 1;
  300. static char kasze[height][width];
  301. for (i = 0; i<height; i++){
  302. for (j = 0; j<width; j++){
  303. tmp = f->field[i][j];
  304. if (pirszyroz){
  305. putcharXY(i, j, tmp);
  306. kasze[i][j] = tmp;
  307. }
  308. else{
  309. if (kasze[i][j] != tmp){
  310. putcharXY(i, j, tmp);
  311. kasze[i][j] = tmp;
  312. }
  313. }
  314. }
  315. }
  316. pirszyroz = 0;
  317. }
  318.  
  319. void food(map *f, Snake *g, Snake *h, Snake *e){
  320. if (g->x == e->x && g->y == e->y){
  321. e->x = rand() % 13 + 1;
  322. e->y = rand() % 28 + 1;
  323. putcharXY(e->x, e->y, e->a);
  324. g->wynik++;
  325. //putcharXY(6, 35, (int)g->wynik);
  326. }
  327. else if (h->x == e->x && h->y == e->y){
  328. e->x = rand() % 13 + 1;
  329. e->y = rand() % 28 + 1;
  330. putcharXY(e->x, e->y, e->a);
  331. h->wynik++;
  332. //putcharXY(11, 35, (int)h->wynik);
  333. }
  334.  
  335. }
  336.  
  337.  
  338. main(){
  339. initScreen();
  340. srand(time(NULL));
  341. Snake s1 = { 176, 14, 20, 49 };
  342. Snake s2 = { 178, 14, 40, 49 };
  343. Snake japko = { 177, rand() % 13 + 1, rand() % 28 + 1 };
  344. map mapa;
  345. char field[height][width];
  346. int h = 0;
  347. initMap(&mapa, &s1, &s2, &japko);
  348.  
  349.  
  350. while (1){
  351. rysuj(&mapa);
  352. move(&s1, &s2);
  353.  
  354. //move1(&s1);
  355. //logic1(&s1);
  356. //move2(&s2);
  357. logic(&s1, &s2);
  358. //logic2(&s2);
  359. //ruch(&s1, &s2);
  360. food(&mapa, &s1, &s2, &japko);
  361. Sleep(500);
  362. }
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement