Looks Like A Multi-Thread, Sounds Like One... Well, is it?

This is a discussion on Looks Like A Multi-Thread, Sounds Like One... Well, is it? within the C++ Programming forums, part of the General Programming Boards category; I want to get a circle to move around the screen. I've got that down. But how do I get ...

  1. #1
    drdroid
    Guest

    Question Looks Like A Multi-Thread, Sounds Like One... Well, is it?

    I want to get a circle to move around the screen. I've got that down. But how do I get it to keep moving until you make it move in another direction(you guessed it... yup the games snake)?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you don't need multithreading for that, just handle possible keyboard input in your animation loop.

  3. #3
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    after you hit the right arrow key say
    Code:
    right = true; //make sure you have bool right=false;
    //code code code
    while(right == true)
    {
       //do snake code
    }
    then, if another key is pressed, like left,
    Code:
    left = true;
    right = false;
    down = false;
    up = false;
    while(left == true)
    {
       //do snake code that makes the snake go down
    }
    hope i helped

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    No, you just need some clever thinking. Someone posted a solution to this before... if I remember correctly, it went like this.

    Code:
    int iKeyPress;
    
    do {
    	while (!kbhit())
    	{
    		moveSnake();
    	}
    	iKeyPress = getch();
    	switch (iKeyPress)
    	{
    	 case 'A': 
    		moveSnakeLeft(); 
    		break;
    	 case 'S': 
    		moveSnakeDown(); 
    		break;
    	 case 'W: 
    		moveSnakeUp(); 
    		break;
    	 case 'D': 
    		moveSnakeRight(); 
    		break;
    	}
    
    } while (!gameIsOver());

  5. #5
    drdroid
    Guest

    Question

    what does the function, moveSnake()?

  6. #6
    drdroid
    Guest
    what does it do?

  7. #7
    drdroid
    Guest
    ??? hello, are you there?

  8. #8
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    try giving people more than 15 minutes to respond to your question? maybe like 15 hours? or 30 hours? and i'm serious btw; people have other things to do in life.

    anyway, movesnake() moves the snake.
    hello, internet!

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    stop spamming and think about it for a second.

    You want the snake to keep moving in whatever direction it was already going in. so....

    function MoveSnake() will check the direction the snakes going in and move it accordingly.

    functions MoveSnakeLeft() etc will only set the direction variable that is checked within the MoveSnake() function.

    For gods sake think about things a little yourself first.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    I'm attaching a snake game I wrote quite a while back. The source and its exe is included.

    Your game loop is the place where you want to move the snake to its next position. Before each next movement, you just want to check if a direction key was hit and if so, set a direction flag to that which corresponds.

    Here's my main game loop:

    Code:
    void snake::loop() {
      initialize();
      while (alive) {
        if (!paused)
          delay(speed);
        if (kbhit()) {
          int key = getch();      
          switch (key) {
            case 'F':
            case 'f': paused = paused ? false : true; froze = true; scoreboard(); break;
            case 'Q':
            case 'q':
            case 27: gameover(); return;
            case 'S':
            case 's': changegamesize(S); break; /* S or s */
            case 'R': // 'm' == 77 && 77 == LARROW (so we're using 'R' for medium - for 'Regular')
            case 'r': changegamesize(M); break; /* R or r */
            case 'L':
            case 'l': changegamesize(L); break; /* L or l */
            case 33: 
            case 49: speed = LOW; scoreboard(); break;  /* 1 */
            case 64: 
            case 50: speed = MED; scoreboard(); break; /* 2 */
            case 35: 
            case 51: speed = HI; scoreboard(); break;  /* 3 */
            case 36: 
            case 52: speed = HI2; scoreboard(); break;  /* 4 */
            //only check for movement if we aren't paused        
            case 72: if (dir == DOWN || paused) break;  dir=UP;    move(); break;
            case 77: if (dir == LEFT || paused) break;  dir=RIGHT; move(); break;
            case 80: if (dir == UP || paused) break;    dir=DOWN;  move(); break;
            case 75: if (dir == RIGHT || paused) break; dir=LEFT;  move(); break;
          }
        }
        else if (!paused)
          move();
      }
    }
    So to put it plainly, just set an enumerated type to the direction and move() will just move wherever it's set to.
    Attached Files Attached Files

  11. #11
    drdroid
    Guest

    Talking Thanks

    Thanks! Sorry, I was such a loser... I gotta headache... anyway it worked, its just a little flickery... i'm going to work on that, but thanks to everyone who posted...

  12. #12
    drdrdoid
    Guest

    Post Flicker

    I got the flickering down, too. I'll post a zip of the game when they let me... the option won't come up... I'll try again in 30 or an hour... Thanks again for the help.

  13. #13
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    I think it's not coming up because you're not registered? Maybe?

    Anyway, glad to help.

  14. #14
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    hm...

    i'm registered... but it came up just now so here goes...
    Attached Files Attached Files

  15. #15
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    any replies?

    any replies to the game in the making?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  2. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  3. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 05:14 AM
  4. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 02:17 PM
  5. Critical Sections, destroying
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2003, 11:36 PM

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