Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <stdlib.h>
- #include <conio.h>
- #include <windows.h>
- #include <thread>//-std=c++11
- #define play_second 60 //自訂遊玩時間
- using namespace std;
- char game[5][6] = {" "," "," "," "," "};
- int point = 0;
- int time1 = play_second;
- bool stop = 0;//停止countdown中之無限迴圈
- int countdown()//計時
- {
- while(true)
- {
- if(stop)
- break;
- if(time1 == 0)
- continue;
- Sleep(1000);
- time1--;
- }
- }
- int main()
- {
- thread T1(countdown);
- char key;
- int a;
- srand(time(NULL));
- for(int i = 1;i<5;i++)//設定初始版面
- game[i][(rand()%3)*2] = 'O';
- while(1)
- {
- switch(point)//依分數變換顏色
- {
- case 0:
- {
- system("color 0d");
- break;
- }
- case 100:
- {
- system("color 0c");
- break;
- }
- case 200:
- {
- system("color 0a");
- break;
- }
- case 300:
- {
- system("color 0e");
- break;
- }
- }
- a = rand()%3;
- if(a == 0)
- game[0][0] = 'O';
- else if(a == 1)
- game[0][2] = 'O';
- else
- game[0][4] = 'O';
- for(int i = 0;i<5;i++)
- {
- for(int j = 0;j<5;j++)
- {
- cout<<game[i][j];
- }
- cout<<endl;
- }
- cout<<"---------"<<endl;
- cout<<"point:"<<point<<endl;
- cout<<"time:"<<time1<<endl;
- while(1)
- {
- if(time1 == 0)
- {
- system("cls");
- cout<<"遊戲結束! 得分:"<<point<<endl;
- cout<<"輸入y繼續/n結束"<<endl;
- while(1)
- {
- char c = getch();
- if(c == 'n')
- {
- stop = 1;
- T1.join();
- return 0;
- }
- else if(c == 'y')
- {
- time1 = play_second;
- point = 0;
- break;
- }
- }
- break;
- }
- key = getch();
- if(key == 'a')
- {
- if(game[4][0] == 'O')//打擊正確
- {
- point++;
- break;
- }
- else//打錯顏色變為白色,暫停一秒
- {
- system("color 0f");
- Sleep(1000);
- while(kbhit()) getch();//清除鍵盤緩衝區
- if(point <=100)
- system("color 0d");
- else if(point>100 && point<=200)
- system("color 0c");
- else if(point>200 && point<=300)
- system("color 0a");
- else
- system("color 0e");
- }
- }
- else if(key == 's')
- {
- if(game[4][2] == 'O')
- {
- point++;
- break;
- }
- else
- {
- system("color 0f");
- Sleep(1000);
- while(kbhit()) getch();
- if(point <=100)
- system("color 0d");
- else if(point>100 && point<=200)
- system("color 0c");
- else if(point>200 && point<=300)
- system("color 0a");
- else
- system("color 0e");
- }
- }
- else if(key == 'd')
- {
- if(game[4][4] == 'O')
- {
- point++;
- break;
- }
- else
- {
- system("color 0f");
- Sleep(1000);
- while(kbhit()) getch();
- if(point <=100)
- system("color 0d");
- else if(point>100 && point<=200)
- system("color 0c");
- else if(point>200 && point<=300)
- system("color 0a");
- else
- system("color 0e");
- }
- }
- }
- for(int i = 3;i>=0;i--)
- for(int j = 0;j<5;j++)
- game[i+1][j] = game[i][j];
- for(int i = 0;i<5;i++)
- game[0][i] = ' ';
- system("cls");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement