Advertisement
Guest User

Ping Pong

a guest
Nov 15th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.65 KB | None | 0 0
  1. Main.c
  2. ------------
  3. #include "display.h"
  4. #include "game.h"
  5.  
  6. #define MIN(x,y) ((x)>(y) ? (y) : (x))
  7. #define DELAY 2
  8. #define PAD_OFFSET 4
  9. #define PAD_LEN_RATIO 5
  10.  
  11. #define LEFT -1
  12. #define RIGHT 1
  13. #define UP -1
  14. #define DOWN 1
  15.  
  16. /* Starting pad1 position */
  17. static struct position pad1_pos(int height, int width);
  18.  
  19. /* Starting pad2 position */
  20. static struct position pad2_pos(int height, int width);
  21.  
  22. /* Starting ball position */
  23. static struct position ball_pos(int height, int width);
  24.  
  25. /* Starting direction position */
  26. static struct position ball_dir(void);
  27.  
  28. /* Pad length */
  29. static int pad_len(int height);
  30.  
  31.  
  32. int main() {
  33. int STOP = 0, cont = 0, height, width;
  34. unsigned int s1, s2;
  35. #ifdef REG
  36. FILE *fp;
  37. fp=fopen("record.txt","w");
  38. #endif
  39.  
  40. display_open(&height,&width);
  41.  
  42. #ifdef REG
  43. fprintf(fp,"%d %d\n",height,width);
  44. #endif
  45.  
  46. setup_game(height,width,ball_pos(height,width),ball_dir(),
  47. pad1_pos(height,width),pad2_pos(height,width),pad_len(height));
  48.  
  49. display_start();
  50.  
  51. do {
  52. // Add a delay to move_ball()
  53. if((cont = (cont + 1) % DELAY) == 0) {
  54. move_ball();
  55. display_table();
  56. #ifdef REG
  57. fprintf(fp,"M");
  58. #endif
  59. }
  60. switch (display_read()) {
  61. case P1_DOWN: move_pad1_down();
  62. #ifdef REG
  63. fprintf(fp,"z");
  64. #endif
  65. break;
  66. case P1_UP: move_pad1_up();
  67. #ifdef REG
  68. fprintf(fp,"a");
  69. #endif
  70. break;
  71. case P2_UP: move_pad2_up();
  72. #ifdef REG
  73. fprintf(fp,"u");
  74. #endif
  75. break;
  76. case P2_DOWN: move_pad2_down();
  77. #ifdef REG
  78. fprintf(fp,"d");
  79. #endif
  80. break;
  81. case PAUSE: display_pause(); break;
  82. case QUIT: STOP = 1; break;
  83. }
  84. display_table();
  85. } while(!STOP && ((s1 = get_pad1_score()) != 9 && (s2 = get_pad2_score()) != 9));
  86.  
  87. if(s1 == 9 || s2 == 9)
  88. display_win(s1 == 9 ? 1 : 2);
  89.  
  90. display_quit();
  91. display_close();
  92. #ifdef REG
  93. fclose(fp);
  94. #endif
  95. }
  96.  
  97. /* Starting pad1 position */
  98. static struct position pad1_pos(int height, int width) {
  99. struct position pos;
  100. pos.x = PAD_OFFSET;
  101. pos.y = height/2;
  102. return pos;
  103. }
  104.  
  105. /* Starting pad2 position */
  106. static struct position pad2_pos(int height, int width) {
  107. struct position pos;
  108. pos.x = width-PAD_OFFSET;
  109. pos.y = height/2;
  110. return pos;
  111. }
  112.  
  113. /* Starting ball position */
  114. static struct position ball_pos(int height, int width) {
  115. struct position pos;
  116. pos.x = width/2;
  117. pos.y = height/2;
  118. return pos;
  119. }
  120.  
  121. /* Starting direction position */
  122. static struct position ball_dir(void) {
  123. struct position pos = {DOWN,RIGHT};
  124. return pos;
  125. }
  126.  
  127. /* Pad length */
  128. static int pad_len(int height) {
  129. return MIN(height/2+1,height/PAD_LEN_RATIO);
  130. }
  131. -------------------------------------------------------
  132. game.h
  133. #ifndef GAME_H
  134. #define GAME_H
  135.  
  136. struct position {
  137. int x;
  138. int y;
  139. };
  140.  
  141. /*
  142. * Setup a game with the following starting configuration:
  143. * - table dimension equal to heigth*width
  144. * - ball starting position at ball_pos
  145. * - ball starting direction towards ball_dir
  146. * - pad1 starting position at pad1_pos
  147. * - pad2 starting position at pad2_pos
  148. * - pad length equal to pad_len
  149. */
  150. void setup_game(int height, int width,
  151. struct position ball_pos, struct position ball_dir,
  152. struct position pad1_pos, struct position pad2_pos, int pad_len);
  153.  
  154. /* Moves pad1 one position up. */
  155. void move_pad1_up(void);
  156.  
  157. /* Moves pad2 one poisiton up */
  158. void move_pad2_up(void);
  159.  
  160. /* Moves pad1 one position down. */
  161. void move_pad1_down(void);
  162.  
  163. /* Moves pad2 one position down. */
  164. void move_pad2_down(void);
  165.  
  166. /* Moves the ball in the current direction */
  167. void move_ball(void);
  168.  
  169. /* Returns ball current position */
  170. struct position get_ball_pos(void);
  171.  
  172. /* Returns pad1 current position */
  173. struct position get_pad1_pos(void);
  174.  
  175. /* Returns pad2 current position */
  176. struct position get_pad2_pos(void);
  177.  
  178. /* Returns the pad length */
  179. unsigned int get_pad_len(void);
  180.  
  181. /* Returns pad1 current score */
  182. unsigned int get_pad1_score(void);
  183.  
  184. /* Returns pad2 current score */
  185. unsigned int get_pad2_score(void);
  186.  
  187. #endif
  188. -----------------------------------------
  189. game.c
  190.  
  191. struct position {
  192. int x;
  193. int y;
  194. };
  195.  
  196. struct position pad1;
  197. struct position pad2;
  198. static int len;
  199. struct position ball_position;
  200. struct position ball_direction;
  201. static int w, h;
  202. static int pad1_score = 0, pad2_score = 0 ;
  203.  
  204. /*
  205. * Setup a game with the following starting configuration:
  206. * - table dimension equal to heigth*width
  207. * - ball starting position at ball_pos
  208. * - ball starting direction towards ball_dir
  209. * - pad1 starting position at pad1_pos
  210. * - pad2 starting position at pad2_pos
  211. * - pad length equal to pad_len
  212. */
  213. void setup_game(int height, int width,
  214. struct position ball_pos, struct position ball_dir,
  215. struct position pad1_pos, struct position pad2_pos, int pad_len) {
  216. pad1.x = pad1_pos.x;
  217. pad1.y = pad1_pos.y;
  218. pad2.x = pad2_pos.x;
  219. pad2.y = pad2_pos.y;
  220. len = pad_len;
  221. ball_position = ball_pos;
  222. ball_direction = ball_dir;
  223. w = width; h = height;
  224. }
  225.  
  226. /* Moves pad1 one position up. */
  227. void move_pad1_up(void) {
  228. if (!(pad1.y == 0)) {
  229. if (!((ball_position.y == pad1.y || ball_position.y == pad1.y - 1) && (ball_position.x == pad1.x || ball_position.x == pad1.x + 1 || ball_position.x == pad1.x - 1))) {
  230. pad1.y -=1;
  231. }
  232. }
  233. }
  234.  
  235. /* Moves pad2 one poisiton up */
  236. void move_pad2_up(void) {
  237. if (!(pad2.y == 0)) {
  238. if (!((ball_position.y == pad2.y || ball_position.y == pad2.y - 1) && (ball_position.x + 2 == pad2.x || ball_position.x + 1 == pad2.x || ball_position.x == pad2.x))) {
  239. pad2.y -= 1;
  240. }
  241. }
  242. }
  243.  
  244. /* Moves pad1 one position down. */
  245. void move_pad1_down(void) {
  246. if (!(pad1.y + len - 1 == h)) {
  247. if (!(ball_position.y == pad1.y + len && (ball_position.x == pad1.x || ball_position.x == pad1.x + 1 || ball_position.x == pad1.x - 1))) {
  248. pad1.y += 1;
  249. }
  250. }
  251. }
  252.  
  253. /* Moves pad2 one position down. */
  254. void move_pad2_down(void) {
  255. if (!(pad2.y + len - 1 == h)) {
  256. if (!(ball_position.y == pad2.y + len && (ball_position.x + 2 == pad2.x || ball_position.x + 1 == pad2.x + 1 || ball_position.x == pad2.x))) {
  257. pad2.y += 1;
  258. }
  259. }
  260. }
  261.  
  262. /* Moves the ball in the current direction */
  263. void move_ball(void) {
  264. //tocco bordi
  265. ball_position.y += ball_direction.y;
  266. ball_position.x += ball_direction.x;
  267.  
  268. //assegnazione punti
  269. if (ball_position.x == w) {
  270. pad1_score++;
  271. ball_position.x = w / 2;
  272. ball_position.y = h / 2;
  273. }
  274.  
  275. if (ball_position.x == 0) {
  276. pad2_score++;
  277. ball_position.x = w / 2;
  278. ball_position.y = h / 2;
  279. }
  280.  
  281. if (ball_position.y == h || ball_position.y == 0) ball_direction.y = -ball_direction.y;
  282. //pieno 1
  283. if (ball_position.x == pad1.x + 1 && (ball_position.y >= pad1.y && ball_position.y <= pad1.y - 1 + len)) {
  284. ball_direction.x = 1;
  285. }
  286.  
  287. //pieno 2
  288. if (ball_position.x == pad2.x - 2 && (ball_position.y >= pad2.y && ball_position.y <= pad2.y - 1 + len)) {
  289. ball_direction.x = -1;
  290. }
  291.  
  292. //tocco bordo sup pad1
  293. if ((ball_position.y == pad1.y - 1) && (ball_position.x == pad1.x || ball_position.x == pad1.x + 1 || ball_position.x == pad1.x + 1 || ball_position.x == pad1.x - 1)) {
  294. if (ball_position.y != 0) {
  295. ball_direction.y = -1;
  296. }
  297. ball_direction.x = 1;
  298. }
  299.  
  300. //tocco bordo sup pad2
  301. if ((ball_position.y == pad2.y - 1) && (ball_position.x == pad2.x || ball_position.x == pad2.x - 2 || ball_position.x == pad2.x + 1 || ball_position.x == pad2.x - 1)) {
  302. if (ball_position.y != 0) {
  303. ball_direction.y = -1;
  304. }
  305. ball_direction.x = -1;
  306. }
  307.  
  308. //tocco inf pad1
  309. if (ball_position.y == pad1.y + len && (ball_position.x == pad1.x || ball_position.x == pad1.x + 1 || ball_position.x == pad1.x - 1)) {
  310. if (ball_position.y != h) {
  311. ball_direction.y = 1;
  312. }
  313. ball_direction.x = 1;
  314. }
  315.  
  316. //tocco inf pad2
  317. if (ball_position.y == pad2.y + len && (ball_position.x == pad2.x || ball_position.x == pad2.x - 2 || ball_position.x == pad2.x + 1 || ball_position.x == pad2.x - 1)) {
  318. if (ball_position.y != h) {
  319. ball_direction.y = 1;
  320. }
  321. ball_direction.x = -1;
  322. }
  323. }
  324. /* Returns ball current position */
  325. struct position get_ball_pos(void) { return ball_position; }
  326.  
  327. /* Returns pad1 current position */
  328. struct position get_pad1_pos(void) { return pad1; }
  329.  
  330. /* Returns pad2 current position */
  331. struct position get_pad2_pos(void) { return pad2; }
  332.  
  333. /* Returns the pad length */
  334. unsigned int get_pad_len(void) { return len; }
  335.  
  336. /* Returns pad1 current score */
  337. unsigned int get_pad1_score(void) { return pad1_score; }
  338.  
  339. /* Returns pad2 current score */
  340. unsigned int get_pad2_score(void) { return pad2_score; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement