Thread: Dat snake game...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    Dat snake game...

    Hello guys,

    Snake just hates me,I just cannot create it in any way, how it can it possible? Anyone can help me? I'm mad...


    Code:
    #include <cstdlib>#include <Windows.h>
    #include <iostream>
    #include <ctime>
    #include <conio.h>
    using namespace std;
    
    
    void setcolor(unsigned char color)
    {
        HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(hcon,color);
    }
    void sp(int spalva)
    {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), spalva);
    }
    void s()
    {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
    }
    
    
    
    
    class Lithuania
    {
    protected:
        double Scoore;
        int rad,FailRandm,x, y,a,b,i,SnakeLength = 2,previous;
        char Ibiza [2400], key;
        bool down ,left,right,up ;
    public:
            Lithuania()
            {
                            Scoore = 0 ;
                            x =  1560;
                            y =  1400;
                            down  = 0  ;
                            left  = 0  ;
                            right = 0  ;
                            up    = 0  ;
                            previous = 0;
                            i = 0;
            }
            void clearscreen     ();
            void ToGetRandomApple();
            void CheckRight      ();
            void CheckLeft       ();
            void ConstSnake      ();
            void Keyboard        ();
            void MoveLogic       ();
            void Keyboard2       ();
    
    
    }jaja;
    
    
    
    
    
    
    int main()
    {
        jaja.ConstSnake();
    }
    
    
    void Lithuania ::ToGetRandomApple()
    {
        if(x < 0 || x > 4800 ){cout << endl << "\n\nGame Over!";cout << "You earned:" << Scoore/125;cin.ignore();}
        if(Ibiza[rad] == Ibiza[x]){
        Scoore += 2;
        SnakeLength++;
                if( left == true )
        {
            x--;
        }
            else if( down == true )
        {
            x+=80;
        }
            else if( up == true )
        {
            x-=80;
        }
            else if( right == true )
        {
           x++;
        }
        rad = rand()%48;
        FailRandm = rand()&48;
        rad*=FailRandm;
    }}
    void Lithuania ::ConstSnake()
    {
        srand((unsigned)time(0));
        system("color 19");
        rad = rand() % 2400+1;
    do{
                cout << "Dollers=" << Scoore/125 << "     " <<endl;
                cout << "LEFT=" << left << endl << "DOWN=" << down << endl << "UP=" << up << endl << "RIGHT=" << right;
                cout << "Previous=" << previous << endl<<endl;
                jaja.ToGetRandomApple();
                setcolor(15);
            for(i = 0 ; i < 2400 ; i++)
            {
                if(x == y)
                {
                    cout << "LOL NOOB";
                    cin.ignore();
                }
                 if(x != i && y != i && rad != i)
                 {
                     cout << Ibiza[i];
                 }
                 else if(i == x)
                 {
                                 Ibiza[i] = 219;
                 sp(240);
                 cout << Ibiza[i] ;
                 }
                 else if(i == rad)
                 {
                                 Ibiza[rad] = 1;
                                 cout << Ibiza[i];
                 }
                 else
                 {
                                  sp(240);
                                 Ibiza[i] = 173;
                 cout << Ibiza[i] ;
                 }
                 s();
            }
                 jaja.Keyboard ();
                 jaja.MoveLogic();
                 jaja.clearscreen   ();
      }
    while(true);
    }
    
    
    void Lithuania::clearscreen()
    {
        HANDLE hOut;
        COORD Position;
    
    
        hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
    
        Position.X = 0;
        Position.Y = 0;
        SetConsoleCursorPosition(hOut, Position);
    }
    
    
    void Lithuania::Keyboard()
    {
    if(kbhit()){
            key = getch();
            jaja.Keyboard2();
            cout << y;
            switch( key )
            {
            case 'd':
    {
            if (left == 0) {
              left = 0, up = 0, down = 0,
              right = 1;
            }
                    break;
    }
    
    
            case 'w':
    {
            if (down == 0) {
              left = 0, down = 0, right = 0,
              up = 1;
            }
                    break;
    }
    
    
            case 'a':
    {
            if (right == 0) {
              down = 0, up = 0, right = 0,
              left = 1;
            }
                    break;
    }
    
    
            case 's':
    {
            if (up == 0) {
              left = 0, up = 0, right = 0,
              down = 1;
            }
            break;
    }
    
    
                }
    
    
                }
    }
    
    
    
    
    void Lithuania::MoveLogic()
    {
             if(left == 1 && right == 0)
        {
            if(previous == 2)
            {
                x--;
                if(x !=y-SnakeLength)
                {
                    y+=80;
                }
                else y--;
            }
            else if(previous == 4)
            {
                x--;
                if(x !=y-SnakeLength)
                {
                    y-=80;
                }
                else y--;
            }
            else if(previous == 1)
            {
                x--;
                y--;
            }
        }
        else if(left == 0 && right == 1)
        {
    
    
            if(previous == 2)
            {
                x++;
                if(x!=y+SnakeLength)
                {
                    y+=80;
                }
                else y++;
            }
            else if(previous == 4)
            {
                x++;
                if(x!=y+SnakeLength)
                {
                    y-=80;
                }
                else y++;
            }
            else if(previous == 3)
            {
                x++;
                y++;
            }
        }
        else if(down == 1 && right == 0)
        {
            if(previous == 2)
            {
                x+=80;
                y+=80;
            }
            else if(previous == 3)
            {
                x+=80;
            if(x!=y+SnakeLength * 80)
                {
                    y++;
                }
                else y+=80;
            }
            else if(previous == 1)
            {
                x+=80;
                if(x!=y+SnakeLength * 80)
                {
                    y--;
                }
                else y+=80;
            }
        }
        else if(up == 1 && right == 0)
        {
            if(previous == 3)
            {
                x-=80;
                if(x!=y-80*SnakeLength)
                {
                    y++;
                }
                else y-=80;
            }
            else if(previous == 4)
            {
                x-=80;
                y-=80;
            }
            else if(previous == 1)
            {
                x-=80;
                if(x!=y-80*SnakeLength)
                {
                    y--;
                }
                else y-=80;
            }
        }
    }
    
    
    void Lithuania::Keyboard2()
    {
            if( left == true )
        {
            previous = 1;
        }
            else if( down == true )
        {
            previous = 2;
        }
            else if( up == true )
        {
            previous = 4;
        }
            else if( right == true )
        {
            previous = 3;
        }
        else {;}
    
    
    }
    Last edited by esrelmantis; 05-19-2013 at 03:46 AM.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    What on earth do you expect us to give you based on your post?? - Form a bloody question - say what difficulties you are having with your program. For starters the code is inconsistently formatted, has magic numbers and badly named data in parts
    Last edited by rogster001; 05-19-2013 at 03:59 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    2
    Oh,sorry.But I guess it's simple to notice what problem I have with it - MOVING.
    It just can't do zigzages,because then, tail starts going to ..who knows where.

    Where is my code inconsistently formatted?

    Also why do you say magic numbers ? It's just one-dimension array ,and that numbers like 173,219 - char symbol numbers.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    simple to notice what problem I have with it - MOVING.
    Why is it simple to see that? You did not say that - Do you expect people to trawl through your code, compile it build it, debug it for you - to find that out? You could easily have written something like " I have a problem with the movement of the snake, whenever i input a left move i get a seg fault" - or something like that.

    Your strangely named class 'Lithuania' (why?? what does that have to do with the program context? Is it a level name or something?) has a keyboard function - the switch case is badly formatted - unless it is just browser rendering, in which case i apologise.

    Why jaja? What does that give to the reader in the context? To me as a spanish speaker it looks like 'haha' It does not seem to have anything to do with a sensible naming method.

    Magic numbers - for loop to 2400 ??? Also testing for left == 0 etc, why not just assign the values to constant names? despite them being simple 0, 1, its better programming to do that.
    Last edited by rogster001; 05-19-2013 at 04:28 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Snake game..
    By Marcus007 in forum C Programming
    Replies: 0
    Last Post: 12-05-2011, 06:58 PM
  2. Help with a snake game?
    By awr7126 in forum Game Programming
    Replies: 2
    Last Post: 12-06-2010, 06:39 PM
  3. Help with snake game in sdl/c
    By joellllmal in forum Game Programming
    Replies: 4
    Last Post: 08-22-2010, 12:14 AM
  4. About SnaKE game..
    By ozumsafa in forum C Programming
    Replies: 3
    Last Post: 10-19-2007, 05:46 PM
  5. game (snake)
    By nps in forum C Programming
    Replies: 1
    Last Post: 10-09-2001, 10:37 PM

Tags for this Thread