【长期维护】C++休闲(修仙)躲方块小游戏

左右键控制小球左右移动,上键加速,Esc退出。

一个‘@’20分

#include <windows.h>
#include <bits/stdc++.h>
#include <conio.h>

using namespace std;

char mmp[10000001][6], putmmp[16][8];

int main() {

    system("color 70");
    srand(time(NULL));

    for (register int i = 1; i <= 10000000; i++) {

        mmp[i][0] = mmp[i][1] = mmp[i][2] = mmp[i][3] = mmp[i][4] = ' ';

        if (i % 3 == 0 && i > 30) {

            mmp[i][rand() % 5] = '#';
            if (i % 20 == 0) {

                mmp[i][rand() % 5] = '@';

            }

        }

    }

    int j = 1, x = 2, score = 1, delay = 10;
    char ch;

    mmp[j][x] = 'O';

    for (register int i = 1; i <= 100000000; i++) {

        Sleep(delay);
        if (i == 500)//500帧后加速(10帧一动)
            delay = 8;
        if (i == 1000)//1000帧后加速(10帧一动)
            delay = 6;
        if (i == 1500)//1500帧后加速(10帧一动)
            delay = 4;

        if (i % 10 == 0) {

            j++;
            score++;
            if (mmp[j][x] == '#') {

                system("cls");
                printf("score:%d", score);
                Sleep(1000);
                system("cls");
                printf("again?(Y/N)");
                ch = 1;
                while(ch ^ 'Y' && ch ^ 'y') {

                    ch = getch();
                    if (ch == 'N' || ch == 'n') {

                        exit(0);

                    }

                }
                main();

            }
            if (mmp[j][x] == '@')
                score += 20;
            mmp[j][x] = 'O';
            system("cls");
            putmmp[0][0] = '+', putmmp[0][1] = putmmp[0][2] = putmmp[0][3] = putmmp[0][4] = putmmp[0][5] = '-', putmmp[0][6] = '+', putmmp[0][7] = '
';
            int p = 1;
            for (register int k = j + 15; k >= j; k--, p++) {

                putmmp[p][0] = '|';

                for (register int l = 0; l < 5; l++) {

                    putmmp[p][l + 1] = mmp[k][l];

                }

                putmmp[p][6] = '|', putmmp[p][7] = '
';

            }
            putmmp[15][x + 1] = 'O';
            fwrite(putmmp, 1, 18 * 7, stdout);
            puts("|
+-----+");

        }

        if (kbhit()) {

            ch = getch();

            switch(ch) {

                case 27 :

                    exit(0);

                    break;

                case -32:

                    ch = getch();

                    switch(ch) {

                        case 75 :

                            if (x > 0) {

                                if (mmp[j][x - 1] == '#') {

                                    system("cls");
                                    printf("score:%d", j);
                                    Sleep(1000);
                                    system("cls");
                                    printf("again?(Y/N)");
                                    ch = 1;
                                    while(ch ^ 'Y' && ch ^ 'y') {

                                        ch = getch();
                                        if (ch == 'N' || ch == 'n') {

                                            exit(0);

                                        }

                                    }
                                    main();

                                }
                                if (mmp[j][x - 1] == '@') {

                                    score += 20;

                                }
                                mmp[j][x] = ' ';
                                x--;
                                mmp[j][x] = 'O';
                            }

                            break;

                        case 77 :

                            if (x < 4) {

                                if (mmp[j][x + 1] == '#') {

                                    system("cls");
                                    printf("score:%d", j);
                                    Sleep(1000);
                                    system("cls");
                                    printf("again?(Y/N)");
                                    ch = 1;
                                    while(ch ^ 'Y' && ch ^ 'y') {

                                        ch = getch();
                                        if (ch == 'N' || ch == 'n') {

                                            exit(0);

                                        }

                                    }
                                    main();

                                }
                                if (mmp[j][x + 1] == '@') {

                                    score += 20;

                                }
                                mmp[j][x] = ' ';
                                x++;
                                mmp[j][x] = 'O';
                            }

                            break;

                        case 72:

                            j++;
                            score++;

                            if (mmp[j][x] == '#') {

                                system("cls");
                                printf("score:%d", j);
                                Sleep(1000);
                                system("cls");
                                printf("again?(Y/N)");
                                ch = 1;
                                while(ch ^ 'Y' && ch ^ 'y') {

                                    ch = getch();
                                    if (ch == 'N' || ch == 'n') {

                                        exit(0);

                                    }

                                }
                                main();

                            }

                            mmp[j][x] = 'O';
                            Sleep(5);
                            system("cls");
                            putmmp[0][0] = '+', putmmp[0][1] = putmmp[0][2] = putmmp[0][3] = putmmp[0][4] = putmmp[0][5] = '-', putmmp[0][6] = '+', putmmp[0][7] = '
';
                            int p = 1;
                            for (register int k = j + 15; k >= j; k--, p++) {

                                putmmp[p][0] = '|';

                                for (register int l = 0; l < 5; l++) {

                                    putmmp[p][l + 1] = mmp[k][l];

                                }

                                putmmp[p][6] = '|', putmmp[p][7] = '
';

                            }
                            putmmp[15][x + 1] = 'O';
                            fwrite(putmmp, 1, 18 * 7, stdout);
                            puts("|
+-----+");


                            break;

                    }

                    break;

            }

            mmp[j][x] = 'O';
            system("cls");
            putmmp[0][0] = '+', putmmp[0][1] = putmmp[0][2] = putmmp[0][3] = putmmp[0][4] = putmmp[0][5] = '-', putmmp[0][6] = '+', putmmp[0][7] = '
';
            int p = 1;
            for (register int k = j + 15; k >= j; k--, p++) {

                putmmp[p][0] = '|';

                for (register int l = 0; l < 5; l++) {

                    putmmp[p][l + 1] = mmp[k][l];

                }

                putmmp[p][6] = '|', putmmp[p][7] = '
';

            }
            putmmp[15][x + 1] = 'O';
            fwrite(putmmp, 1, 18 * 7, stdout);
            puts("|
+-----+");


        }

    }

    return 0;

}

更新于二〇一九年八月六日(星期二)

#include <windows.h>#include <bits/stdc++.h>#include <conio.h>
using namespace std;
char mmp[10000001][6], putmmp[16][8];
int main() {
system("color 70");srand(time(NULL));
for (register int i = 1; i <= 10000000; i++) {
mmp[i][0] = mmp[i][1] = mmp[i][2] = mmp[i][3] = mmp[i][4] = ' ';
if (i % 3 == 0 && i > 30) {
mmp[i][rand() % 5] = '#';if (i % 20 == 0) {
mmp[i][rand() % 5] = '@';
}
}
}
int j = 1, x = 2, score = 1, delay = 10;char ch;
mmp[j][x] = 'O';
for (register int i = 1; i <= 100000000; i++) {
Sleep(delay);if (i == 500)//500帧后加速(10帧一动)delay = 8;if (i == 1000)//1000帧后加速(10帧一动)delay = 6;if (i == 1500)//1500帧后加速(10帧一动)delay = 4;
if (i % 10 == 0) {
j++;score++;if (mmp[j][x] == '#') {
system("cls");printf("score:%d", score);Sleep(1000);system("cls");printf("again?(Y/N)");ch = 1;while(ch ^ 'Y' && ch ^ 'y') {
ch = getch();if (ch == 'N' || ch == 'n') {
exit(0);
}
}main();
}if (mmp[j][x] == '@')score += 20;mmp[j][x] = 'O';system("cls");putmmp[0][0] = '+', putmmp[0][1] = putmmp[0][2] = putmmp[0][3] = putmmp[0][4] = putmmp[0][5] = '-', putmmp[0][6] = '+', putmmp[0][7] = ' ';int p = 1;for (register int k = j + 15; k >= j; k--, p++) {
putmmp[p][0] = '|';
for (register int l = 0; l < 5; l++) {
putmmp[p][l + 1] = mmp[k][l];
}
putmmp[p][6] = '|', putmmp[p][7] = ' ';
}putmmp[15][x + 1] = 'O';fwrite(putmmp, 1, 18 * 7, stdout);puts("| +-----+");
}
if (kbhit()) {
ch = getch();
switch(ch) {
case 27 :
exit(0);
break;
case -32:
ch = getch();
switch(ch) {
case 75 :
if (x > 0) {
if (mmp[j][x - 1] == '#') {
system("cls");printf("score:%d", j);Sleep(1000);system("cls");printf("again?(Y/N)");ch = 1;while(ch ^ 'Y' && ch ^ 'y') {
ch = getch();if (ch == 'N' || ch == 'n') {
exit(0);
}
}main();
}if (mmp[j][x - 1] == '@') {
score += 20;
}mmp[j][x] = ' ';x--;mmp[j][x] = 'O';}
break;
case 77 :
if (x < 4) {
if (mmp[j][x + 1] == '#') {
system("cls");printf("score:%d", j);Sleep(1000);system("cls");printf("again?(Y/N)");ch = 1;while(ch ^ 'Y' && ch ^ 'y') {
ch = getch();if (ch == 'N' || ch == 'n') {
exit(0);
}
}main();
}if (mmp[j][x + 1] == '@') {
score += 20;
}mmp[j][x] = ' ';x++;mmp[j][x] = 'O';}
break;
case 72:
j++;score++;
if (mmp[j][x] == '#') {
system("cls");printf("score:%d", j);Sleep(1000);system("cls");printf("again?(Y/N)");ch = 1;while(ch ^ 'Y' && ch ^ 'y') {
ch = getch();if (ch == 'N' || ch == 'n') {
exit(0);
}
}main();
}
mmp[j][x] = 'O';Sleep(5);system("cls");putmmp[0][0] = '+', putmmp[0][1] = putmmp[0][2] = putmmp[0][3] = putmmp[0][4] = putmmp[0][5] = '-', putmmp[0][6] = '+', putmmp[0][7] = ' ';int p = 1;for (register int k = j + 15; k >= j; k--, p++) {
putmmp[p][0] = '|';
for (register int l = 0; l < 5; l++) {
putmmp[p][l + 1] = mmp[k][l];
}
putmmp[p][6] = '|', putmmp[p][7] = ' ';
}putmmp[15][x + 1] = 'O';fwrite(putmmp, 1, 18 * 7, stdout);puts("| +-----+");

break;
}
break;
}
mmp[j][x] = 'O';system("cls");putmmp[0][0] = '+', putmmp[0][1] = putmmp[0][2] = putmmp[0][3] = putmmp[0][4] = putmmp[0][5] = '-', putmmp[0][6] = '+', putmmp[0][7] = ' ';int p = 1;for (register int k = j + 15; k >= j; k--, p++) {
putmmp[p][0] = '|';
for (register int l = 0; l < 5; l++) {
putmmp[p][l + 1] = mmp[k][l];
}
putmmp[p][6] = '|', putmmp[p][7] = ' ';
}putmmp[15][x + 1] = 'O';fwrite(putmmp, 1, 18 * 7, stdout);puts("| +-----+");

}
}
return 0;
}

原文地址:https://www.cnblogs.com/Ice-watermelon233/p/C_with_Class_xiuxian_dfkxiaoyouxi.html