乒乓球游戏

· · 休闲·娱乐

本人自己制作的c++游戏,可复制到c++上运行,单纯分享一下

#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;

struct ball {
    int x;
    int y;
    bool way; // 0左 1右 
    bool onRacquet; // 是否在球拍上
    int racquetOwner; // 球拍所有者,1 表示玩家 1,2 表示玩家 2
};

struct racquet {
    int x;
};

// 设置控制台光标位置
void gotoxy(int x, int y) {
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void printmap(int x1, int x2, ball b, int score1, int score2) {
    system("cls");
    cout << "玩家1得分: " << score1 << "  玩家2得分: " << score2 << endl;
    cout << "玩家1:上w下s 发射x  玩家2:上i下k 发射m  按 Esc 退出游戏" << endl;
    for (int i = 0; i < 20; i++) {
        for (int j = 0; j < 20; j++) {
            if (j == 10) cout << '|';
            else if (j == 0 || j == 19) cout << '#';
            else if (j == 1 && (i == x1 || i == x1 - 1 || i == x1 + 1)) cout << ']';
            else if (j == 18 && (i == x2 || i == x2 - 1 || i == x2 + 1)) cout << '[';
            else if (i == b.y && j == b.x) cout << 'O';
            else cout << ' ';
        }
        cout << endl;
    }
}

bool Does_it_go_too_far(int x) {
    if (x > 17 || x < 2) return 1;
    return 0;
}

void operate(char& k, int& x1, int& x2, ball& b) {
    if (_kbhit()) {
        k = _getch();
        switch (k) {
        case 'w': {
            if (!Does_it_go_too_far(x1 - 1)) {
                x1--;
                if (b.onRacquet && b.racquetOwner == 1) {
                    b.y = x1;
                }
            }
            break;
        }
        case 's': {
            if (!Does_it_go_too_far(x1 + 1)) {
                x1++;
                if (b.onRacquet && b.racquetOwner == 1) {
                    b.y = x1;
                }
            }
            break;
        }
        case 'i': {
            if (!Does_it_go_too_far(x2 - 1)) {
                x2--;
                if (b.onRacquet && b.racquetOwner == 2) {
                    b.y = x2; 
                }
            }
            break;
        }
        case 'k': {
            if (!Does_it_go_too_far(x2 + 1)) {
                x2++;
                if (b.onRacquet && b.racquetOwner == 2) {
                    b.y = x2;
                }
            }
            break;
        }
        case 'x': {
            if (b.onRacquet && b.racquetOwner == 1) {
                b.onRacquet = false;
                b.way = 1;
                b.x = 2;
            }
            break;
        }
        case 'm': {
            if (b.onRacquet && b.racquetOwner == 2) {
                b.onRacquet = false;
                b.way = 0;
                b.x = 17;
            }
            break;
        }
        case 27: // Esc 键的 ASCII 码是 27
            exit(0);
        }
    }
}

void moveBall(ball& b, int x1, int x2, int& score1, int& score2) {
    if (b.onRacquet) {
        return;
    }

    if (b.way) {
        b.x++;
    }
    else {
        b.x--;
    }

    // 上下边界检测
    if (b.y == 0 || b.y == 19) {
        b.y = (b.y == 0) ? 1 : 18;
    }

    // 球拍碰撞检测
    if (b.x == 2) {
        if (b.y >= x1 - 1 && b.y <= x1 + 1) {
            b.onRacquet = true;
            b.racquetOwner = 1;
        }
        else {
            score2++;
            b.onRacquet = true;
            b.racquetOwner = 1;
            b.x = 2;
            b.y = x1;
        }
    }
    else if (b.x == 17) {
        if (b.y >= x2 - 1 && b.y <= x2 + 1) {
            b.onRacquet = true;
            b.racquetOwner = 2;
        }
        else {
            score1++;
            b.onRacquet = true;
            b.racquetOwner = 2;
            b.x = 17;
            b.y = x2;
        }
    }
}

int main() {
    ball b = { 2, 10, 1, true, 1 };
    racquet r1 = { 10 };
    racquet r2 = { 10 };
    char k;
    int score1 = 0;
    int score2 = 0;

    while (true) {
        printmap(r1.x, r2.x, b, score1, score2);
        operate(k, r1.x, r2.x, b);
        moveBall(b, r1.x, r2.x, score1, score2);
        Sleep(100);
    }

    return 0;
}    

如果你感兴趣的话也可以看看我的其他作品

扫雷

贪吃蛇

口袋奇兵

逃离后室游戏

不要脸地要个小赞