Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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;
}
Posted 7-Nov-11 18:53pm
Edited 8-Nov-11 22:53pm
v4
Comments
Stefan_Lang at 9-Nov-11 3:56am
   
fixed the incorrectly set HTML tags
(please, whoever did paste this code in there: make sure you set the correct pre type!! The only symbols that for some reason require HTML tags are the angular brackets used for include statements and for template arguments; everything else should not use HTML tags, it might just garble the output)
Member 9432459 at 16-Sep-12 13:13pm
   
what if i want to have a speed level like level 1 for slow 2 for moderate until 5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Noone is going to provide you with homework answers. You need to show that you have attempted the work yourself then post the bit of code that you are stuck on.
 
I'm sure if you googled you would be able to find plenty of source code just waiting for you to use...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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 ......
 
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));
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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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. 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;
    }
 
}
  Permalink  
Comments
simoncowell at 18-Nov-11 21:49pm
   
i have another question..how to make file processing on this code..hopefully anyone can help me..please!
Stefan_Lang at 21-Nov-11 6:25am
   
What do you mean by file processing? Do you want to read from and write into files (and what)? You can use ifstream and ofstream for that, they work almost exactly like cin and cout, respectively. You can google for more info on these streams, and for example code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 275
1 DamithSL 265
2 CPallini 235
3 Maciej Los 185
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 20 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100