c ++中的源代码蛇游戏 [英] source code snake game in c++

查看:70
本文介绍了c ++中的源代码蛇游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个c ++的蛇游戏源代码用于我的项目..这只是一个简单的游戏......希望有人可以帮助我......谢谢...



------------------------------------------------- -------------------------------------------------- ----------------------

我有简单的蛇游戏代码,但它不起作用..当我编译它时有很多错误..hope任何人都可以帮助我..谢谢很多..

i need a snake game souce code in c++ for my project..it just a simple game..hopefully someone can help me..thanks...

-------------------------------------------------------------------------------------------------------------------------
I have simple code for snake game but it doesnt work..have many error when i compile it..hope anyone can help me..thanks alot..

#include <stdio>
#include <stdlib>
#include <windows>
#include <conio>
#include <time>
#include <iostream>

void draw(char main[][75], int score);
void reset(char main[][75]);
void move(char main[][75], int &parts, int pastCounter, int past[][2], int &apples, int &score, int &quit);
void check (int &direction);
void directionn(int direction, int &pastCounter, int past[][2]);
void apple (int &apples, char main[][75]);
void quitGame (int score);

int main()
{
    int past[1000][2];
    int parts = 3;
    char main[23][75];
    int pastCounter = 6;
    int direction = 0;
    int apples = 0;
    int score = 0;
    int quit = 0;
    int playAgain = 1;
    unsigned time;
    srand(time(0));

    for (int x = 0; x < 1000; x ++)
    {
        for (int y = 0; y < 2; y ++)
        {
            past [x][y] = 0;
        }
    }
    past[pastCounter][0] = 1;
    past[pastCounter][1] = 1;
    while(quit == 0)
    {
        draw(main, score);
        check(direction);
        directionn(direction, pastCounter, past);
        reset(main);
        move(main, parts, pastCounter, past, apples, score, quit);

        if (apples == 0)
        {
            apple(apples, main);
        }

    }
    quitGame(score);



}
void draw(char main[][75], int score)
{
    system("cls");
    cout<<"Score : %d\n"<<score;
    for (int x = 0; x < 23; x ++)
    {
        for (int y = 0; y < 75; y ++)
        {
            cout << "%c"<< main[x][y];
        }
        cout<<"\n";
    }

}

void reset(char main[][75])
{
    for (int x = 0; x < 23; x++)
        {
            for (int y = 0; y < 75; y++)
            {
                if (main[x][y] == '@')
                {
                    main[x][y] == '@';
                }
                else
                {
                    if (x == 0 || x == 22 || y == 0 || y == 74)
                    {
                        main[x][y] = 177;
                    }
                    else
                    {
                        main[x][y] = ' ';
                    }
                }
            }
        }
}

void move(char main[][75], int &parts, int pastCounter, int past[][2], int &apples, int &score, int &quit)
{
    if (past[pastCounter][0] == 22 || past[pastCounter][0] == 0)
    {
        quit = 1;
    }
    if (past[pastCounter][1] == 74 || past[pastCounter][1] == 0)
    {
        quit = 1;
    }

    for (int x = 0; x < parts; x++)
    {
        if (main[past[pastCounter - x][0]][past[pastCounter - x][1]] == '@')
        {
            apples--;
            parts++;
            score += 10;
        }
        if (main[past[pastCounter - x][0]][past[pastCounter - x][1]] == 'o')
        {
            quit = 1;
        }
        else
        {
        main[past[pastCounter - x][0]][past[pastCounter - x][1]] = 'o';
        }
    }
}
void check (int &direction)
{
    int key = 0;
    if (kbhit())
    {
        key = -getch();
        switch (key)
        {
        case -72:
            direction = 2;
            break;
        case -77:
            direction = 0;
            break;
        case -80:
            direction = 3;
            break;
        case -75:
            direction = 1;
            break;
        }
    }

}
void directionn(int direction, int &pastCounter, int past[][2])
{
    int down;
    right = past[pastCounter][1];
    down = past[pastCounter][0];
    switch (direction)
    {
    case 0:
        right ++;
        break;
    case 1:
        right --;
        break;
    case 2:
        down --;
        break;
    case 3:
        down ++;
    }

    pastCounter ++;
    past[pastCounter][0] = down;
    past[pastCounter][1] = right;
}
void apple (int &apples, char main[][75])
{
    int up = 0;
    int left = 0;
    apples = 3;

    for (int x = 0; x < apples; x ++)
    {
        up = (rand() % 22);
        left = (rand() % 74);

        if (main[up][left] == 'o')
        {
            apple(apples, main);
        }
        if (main[up][left] == '*')
        {
            apple(apples, main);
        }
        else
        {
            main[up][left] = '@';
        }
    }
}




void quitGame (int score)
{
    int quit = 0;
    system ("cls");
    cout << "GAME OVER!!!!\n\n";
    cout << "You got a score of %d\n"<<score;
}

推荐答案

为了回答您的问题,编译器错误消息的重点是指示语法不正确的代码片段。为此,编译器至少会打印导致错误的行号,错误代码以及解释代码的消息。因为有时在下一行代码之前无法确定语法错误(例如,当缺少';'时),指示的行有时实际上可能太低,所以请确保您不要只查看该行,也是上面的一行。



检查编译器输出是否存在第一个错误或警告(即代码中的第一行)。然后看看那条线以及之前的情况。尝试理解错误消息 - 有时候文本可能对您没有意义,但通常它可以提供有用的提示,确定错误。现在试着辨别,为什么你所看到的那条线在语法上是错误的。如果你能找到它,请修复它。你可能也希望用其他错误做到这一点,但是通常一个语法错误会让编译器感到困惑,导致他指出更多的错误,这些错误实际上只是混淆状态的结果,而不是实际的错误。因此,在修复找到的第一个错误后重新运行编译器可能是谨慎的。



如果您偶然发现一条无法自行解决的错误消息,请随意回到我们面前,专门询问这个错误。当你这样做时,请在指定的行发布源代码,以及之前的几行。同时发布您所获得的确切错误消息,以便我们预测可能出现的错误。在大多数情况下,这应该足以让我们找到问题,或者至少要问正确的问题来解决问题。
In response to your question, the point of compiler error messages is to indicate pieces of code with incorrect syntax. To that end, the compiler will at the very least print the line number that caused the error, and an error code, along with a message explaining the code. Because sometimes a syntax error cannot be decided until the next line of code (e. g. when a ';' is missing), the indicated line can sometimes actually be one too low, so make sure you don't just look at that line, but also the line above.

Check your compiler output for the very first error or warning (i. e. the first line in your code). Then look at that line and what comes immediately before that. Try to understand the error message - sometimes that text may not appear meaningful to you, but often it can at give a useful hint at what is really wrong. Now try to discern, why the line you're looking at is syntacticaly wrong. If you can find it, fix it. You might want to do that with the other errors as well, but often one syntactical error will get the compiler confused, causing him to indicate a lot more errors that are in truth just a consequence of it's confused state, rather than actual errors. Therefore it may be prudent to just rerun the compiler after fixing the first error you find.

If you stumble over an error message that you cannot resolve by yourself, feel free to come back to us and ask specifically about that error. When you do, please post the source code at the indicated line, as well as a few lines before that. Also post the exact error message you got, so we get a hunch of what might be wrong. In most cases that should be enough for us to find the problem, or at the very least ask the right questions to get at it's core.


要添加到'宝石'中查找Chuck,似乎对io流完全缺乏了解。 E. g。当你看到函数 draw()

To add to the 'gems' Chuck found, there appears to be a thorough lack of understanding of io streams. E. g. when you look at the function draw() :
void draw(char main[][75], int score)
{
    system("cls");
    cout<<"Score : %d\n"<<score;        // 1
    for (int x = 0; x < 23; x ++)
    {
        for (int y = 0; y < 75; y ++)
        {
            cout << "%c"<< main[x][y];  // 2
        }
        cout<<"\n";                     // 3
    }
 
}



1.您似乎误将输出流误认为 printf()。输出流的字符串格式完全不同!无需指定变量的类型,因为它是自动派生的!所以只省略输出字符串中的'%d',否则它将按字面打印。

此外,最后的行尾字符将打印出字符串的其余部分,在得分之前。我怀疑那是你的意图。 (另请参阅3.)



2.与一个相同,此外,您将使用无数字符串输出'%c'来扰乱您的输出。只需删除该字符串 - main [x] [y] 的类型为 char ,并且将打印出来就好了。



3.虽然强制像这样的换行符适用于大多数系统,但有些系统会使用不同的符号来换行。因此,更好的方式是使用常量 std :: endl (对于终点线)



这里是固定版本:


1. you seem to be mistaking output streams for printf(). String formatting for output streams is entirely different! There is no need to specify the type of a variable, as it is automatically derived! So just omit the '%d' in your output string, or else it will be printed, literally.
Also, the end of line character at the end will be printed with the rest of the string, before the score. I doubt that was your intention. (also see 3.)

2. same as one, and in addition you'll scramble your output with countless outputs of the string, '%c'. Just remove that string - main[x][y] is of type char and will be printed as such just fine.

3. while forcing a line break like this will work on most systems, some systems use different symbols for a line break. Therefore it is better style to instead use the constant std::endl (for end-line)

Here is the fixed version:

void draw(char main[][75], int score)
{
    system("cls");
    cout<<"Score : "<<score << endl;
    for (int x = 0; x < 23; x ++)
    {
        for (int y = 0; y < 75; y ++)
        {
            cout << main[x][y];
        }
        cout<<endl;
    }
 
}


因为我很无聊,我试图将其编译(使用更正后的帖子所有<'s和>已经修好了,谢谢Stefan!)。



你需要回到你得到的地方并联系作者或在该网站上进行讨论。这段代码搞砸了。



有这样的序列:

Because I was bored, I tried to get this to compile (using the corrected post with all the <'s and >'s fixed, thanks Stefan!).

You need to go back to the place you got this and either contact the author or enter a discussion on that web site. This code is screwed up.

There are sequences like this:
if (main[x][y] == '@')
{
    main[x][y] == '@';
}
else ......





注意第二行中的双=。即使它只是一个'=',这句话也没有意义。 如果它是at符号,则将其设置为at符号。也许它只是填充物,因为其他情况更有趣但它也可能只是搞砸了编程。



还有其他类似的宝石。



Note the double '=' in the second line. And even if it were a single '=', the statement makes no sense. "if it is an at sign, set it to be an at sign". Maybe it's just filler because the else case is the more interesting one but it could also be just screwed up programming.

There are other gems like this in there.

unsigned time;
srand(time(0));

其中time是*都是*变量和一个获取时间的函数种子的基础(unix / linux我假设)。编译器也不喜欢它。



我会说,即使你编译它,也有可能它不起作用。



这就是我的。

where time is *both* a variable and a function that gets the time as a base for the seed (unix / linux I assume). The compiler doesn't like that either.

I'd say even if you get it to compile, there's a chance that it won't work.

That's it for me on this one.


这篇关于c ++中的源代码蛇游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆