Last visit was: It is currently Thu Mar 28, 2024 3:07 am


All times are UTC-05:00




Post new topic Reply to topic  [8 posts ] 
    Author Message
     Post subject:Please check my code
    PostPosted:Sat Aug 18, 2007 1:01 am 
     

    Joined:Tue Aug 14, 2007 10:07 am
    Posts:55
    Yahoo Messenger:azurozeta@yahoo.com
    Location:Indonesia
    well, i'm working on simple snake game, here's partial code that instruct the snake to move in the direction the want. The problem is, let's say i hit 'a', it just move left 1 square and then stop. Is there a way to make it move continuosly until i hit for another direction?

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

    do
    {
    input = getch ();
    switch (input)
    {
    case 'a' : moveLeft (); break;
    case 'w' : moveUp (); break;
    case 'd' : moveRight (); break;
    case 's' : moveDown (); break;
    }
    }while (input != 'k');


    Top
    Offline  
     Post subject:Re: Please check my code
    PostPosted:Sat Aug 18, 2007 1:18 am 
    Literally Nine
    User avatar
     

    Joined:Sat Apr 02, 2005 3:31 pm
    Posts:1171
    Location:The vicinity of an area adjacent to a location.
    Quote:
    well, i'm working on simple snake game, here's partial code that instruct the snake to move in the direction the want. The problem is, let's say i hit 'a', it just move left 1 square and then stop. Is there a way to make it move continuosly until i hit for another direction?

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

    do
    {
    input = getch ();
    switch (input)
    {
    case 'a' : moveLeft (); break;
    case 'w' : moveUp (); break;
    case 'd' : moveRight (); break;
    case 's' : moveDown (); break;
    }
    }while (input != 'k');
    It's possible that the person is giving no input at that precise moment. You need to keep a variable that stores the current direction and then the switch statement would process that variable instead of what the input is. If there -is- some input, then have it assign the new input value to the variable if it's valid.

    I don't know if I can explain better than that. Anyone else?

    _________________
    - Tycho

    Image


    Top
    Offline  
     Post subject:
    PostPosted:Sat Aug 18, 2007 1:21 am 
     

    Joined:Tue Aug 14, 2007 10:07 am
    Posts:55
    Yahoo Messenger:azurozeta@yahoo.com
    Location:Indonesia
    still don't get it (excuse my newbieness). Perhaps you can change my code to match your meaning?


    Top
    Offline  
     Post subject:Re: Please check my code
    PostPosted:Sat Aug 18, 2007 1:31 am 
    Literally Nine
    User avatar
     

    Joined:Sat Apr 02, 2005 3:31 pm
    Posts:1171
    Location:The vicinity of an area adjacent to a location.
    Code:
    char input = 0; char currentDirection = 0; do { input = getch (); switch ( input ) { case 'a': case 'w': case 's': case 'd': currentDirection = input; break; default: printf ( "Invalid input ('%c')!", input ); break; } switch (currentDirection ) { case 'a' : moveLeft (); break; case 'w' : moveUp (); break; case 'd' : moveRight (); break; case 's' : moveDown (); break; } }while (input != 'k');

    _________________
    - Tycho

    Image


    Top
    Offline  
     Post subject:
    PostPosted:Sat Aug 18, 2007 2:08 am 
     

    Joined:Tue Aug 14, 2007 10:07 am
    Posts:55
    Yahoo Messenger:azurozeta@yahoo.com
    Location:Indonesia
    it's not working tycho, unless i hit something, the snake would stop. Maybe there's another IO method you suggest?


    Top
    Offline  
     Post subject:
    PostPosted:Sat Aug 18, 2007 6:01 am 
     

    Joined:Tue Aug 14, 2007 10:07 am
    Posts:55
    Yahoo Messenger:azurozeta@yahoo.com
    Location:Indonesia
    nevermind then, i solved it :p thanks for the help though


    Top
    Offline  
     Post subject:
    PostPosted:Sat Aug 18, 2007 11:51 am 
    Sagely Amphibian
    User avatar
     

    Joined:Sun Jun 18, 2006 3:06 pm
    Posts:69
    So, what your final code looks like?


    Top
    Offline  
     Post subject:
    PostPosted:Sun Aug 19, 2007 6:03 am 
     

    Joined:Tue Aug 14, 2007 10:07 am
    Posts:55
    Yahoo Messenger:azurozeta@yahoo.com
    Location:Indonesia
    Well, in my case, the change applied to the 4 directional function i used. Well, because there are 4 directions, i'll just state one. So here goes:

    #include <constrea.h> // not sure if these libs are useful for my codes.
    #include <conio.h>
    #include <dos.h>
    #include <time.h>
    #include <stdlib.h>

    int x, y; // cartesian based positioning grid
    constream p;

    void main ()
    {
    char input;

    randomize (); // invoke random time quantum << what the hell?
    x = rand () % 80; // make x value random from 0 to 80, 80 is the length of the screen, btw
    y = rand () % 50; // this is for random snake starting point

    do
    {
    input = getch ();
    switch (input)
    {
    case 'a' : moveLeft (); break;
    case 's' : moveDown (); break;
    case 'd' : moveRight (); break;
    case 'w' : moveUp (); break;
    }
    } while (input != 27) // define esc for bail out
    }

    //i'll just type one direction, the other's a bit similar.
    void moveLeft ()
    {
    while (!kbhit ()) // << this is the solution for my problem
    {
    delay (200); // set delay, not sure how much, 200 milisecond?
    p << setxy (x,y) << "*";
    x--;
    p << setxy (x,y) << " "; //for covering track
    }
    }


    Top
    Offline  
    Display posts from previous: Sort by 
    Post new topic Reply to topic

      All times are UTC-05:00


      Who is online

      Users browsing this forum: No registered users and 3 guests


      You cannot post new topics in this forum
      You cannot reply to topics in this forum
      You cannot edit your posts in this forum
      You cannot delete your posts in this forum
      You cannot post attachments in this forum

      Search for:
      Jump to:  
      cron
      Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
      Theme created by Miah with assistance from hyprnova