返回首页

我为我的项目需要在C蛇游戏烃源代码..它只是一个简单的游戏......希望有人能帮助我..感谢...

我有蛇游戏简单的代码,但它不起作用......有很多错误,当我编译它。希望有人能帮助我......感谢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;

}

回答

评论会员:Stefan_Lang 时间:2012/02/06
回应你的问题,编译器错误消息的一点是不正确的语法显示的代码块。为此,编译器将至少打印行号所造成的错误,错误代码,随着一条消息,说明该代码。因为有时一个语法错误不能被决定,直到下一行代码(例如,当缺少';'),指定的路线,有时实际上是一个太低,所以一定要确保你不只是看在该行,但还行。

检查你的编译器输出的第一个错误或警告(即在您的代码的第一行)。再看看这一行,在此之前,立即随之而来。尝试理解的错误消息 - 有时文本可能不会出现对您有意义的,但往往可以在一个有用的提示,在什么是真的错了。现在,尝试辨别,为什么你看行是syntacticaly错误。如果你能找到它,解决它。你可能需要做其他的错误,以及,但往往一个语法错误,会得到编译器混淆,造成他指出很多错误,在真理,只是它是混乱的状态,而不是实际的错误,后果。因此,它可能会审慎确定你找到的第一个错误后,刚刚重新编译器。

如果超过一个错误消息绊倒,你不能自行解决,随时回来给我们,并要求有关专门错误。当你这样做,请张贴在指定的行的源代码,以及在此之前,几行。还发表你有确切的错误信息,所以我们得到了什么可能是错误的预感。在大多数情况下,我们应该足以发现问题,或至少提出正确的问题,在它的核心。_Damian S_
评论会员:游客 时间:2012/02/06
没有人会提供作业答案。你需要证明你已经尝试自己的工作,然后发表你被卡住位的代码。我敢肯定,如果你用Google搜索你就可以找到大量的源代码,只是在等待供您使用...
查克・奥图尔
评论会员:游客 时间:2012/02/06
因为我很无聊,我试图让编译(更正张贴所有LT的GT;'!的固定,感谢斯特凡)你必须回到你这个地方,或者与作者联系或进入该网站上的讨论。此代码是搞砸了。有这样的序列:注意在第二行的双'='。即使它是一个单一的'=',声明是没有意义的。"如果它是一个在标志,将其设置为符号"。也许这只是填充,因为在其他的情况是更有趣的,但它也可以搞砸了编程。有其他类似这样的宝石在那里。codeprelang="cs"unsignedtime;srand(time(spanclass="code-digit"0/span));/pre/code的时间是*两个变量和一个函数,得到的时间作为种子基地(我假设的Unix/Linux)。编译器不喜欢这样。我要说,即使你得到它来编译,有一个机会,它将无法正常工作。这是对一个为我Stefan_Lang
添加至"宝石"恰克发现,出现IO流的理解是一个彻底的缺乏。如。当你看的draw()函数:
{体C3}1。你似乎是误printf()的输出流。字符串格式化输出流是完全不同的!有没有必要指定一个变量的类型,因为它是自动获得!所以只是省略为'%d'在输出字符串中,否则将被打印,从字面上。
此外,将在年底结束的行字符打印字符串的其余部分,前得分。我怀疑这是你的意图。 (另见3。)

2。相同的,除了作为一个和你争夺你无数输出的字符串,'%c'的输出。只需删除该字符串 - 主[X] [Y]是char类型,将印作为这样就好了

3。迫使这样一个换行符将在大多数系统上工作时,有些系统使用不同的符号,换行符。因此,它是更好的风格,而不是使用常量的std :: endl(结束行)

这里是固定的版本:
{
评论会员:游客 时间:2012/02/06
simoncowell的C4}|有人能帮助我..如何让漂亮的代码,这是有1.NEW游戏,2.instruction,3.exit......希望有人能帮助我..请..