Snake game help

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 1516 Views - Last Post: 23 June 2013 - 06:53 PM Rate Topic: -----

#1 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Snake game help

Posted 17 June 2013 - 12:18 PM

Hi guys I need help please,

when the snake is moving in a certain direction let's say RIGHT(6) and you try to make it go immediately back on its self by going LEFT(4) it doesn't it just carries on to the right, that fine.

but if it is travailing in the same direction RIGHT(6) and in quick succession you press UP(8) then LEFT(4) it does go back on its self!!!!

why?
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <math.h>
#include <string.h>
#include <conio.h>

void gotoxy(int x, int y);
void grid(int hx, int hy, int tx, int ty,int mx1,int mx2,int mx3,int mx4,int mx5,int mx6,
                                         int my1,int my2,int my3,int my4,int my5,int my6);

int main(void)
{
    int hx=1, tx=1, mx1=1, mx2=1, mx3=1, mx4=1, mx5=1, mx6=1;
    int hy=2, ty=2, my1=2, my2=2, my3=2, my4=2, my5=2, my6=2;
    int direction=6, x=2, y=2;
    char key;

    while(TRUE)
    {
        while(!kbhit())
        {
            tx=mx6; ty=my6;
            mx6=mx5; my6=my5;
            mx5=mx4; my5=my4;
            mx4=mx3; my4=my3;
            mx3=mx2; my3=my2;
            mx2=mx1; my2=my1;
            mx1=hx; my1=hy;
            hx=x; hy=y;
            grid(hx, hy, tx, ty, mx1, mx2, mx3, mx4, mx5, mx6, my1, my2, my3, my4, my5, my6);
            if(direction==6)
            {
                x=x+2;
                if(x>=21)
                    x=2;
            }
            else
            if(direction==4)
            {
                x=x-2;
                if(x<=1)
                    x=20;
            }
            else
            if(direction==8)
            {
                y--;
                if(y<=1)
                    y=11;
            }
            else
            if(direction==5)
            {
                y++;
                if(y>=12)
                    y=2;
            }
            Sleep(300);
        }
        key=getch();
        if(key=='6' && direction!=4)
            direction=6;
        else
        if(key=='4' && direction!=6)
            direction=4;
        else
        if(key=='8' && direction!=5)
            direction=8;
        else
        if(key=='5' && direction!=8)
            direction=5;
    }
    return 0;
}

void grid(int hx, int hy, int tx, int ty,int mx1,int mx2,int mx3,int mx4,int mx5,int mx6,
                                         int my1,int my2,int my3,int my4,int my5,int my6)
{
    gotoxy(1,1);
    printf("* * * * * * * * * * *   6 = RIGHT\n"
           "*                   *   4 = LEFT\n"
           "*                   *   8 = UP\n"
           "*                   *   5 = DOWN\n"
           "*                   *\n"
           "*                   *\n"
           "*                   *\n"
           "*                   *\n"
           "*                   *\n"
           "*                   *\n"
           "*                   *\n"
           "* * * * * * * * * * *");
    gotoxy(hx,hy); puts("*");
    gotoxy(tx,ty); puts("*");
    gotoxy(mx1,my1); puts("*");
    gotoxy(mx2,my2); puts("*");
    gotoxy(mx3,my3); puts("*");
    gotoxy(mx4,my4); puts("*");
    gotoxy(mx5,my5); puts("*");
    gotoxy(mx6,my6); puts("*");


    return;
}

void gotoxy( int x, int y )
{
    COORD c;

      c.X = x - 1 ;
      c.Y = y - 1 ;

      SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), c );
      return;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Snake game help

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10668
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: Snake game help

Posted 17 June 2013 - 12:19 PM

Moved to C/C++. Please post programming questions in the appropriate forums. Site Questions and Support is for site usability issues.
Was This Post Helpful? 0
  • +
  • -

#3 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Snake game help

Posted 17 June 2013 - 12:21 PM

View Postmacosxnerd101, on 17 June 2013 - 12:19 PM, said:

Moved to C/C++. Please post programming questions in the appropriate forums. Site Questions and Support is for site usability issues.

Sorry my bad. I meant this forum.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Re: Snake game help

Posted 17 June 2013 - 12:59 PM

Line 61 is GetKey() - Doesn't that mean its getting from the keyboard buffer and not checking the actual key pressed at this microsecond?

LIne 59 .Sleep(300) - A very bad thing to do. The program is TOTALLY sleeping. Not doing anything. Including listening to Windows events like key presses. .Sleep puts the entire thread to sleep. Not a good thing when you are coding like this where everything is in one thread. It has a specific use that generally involves a different thread (not your main logic/GUI thread).

My first guess is that key-repeat on your PC has numerous strokes in the buffer that the program is reacting to. Its just lagging behind what the operator is doing.

6 hardcoded variables for x, and another for y? Not good design. At the very least use an array. Then all you have to do is loop and calculate. This would let the snake grow and shrink with ease.

I would also create an enum for the direction so you can make the code more readible.

if (direction == Directions.UP)
// is a lot nicer than
if (direction == 8)

Was This Post Helpful? 2
  • +
  • -

#5 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Snake game help

Posted 17 June 2013 - 01:22 PM

Ok, how do i wait on a line without totally sleeping the program and at the same time record a key press without refering to the buffer?
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Re: Snake game help

Posted 17 June 2013 - 01:36 PM

The speed of that reply makes me think you didn't even bother to try to figure it out, look it up in your text book, or do a simple search. Sorry for the harshness of this but... I'm not looking to collaborate on a homework project with someone that can't be bothered with doing the basics of a little research.

The experts here will help you with your code. But they aren't going to write it for you, or just hand you finished code. I gave you plenty of critiques that should get you started on a rev. 2 of your project. Post your updated code once you have made an effort and I'm sure someone can help you.
Was This Post Helpful? 0
  • +
  • -

#7 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Snake game help

Posted 17 June 2013 - 01:53 PM

Ok noted and thankyou, i have looked into other ways of pausing and found: sleep() but as you said that suspends the entire program, pause() but my compiler dose not support it, creating a loop that does nothing but any of these ways still require me to read the last key from the buffer.

Can i do something like while( !(a=kbhit()) ) and will this save the key pressed that made kbhit()=TRUE?

I am thinking about it i have just got a little lost, also i know there are solutions to this game available and could easily copy them, but i dont want to cheat i want to crack it myself.

I am not a student this is just a hobby/interest of mine.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Re: Snake game help

Posted 17 June 2013 - 02:02 PM

Let me point you in one direction for the pause...
There are probably as many ways of doing this as programs that would think up something.

  • Get the current time.
  • Calculate a time from now + 300 miliseconds and set that to a variable outside of the loop.
  • Within the loop do a check of the current time. If time now is less than time saved then just .Continue the loop before doing anything. It will keep the loop and program alive, without doing anything else and without .Sleeping


You might like to do a search for "C++ Clear Keyboard buffer". That way you can clear before doing a GetKey. Just one idea that doesn't involve checking the KeyDown at a lower level.
Was This Post Helpful? 1
  • +
  • -

#9 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Snake game help

Posted 17 June 2013 - 02:13 PM

View PosttlhIn`toq, on 17 June 2013 - 02:02 PM, said:

Let me point you in one direction for the pause...
There are probably as many ways of doing this as programs that would think up something.

  • Get the current time.
  • Calculate a time from now + 300 miliseconds and set that to a variable outside of the loop.
  • Within the loop do a check of the current time. If time now is less than time saved then just .Continue the loop before doing anything. It will keep the loop and program alive, without doing anything else and without .Sleeping


You might like to do a search for "C++ Clear Keyboard buffer". That way you can clear before doing a GetKey. Just one idea that doesn't involve checking the KeyDown at a lower level.

Thankyou this has helped me with the "sleep" problem. I think you might have misunderstood my code intention: I want the loop to run until a key is pressed, then when it exits the loop because of keypress i want to grab that key from the buffer and use it to change direction, which seems to work fine. What isnt supposed to happen is when you press UP(8) then LEFT(4) quickly when the snake is traviling RIGHT(6) the snake shouldnt go back on its self it should go up(8) since this was the first key pressed.

I have probably misunderstood you sorry if thats the case.

This post has been edited by lewm: 17 June 2013 - 02:34 PM

Was This Post Helpful? 0
  • +
  • -

#10 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Snake game help

Posted 17 June 2013 - 02:31 PM

ok sleep problem changed:
int main(void)
{
    int hx=1, tx=1, mx1=1, mx2=1, mx3=1, mx4=1, mx5=1, mx6=1;
    int hy=2, ty=2, my1=2, my2=2, my3=2, my4=2, my5=2, my6=2;
    int direction=6, x=2, y=2;
    char key;

    while(TRUE)
    {
        while(!kbhit())
        {
            tx=mx6; ty=my6;
            mx6=mx5; my6=my5;
            mx5=mx4; my5=my4;
            mx4=mx3; my4=my3;
            mx3=mx2; my3=my2;
            mx2=mx1; my2=my1;
            mx1=hx; my1=hy;
            hx=x; hy=y;
            grid(hx, hy, tx, ty, mx1, mx2, mx3, mx4, mx5, mx6, my1, my2, my3, my4, my5, my6);
            if(direction==6)
            {
                x=x+2;
                if(x>=21)
                    x=2;
            }
            else
            if(direction==4)
            {
                x=x-2;
                if(x<=1)
                    x=20;
            }
            else
            if(direction==8)
            {
                y--;
                if(y<=1)
                    y=11;
            }
            else
            if(direction==5)
            {
                y++;
                if(y>=12)
                    y=2;
            }
        int i;
        do
        {
            i++;
        }while(i<100000000);
        i=0;
        }
        key=getch();
        if(key=='6' && direction!=4)
            direction=6;
        else
        if(key=='4' && direction!=6)
            direction=4;
        else
        if(key=='8' && direction!=5)
            direction=8;
        else
        if(key=='5' && direction!=8)
            direction=5;
    }
    return 0;
}
Same problem though.

This post has been edited by lewm: 17 June 2013 - 02:38 PM

Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Snake game help

Posted 17 June 2013 - 02:42 PM

Quote

but if it is travailing in the same direction RIGHT(6) and in quick succession you press UP(8) then LEFT(4) it does go back on its self!!!!

Have you checked to see what the key value is being returned when you press the keys. What keys are you using, exactly? The standard number pads above the alpha keys? Keys on the numeric entry keypad?

If you are trying to use the arrows, either separate arrow keys or the numeric keypad with Num-Lock off you'll get several "characters" for each key stroke.

Jim
Was This Post Helpful? 3
  • +
  • -

#12 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Snake game help

Posted 17 June 2013 - 02:53 PM

View Postjimblumberg, on 17 June 2013 - 02:42 PM, said:

Quote

but if it is travailing in the same direction RIGHT(6) and in quick succession you press UP(8) then LEFT(4) it does go back on its self!!!!

Have you checked to see what the key value is being returned when you press the keys. What keys are you using, exactly? The standard number pads above the alpha keys? Keys on the numeric entry keypad?

If you are trying to use the arrows, either separate arrow keys or the numeric keypad with Num-Lock off you'll get several "characters" for each key stroke.

Jim

No i havent checked the key value thanks.
I am using the num pad with num lock on 8-up 5-down 6-right 4-left.
If num lock is of nothing happens it keeps going from left to right with no change in direction when run.
Also the snake moves in the desired direction so if i press 5-DOWN it goes down no problem but then if i press 4-LEFT or 6-RIGHT then 8-UP !QUICKLY! it goes directly back on itself UP.

This post has been edited by lewm: 17 June 2013 - 02:59 PM

Was This Post Helpful? 0
  • +
  • -

#13 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Snake game help

Posted 17 June 2013 - 03:14 PM

It does the same if i use the numbers above letters or the number pad, it goes DIRECTLY back on itself which isnt supposed to happen:
if(key=='6' && direction!=4)
            direction=6;
        else
        if(key=='4' && direction!=6)
            direction=4;
        else
        if(key=='8' && direction!=5)
            direction=8;
        else
        if(key=='5' && direction!=8)
            direction=5;
    }

Was This Post Helpful? 0
  • +
  • -

#14 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Snake game help

Posted 17 June 2013 - 03:35 PM

Ok i added new code to check key returned.
    while(TRUE)
    {
        while(!kbhit())
        {
            tx=mx6; ty=my6;
            mx6=mx5; my6=my5;
            mx5=mx4; my5=my4;
            mx4=mx3; my4=my3;
            mx3=mx2; my3=my2;
            mx2=mx1; my2=my1;
            mx1=hx; my1=hy;
            hx=x; hy=y;
            grid(hx, hy, tx, ty, mx1, mx2, mx3, mx4, mx5, mx6, my1, my2, my3, my4, my5, my6);
            if(direction==6)
            {
                x=x+2;
                if(x>=21)
                    x=2;
            }
            else
            if(direction==4)
            {
                x=x-2;
                if(x<=1)
                    x=20;
            }
            else
            if(direction==8)
            {
                y--;
                if(y<=1)
                    y=11;
            }
            else
            if(direction==5)
            {
                y++;
                if(y>=12)
                    y=2;
            }
        int i;
        do
        {
            i++;
        }while(i<100000000);
        i=0;
        }
        key=getch();
        
        gotoxy(1,z);
        printf("%c", key);///new bit to check key press///
        z++;
        
        if(key=='6' && direction!=4)
            direction=6;
        else
        if(key=='4' && direction!=6)
            direction=4;
        else
        if(key=='8' && direction!=5)
            direction=8;
        else
        if(key=='5' && direction!=8)
            direction=5;
    }
    return 0;
}

Now when i run it the snake starts of moving to the right, when i press down then left QUICKLY it goes DIRECTLY back on itself and i get the output 5 4. Why is it getting 2 key press' ? and even if it is looping why dosent it move down(5) then left(4)?

This post has been edited by lewm: 17 June 2013 - 04:21 PM

Was This Post Helpful? 1
  • +
  • -

#15 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 29-March 13

Re: Snake game help

Posted 17 June 2013 - 04:03 PM

I got it!!!
    while(TRUE)
    {
        while(!kbhit())
        {
            tx=mx6; ty=my6;
            mx6=mx5; my6=my5;
            mx5=mx4; my5=my4;
            mx4=mx3; my4=my3;
            mx3=mx2; my3=my2;
            mx2=mx1; my2=my1;
            mx1=hx; my1=hy;
            hx=x; hy=y;
            grid(hx, hy, tx, ty, mx1, mx2, mx3, mx4, mx5, mx6, my1, my2, my3, my4, my5, my6);
            if(direction==6)
            {
                x=x+2;
                if(x>=21)
                    x=2;
            }
            else
            if(direction==4)
            {
                x=x-2;
                if(x<=1)
                    x=20;
            }
            else
            if(direction==8)
            {
                y--;
                if(y<=1)
                    y=11;
            }
            else
            if(direction==5)
            {
                y++;
                if(y>=12)
                    y=2;
            }
        int i;
        do
        {
            i++;
        }while(i<100000000);
        i=0;
        }
        key=getch();
        
        if(kbhit())
            getch();///SOLVED///
        
        gotoxy(1,z);
        printf("%c", key);///new bit to check key press///
        z++;
        if(key=='6' && direction!=4)
            direction=6;
        else
        if(key=='4' && direction!=6)
            direction=4;
        else
        if(key=='8' && direction!=5)
            direction=8;
        else
        if(key=='5' && direction!=8)
            direction=5;
    }
    return 0;
}

Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3