Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,501 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,380 people online right now. Registration is fast and FREE... Join Now!




Odd Bullet Movement

 
Reply to this topicStart new topic

Odd Bullet Movement

whatsmyface
28 Feb, 2007 - 11:44 AM
Post #1

New D.I.C Head
*

Joined: 20 Feb, 2007
Posts: 7


My Contributions
When I run this code and fire several bullets (space) and then move to the right (d) three spaces the bullets and player move unexpectedly

CODE

/*First Visual (using ASCII characters) Program*/

/*Include Standard Input Output Stream Capabilities As Well As CONIO.H, STDLIB,H, and WINDOWS.H*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>




/*A Function For Clearing The Screen*/
void clrscr()
{
  HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  COORD coord = {0, 0};
  DWORD count;

  CONSOLE_SCREEN_BUFFER_INFO csbi;
  GetConsoleScreenBufferInfo(hStdOut, &csbi);

  FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
  //note that you can use this function to
  //position the cursor in the console.
  SetConsoleCursorPosition(hStdOut, coord);

}

/*Start Main Function*/
int main(void)
{
    /*Set Up Some Variables Note: Not All Variables Are Used*/
    char key; //which key is down
    char player[6] = "<-o->"; //visual player
    short playerx = 0; //players x coordinate
    short playery = 20; //players y coordinate
    short drawx; //current x drawing coordinate
    short drawy; //current y drawing coordinate
    short endprog = 0; //variable for ending the program
    short clearScreen = 50;
    double pauseTimer; //For Slowing Down The Program
    short bulletx[50]; //Bullets X and Y
    short bullety[50];
    int n = 0;
    //Too Set All The Bullet X and Y Values To 0
    while(n < 51)
    {
         bulletx[n] = 0;
         bullety[n] = 0;
         n = n + 1;
    }
    short currentBulletNum = 1;
    long checkForBullets;
    
    /*Start Main Loop*/
    while(endprog == 0)
    {    
        
         /* Slow Down The Program*/
         pauseTimer = 0;
         while(pauseTimer < 5000000)
         {
              pauseTimer = pauseTimer + 1;
         }
        
         //Clear The Screen
         clrscr();
        
         if(kbhit()) //if a key is down ...
         {
              key = getch(); //check which key it is ...
              
              if(key == 'a') //if its the 'a' key ...
              {
                   playerx = playerx - 1; //move the player one to the left
              }
              else if(key == 'd') //if its the 'd' key ...
              {
                   playerx = playerx + 1; //move the player one to the right
              }
              else if(key == '0') //End The Program
              {
                   endprog = 1;
              }
              else if(key == ' ') //Create A Bullet And ...
              {
                   //Set Its X and Y Values
                   bulletx[currentBulletNum] = playerx + 3;
                   bullety[currentBulletNum] = playery - 1;
                   currentBulletNum = currentBulletNum + 1;
                   //If The Current Bullet Number Gets Over 50 Set It To One So We Can Reuse That Bullet
                   if(currentBulletNum > 50)
                   {
                        currentBulletNum = 1;
                   }
              }
              else if(key == ';') //Interesting Little Thing
              {
                   cout << "YOU SUCK";
              
              //Pause The Program So You Can Read 'YOU SUCK'
              pauseTimer = 0;
              while(pauseTimer < 100000000)
              {
                   pauseTimer = pauseTimer + 1;
              }
              }
         }
        
         /*Set Up Drawing Coordinate*/
         drawy = 0;
         /*Start Drawing Loop*/
         while(drawy < 24)
         {
              /*Set The Drawing X To 0 And Loop Through All The Xs*/      
              drawx = 0;
              while(drawx < 81)
              {
                   //Check If A Bullet Is In The Current Location And Draw It
                   checkForBullets = 1;
                   while(checkForBullets < 51)
                   {    
                            
                        if(drawx+drawy == 0)
                        {
                        }                
                        if(bulletx[checkForBullets] == drawx)
                        {
                             if(bullety[checkForBullets] == drawy)
                             {
                                  cout << ".";
                                  drawx = drawx + 1;
                             }
                        }
                        checkForBullets = checkForBullets + 1;
                   }                          
                      
                   //Check If The Player Is There Draw Him/Her      
                   if(drawx == playerx)
                   {
                        if(drawy == playery)
                        {  
                             cout << "<-o->";
                             drawx = drawx + 5;
                        }
                   }
                   else
                   {
                       cout << " ";
                   }
                  
                   if(drawx < 81)
                   {
                        drawx = drawx + 1;
                   }
              }
              drawy = drawy + 1;
         }
        
         //Move All The Bullets Up One
         checkForBullets = 1;
         while(checkForBullets < 51)
         {
              bullety[checkForBullets] = bullety[checkForBullets] - 1;
              checkForBullets = checkForBullets + 1;
         }                      
    }
}


User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Odd Bullet Movement
28 Feb, 2007 - 05:52 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
Well I am looking at your code and I can't get it to work very well. I am learning quite a bit about windows console programming that I did not know.
I *think* that the problem is in the print routine's use of spaces. This may be less of a logic error as it is a misunderstanding of how windows handles the output.

I will get back to you if I get anywhere.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Odd Bullet Movement
28 Feb, 2007 - 06:42 PM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
This is fix. If we use the other windows console routines we can create a "PrintAt" function which can place the graphic where we want it.

CODE
/*First Visual (using ASCII characters) Program*/

/*Include Standard Input Output Stream Capabilities As Well As CONIO.H, STDLIB,H, and WINDOWS.H*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>



void clrscr();
void gotoxy(int x, int y);
void PrintAt(int x, int y, char *CharBuffer, int len);

/*Start Main Function*/
int main(void)
{
    /*Set Up Some Variables Note: Not All Variables Are Used*/
    char key; //which key is down
    char player[] = "<-o->"; //visual player
    char bullet[] =".";
    short playerx = 20; //players x coordinate
    short playery = 20; //players y coordinate
    short endprog = 0; //variable for ending the program
    short clearScreen = 50;
    double pauseTimer; //For Slowing Down The Program
    short bulletx[50]; //Bullets X and Y
    short bullety[50];
    int n = 0;
    short currentBulletNum = 1;
    long checkForBullets;

    int NeedRedraw;        //Used to ensure we only redraw if we need to...
                        //  this will minimize flicker.

    //Too Set All The Bullet X and Y Values To 0
    while(n < 51)
    {
         bulletx[n] = 0;
         bullety[n] = 0;
         n = n + 1;
    }
    
    /*Start Main Loop*/
    while(endprog == 0)
    {    
        
         /* Slow Down The Program*/
         pauseTimer = 0;
         while(pauseTimer < 5000000)
         {
              pauseTimer = pauseTimer + 1;
         }
        
         NeedRedraw = 0;
        
         if(kbhit()) //if a key is down ...
         {
              key = getch(); //check which key it is ...
              
              if(key == 'a') //if its the 'a' key ...
              {
                   playerx = playerx - 1; //move the player one to the left
                   NeedRedraw = 1;
              }
              else if(key == 'd') //if its the 'd' key ...
              {
                   playerx = playerx + 1; //move the player one to the right
                   NeedRedraw = 1;
              }
              else if(key == '0') //End The Program
              {
                   endprog = 1;
              }
              else if(key == ' ') //Create A Bullet And ...
              {
                   //Set Its X and Y Values
                   bulletx[currentBulletNum] = playerx + 2;
                   bullety[currentBulletNum] = playery - 1;
                   currentBulletNum = currentBulletNum + 1;
                   //If The Current Bullet Number Gets Over 50 Set It To One So We Can Reuse That Bullet
                   if(currentBulletNum > 50)
                   {
                        currentBulletNum = 1;
                   }
              }
              else if(key == ';') //Interesting Little Thing
              {
                   PrintAt(0, 0, "YOU SUCK", 8);  
              //Pause The Program So You Can Read 'YOU SUCK'
                  pauseTimer = 0;
                  while(pauseTimer < 100000000)
                  {
                       pauseTimer = pauseTimer + 1;
                  }
              }
         }
        
//        if (NeedRedraw)
//        {
            clrscr();
            PrintAt(playerx, playery, player, 6);
            checkForBullets = 1;
            while(checkForBullets < 51)
            {    
                if((bulletx[checkForBullets] > 0) && (bullety[checkForBullets] >0))
                {
                    PrintAt(bulletx[checkForBullets], bullety[checkForBullets], bullet, 1);
                }
                checkForBullets = checkForBullets + 1;
            }
//        }
            
            
             //Move All The Bullets Up One
        checkForBullets = 1;
        while(checkForBullets < 51)
        {
            if (bullety[checkForBullets]>0)
            {
                bullety[checkForBullets] = bullety[checkForBullets] - 1;
//                NeedRedraw=1;
            }
            checkForBullets = checkForBullets + 1;
        }
    }

    clrscr();
    return;
}


/*A Function For Clearing The Screen*/
void clrscr()
{
  HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  COORD coord = {0, 0};
  DWORD count;

  CONSOLE_SCREEN_BUFFER_INFO csbi;
  GetConsoleScreenBufferInfo(hStdOut, &csbi);

  FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
  //note that you can use this function to
  //position the cursor in the console.
  SetConsoleCursorPosition(hStdOut, coord);
  return;
}

//This function will set the cursor to (x, y).
void gotoxy(int x, int y)
{
    COORD coord = {x, y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    return;
}

//This function will print CharBuffer at the location (x, y)
void PrintAt(int x, int y, char *CharBuffer, int len)
{
    COORD coord = {x, y};
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hStdOut, coord);
    WriteConsole(hStdOut, CharBuffer, len, NULL, NULL);
    return;
}


[EDIT] *** There is an error in the code above which causes an access violation, here is the corrected function:

CODE
//This function will print CharBuffer at the location (x, y)
void PrintAt(int x, int y, char *CharBuffer, int len)
{
    unsigned long count; needed for WriteConsole...
    COORD coord = {x, y};
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hStdOut, coord);
    WriteConsole(hStdOut, CharBuffer, len, &count, NULL);
    return;
}


This post has been edited by NickDMax: 28 Feb, 2007 - 07:31 PM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Odd Bullet Movement
1 Mar, 2007 - 05:00 AM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
I added a code snippet that shows some more console output functions, which you may find useful.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 06:34PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month