贪吃蛇C++代码

运行情况:

#include <iostream>
#include <conio.h> 
#include <windows.h> 
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
const int WIDTH = 20;
const int HEIGHT = 20;
bool gameOver;
int x, y, fruitX, fruitY, score;
vector<pair<int, int>> snake; 
enum Direction { STOP = 0, LEFT, RIGHT, UP, DOWN };
Direction dir;
void Setup() {
    gameOver = false;
    dir = STOP;
    x = WIDTH / 2;
    y = HEIGHT / 2;
    fruitX = rand() % WIDTH;
    fruitY = rand() % HEIGHT;
    score = 0;
    snake.push_back(make_pair(x, y)); 
}

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 == y && j == x)cout << "O"; 
            else if (i == fruitY && j == fruitX)cout << "F"; 
            else {
                bool isSnakePart = false;
                for (auto part : snake) {
                    if (part.first == j && part.second == i) {
                        cout << "o"; 
                        isSnakePart = true;
                        break;
                    }
                }
                if (!isSnakePart) cout << " ";
            }
            if (j == WIDTH - 1)cout << "#"; 
        }
        cout << endl;
    }

    for (int i = 0; i < WIDTH + 2; i++)
        cout << "#";
    cout << endl;
    cout << "Score: " << score << endl;
}

void Input() {
    if (_kbhit()) {
        switch (_getch()) {
            case 'a':
                dir = LEFT;
                break;
            case 'd':
                dir = RIGHT;
                break;
            case 'w':
                dir = UP;
                break;
            case 's':
                dir = DOWN;
                break;
            case 'x':
                gameOver = true; 
                break;
        }
    }
}

void Logic() {
    int prevX = snake[0].first;
    int prevY = snake[0].second;
    int prev2X, prev2Y;
    snake[0].first = x;
    snake[0].second = y;
    for (size_t i = 1; i < snake.size(); i++) {
        prev2X = snake[i].first;
        prev2Y = snake[i].second;
        snake[i].first = prevX;
        snake[i].second = prevY;
        prevX = prev2X;
        prevY = prev2Y;
    }
    switch (dir) {
        case LEFT:
            x--;
            break;
        case RIGHT:
            x++;
            break;
        case UP:
            y--;
            break;
        case DOWN:
            y++;
            break;
        default:
            break;
    }
    if (x >= WIDTH) x = 0; else if (x < 0) x = WIDTH - 1;
    if (y >= HEIGHT) y = 0; else if (y < 0) y = HEIGHT - 1;
    if (x == fruitX && y == fruitY) {
        score += 10;
        fruitX = rand() % WIDTH;
        fruitY = rand() % HEIGHT;
        snake.push_back(make_pair(-1, -1));
    }
    for (size_t i = 1; i < snake.size(); i++) {
        if (snake[i].first == x && snake[i].second == y) {
            gameOver = true;
        }
    }
}

int main() {
    srand(static_cast<unsigned>(time(0))); 
    Setup();
    while (!gameOver) {
        Draw();
        Input();
        Logic();
        Sleep(100); 
    }
    return 0;
}

鄙人不才,若不和大佬的观点,望谅解

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的贪吃蛇 C++ 代码示例,使用了基本的控制台输入输出: ```c++ #include <iostream> #include <conio.h> #include <windows.h> using namespace std; bool GameOver; const int Width = 20; const int Height = 20; int x, y, fruitX, fruitY, score; int tailX[100], tailY[100]; int nTail; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; eDirection dir; void Setup() { GameOver = false; dir = STOP; x = Width / 2; y = Height / 2; fruitX = rand() % Width; fruitY = rand() % Height; score = 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 == y && j == x) cout << "O"; else if (i == fruitY && j == fruitX) cout << "F"; else { bool print = false; for (int k = 0; k < nTail; k++) { if (tailX[k] == j && tailY[k] == i) { cout << "o"; print = true; } } if (!print) cout << " "; } if (j == Width - 1) cout << "#"; } cout << endl; } for (int i = 0; i < Width + 2; i++) cout << "#"; cout << endl; cout << "Score: " << score << endl; } void Input() { if (_kbhit()) { switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': GameOver = true; break; } } } void Logic() { int prevX = tailX[0]; int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; tailY[0] = y; for (int i = 1; i < nTail; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } if (x < 0 || x >= Width || y < 0 || y >= Height) GameOver = true; for (int i = 0; i < nTail; i++) if (tailX[i] == x && tailY[i] == y) GameOver = true; if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % Width; fruitY = rand() % Height; nTail++; } } int main() { Setup(); while (!GameOver) { Draw(); Input(); Logic(); Sleep(50); } return 0; } ``` 使用了 `_kbhit()` 和 `_getch()` 函数来检测键盘输入,利用了 Windows.h 库的 `Sleep()` 函数来控制游戏帧率。此外,还使用了两个数组来存储蛇的每一节身体的坐标,来实现蛇的移动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值