Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*小時候我玩過一種簡單的小遊戲,忘記名字了,感覺是叫做"乒乓球"?
- 一顆球會一直彈到牆壁,並且撞擊空間內的磚塊使它被敲破,我們要操控一個可移動的擋板擋住球,
- 讓球撞擊越多磚塊越好,撞一次得一分,並且會隨著分數越多,球會愈來越快*/
- #include <conio.h>
- #include <iostream>
- #include <time.h>
- #include <windows.h>
- using namespace std;
- int map[600]={0};
- int board_idx; //板子最左方那一格的座標
- int ball; //球所在的座標
- int vx = 30;
- int vy = 1;
- int score = 0;
- int SleepTime = 150;
- void starting(){
- cout<<"\n提醒 : 因為本遊戲的物體移動採用重繪方式呈現,故常會有小lag發生,請不要卯起來按a和d\n";
- cout<<"如果已經讀完注意事項,請按下z開始遊戲,並確保輸入法保持在英文,caps lock不要打開";
- while(1){
- if(kbhit()){
- char k = _getch();
- if (k == 'z'){
- cout<<endl <<endl;
- cout<<"The game is about to start in 3 secs\n";
- Sleep(1000);
- cout << "3\n";
- Sleep(1000);
- cout << "2\n";
- Sleep(1000);
- cout << "1\n";
- Sleep(1000);
- cout << "Start!\n";
- Sleep(1000);
- break;
- }
- else{}
- }
- }
- }
- //■□●
- void draw_map(){
- system("cls");
- cout << "Score : " << score <<endl;
- for(int i = 1 ; i <= 600 ; i++){
- if(map[i] == 0){
- cout << " ";
- }
- else if(map[i] == 1 || map[i] == 4){
- cout << "■";
- }
- else if(map[i] == 2){
- cout << "●";
- }
- else if(map[i] == 3){
- cout << "□";
- }
- if(i % 30 == 0){
- cout << endl;
- }
- }
- }
- // 1 for wall, 2 for ball, 3 for the board, 4 for the blocks
- void init_map(){
- for(int i = 1 ; i <= 600 ; i++){
- if(i<=30||i%30 ==1 ||i%30 ==0){
- map[i]=1;
- }
- }
- for(int i = 32 ; i <= 150 ; i++){
- if(map[i]!=1){
- map[i]=4;
- }
- }
- ball = 160;
- map[ball]=2; //球的起始點
- for(int i = 573 ; i<=577 ; i++){
- map[i]=3;
- }
- board_idx = 573;
- }
- void turn_wall(){
- //彈到左上方牆壁
- if(map[ball-30]==1 && map[ball-1]==1){
- vx = -vx; //-31 -> +31
- vy = -vy;
- }
- //彈到右上方牆壁
- else if(map[ball-30]==1 && map[ball+1] ==1){
- vx = -vx ; //-29 -> +29
- vy = -vy;
- score += 1;
- }
- //彈到上方牆壁
- else if(map[ball-30] ==1){
- vx = -vx; //-31 -> +29 -29 -> +31
- }
- //彈到左方牆壁
- else if(map[ball-1]==1){
- vy = -vy; //-31 -> -29 +29 -> +31
- }
- //彈到右方牆壁
- else if(map[ball+1]==1){
- vy = -vy; //+31 -> +29 -29 -> -31
- }
- //彈到板子上
- else if(map[ball+30] ==3){
- vx = -vx; //+31 -> -29 +29 -> -31
- }
- }
- void turn_block(){
- /*//敲到左上磚塊的尖端,消去
- if(map[ball-30]==0 && map[ball-1]==0 && map[ball-31]==4){
- map[ball-31]==0;
- vx = -vx; //-31 -> +31
- vy = -vy;
- score += 1;
- }
- //敲到右上磚塊的尖端,消去
- else if(map[ball-30]==0 && map[ball+1]==0 &&map[ball-29]==4){
- map[ball-29]=0;
- vx = -vx ; //-29 -> +29
- vy = -vy;
- score += 1;
- }*/
- //彈到左上方磚塊,消去左方磚塊
- if(map[ball-30]==4 && map[ball-1]==4){
- map[ball-1]==0;
- vx = -vx; //-31 -> +31
- vy = -vy;
- score += 1;
- }
- //彈到右上方磚塊,消去左方磚塊
- else if(map[ball-30]==4 && map[ball+1] ==4){
- map[ball+1]=0;
- vx = -vx ; //-29 -> +29
- vy = -vy;
- score += 1;
- }
- //彈到上方磚塊,消去
- else if(map[ball-30] ==4){
- map[ball-30]=0;
- vx = -vx; //-31 -> +29 -29 -> +31
- score += 1;
- }
- //彈到左方磚塊,消去
- else if(map[ball-1]==4){
- map[ball-1]==0;
- vy = -vy; //-31 -> -29 +29 -> +31
- score += 1;
- }
- //彈到右方磚塊,消去
- else if(map[ball+1]==4){
- map[ball+1] =0;
- vy = -vy; //+31 -> +29 -29 -> -31
- score += 1;
- }
- }
- void move_ball(){
- map[ball]=0;
- map[ball+vx+vy]=2;
- ball = ball+vx+vy;
- turn_wall();
- turn_block();
- }
- int main(){
- starting();
- init_map();
- draw_map();
- while(1){
- Sleep(SleepTime);
- move_ball();
- if(kbhit()){
- char k =_getch();
- if(k =='a'){
- if(board_idx !=572){
- if(board_idx == 573){
- board_idx -=1;
- for(int i = 0 ; i<5 ; i++){
- map[board_idx+i]=3;
- }
- map[board_idx+5] =0;
- }
- else{
- board_idx -=2;
- for(int i = 0 ; i<5 ; i++){
- map[board_idx+i]=3;
- }
- map[board_idx+5]=0;
- map[board_idx+6]=0;
- }
- }
- }
- else if(k =='d'){
- if(board_idx !=595){
- if(board_idx ==594){
- board_idx += 1;
- for(int i = 0 ; i<5 ; i++){
- map[board_idx+i]=3;
- }
- map[board_idx-1]=0;
- }
- else{
- board_idx +=2;
- for(int i = 0 ; i<5 ; i++){
- map[board_idx+i]=3;
- }
- map[board_idx-1]=0;
- map[board_idx-2]=0;
- }
- }
- }
- else if(k =='q'){
- break;
- }
- }
- if(score >= 10){
- SleepTime = 50;
- }
- else if(score >=5){
- SleepTime = 100;
- }
- if(ball > 570){
- draw_map();
- cout << "Game over";
- break;
- }
- draw_map();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment