Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <conio.h>
  5. #include <windows.h>
  6. #include <unistd.h>
  7. #include <stdbool.h>
  8. #include <pthread.h>
  9.  
  10. #define UP 72
  11. #define DOWN 80
  12. #define LEFT 75
  13. #define RIGHT 77
  14.  
  15. void menuIniziale();
  16. void iniziaGioco();
  17. void sopra();
  18. void sotto();
  19. void sinistra();
  20. void destra();
  21. void * snake(void *);
  22. void gestionePiegamento();
  23. void gameOver();
  24.  
  25. boolean snakevivo = false, snakespostato = false;
  26.  
  27. int len, length, bend_no, vite;
  28. char key;
  29.  
  30. typedef struct
  31. {
  32. int x;
  33. int y;
  34. int dir;
  35. } Coordinate;
  36.  
  37. Coordinate head, bend[500], cibo, body[30];
  38.  
  39. int main(int argc, char *argv[]) {
  40. iniziaGioco();
  41. return 0;
  42. }
  43.  
  44. // Functions to go to a certain position
  45. void gotoxy(int x, int y) {
  46. COORD dwCursorPosition = {x, y};
  47. SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), dwCursorPosition);
  48. }
  49.  
  50. void GotoXY(int x, int y) {
  51. HANDLE a;
  52. COORD b;
  53. fflush(stdout);
  54. b.X = x;
  55. b.Y = y;
  56. a = GetStdHandle(STD_OUTPUT_HANDLE);
  57. SetConsoleCursorPosition(a,b);
  58. }
  59.  
  60. void iniziaGioco() {
  61. system("cls");
  62. length = 5; // Initial snake length
  63. head.x = 25;// Initial coordinates
  64. head.y = 20;
  65. head.dir = RIGHT; // Initial snake direction
  66. vite = 3; // Number of lives
  67. bend[0] = head;
  68. snakevivo = true;
  69.  
  70. // Let's start threads
  71. // tSnake = Controlla Snake
  72.  
  73. pthread_t tSnake;
  74. pthread_create(&tSnake, NULL, &snake, NULL);
  75. pthread_join(tSnake, NULL);
  76.  
  77. //movimentoGiocatore();*/
  78. }
  79.  
  80. // Snake movement functions
  81. /// UP
  82. void sopra() {
  83. for (int i = 0 ;i <= (bend[bend_no].y - head.y) && len < length; i++) {
  84. GotoXY(head.x,head.y+i);
  85.  
  86. if(len==0) printf("^");
  87. else printf("*");
  88.  
  89. body[len].x = head.x;
  90. body[len].y = head.y+i;
  91.  
  92. len++;
  93. }
  94.  
  95. gestionePiegamento();
  96.  
  97. if(!kbhit()) head.y--;
  98. }
  99.  
  100. /// DOWN
  101. void sotto() {
  102. for (int i = 0; i <= (head.y - bend[bend_no].y) && len < length; i++) {
  103. GotoXY(head.x,head.y-i);
  104.  
  105. if(len==0) printf("v");
  106. else printf("*");
  107.  
  108. body[len].x = head.x;
  109. body[len].y = head.y-i;
  110.  
  111. len++;
  112. }
  113.  
  114. gestionePiegamento();
  115.  
  116. if(!kbhit()) head.y++;
  117. }
  118.  
  119. /// LEFT
  120. void sinistra() {
  121. for (int i = 0; i <= (bend[bend_no].x - head.x) && len < length; i++) {
  122. GotoXY((head.x+i),head.y);
  123.  
  124. if(len == 0) printf("<");
  125. else printf("*");
  126.  
  127. body[len].x = head.x+i;
  128. body[len].y = head.y;
  129.  
  130. len++;
  131. }
  132.  
  133. gestionePiegamento();
  134.  
  135. if(!kbhit()) head.x--;
  136.  
  137. }
  138.  
  139. /// RIGHT
  140. void destra() {
  141. for (int i = 0; i <= (head.x - bend[bend_no].x) && len < length; i++) {
  142. GotoXY(body[len].x, body[len].y);
  143.  
  144. if(len == 0) printf(">");
  145. else printf("*");
  146.  
  147. body[len].x = head.x-i;
  148. body[len].y = head.y;
  149.  
  150. len++;
  151. }
  152.  
  153. gestionePiegamento();
  154.  
  155. if (!kbhit()) head.x++;
  156. }
  157.  
  158. // Funzione per movement and input
  159. void * snake(void *)
  160. {
  161. while (snakevivo) {
  162. fflush(stdin);
  163. len = 0;
  164.  
  165. for (int i=0;i<30;i++)
  166. {
  167. body[i].x = 0;
  168. body[i].y = 0;
  169. if (i == length) break;
  170. }
  171.  
  172. if (head.dir == RIGHT) destra();
  173. else if (head.dir == LEFT) sinistra();
  174. else if (head.dir == DOWN) sotto();
  175. else if (head.dir == UP) sopra();
  176.  
  177. // Input movimento
  178. key = getch();
  179.  
  180. // Controlla se l'input permette un movimento valido
  181. if((key == RIGHT && head.dir != LEFT &&head.dir != RIGHT)||
  182. (key == LEFT && head.dir != RIGHT && head.dir != LEFT)||
  183. (key == UP && head.dir != DOWN && head.dir != UP)||
  184. (key == DOWN && head.dir != UP && head.dir != DOWN)) {
  185.  
  186. bend_no++;
  187. bend[bend_no] = head;
  188. head.dir = key;
  189.  
  190. switch (key) {
  191. case UP:
  192. head.y--;
  193. break;
  194. case DOWN:
  195. head.y++;
  196. break;
  197. case RIGHT:
  198. head.x++;
  199. case LEFT:
  200. head.x--;
  201. break;
  202. }
  203. }
  204.  
  205. if (getch() == 27) gameOver();
  206.  
  207. snakespostato = true;
  208. sleep(200);
  209. }
  210. }
  211.  
  212. // Function to manage the snake's tail when direction changes
  213. void gestionePiegamento()
  214. {
  215. int i, j, diff;
  216.  
  217. for(i = bend_no; i >= 0 && len < length; i--)
  218. {
  219. if(bend[i].x == bend[i-1].x)
  220. {
  221. diff=bend[i].y-bend[i-1].y;
  222.  
  223. if(diff < 0) {
  224. for(j = 1; j <= (-diff); j++) {
  225. body[len].x = bend[i].x;
  226. body[len].y = bend[i].y+j;
  227.  
  228. GotoXY(body[len].x,body[len].y);
  229. printf("*");
  230. len++;
  231.  
  232. if (len == length) break;
  233. }
  234. }
  235. else if (diff > 0) {
  236. for(j = 1; j <= diff;j++)
  237. {
  238. body[len].x = bend[i].x;
  239. body[len].y = bend[i].y-j;
  240.  
  241. GotoXY(body[len].x,body[len].y);
  242. printf("*");
  243. len++;
  244.  
  245. if (len == length) break;
  246. }
  247. }
  248. }
  249. else if (bend[i].y == bend[i-1].y)
  250. {
  251. diff = bend[i].x - bend[i-1].x;
  252.  
  253. if (diff < 0) {
  254. for(j = 1;j <= (-diff) && len < length; j++)
  255. {
  256. body[len].x = bend[i].x+j;
  257. body[len].y = bend[i].y;
  258.  
  259. GotoXY(body[len].x,body[len].y);
  260. printf("*");
  261.  
  262. len++;
  263. if (len == length) break;
  264. }
  265. }
  266. else if (diff > 0) {
  267. for(j = 1; j <= diff && len < length; j++)
  268. {
  269. body[len].x = bend[i].x-j;
  270. body[len].y = bend[i].y;
  271.  
  272. GotoXY(body[len].x,body[len].y);
  273. printf("*");
  274. len++;
  275.  
  276. if (len == length) break;
  277. }
  278. }
  279. }
  280. }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement