Couting a bullet

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
closed account (E0p9LyTq)
http://www.cplusplus.com/forum/beginner/196404/
what does that have to do with this ?
closed account (48T7M4Gy)
what does that have to do with this ?

Not a world shattering event but it's a way of showing everybody there is another thread opened up on the same program. This avoids duplication of effort and you getting a better response. There's no restriction here of one-problem = one thread and then it must close, if it's the same program. :)
i already marked that one as a solved thats why i open a new one
closed account (48T7M4Gy)
Yeah, doesn't matter much though. There are lots that continue long after the green ticks and there are lots here that are resolved (often after a lot of input) that never get a tick. I can understand why you might have thought otherwise though.

Best bet is to simply abandon this one and use the other one. Just ask if you need additional help. It bumps up to the top of the pile so somebody(ies) will see it :)
The logic may be simpler, and the progrM more easily extensible if we paint the game map on an array in memory.

Something along these lines, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <string>
#include <array>

const std::size_t CANVAS_WIDTH = 50 ;
const std::size_t CANVAS_HEIGHT = 9 ;
using canvas_type = std::array< std::string, CANVAS_HEIGHT > ;

const std::size_t BULLET_WIDTH = 5 ;
const std::size_t BULLET_HEIGHT = 3 ;
using bullet_type = char [BULLET_HEIGHT][BULLET_WIDTH] ;

const canvas_type empty_canvas =
{{
    "##############################################",
    "#                                            #",
    "#                                            #",
    "#                                            #",
    "#                                            #",
    "#                                            #",
    "#                                            #",
    "#                                            #",
    "##############################################"
}};

const bullet_type bullet =
{
    { '>', '|', '\\', ' ', ' ' },
    { ' ', '#', '#', '#', '>' },
    { '>', '|', '\\', ' ', ' ' },
};

canvas_type place_bullet( std::size_t pos, canvas_type canvas )
{
    if( pos < (CANVAS_WIDTH-BULLET_WIDTH) )
    {
        //std::cout <<
        std::size_t canvas_row = CANVAS_HEIGHT/2 - BULLET_HEIGHT/2 ;

        for( std::size_t i = 0 ; i < BULLET_HEIGHT ; ++i )
        {
            for( std::size_t j = 0 ; j < BULLET_WIDTH ; ++j ) canvas[canvas_row][pos+j] = bullet[i][j] ;
            ++canvas_row ;
        }
    }

    return canvas ;
}

void print_canvas( std::size_t bullet_pos, canvas_type canvas = empty_canvas )
{
    std::cout << "\n\n" ;

    for( const auto& row : place_bullet( bullet_pos, canvas ) )
    {
        for( char c : row ) std::cout << c ;
        std::cout << '\n' ;
    }
}

int main()
{
    for( std::size_t bullet_pos = 5 ; bullet_pos < 30 ; bullet_pos += 8 ) print_canvas(bullet_pos) ;
}

http://coliru.stacked-crooked.com/a/d5b60e94830531fc
Topic archived. No new replies allowed.