Easier way?

This is a discussion on Easier way? within the Game Programming forums, part of the General Programming Boards category; If i wanted to make a snake or pacman type game... Is there a way to make the character move ...

  1. #1
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    Question Easier way?

    If i wanted to make a snake or pacman type game...
    Is there a way to make the character move instead of this:
    Code:
    if(mve == dwn && grid[0][1] == ' '){
    grid[0][1] = 'X';
    grid[0][0] =' ';
    doing that for every grid and evry possibility (up, dow, left, right)
    would be soo tedious and time consuming.



    What is C++?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >would be soo tedious and time consuming
    Well, first you should use variables for the coordinate indices. Second, for very tedious/repetitive tasks you should consider writing a generic function that can take care of it for you so that all you have to do is call the function instead of implement it every time you wish to change the location:
    Code:
    void change_location ( const char modex, const char modey )
    {
      grid[x][y] = CLEAR;
      if ( modex == '+' )      x++;
      else if ( modex == '-' ) x--;
      if ( modey == '+' )      y++;
      else if ( modey == '-' ) y--;
      grid[x][y] = FILL;
    }
    .
    .
    .
    switch ( move ) {
    case UP:    change_location ( 0, '-' ); break;
    case DOWN:  change_location ( 0, '+' ); break;
    case LEFT:  change_location ( '-', 0 ); break;
    case RIGHT: change_location ( '+', 0 ); break;
    default:
      perror ( "Invalid input" );
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    wow, that will take me a while to understand....
    Hmmm.. could you edit that with comments?
    I cant understand what certain things like + and - are doing...
    What is C++?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Edited with liberal comments.
    Code:
    /* This function takes two characters which tell it
    ** whether to add or subtract a grid cell from the
    ** two coordinate variables x and y. x and y are 
    ** assumed to be global in this example.
    */
    void change_location ( const char modex, const char modey )
    {
      /* Place a space character at the
      ** current location to clear the
      ** grid cell.
      */
      grid[x][y] = CLEAR;
      /* Test modex, which will determine
      ** how the x coordinate is to be
      ** changed. '+' means add, '-' means
      ** subtract, and any other value 
      ** means to do nothing with x.
      */
      if ( modex == '+' )      x++;
      else if ( modex == '-' ) x--;
      /* Test modey which effects the x
      ** coordinate in the same manner as
      ** the x coordinate.
      */
      if ( modey == '+' )      y++;
      else if ( modey == '-' ) y--;
      /* The coordinates are set for the new
      ** location, place the character that
      ** was to be moved at the new location.
      */
      grid[x][y] = FILL;
    }
    .
    .
    .
    /* Switch with the move variable. Each direction
    ** is denoted by a named constant for ease of
    ** reading.
    */
    switch ( move ) {
    /* Each call to change_location effects either
    ** x or y, the first argument to change_location
    ** effects x and the second effects y. A '+' means
    ** to add, a '-' means to subtract, and in this
    ** case, 0 means to do nothing though any value
    ** other than '+' and '-' could be used.
    */
    case UP:    change_location ( 0, '-' ); break;
    case DOWN:  change_location ( 0, '+' ); break;
    case LEFT:  change_location ( '-', 0 ); break;
    case RIGHT: change_location ( '+', 0 ); break;
    default:
      perror ( "Invalid input" );
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Ok, im going to asume there is alot to declare?

    And will this work with dev-c++?
    I really want to use the kbhit() func. nut it doesnt seem to work with my compiler.
    And with a snake or Pacman type game, how would you make the character move in a set direction by itself? (with a controlled speed).

    Thanks for your help Prelude you really know this stuff
    What is C++?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Ok, im going to asume there is alot to declare?
    Well, you need a grid and two coordinate variables. Past that is just making things more readable and easier to maintain. Instead of CLEAR and FILL I could have easily used ' ' and 'X'.

    >And will this work with dev-c++?
    Yes.

    >And with a snake or Pacman type game, how would you make
    >the character move in a set direction by itself?
    Yes, have one of the coordinates increment or decrement with every iteration of the loop which tests for a new location. When the user chooses a new direction ( usually only left or right respective to the current direction ) then a new coordinate will be picked to change constantly:
    Code:
    for ( ; ; ) {
      if ( direction == x )
        change_location ( change, 0 );
      else
        change_location ( 0, change );
      pause_for_input ( &move );
      switch ( move ) {
      case LEFT:  
        change_location ( '-', 0 );
        direction = ( direction == x ) ? y : x;
        break;
      case RIGHT: 
        change_location ( '+', 0 );
        direction = ( direction == x ) ? y : x;
        break;
      }
      /* Reset change so that direction
      ** is incremented or decremented
      ** accordingly
      */
    }
    This should work if you can write pause_for_input() so that it waits for a certain length of time ( around 200 milliseconds ) while at the same time waiting for input and ends even if there is no input. This is very implementation and platform dependent, so I'll leave that up to you since you know your platform better than I do.

    -Prelude
    My best code is written with the delete key.

  7. #7
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    ok youve given me much to think about

    One more thing...
    If i cant use kbhit(),
    how can I get the same results with a different function?

    [edit]
    Prelude, you rock
    [/edit]
    What is C++?

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how can I get the same results with a different function?
    The functionality of kbhit() is nonstandard and any function that does it may not be on all implementations. I'm not familiar with Bloodshed, so you'll have to look around your documentation to see if there is a kbhit() equivalent. I can't remember exactly, but I think there is a WinAPI function called MsgWaitForMultipleObjects or MsgWaitForMultipleObjectsEx that will wait until certain criteria that you give it are met, such as a key being hit or a time limit being reached. Don't quote me on that though because I'm not sure.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    kbhit() does work for Dev, Vicious. Well, actually I'm not sure if it does work for Dev4.01, but I know that it does work with Dev5beta4.9.2. Go to bloodshed.com to download it. Remember, though, that it is still a beta and there a few problems with it. The only problem that I notice is that when you run the .exe inside of Dev, Dev closes. But as long as you keep the folder (window) open that has the .exe inside it and run the .exe from there it will be no big problem. There is, also, a bug that if you change the text color it sometimes won't work properly. Other than that I haven't found any other bugs. If you need to look at how you use the kbhit() function look at my maze game source code (refer to my sig for link to site).

  10. #10
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Well the desktop PC has some Viruses and I cant get online to do anything until we get it fixed.
    What is C++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make it easier?
    By Stiopa in forum C++ Programming
    Replies: 2
    Last Post: 12-15-2006, 03:57 AM
  2. easier way to flush input?
    By GSG-9 in forum C Programming
    Replies: 2
    Last Post: 02-05-2005, 02:52 PM
  3. How to easier use a Class?
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 08-15-2003, 02:42 PM
  4. dialogs are easier, but what about the main window?
    By stallion in forum Windows Programming
    Replies: 4
    Last Post: 01-21-2003, 01:42 PM
  5. What's easier: Open GL or Direct X
    By Ryeguy457 in forum Game Programming
    Replies: 2
    Last Post: 09-13-2002, 01:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21