Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. // File POLLGAMES.C in c:\Ptikva14\Polling 27-4-2014
  2. // A witty polling swap gamer ( state machine)
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include <windows.h>
  6. #include <stdio.h>
  7.  
  8. #define UP 72
  9. #define DOWN 80
  10. #define LEFT 75
  11. #define RIGHT 77
  12.  
  13. void showMenu (void);
  14. int game1 (void);
  15. int game2 (void);
  16. int game3 (void);
  17. int selectGame (int choice);
  18.  
  19.  
  20. void main (void)
  21. {
  22. char key='1';
  23.  
  24. showMenu();
  25. while ( key!='4')
  26. {
  27. key= selectGame(key);
  28. }
  29. }
  30.  
  31. void showMenu (void)
  32. {
  33. clrscr();
  34. _setcursortype(_NOCURSOR);
  35. gotoxy(55,12);
  36. textcolor(WHITE);
  37. cprintf("MENU");
  38. textcolor(WHITE);
  39. gotoxy(55,13);
  40. cprintf("1.GAME1");
  41. gotoxy(55,14);
  42. cprintf("2.GAME2");
  43. gotoxy(55,15);
  44. cprintf("3.GAME3");
  45. gotoxy(55,16);
  46. cprintf("4.EXIT");
  47. }
  48.  
  49. int game1 (void)
  50. {
  51. char key;
  52. int x =58;
  53. int y =35;
  54. int ballx=50;
  55. int bally= 15;
  56. int dirx=1;
  57. int diry=1;
  58. int i;
  59. int counter =0;
  60. while(1)
  61. {
  62. gotoxy(55,12);
  63. textcolor(WHITE);
  64. cprintf("MENU");
  65. textcolor(WHITE);
  66. gotoxy(55,13);
  67. cprintf("1.GAME1");
  68. gotoxy(55,14);
  69. cprintf("2.GAME2");
  70. gotoxy(55,15);
  71. cprintf("3.GAME3");
  72. gotoxy(55,16);
  73. cprintf("4.EXIT");
  74. gotoxy(100,10);
  75. Sleep(50);
  76. textcolor(random(15)+1);
  77. for (i=0; i< 14; i++)
  78. {
  79. gotoxy(x+i,y);
  80. cprintf("-");
  81. }
  82.  
  83.  
  84. if(ballx ==1)
  85. dirx=1;
  86. if(ballx==118)
  87. dirx=-1;
  88. if(bally==7)
  89. diry=1;
  90. if(bally==35)
  91. diry=-1;
  92. gotoxy(ballx,bally);
  93. cprintf(" ");
  94. ballx = ballx+ dirx;
  95. bally = bally +diry;
  96. gotoxy(ballx,bally);
  97. cprintf("*");
  98. if(bally==35 &&(ballx-14>x||ballx<x))
  99. {
  100. Sleep(3000);
  101. counter++;
  102. }
  103.  
  104. if( kbhit())
  105. {
  106. key=getch();
  107. for (i=0; i<3; i++)
  108. {
  109.  
  110. if(GetAsyncKeyState(VK_RIGHT)<0)
  111. {
  112. if (x+14!=120)
  113. {
  114. gotoxy(x,y);
  115. cprintf(" ");
  116. x++;
  117. }
  118. }
  119. if(GetAsyncKeyState(VK_LEFT)<0)
  120. {
  121. if (x!=0)
  122. {
  123. gotoxy(x+13,y);
  124. cprintf(" ");
  125. x--;
  126. }
  127. }
  128. if( key>='1' && key<='4')
  129. {
  130. clrscr();
  131. showMenu();
  132. return(key);
  133.  
  134. }
  135. }
  136. }
  137.  
  138. }
  139. }
  140.  
  141. int game2 (void)
  142. {
  143. char key;
  144. int x =58;
  145. int y =35;
  146. int ballx=50;
  147. int bally= 15;
  148. int dirx=1;
  149. int diry=1;
  150. int x1=58;
  151. int y1= 7;
  152. int i;
  153. int counter =0;
  154. while(1)
  155. {
  156. gotoxy(55,12);
  157. textcolor(WHITE);
  158. cprintf("MENU");
  159. textcolor(WHITE);
  160. gotoxy(55,13);
  161. cprintf("1.GAME1");
  162. gotoxy(55,14);
  163. cprintf("2.GAME2");
  164. gotoxy(55,15);
  165. cprintf("3.GAME3");
  166. gotoxy(55,16);
  167. cprintf("4.EXIT");
  168. gotoxy(100,10);
  169. Sleep(50);
  170. textcolor(WHITE);
  171. for (i=0; i< 14; i++)
  172. {
  173. gotoxy(x+i,y);
  174. cprintf("-");
  175. }
  176. for (i=0; i<14; i++)
  177. {
  178. gotoxy(x1+i,y1);
  179. cprintf("-");
  180. }
  181.  
  182.  
  183. if(ballx ==1)
  184. dirx=1;
  185. if(ballx==118)
  186. dirx*=-1;
  187. if(bally==7)
  188. {
  189. diry=1;
  190. dirx= rand() % (2+ 1 - 1) + 1 ;
  191. }
  192. if(bally==35)
  193. {
  194. diry=-1;
  195. dirx = rand() % (2+1-1)+1;
  196. }
  197. gotoxy(ballx,bally);
  198. cprintf(" ");
  199. ballx = ballx+ dirx;
  200. bally = bally +diry;
  201. gotoxy(ballx,bally);
  202. cprintf("*");
  203. if(bally==35 &&(ballx-14>x||ballx<x))
  204. {
  205. Sleep(3000);
  206. counter++;
  207. }
  208. if(bally==7 &&(ballx-14>x1||ballx<x1))
  209. {
  210. Sleep(3000);
  211. counter++;
  212. }
  213.  
  214. // if( kbhit())
  215. //{
  216. //key=getch();
  217. for (i=0; i<3; i++)
  218. {
  219. //if(GetKeyState(RIGHT) & 0x8000/*check if high-order bit is set (1 << 15)*/)
  220. //if(key ==RIGHT)
  221. if(GetAsyncKeyState(VK_RIGHT)<0)
  222. {
  223. if (x+14!=120)
  224. {
  225. gotoxy(x,y);
  226. cprintf(" ");
  227. x++;
  228. }
  229. }
  230. if(GetAsyncKeyState(VK_LEFT)<0)
  231. {
  232. if (x!=0)
  233. {
  234. gotoxy(x+13,y);
  235. cprintf(" ");
  236. x--;
  237. }
  238. }
  239. //if (key=='q')
  240. //if(GetKeyState('e') & 0x8000/*check if high-order bit is set (1 << 15)*/)
  241. if (GetKeyState(0x51) <0)
  242. {
  243. if(x1!=0)
  244. {
  245. gotoxy(x1+13,y1);
  246. cprintf(" ");
  247. x1--;
  248. }
  249. }
  250.  
  251.  
  252. if (GetKeyState(0x45) <0)
  253. // if (key=='e')
  254. {
  255. if(x1+14!=120)
  256. {
  257. gotoxy(x1,y1);
  258. cprintf(" ");
  259. x1++;
  260. }
  261. }
  262. if( GetKeyState(0x31)<0)
  263. {
  264. clrscr();
  265. showMenu();
  266. return(0x31);
  267.  
  268. }
  269. if( GetKeyState(0x32)<0)
  270. {
  271. clrscr();
  272. showMenu();
  273. return(0x32);
  274. }
  275. if( GetKeyState(0x33)<0)
  276. {
  277. clrscr();
  278. showMenu();
  279. return(0x33);
  280. }
  281. if( GetKeyState(0x34)<0)
  282. {
  283. clrscr();
  284. showMenu();
  285. return(0x34);
  286. }
  287. }
  288. // }
  289.  
  290. }
  291. }
  292.  
  293. int game3 (void)
  294. {
  295. char key;
  296.  
  297. while(1)
  298. {
  299. gotoxy(18,20);
  300. textcolor(random(15)+1);
  301. cprintf("GAME3");
  302. Sleep(100);
  303. if( kbhit())
  304. {
  305. key=getch();
  306. if( key>='1' && key<='4')
  307. {
  308. clrscr();
  309. showMenu();
  310. return(key);
  311. }
  312. }
  313. }
  314. }
  315.  
  316. int selectGame(int choice)
  317. {
  318. switch(choice)
  319. {
  320. case '1':
  321. choice=game1();
  322. break;
  323. case '2':
  324. choice=game2();
  325. break;
  326. case '3':
  327. choice=game3();
  328. break;
  329. default:
  330. break;
  331. }
  332. return(choice);
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement