吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 597|回复: 31
收起左侧

[学习记录] C++ 版本的贪吃蛇 源代码

  [复制链接]
linxukun 发表于 2024-3-15 00:51



#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 eDirect { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirect 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 printTail = false;
                for (int k = 0; k < nTail; k++)
                {
                    if (tailX[k] == j && tailY[k] == i)
                    {
                        cout << "o";
                        printTail = true;
                    }
                }
                if (!printTail)
                    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;
        prev2Y = tailY;
        tailX = prevX;
        tailY = 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;


    for (int i = 0; i < nTail; i++)
    {
        if (tailX == x && tailY == y)
        {
            gameOver = true;
            break;
        }
    }


    if (x == fruitX && y == fruitY)
    {
        score += 10;
        fruitX = rand() % width;
        fruitY = rand() % height;
        nTail++;
    }
}


int main()
{
    Setup();
    while (!gameOver)
    {
        Draw();
        Input();
        Logic();
        Sleep(10); // 控制游戏速度(可根据需要调整)
    }
    return 0;
}
1710435012709.png

免费评分

参与人数 7吾爱币 +6 热心值 +7 收起 理由
Sixpence27 + 1 我很赞同!
破竹而入 + 1 + 1 算法真牛逼
suimengzero + 1 + 1 谢谢@Thanks!
LittleHedgehog + 1 + 1 厉害&amp;#128077;&amp;#127995;&amp;#128077;&amp;#127995;&amp;#128077;&am
aizhetengde11 + 1 + 1 谢谢@Thanks!
rnaunture + 1 + 1 我很赞同!
gym66777 + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

草原心521 发表于 2024-3-15 03:23
这个可以挺牛的!
WM715 发表于 2024-3-15 05:27
龍謹 发表于 2024-3-15 08:01
很好很强大,谢谢楼主分享源码,看来又得研究一段时间了。
QQ165888 发表于 2024-3-15 08:05
谢谢分享,牛人呀!
xuanwu126 发表于 2024-3-15 08:11

这个可以,学习学习
Setsugekka 发表于 2024-3-15 08:11
很赞!!
igwd01eFwiza 发表于 2024-3-15 08:14
学习了,赞一个!
hwjqc 发表于 2024-3-15 08:22
牛逼,学习一下
kendy20 发表于 2024-3-15 08:24
赞赞赞赞支持&#9994;
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-1 09:21

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表