Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<windows.h>
- #include<conio.h>
- #include<ctime>
- #include<cstdlib>
- #include<thread>
- #include <process.h>
- #include<string>
- using namespace std;
- int bouble1, bouble2, state = 0, trave = 0, dkdung = 0;
- int buttonA;
- int buttonB;
- int buttonC, bienCorrect;
- int score = 0, KtraThread = 0;
- void InScore(int Sc);
- void ChonKey(int &buttonA, int &buttonB, int &buttonC, int &bienCorrect, int &score);
- //int inBouble(int &bouble1,int &bouble2);
- void resizeConsole(int width, int height)
- {
- HWND console = GetConsoleWindow();
- RECT r;
- GetWindowRect(console, &r);
- MoveWindow(console, r.left, r.top, width, height, TRUE);
- }
- void textcolor(int x)
- {
- HANDLE mau;
- mau = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(mau, x);
- }
- void Nocursortype()//an dau nhay
- {
- CONSOLE_CURSOR_INFO Info;
- Info.bVisible = FALSE;
- Info.dwSize = 20;
- SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &Info);
- }
- void gotoxy(int x, int y)
- {
- HANDLE hConsoleOutput;
- COORD Cursor_an_Pos = { x - 1, y - 1 };
- hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
- }
- void XoaManHinh()
- {
- HANDLE hOut;
- COORD Position;
- hOut = GetStdHandle(STD_OUTPUT_HANDLE);
- Position.X = 0;
- Position.Y = 0;
- SetConsoleCursorPosition(hOut, Position);
- }
- void veKhung()
- {
- resizeConsole(800, 600);
- /*for (int i =2; i < 35; i++)
- {
- gotoxy(40,i);
- cout<<"|\n";
- }*/
- for (int i = 1; i<35; i++)
- {
- textcolor(34);
- cout << " \n";
- }
- textcolor(12);
- }
- void InScore(int Sc)
- {
- gotoxy(25, 1);
- textcolor(39);
- cout << "Score:" << Sc;
- textcolor(12);
- gotoxy(28, 4);
- }
- int HamBouble()
- {
- srand(time(0));
- bouble1 = rand() % (51);
- bouble2 = rand() % (51);
- trave = bouble1 + bouble2;
- //Sleep(50);
- gotoxy(10, 10);
- textcolor(129);
- cout << " " << bouble1 << " ";
- textcolor(34);
- cout << "\t\t";
- textcolor(129);
- cout << " " << bouble2 << " ";
- return 0;
- }
- /*int inBouble(int &bouble1,int &bouble2)
- {
- gotoxy(10,10);
- textcolor(129);
- cout<<" "<<bouble1<<" ";
- textcolor(34);
- cout<<"\t\t";
- textcolor(129);
- cout<<" "<<bouble2<<" ";
- return 0;
- }*/
- void TaoButton(int &score)
- {
- /*int buttonA;
- int buttonB;
- int buttonC; */
- bienCorrect = trave;
- srand(time(0));
- int buttonKey;
- buttonKey = (1 + rand() % (3));
- switch (buttonKey){
- case 1:
- buttonA = bienCorrect;
- buttonC = (rand() % (100));
- buttonB = (rand() % (100));
- break;
- case 2:
- buttonC = bienCorrect;
- buttonA = (rand() % (100));
- buttonB = (rand() % (100));
- break;
- case 3:
- buttonB = bienCorrect;
- buttonC = (rand() % (100));
- buttonA = (rand() % (100));
- break;
- default:
- break;
- }
- gotoxy(5, 25);
- textcolor(23);
- cout << " A." << buttonA << " ";
- cout << " B." << buttonB << " ";
- cout << " C." << buttonC << " ";
- textcolor(32);
- }
- int HamTime()
- {
- gotoxy(23, 4);
- textcolor(218);
- cout << "Time:";
- textcolor(200);
- int i = 0;
- while (i<10)
- {
- cout << " ";
- i++;
- Sleep(1000);
- if (GetAsyncKeyState(0x41) || GetAsyncKeyState(0x42) || GetAsyncKeyState(0x43))
- {
- gotoxy(28, 4);
- textcolor(34);
- cout << " ";
- dkdung = 1;
- //Sleep(100);
- break;
- }
- if (i == 8)
- {
- state = 1;
- }
- }
- return 0;
- }
- void ChonKey(int &buttonA, int &buttonB, int &buttonC, int &bienCorrect, int &score)
- {
- //Chon Key
- //gotoxy(12,22);
- //cout<<"\nBan Chon : ";
- textcolor(14);
- gotoxy(50, 12);
- //vì hk hiêu cở chế game đưa vô nên cout nó ra để nhìn thôi
- cout << buttonA << "\t" << buttonB << "\t" << buttonC;
- int key, kt = true;
- int c;
- int ketqua;
- for (; kt == true;)
- {
- //đợi người dùng ấn phím
- while (!_kbhit());
- //c đc gán lại bằng phím vừa ấn
- c = getch();
- //và lấy giá trị trong các button đưa vào c để kiểm tra
- //bạn thấy mình chỉ cần 1 xét để đưa ra kết quả
- //test
- //ổn định. oke??/
- if (c == 'c') c = buttonC;
- if (c == 'b') c = buttonB;
- if (c == 'a') c = buttonA;
- if (c == bienCorrect)
- {
- score++;
- InScore(score);
- }
- else
- {
- gotoxy(12, 12);
- state = 1;
- cout << "GAME OVER_b!";
- }
- break;
- }
- }
- int main()
- {
- //SetConsoleTitle("Game Don Gian");
- int diembd = 0, dkdung2 = 0;;
- veKhung();
- Nocursortype();
- InScore(diembd);
- thread t[30];
- int i = 0;
- while (true)
- {
- HamBouble();
- TaoButton(score);
- Sleep(100);
- //t[i+1]=thread (HamTime);
- HamTime();
- //ChonKey( buttonA, buttonB, buttonC, bienCorrect, score);
- if (state == 1) //Sau moi lan luot no se kiem tra state neu state =1 thi out
- {
- gotoxy(45, 4);
- textcolor(14);
- cout << "Diem cua ban :" << score << "\n";
- break;
- }
- else
- {
- ;
- }
- /*if(dkdung==1){
- t[i].join();
- dkdung2=1;
- }*/
- t[i] = thread(ChonKey, buttonA, buttonB, buttonC, bienCorrect, score);
- //if(dkdung2==0)
- t[i].join();
- //t[i+1].join();
- //Phai co ham sleep de ngung vong lap trong 0.1s
- i++;
- if (i == 30)
- i = 0;
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement