双人贪吃蛇

#include <iostream>
#include <conio.h> // For _kbhit() and getch()
#include <windows.h> // For Sleep()

using namespace std;

bool gameOver;
const int width = 20;
const int height = 20;
int x1, y1, fruitX, fruitY, score1;
int x2, y2, score2;
int tailX1[100], tailY1[100], tailX2[100], tailY2[100];
int nTail1, nTail2;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir1, dir2;

void Setup();
void Draw();
void Input();
void Logic();
int main() {
    Setup();
    while (!gameOver) {
        Draw();
        Input();
        Logic();
        Sleep(10); // 控制游戏速度
    }
    return 0;
}

void Setup() {
    gameOver = false;
    dir1 = STOP;
    dir2 = STOP;
    x1 = width / 2;
    y1 = height / 2;
    x2 = width / 2 - 1;
    y2 = height / 2 - 1;
    fruitX = rand() % width;
    fruitY = rand() % height;
    score1 = 0;
    score2 = 0;
}

void Draw() {
    system("cls");
    for (int i = 0; i < width + 2; i++)
        cout << "#";
    cout << endl;

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if (j == 0)
                cout << "#";
            if (i == y1 && j == x1)
                cout << "O"; // 蛇1头部
            else if (i == y2 && j == x2)
                cout << "o"; // 蛇2头部
            else {
                bool print = false;
                for (int k = 0; k < nTail1; k++) {
                    if (tailX1[k] == j && tailY1[k] == i) {
                        cout << "o"; // 蛇1身体部分
                        print = true;
                    }
                }
                for (int k = 0; k < nTail2; k++) {
                    if (tailX2[k] == j && tailY2[k] == i) {
                        cout << "O"; // 蛇2身体部分
                        print = true;
                    }
                }
                if (!print) {
                    if (i == fruitY && j == fruitX)
                        cout << "F"; // 食物
                    else
                        cout << " ";
                }
            }

            if (j == width - 1)
                cout << "#";
        }
        cout << endl;
    }

    for (int i = 0; i < width + 2; i++)
        cout << "#";
    cout << endl;
    cout << "Score1: " << score1 << " Score2: " << score2 << endl;
}

void Input() {
    if (_kbhit()) {
        switch (getch()) {
        case 'a':
            if (dir1 != RIGHT) dir1 = LEFT;
            break;
        case 'd':
            if (dir1 != LEFT) dir1 = RIGHT;
            break;
        case 'w':
            if (dir1 != DOWN) dir1 = UP;
            break;
        case 's':
            if (dir1 != UP) dir1 = DOWN;
            break;
        case 'j':
            if (dir2 != RIGHT) dir2 = LEFT;
            break;
        case 'l':
            if (dir2 != LEFT) dir2 = RIGHT;
            break;
        case 'i':
            if (dir2 != DOWN) dir2 = UP;
            break;
        case 'k':
            if (dir2 != UP) dir2 = DOWN;
            break;
        case 'x':
            gameOver = true;
            break;
        }
    }
}

void Logic() {
    int prevX1 = tailX1[0], prevY1 = tailY1[0], prevX2 = tailX2[0], prevY2 = tailY2[0];
    int prev2X1, prev2Y1, prev2X2, prev2Y2;
    tailX1[0] = x1;
    tailY1[0] = y1;
    tailX2[0] = x2;
    tailY2[0] = y2;
    for (int i = 1; i < nTail1; i++) {
        prev2X1 = tailX1[i];
        prev2Y1 = tailY1[i];
        tailX1[i] = prevX1;
        tailY1[i] = prevY1;
        prevX1 = prev2X1;
        prevY1 = prev2Y1;
    }
    for (int i = 1; i < nTail2; i++) {
        prev2X2 = tailX2[i];
        prev2Y2 = tailY2[i];
        tailX2[i] = prevX2;
        tailY2[i] = prevY2;
        prevX2 = prev2X2;
        prevY2 = prev2Y2;
    }
    switch (dir1) {
    case LEFT:
        x1--;
        break;
    case RIGHT:
        x1++;
        break;
    case UP:
        y1--;
        break;
    case DOWN:
        y1++;
        break;
    default:
        break;
    }
    switch (dir2) {
    case LEFT:
        x2--;
        break;
    case RIGHT:
        x2++;
        break;
    case UP:
        y2--;
        break;
    case DOWN:
        y2++;
        break;
    default:
        break;
    }

    // 撞墙判断
    if (x1 >= width || x1 < 0 || y1 >= height || y1 < 0 ||
        x2 >= width || x2 < 0 || y2 >= height || y2 < 0)
        gameOver = true;
    
    // 自相撞或互撞判断
    for (int i = 0; i < nTail1; i++)
        if (tailX1[i] == x1 && tailY1[i] == y1)
            gameOver = true;
    for (int i = 0; i < nTail2; i++)
        if (tailX2[i] == x2 && tailY2[i] == y2)
            gameOver = true;
    if (x1 == x2 && y1 == y2)
        gameOver = true;

    // 吃到食物后增长
    if (x1 == fruitX && y1 == fruitY) {
        score1 += 10;
        fruitX = rand() % width;
        fruitY = rand() % height;
        nTail1++;
    }
    if (x2 == fruitX && y2 == fruitY) {
        score2 += 10;
        fruitX = rand() % width;
        fruitY = rand() % height;
        nTail2++;
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值