(游戏:乒乓球)如何让我的球从我的移动物体反弹? [英] (Game: ping pong) how do I make my ball bounce back from my moving object?

查看:90
本文介绍了(游戏:乒乓球)如何让我的球从我的移动物体反弹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我是游戏初学者,但我确实很挣扎。

我最近在编写一个基本的ping只有一个移动物体的乒乓球游戏,球从墙壁反弹,我想让我的球从我移动的矩形物体反弹。

你可以通过这个链接看到设计;

http://orig11.deviantart.net/d2ed/f/2016/095/8/a/test_by_benjaminhallows-d9xt9r4.png [ ^ ]



我完成了比分,计时和球运动等所有内容,但是在比赛期间我的球不会从我的矩形反弹回来。



如果您知道某些事情请告诉我。



代码中的信息;

球= picturebox1和红色矩形= picturebox2




Hello,

I am a beginner in making games but I do struggle with things.
I was lately programming a basic ping pong game with only one moving object, the ball bounces from the wall and I want to make my ball bounce from my moving rectangle object.
You can see the design with this link;
http://orig11.deviantart.net/d2ed/f/2016/095/8/a/test_by_benjaminhallows-d9xt9r4.png[^]

I did complete everything like the score, timing and ball movement but my ball doesn't bounce back from my rectangle when making contact during the game.

If you know something let me know.

For your information in the code;
The ball = picturebox1 and the red rectangle = picturebox2


namespace basicpingpong
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        int speed = 5;
        int cont = 0;
        int score = 0;

        bool Up;
        bool Left;

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (pictureBox1.Left > pictureBox2.Left)
            {
               timer1.Enabled = false;
               MessageBox.Show("Score: " + score.ToString() + " points!");
               score = 0;
               speed = 5;
               cont = 0;

            }

            if(pictureBox1.Left+pictureBox1.Width >= pictureBox2.Left &&
                pictureBox1.Left+pictureBox1.Width <= pictureBox2.Left+pictureBox2.Width &&
                pictureBox1.Top+pictureBox1.Height >= pictureBox2.Top &&
                pictureBox1.Top+pictureBox1.Height <= pictureBox2.Top+pictureBox2.Height )
            {
                Left = true;
                score += 1;
                this.Text = "Score: " + score.ToString() + "";
                cont += 1;
                if(cont > 5)
                {
                    speed += 1;
                    cont = 0;
                }

            }


            #region Ball movement
            if(Left)
            {
                pictureBox1.Left += speed;

            }
            else
            {
                pictureBox1.Left -= speed;
            }

            if(Up)
            {
                pictureBox1.Top += speed;

            }
            else
            {
                pictureBox1.Top -= speed;
            }

            if (pictureBox1.Top >= this.Height -60)
            {
                Up = false;

            }
            if (pictureBox1.Top <= 0)
            {
                Up = true;
            }
 


            #endregion


        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Owner.Show();
        }

        private void Form2_MouseMove(object sender, MouseEventArgs e)
        {
            pictureBox2.Top = e.Y;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            this.Text = "Score: 0";
            Random random = new Random();
            pictureBox1.Location = new Point(0, random.Next(this.Height));
            Up = true;
            Left = true;
            timer1.Enabled = true;
            score = 0;

        }
    }
}

推荐答案

请参阅我对该问题的评论和我过去的答案:

在图片框中附加图片 [ ^ ],

在C#中绘制一个矩形 [ ^ ],

如何从旧图纸中清除面板 [ ^ ];



以及使用进行图形渲染> System.Drawing

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

http://www.codeproject.com/Answers/523484/Howplustoplusspeedplusupplusmyplusvb-netplusapplic#answer3 [ ^ ],

如何避免DatagridView C#中的红十字[ [ ^ ]。



这就是全部。我希望,物理学(游戏行业中所谓的物理学)将留在你身上。



-SA
Please see my comment to the question and my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^];

and also on graphics rendering with System.Drawing:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
http://www.codeproject.com/Answers/523484/Howplustoplusspeedplusupplusmyplusvb-netplusapplic#answer3[^],
How to avoid Red Cross in DatagridView C#[^].

That's all. I hope, physics (what is called "physics" in gaming industry) will remain on you.

—SA


这篇关于(游戏:乒乓球)如何让我的球从我的移动物体反弹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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