碰撞后拆除障碍物? [英] Removing the barrier after the collision?

查看:85
本文介绍了碰撞后拆除障碍物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个游戏(Breakout),我有一个问题,



问题是我怎样才能在被球击中后消除障碍?当然,之后球必须能够通过赛道(比如一般的突破游戏)



下一个问题是我可以在跑步时设置障碍时间模式?



谢谢



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



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[^]

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;

    }



}

推荐答案



你怎么说使你的代码更面向对象?

将不同的实体分成类应该可以解决问题,代码将更容易阅读更开放的新功能:)



我会将你的游戏基于或多或少的东西:





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:


 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..));
	}
}


这篇关于碰撞后拆除障碍物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆