Advertisement
huutho_96

trả code

Apr 2nd, 2015
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. #include<iostream>
  2. #include<windows.h>
  3. #include<conio.h>
  4. #include<ctime>
  5. #include<cstdlib>
  6. #include<thread>
  7. #include <process.h>
  8. #include<string>
  9.  
  10.  
  11. using namespace std;
  12.  
  13. int bouble1, bouble2, state = 0, trave = 0, dkdung = 0;
  14. int buttonA;
  15. int buttonB;
  16. int buttonC, bienCorrect;
  17. int score = 0, KtraThread = 0;
  18. void InScore(int Sc);
  19. void ChonKey(int &buttonA, int &buttonB, int &buttonC, int &bienCorrect, int &score);
  20. //int inBouble(int &bouble1,int &bouble2);
  21. void resizeConsole(int width, int height)
  22. {
  23. HWND console = GetConsoleWindow();
  24. RECT r;
  25. GetWindowRect(console, &r);
  26. MoveWindow(console, r.left, r.top, width, height, TRUE);
  27. }
  28. void textcolor(int x)
  29. {
  30. HANDLE mau;
  31. mau = GetStdHandle(STD_OUTPUT_HANDLE);
  32. SetConsoleTextAttribute(mau, x);
  33. }
  34.  
  35. void Nocursortype()//an dau nhay
  36. {
  37. CONSOLE_CURSOR_INFO Info;
  38. Info.bVisible = FALSE;
  39. Info.dwSize = 20;
  40. SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &Info);
  41. }
  42. void gotoxy(int x, int y)
  43. {
  44. HANDLE hConsoleOutput;
  45. COORD Cursor_an_Pos = { x - 1, y - 1 };
  46. hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  47. SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
  48. }
  49.  
  50.  
  51. void XoaManHinh()
  52. {
  53. HANDLE hOut;
  54. COORD Position;
  55. hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  56. Position.X = 0;
  57. Position.Y = 0;
  58. SetConsoleCursorPosition(hOut, Position);
  59. }
  60. void veKhung()
  61. {
  62. resizeConsole(800, 600);
  63. /*for (int i =2; i < 35; i++)
  64. {
  65. gotoxy(40,i);
  66. cout<<"|\n";
  67. }*/
  68.  
  69. for (int i = 1; i<35; i++)
  70. {
  71. textcolor(34);
  72. cout << " \n";
  73. }
  74. textcolor(12);
  75. }
  76. void InScore(int Sc)
  77. {
  78. gotoxy(25, 1);
  79. textcolor(39);
  80. cout << "Score:" << Sc;
  81. textcolor(12);
  82. gotoxy(28, 4);
  83. }
  84.  
  85.  
  86. int HamBouble()
  87. {
  88. srand(time(0));
  89. bouble1 = rand() % (51);
  90. bouble2 = rand() % (51);
  91. trave = bouble1 + bouble2;
  92. //Sleep(50);
  93. gotoxy(10, 10);
  94. textcolor(129);
  95. cout << " " << bouble1 << " ";
  96. textcolor(34);
  97. cout << "\t\t";
  98. textcolor(129);
  99. cout << " " << bouble2 << " ";
  100. return 0;
  101. }
  102. /*int inBouble(int &bouble1,int &bouble2)
  103. {
  104. gotoxy(10,10);
  105. textcolor(129);
  106. cout<<" "<<bouble1<<" ";
  107. textcolor(34);
  108. cout<<"\t\t";
  109. textcolor(129);
  110. cout<<" "<<bouble2<<" ";
  111. return 0;
  112. }*/
  113. void TaoButton(int &score)
  114. {
  115. /*int buttonA;
  116. int buttonB;
  117. int buttonC; */
  118. bienCorrect = trave;
  119. srand(time(0));
  120. int buttonKey;
  121. buttonKey = (1 + rand() % (3));
  122. switch (buttonKey){
  123.  
  124. case 1:
  125. buttonA = bienCorrect;
  126. buttonC = (rand() % (100));
  127. buttonB = (rand() % (100));
  128. break;
  129. case 2:
  130.  
  131. buttonC = bienCorrect;
  132. buttonA = (rand() % (100));
  133. buttonB = (rand() % (100));
  134. break;
  135. case 3:
  136. buttonB = bienCorrect;
  137. buttonC = (rand() % (100));
  138. buttonA = (rand() % (100));
  139. break;
  140. default:
  141. break;
  142. }
  143. gotoxy(5, 25);
  144. textcolor(23);
  145. cout << " A." << buttonA << " ";
  146. cout << " B." << buttonB << " ";
  147. cout << " C." << buttonC << " ";
  148. textcolor(32);
  149. }
  150. int HamTime()
  151. {
  152. gotoxy(23, 4);
  153. textcolor(218);
  154. cout << "Time:";
  155. textcolor(200);
  156. int i = 0;
  157.  
  158. while (i<10)
  159. {
  160. cout << " ";
  161. i++;
  162. Sleep(1000);
  163. if (GetAsyncKeyState(0x41) || GetAsyncKeyState(0x42) || GetAsyncKeyState(0x43))
  164. {
  165. gotoxy(28, 4);
  166. textcolor(34);
  167. cout << " ";
  168. dkdung = 1;
  169. //Sleep(100);
  170. break;
  171.  
  172. }
  173. if (i == 8)
  174. {
  175. state = 1;
  176.  
  177. }
  178. }
  179.  
  180. return 0;
  181.  
  182. }
  183. void ChonKey(int &buttonA, int &buttonB, int &buttonC, int &bienCorrect, int &score)
  184. {
  185. //Chon Key
  186.  
  187. //gotoxy(12,22);
  188. //cout<<"\nBan Chon : ";
  189. textcolor(14);
  190. gotoxy(50, 12);
  191. //vì hk hiêu cở chế game đưa vô nên cout nó ra để nhìn thôi
  192. cout << buttonA << "\t" << buttonB << "\t" << buttonC;
  193. int key, kt = true;
  194. int c;
  195. int ketqua;
  196. for (; kt == true;)
  197. {
  198. //đợi người dùng ấn phím
  199. while (!_kbhit());
  200. //c đc gán lại bằng phím vừa ấn
  201. c = getch();
  202. //và lấy giá trị trong các button đưa vào c để kiểm tra
  203. //bạn thấy mình chỉ cần 1 xét để đưa ra kết quả
  204. //test
  205. //ổn định. oke??/
  206. if (c == 'c') c = buttonC;
  207. if (c == 'b') c = buttonB;
  208. if (c == 'a') c = buttonA;
  209. if (c == bienCorrect)
  210. {
  211. score++;
  212. InScore(score);
  213. }
  214. else
  215. {
  216. gotoxy(12, 12);
  217. state = 1;
  218. cout << "GAME OVER_b!";
  219. }
  220. break;
  221. }
  222.  
  223. }
  224.  
  225.  
  226. int main()
  227. {
  228.  
  229. //SetConsoleTitle("Game Don Gian");
  230. int diembd = 0, dkdung2 = 0;;
  231. veKhung();
  232. Nocursortype();
  233.  
  234. InScore(diembd);
  235. thread t[30];
  236. int i = 0;
  237. while (true)
  238. {
  239. HamBouble();
  240. TaoButton(score);
  241. Sleep(100);
  242.  
  243. //t[i+1]=thread (HamTime);
  244. HamTime();
  245. //ChonKey( buttonA, buttonB, buttonC, bienCorrect, score);
  246. if (state == 1) //Sau moi lan luot no se kiem tra state neu state =1 thi out
  247. {
  248. gotoxy(45, 4);
  249. textcolor(14);
  250. cout << "Diem cua ban :" << score << "\n";
  251. break;
  252. }
  253. else
  254. {
  255. ;
  256. }
  257. /*if(dkdung==1){
  258. t[i].join();
  259. dkdung2=1;
  260. }*/
  261. t[i] = thread(ChonKey, buttonA, buttonB, buttonC, bienCorrect, score);
  262. //if(dkdung2==0)
  263. t[i].join();
  264. //t[i+1].join();
  265. //Phai co ham sleep de ngung vong lap trong 0.1s
  266.  
  267. i++;
  268. if (i == 30)
  269. i = 0;
  270. }
  271.  
  272.  
  273. system("pause");
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement