Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   2d array collision detection (http://www.go4expert.com/forums/2d-array-collision-detection-t19996/)

static1991 5Nov2009 19:27

2d array collision detection
 
Code:

//////////////////////////////////////////////////////////

inline void Clear_Screen(void)
{
// this function clears the screen

// set color to white on black
Set_Color(15,0);

// clear the screen
for (int index=0; index<=25; index++)
    Draw_String(0, SCROLL_POS,"\n");

} // end Clear_Screen

struct Vector
{
    Vector() : x(0), y(0) {}

    int x, y;
};

// MAIN GAME LOOP /////////////////////////////////////////

void main(void)
{
char key;            // player input data
int  player_x = 40;  // player's x position
int  player_y = 0;  // player's y position
char text[50] = {0};
int screen[80][25] = {0};        //screen[0][0] = 7;

Vector pos[10];
// SECTION: initialization

for (int i = 0; i < 10; i ++)
{
    pos[i].x = rand() % MAX_X;
    pos[i].y = SCROLL_POS;
}



// SECTION: main event loop, this is where all the action 
// takes place, the general loop is erase-move-draw


int counter2 = 0;


while( counter2 < 200)
    {

   

   
        } // end if 

    // SECTION: game logic and further processing
   
    // make sure player stays on screen
    if (++player_x > MAX_X)
        player_x=MAX_X;   

    if (--player_x < 0)
        player_x=0; 

      if (++player_y > SCROLL_POS)
        player_y=SCROLL_POS ;   

    if (--player_y < 0)
        player_y=0;


   

    // SECTION: draw everything

    // draw next star at random position

    // 2D array used to track and record star locations

    Set_Color(15,0);
    int randomNumber = rand()%70;
    screen[randomNumber][SCROLL_POS] = 1;
    Draw_String(randomNumber, SCROLL_POS,"^\n");
   
    // Move star locations up one as the scroll up screen each frame
    //for (int star = 0; star < 10; star++)
    //{
        ///pos[star].y--;

        ///Draw_String(pos[star].x, pos[star].y, "^");

    //}


    // draw player
    Set_Color(2,0);
    Draw_String(player_x,player_y,"{-8-}");
    Draw_String(player_x-3,player_y-1,"                ");
    Draw_String(player_x-2,player_y-2,"                ");
    Draw_String(player_x-1,player_y-3,"                ");
    Draw_String(0,0,""); 

    // SECTION: synchronize to a constant frame rate
    Sleep(50);

sorry guys for the massive messy code above but im really struggling to get a simple game running ... i have managed all the code above but am lacking a form of collision detection for my game. my goal is for the ^ enemies to deduct points for every time they make contact with the mother ship {-8-}

i have started some code in the form of 2d arrays but im lost and have no idea how to finish it ... some of the code is commented out.

if anyone could write me a piece of code to create a 2d array type collision detection ill be forever thankful or atleast point me in the right direction thanks guys.

some of the code has been removed but i hope i have left in enough for u to help me out


All times are GMT +5.5. The time now is 23:59.