如何在C#中制作弹跳球? [英] How to make a Bouncing Ball in C#?

查看:132
本文介绍了如何在C#中制作弹跳球?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我是C#的新手,我一直试图做出一个Bouncing但不幸但我只是失败了我也看了很多源代码但是他们都不是将4个计时器放在一个表格中太难也不愚蠢

但我需要非常简单易用的代码

解决方案

而不是计时器,请使用表示场景的单独线程。用一些 Thread.Sleep 创建一个循环,然后使用类 System.Diagnostics.Stopwatch 检查实时:

http://msdn.microsoft.com/en-us /library/system.diagnostics.stopwatch.aspx [ ^ ]。



在所有坐标计算中,使用实时,而不是帧数。不要对运动阶段之间的实际时间延迟做任何假设,使用了令牌时间测量。



物理非常简单,但如果你需要消耗能量考虑到,难点将是停止跳跃的标准。如果这是2D或3D,那么硬点将是在地板上跳跃和滚动之间的过渡时刻,并且它们从滚动到完全停止。想一想。



现在,将模型与演示文稿分离得很好。您需要一些不同的演示选项。我建议三个选项:1) System.Windows.Forms ,带直接渲染的自定义控件,2)WPF直接渲染,3)WPF Canvas 带有一个对象,这些对象可以是在Inkscape等矢量编辑器中单独创建的XAML矢量图像。对于WPF方法,请参阅:

http://msdn.microsoft.com /en-us/library/ms748373.aspx [ ^ ],

http ://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.aspx [ ^ ]。



对于 System.Windows.Forms 方法,请查看我过去的答案:

mdi子窗体之间的绘制线 [ ^ ],

在面板上捕获图纸 [ ^ ],

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

如何加速我的vb.net应用程序? [ ^ ]。



使用线程(甚至是计时器,除了 System.Windows。时间由于精度极差,你不应该用于动画),有一个小问题:



你不能打电话给任何相关的东西从非UI线程到UI。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



我必须说,这个简单的问题可能是应用不同技术的一个很好的例子。采用图形,每种方法都会在每种方法上都有一篇非常大的文章来解释所有细节。优秀的编程练习:线程,物理,图形和UI。



祝你好运,

-SA

Hi guys
I''m pretty new to C# and I''ve been trying to make a Bouncing but unfortunately but I just failed I also looked at many source codes but neither of them were too hard nor stupid like putting 4 timer in a form
but i need very simple and easy code

解决方案

Instead of timer, use a separate thread which represents the scenario. Make a loop with some Thread.Sleep and check up the real time using the class System.Diagnostics.Stopwatch:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^].

In all coordinate calculations, use the real time, not number of frames. Do not make any assumptions of the actual time delay between motion phases, used the ream time measurements.

The physics is very simple, but if you need to take energy losses into account, the hard point would be the criterion to stop jumping. If this is 2D or 3D, the hard point would be a moment of transition between jumping and rolling on the floor, and them from rolling to complete stop. Think about it.

Now, isolate model from presentation very well. You need some different options for presentation. I would suggest some three options: 1) System.Windows.Forms, custom control with direct rendering, 2) WPF direct rendering, 3) WPF Canvas with a objects which could be XAML vector images created separately in a vector editor like Inkscape. For WPF approaches, please see:
http://msdn.microsoft.com/en-us/library/ms748373.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.aspx[^].

For the System.Windows.Forms approach, please see my past answers:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my vb.net application?[^].

With a thread (or even a timer, except System.Windows.Time which you should never use for animation due to its prohibitively poor accuracy), there is one little problem:

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

I must say, this simple problem could be a great illustration for application of different technologies. Take the graphics along, each approach would take a really big article on each approach, to explain all the detail. Excellent exercise in programming: threading, physics, graphics and UI.

Good luck,
—SA


这篇关于如何在C#中制作弹跳球?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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