I am working on a Pong game that also uses a gun to shoot out the rear wall of the playing box, so that the ball may escape. I'm using Dev-C++ and the Allegro library.

There are two slight problems...

1) The "bullet" fired from the gun hits its target, but doesn't disappear. Each last round fired hangs around until a next round is fired. At that point, they can be erased by the ball bouncing around. Also, the last round's bullet's remaining carcass will hang around into the next round of the game.

2) I'd like to slow down the bullet's speed, because it's pretty much instantaneous target detonation right now. I'd like to actually see the projectile traveling, but I'm not sure of the best way to do that.

Below is the chunk of code I have for the gun's actions.
Let me know if you need any other info, and thanks for any help.

void shootGun() {
     
     char hit = FALSE;
     
     bullet_x = paddle_x+30;
     bullet_y = paddle_y-3;
     bullet_tempX = bullet_x;
     bullet_tempY = bullet_y;
        
     while (!hit) {
           --bullet_y;
           if (getpixel(screen, bullet_x, bullet_y-3)!=BLACK) {
              hit = TRUE;
              explodeMe(bullet_x, bullet_y-10);
              //eatN(bullet_x, bullet_y-10);
              //circle(screen, bullet_x, bullet_y, 2, BLACK);
              rectfill(screen, 171, 211, 459, 479, BLACK);
           }
     }
     
}


void loadGun() {
     
     acquire_screen();
     circle(screen, bullet_x, bullet_y, 2, YELLOW);
     circle(screen, bullet_tempX, bullet_tempY, 2, BLACK);
     
     //draw_sprite(screen, buffer, 0, 0);
     release_screen();
     bool fire = TRUE;
     
     if (key[KEY_SPACE]) {
        shootGun();                
     }
     
}

To slow down bullet, use a float data type with . below or set it to move every couple milliseconds.

If you want to disable the ball when its not playing, simply put a check for it by setting its movement speed to zero, returning on its drawing call, and invalidating its area to clean it from the screen.

Also I wouldnt use getpixel to detect collosion, but rather put every object into a rect array and have them filter through a for loop that checks in this kind of matter

BOOL IsArea( POINT pt, RECT Rect )
{
	if( pt.x > Rect.left && pt.x < Rect.right && pt.y > Rect.top && pt.y < Rect.bottom  )
		return TRUE;
	return FALSE;
}

But you could use 2 rects if needed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.