C# - “Pong Game” - Collision with edges of paddle

2019-03-04 14:11发布

问题:

I dont know how to make ball bounce when hits Paddle´s Top or Bottom, my problem is that the ball goes through the Paddle and then keep going...

So this is the code, some things are in my language so here is translator:

Mustek = Paddle

Mic = Ball

body = Points

HraciPole = Name for graphics array, something like "GameArray"

casovac = name for timer

Kolize = Collision

PohybDolu = MoveUp (bool)

PohybNahoru = MoveDown (bool)

public partial class Form_Pong : Form
{
    public static Mustek p1 = new Mustek();
    public static Mustek p2 = new Mustek();

    public static Mic ball = new Mic();
    public int body1 = 0;
    public int body2 = 0;
    Graphics draw;

    public Form_Pong()
    {
        InitializeComponent();

        draw = picBoxHraciPole.CreateGraphics();            
    }

    public static void Draw(Graphics draw)
    {
        draw.Clear(Color.Black);

        draw.FillRectangle(new SolidBrush(Color.Blue), new Rectangle(p1.location, p1.size));
        draw.FillRectangle(new SolidBrush(Color.Red), new Rectangle(p2.location, p2.size));

        draw.FillRectangle(new SolidBrush(Color.White), new Rectangle(ball.location, ball.size));            
    }

    private void casovac_Tick(object sender, EventArgs e)
    {
        if (casovac.Interval == 10)
        {
            p1.location.X = 30;
            p1.location.Y = (picBoxHraciPole.Height - p1.size.Height) / 2;

            p2.location.X = (picBoxHraciPole.Width - p2.size.Width) - 30;
            p2.location.Y = p1.location.Y;

            ball.location = new Point(picBoxHraciPole.Width / 2, picBoxHraciPole.Height / 2);
            casovac.Interval = 5;
        }

        Kolize();
        Pohyb();
        Draw(draw);
    }

    public void Kolize()
    {
        if (p1.location.Y < 0)
        {
            p1.location.Y = 0;
        }

        if (p1.location.Y + p1.size.Height > picBoxHraciPole.Height)
        {
            p1.location.Y = picBoxHraciPole.Height - p1.size.Height;
        }

        if (p2.location.Y < 0)
        {
            p2.location.Y = 0;
        }

        if (p2.location.Y + p2.size.Height > picBoxHraciPole.Height)
        {
            p2.location.Y = picBoxHraciPole.Height - p2.size.Height;
        }

        if (ball.location.Y < 0)
        {
            ball.speed.Y = -ball.speed.Y;
        }

        if (ball.location.Y + ball.size.Height > picBoxHraciPole.Height)
        {
            ball.speed.Y = -ball.speed.Y;
        }

        if (ball.location.X < 0)
        {
            body2 += 1;
            labelBody2.Text = body2.ToString();
            Reset();
        }

        if (ball.location.X + ball.size.Width > picBoxHraciPole.Width)
        {
            body1 +=1;
            labelBody1.Text = body1.ToString();
            Reset();
        }

        if (new Rectangle(ball.location, ball.size).IntersectsWith(new Rectangle(p1.location, p1.size)) ||
            new Rectangle(ball.location, ball.size).IntersectsWith(new Rectangle(p2.location, p2.size)))
        {
            ball.speed.X = -ball.speed.X;

        }

        if (new Rectangle(ball.location, ball.size).IntersectsWith(new Rectangle(p1.location, p1.size)) ||
            new Rectangle(ball.location, ball.size).IntersectsWith(new Rectangle(p2.location, p2.size)))
        {

        }
    }

    public void Reset()
    {
        ball.location = new Point(picBoxHraciPole.Width / 2, picBoxHraciPole.Height / 2);
        ball.speed.X = -ball.speed.X;
        ball.speed.Y = -ball.speed.Y;
    }

    public static void Pohyb()
    {
        if (p1.PohybNahoru == true)
        {
            p1.location.Y -= 8;
        }

        if (p1.PohybDolu == true)
        {
            p1.location.Y += 6;
        }

        if (p2.PohybNahoru == true)
        {
            p2.location.Y -= 6;
        }

        if (p2.PohybDolu == true)
        {
            p2.location.Y += 6;
        }

        ball.location.X += ball.speed.X;
        ball.location.Y += ball.speed.Y;
    }



    private void Form_Pong_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.W)
        {
            p1.PohybNahoru = true; 
        }

        if (e.KeyCode == Keys.S)
        {
            p1.PohybDolu = true;
        }

        if (e.KeyCode == Keys.Escape)
        {
            Application.Exit();
        }

        if (e.KeyCode == Keys.Up)
        {
            p2.PohybNahoru = true;
        }

        if (e.KeyCode == Keys.Down)
        {
            p2.PohybDolu = true;
        }

        if (e.KeyCode == Keys.F1)
        {
            casovac.Enabled = false;
        }

        if (e.KeyCode == Keys.F2)
        {
            casovac.Enabled = true;
        }

    }

    private void EnableDoubleBuffering()
    {
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
    }


    private void Form_Pong_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.W)
        {
            p1.PohybNahoru = false;
        }

        if (e.KeyCode == Keys.S)
        {
            p1.PohybDolu = false;
        }

        if (e.KeyCode == Keys.Up)
        {
            p2.PohybNahoru = false;
        }

        if (e.KeyCode == Keys.Down)
        {
            p2.PohybDolu = false;
        }
    }
}

public class Mustek
{
    public Point location;
    public Size size;
    public bool PohybNahoru, PohybDolu;
    public int Score;

    public Mustek()
    {
        Score = 0;
        size.Width = 25;
        size.Height = 125;
    }
}

public class Mic
{
    public Point location;
    public Size size;
    public Point speed;

    public Mic()
    {
        speed.X = -5;
        speed.Y = 5;
        this.size.Width = 25;
        this.size.Height = 25;
    }
}

回答1:

The basic logic that you are looking for is If edge of ball =edge of paddle then change direction so as if x was decreasing bow x is to ve increasing Once you have that you can work out angles for different conditions such as if it gits the edge of the paddle ect.