Click here to Skip to main content
15,886,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm making a game (Breakout) and I have a question,

The question is how can I remove the barriers after they get hit by the ball? Of course, the ball must be able to go through the track after that (like Breakout game in general)

the next question is that can I make the barriers in run time mode?

Thanks

https://dl.dropboxusercontent.com/s/4rrxevluvrwok4d/game.JPG?dl=0[^]

C#
private void timer1_Tick(object sender, EventArgs e)
{


    ball.Top += step;
    ball.Left += stepleft;

    //board simulate collision
    bool collisonX = ball.Location.X + ball.Width > board.Location.X && ball.Location.X < board.Location.X + board.Width;
    bool collisonY = ball.Top + ball.Height == board.Location.Y || ball.Top + ball.Height - 1 == board.Location.Y;


    //board2(button1) simulate collision
    bool collisonX2 = ball.Location.X + ball.Width > board2.Location.X && ball.Location.X < board2.Location.X + board2.Width;
    bool collisonY2 = ball.Top + ball.Height == board2.Location.Y || ball.Top + ball.Height - 1 == board2.Location.Y;

    //Collision the ball with under buttons
    bool collsionButtonY = ball.Top - ball.Height == board2.Location.Y || ball.Top - ball.Height == board2.Location.Y - 1;

    //collision leftwall
    bool leftWall = ball.Left == 0 || ball.Left == -1 || ball.Left == 1;
    //collision rightwall
    bool topWall = ball.Top == 0 || ball.Top == -1 || ball.Top == 1;

    bool bottomWall = collisonX && collisonY;
    bool toppWall = collisonX2 && collisonY2;


    //collision
    bool barrier = collisonX2 && collsionButtonY;


    bool collisionLeft = ((ball.Location.Y + ball.Height >= board2.Location.Y) && (ball.Location.Y <= board2.Location.Y + board2.Height) && (ball.Location.X + ball.Width >= board2.Location.X) && (ball.Location.X <= board2.Location.X + board2.Height));

    //rightwall
    bool rightWall = ball.Left + ball.Width == this.ClientSize.Width || ball.Left + ball.Width == this.ClientSize.Width - 1;
    // sidewall = collision rightwall or leftwall
    bool sideWall = leftWall || rightWall;

    //Check the ball hit the ground
    bool check = ball.Top + ball.Height < this.ClientSize.Height;


    //if topWall true,This means that the ball is hit to the topwall

    if (topWall)
    {
        flagBottom = false;
        flagTop = true;
        if (stepleft > 0)
        {
            step = 2;
        }
        else if (stepleft < 0)
        {
            step = 2;
        }



    }
    //if bottomWall true,This means that the ball is hit to the board

    else if (bottomWall)
    {
        flagBottom = true;
        flagTop = false;
        if (stepleft > 0)
        {
            step = step * -1;


        }
        else if (stepleft < 0)
        {

            step = step * -1;
        }


    }
    //if barrier true and flagbottom true,This means that the ball is hit to the board2(button1)

    else if (barrier && flagBottom)
    {
        collisionLeft = false;

        if (stepleft > 0)
        {

            step = step * -1;

        }
        else if (stepleft < 0)
        {
            step = step * -1;

        }


    }
    //if toppWall true and flagTop true,This means that the ball is hit to The top button is hit

    else if (toppWall && flagTop)
    {
        collisionLeft = false;
        if (stepleft > 0)
        {
            step = step * -1;
        }
        else if (stepleft < 0)
        {
            step = step * -1;

        }
    }

    else if (flagTop && collisionLeft)
    {
        barrier = false;

        if (stepleft > 0)
        {
            stepleft = -2;
            step = 2;

        }
        else if (stepleft < 0)
        {
            stepleft = 2;
            step = 2;

        }
    }


    else if (flagBottom && collisionLeft)
    {
        barrier = false;

        if (stepleft > 0)
        {

            stepleft = -2;
            step = -2;

        }
        else if (stepleft < 0)
        {
            stepleft = 2;
            step = -2;

        }
    }









    else if (sideWall)
    {
        //if leftwall true,This means that the ball is hit to the left side wall
        if (leftWall)
        {
            if (flagTop)
            {
                stepleft = 2;
            }
            else if (flagBottom)
            {

                stepleft = 2;
            }
        }
        //if rightWall true,This means that the ball is hit to the left side wall
        else if (rightWall)
        {
            if (flagTop)
            {
                stepleft = -2;
            }
            else if (flagBottom)
            {
                stepleft = -2;
            }

        }


    }
    //check if ckeck==ture,this mean the ball is hit the ground
    else if (!check)
    {
        timer1.Enabled = false;
    }





}






private void board_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        MouseDownLocation = e.Location;
    }

}

private void board_MouseMove(object sender, MouseEventArgs e)
{

    if (e.Button == MouseButtons.Left)
    {
        board.Left = e.X + board.Left - MouseDownLocation.X;

    }



}
Posted
Updated 7-Oct-14 20:52pm
v2
Comments
johannesnestler 8-Oct-14 5:43am    
Nice timer_tick handler - I hope it's not a GUI-thread timer ;-) Your spaghetti code here is to much for someone to extend or modify (I doubt someone will read it) - at least you have good variable names. I assume this is just a homework/learning project so this is maybe acceptable. So I can give just a general advice to first calculate a "preview" where the ball will be in the next tick - if it will hit a "barrier", calculate "reflection" of the ball then and remove the "barrier" (or whatever the gamelogic should do) after that. Just don't couple calculations and "animations" in one step. A "classical" gameloop will do (Update-Draw-Update-Draw...).

1 solution

Hi,
What do you say about making your code more object oriented?
Separating different entities into classes should do the trick, code will be easier to read and more open to new features:)

I would base your game on something more or less like:


C#
 class Ball{
	Point Location;
	Point Velocity;
	Size BallSize;
}
	
class Barrier{
	Point Location;
	Size BarrierSize;
	bool IsActive;
	BarrrierType Type;/*Wall, EasilyRemoved, HardToRemove etc..*/
	public virtual void HandleHit(){
	/*here you can make it inactive by setting IsActive after hit,
		you can introduce a counter which will remove barrier after several hits,
		or you can make it irremovable to emulate wall*/
	}
}

class Board:Barrier{
	public override void HandleHit(){
		//do whatever you want when the board is hit
	}
}

class World{
	List<Barrier> Barriers;
	Ball MyBall;
	Board MyBoard;
	
	public void Init(){
		//add side walls and button here as barriers
		//initialize MyBall
		//initialize MyBoard
	}
		
	public void Tick(){
		HandleCollisions();
		MyBall.Location = MyBall.Location+MyBall.Velocity;
                Draw();
	}
	
	private void HandleCollisions(){
		foreach(Barrier b in Barriers){
		if(b.IsActive){
			//check if ball hits barrier b and change velocity accordingly
			if(/* ball hits barrier*/){
				b.HandleHit();
			}
			}
		}
	}
        
	private void Draw(){
		foreach(Barrier b in Barriers){
		{
			//draw barriers
		}
		//draw MyBall
		//draw MyBoard
    }
	
	public void AddSomeBarier(){
		Barriers.Add(new Barrier(location, size..));
	}
}
 
Share this answer
 
Comments
johannesnestler 10-Oct-14 4:37am    
5ed - the way to go - OOP!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900