Need help with this mini game

Hello guys need your help to fix my code or just find what i did wrong ,
so basically i have a game where is a spaceship and a meteors flying around and u have to shot them all .
i wont paste whole code just a parts witch dealing with a shooting:
//main:
1
2
3
4
5
6
7
8
9
10
11
  int main()
{
    while(gamestart && !gameover){
        gamedraw(); // drawing a map,spaceship,meteors ,and ect.
        input(); // key inputs
        movement(); // mowing
        Sleep(10);
        
    }
    return 0;
}

// structure for a bullets
1
2
3
4
struct bullet{
    int x,y;
    int dmg;
} blt[maxblt];

//input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void input(){
if(gamestart && !gameover){
        if(_kbhit()){
            switch(_getch()){
                case 'f': // on key f pressed 
                    if(bb == 10){ // max 10 bullets at once 
                    bb = 0;
                    }else{ // assing a starting cordinates and dmg 
                        blt[bb].x = x; 
                        blt[bb].y = y+6;
                        blt[bb].dmg = 1;
                        bb++;
                    }
                    break;
            }
        }
    }
}

//drawing part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  void gamedraw(){
    system("cls");
    for(int a = 0; a <= mapsizey;a++){
        for(int b = 0; b <= mapsizex;b++){
                bool bultprt = false;
                for(int d = 0; d < maxblt;d++){
                    if(a == blt[d].x && b == blt[d].y){ // if cordinates match cout  "-" and each time increase  y+1 so bullet will move forward 
                        cout << "-";
                        blt[d].y++;
                        bultprt = true;
                    }
                }
                if(!bultprt){
                    cout << " ";
            }
        }
    }
}

the result i got is kind a strange i spend few hours trying to solve it and i could not find a problem the result i got now once i press "f" key is a straight line "-------" till the end of the map
no animation or moving of bullet looks like this :
1
2
3
4
5
6
7
8
9
##############################################
#                                            #
#                                            #
#>|\                                         #
# ###>---------------------------------------# // f key pressed 1 time 
#>|/                                         #
#                                            #
#                                            #
############################################## 

how ever if i change this "blt[d].y++;" to this "blt[d].y--;" in a drawing part its works but the bullet goes backward so thats is confusing me its looks like this :
1
2
3
4
5
6
7
8
9
##############################################
#                                            #
#                                            #
#  -        -                >|\             #
#                   -       - ###>*          # // f key pressed  4 times 
#                            >|/             #
#                                            #
#                                            #
############################################## 

i have worked on a acordinates its seems everything to be right so this is probably something wrong about drawing
starting bullet coordinates is x 10; and y+6(y=1), i marked "*" where is starting bullet position , any help ? can provide full code if necessary
Last edited on
bump
Last edited on
If you get a CPU that's twice as fast, the game will run twice as fast. You need a timer in teh loop so it executes at a fixed rate.
something like a Sleep() function from windows.h ?
Last edited on
Have you looked at std::chrono? I use that library for all my timing.
http://en.cppreference.com/w/cpp/chrono
up
Topic archived. No new replies allowed.