左右键控制小球左右移动,Esc退出。
1 #include <bits/stdc++.h> 2 #include <windows.h> 3 #include <conio.h> 4 5 using namespace std; 6 7 char mmp[10000100][4]; 8 9 int main() { 10 11 system("color 5f"); 12 system("mode con cols=14 lines=14"); 13 14 srand(time(NULL)); 15 16 char ch; 17 int delay = 20; 18 19 for (register int i = 1; i <= 10000000; i++) { 20 21 int r = rand() % 3; 22 23 mmp[i][0] = mmp[i][1] = mmp[i][2] = ' '; 24 25 if (i > 20 && i % 2 == 0) 26 mmp[i][r] = '#'; 27 28 } 29 30 mmp[1][1] = 'O'; 31 32 int k = 0, x = 1, score = 0; 33 34 for (register int i = 1; i <= 10000000; i++) { 35 36 Sleep(delay); 37 38 if (i % 500 == 0) { 39 40 delay--; 41 42 } 43 44 if (i % 10 == 0) { 45 46 ++k; 47 48 if (mmp[k][x] == '#') { 49 50 system("cls"); 51 printf("score:%d", score); 52 Sleep(1000); 53 return 0; 54 55 } else { 56 57 mmp[k][x] = 'O'; 58 59 } 60 61 62 score++; 63 64 } 65 66 if (kbhit()) { 67 68 ch = getch(); 69 switch(ch) { 70 71 case -32 : 72 ch = getch(); 73 switch(ch) { 74 case 75 : 75 76 if (x > 0) { 77 78 if (mmp[k][x - 1] == '#') { 79 80 system("cls"); 81 printf("score:%d", score); 82 Sleep(1000); 83 return 0; 84 85 } else { 86 87 mmp[k][x] = ' '; 88 x--; 89 mmp[k][x] = 'O'; 90 91 } 92 93 } 94 95 break; 96 case 77 : 97 98 if (x < 2) { 99 100 if (mmp[k][x + 1] == '#') { 101 102 system("cls"); 103 printf("score:%d", score); 104 Sleep(1000); 105 return 0; 106 107 } else { 108 109 mmp[k][x] = ' '; 110 x++; 111 mmp[k][x] = 'O'; 112 113 } 114 115 } 116 117 break; 118 } 119 break; 120 case 27 : 121 122 exit(0); 123 124 break; 125 126 } 127 128 } 129 130 mmp[k][x] = 'O'; 131 system("cls"); 132 printf("+---+\n"); 133 for (register int j = k + 10; j >= k; j--) { 134 135 printf("|%s|\n", mmp[j]); 136 137 } 138 printf("+---+\n"); 139 140 } 141 142 return 0; 143 144 }