碰撞-C#游戏,第2部分:跟踪游戏元素并添加交互(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/collision-a-csharp-game-part-2-tracking-game-eleme-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 5 分钟阅读 - 2206 个词 阅读量 0碰撞-C#游戏,第2部分:跟踪游戏元素并添加交互(译文)
原文地址:https://www.codeproject.com/Articles/2133/Collision-A-Csharp-Game-Part-2-Tracking-Game-Eleme
原文作者:Christian Graus
译文由本站 robot-v1.0 翻译
前言
Continuing my attempt at a simple game in C#
继续尝试使用C#进行简单的游戏
目标(Goals)
确定我的原始设计过于雄心勃勃之后,现在的想法是简化绘制过程,同时增加一些游戏玩法元素-不在重复模式中的行星,随着游戏的进行而增加的数量,以及船,我们可以控制.(Having established that my original design was too ambitious, the idea now is to simplify the drawing process while at the same time adding some elements of gameplay - planets that are not in a repeating pattern, that grow in number as the game progresses, and a ship we can control.)
绘制背景(Drawing the Background)
我保留了使星场每次都不同的代码,但是现在星场不再滚动了.这使工作变得更加轻松,因为现在所有绘图都发生在(I’ve kept the code that makes the star field different each time, but now the star field no longer scrolls. This makes life a lot easier, because now the drawing all occurs in the) OnPaint
方法.因为我们设置了(method. Because we set the) DoubleBuffering
要在我们的启动代码中使用,我们将免费进行无闪烁绘制(就工作量而言),这使所有这些工作变得更加简单.(to be on in our startup code, we have got flicker free drawing at no cost (in terms of effort) which makes all of this much simpler.)
现在,计时器代码重新定位了船和小行星,并添加/删除了它们,但是行星和船都绘制在(The timer code now repositions the ship and the asteroids, and adds/removes them, but the planets and ship are drawn in the) OnPaint
方法,直接到屏幕.这类似于在GDI +中实现橡皮筋的方式-我们保留不改变的位,并且将直接绘制到屏幕上并因此不被存储的任何内容,也不会影响我们的背景位图.(method, direct to the screen. This is similar to how rubber banding would be achieved in GDI+ - we keep the bit that does not change and any stuff that will is drawn direct to the screen and thus is not stored, nor does it affect our background bitmap.)
加行星和计分(Adding Planets and Scoring)
要以半随机方式添加行星,我使用(To add planets in a semi random manner, I use the) Random
类,其中有一个(class, which has a) Next
该方法接受一个值,该值是随机数生成器返回的最大值.我为每颗经过的行星得分10分,并根据得分增加了添加行星的可能性.计数器的初始值设置为负数,以强制行星之间的最小间隙.代码如下:(method which accepts a value which is the maximum returned by the random number generator. I score 10 points for every planet passed, and I increase the likelihood of a planet being added according to the score. The initial value of the counter is set to a negative to force a minimum gap between planets. The code looks like this:)
++m_nAddPlanet;
Random r = new Random();
if (m_nAddPlanet > r.Next(Math.Max(300 - m_nScore, 2)))
{
m_nAddPlanet = - 10;
bool b = true;
Rectangle rcDest = new Rectangle();
while (b)
{
b = false;
rcDest = new Rectangle(640, r.Next(280), 87, 87);
for (int i = 0; i < m_arAsteroids.Count; ++i)
{
Asteroid arTemp = (Asteroid)m_arAsteroids[i];
if (rcDest.IntersectsWith(arTemp.rcAsteroid))
b = true;
}
}
Asteroid arAdd;
arAdd.rcAsteroid = rcDest;
arAdd.nBitmap = r.Next(8);
m_arAsteroids.Add(arAdd);
}
追踪行星(Tracking the Planets)
行星(仍称为(The planets (which are still called) Asteroid
由于最初的设计,代码中的s)在(s in the code because of the initial design) are defined in a) struct
像这样:(like this:)
public struct Asteroid
{
public Rectangle rcAsteroid;
public int nBitmap;
}
因为这是一个(Because it’s a) struct
,它按值传递.它存储(, it is passed by value. It stores the) Rectangle
它将被绘制到索引中(it will be drawn into, and the index of the) bitmap
画出来.我最初打算一步一步旋转(to draw it. I initially intended to step this to rotate an) asteroid
,但随着行星的发展,我对它们没有旋转的看法.在添加的代码中,您可以看到行星以随机的Y值刚好位于屏幕之外.它们在绘制时被移动(, but having gone with the planets, I have no rotating views of them. You can see in the adding code that the planets are positioned just off the screen at a random Y value. They are moved as they are drawn in the) paint
方法如下:(method, as follows:)
for (int i = 0; i < m_arAsteroids.Count; ++i)
{
Asteroid arTemp = (Asteroid)m_arAsteroids[i];
arTemp.rcAsteroid.X -= 5;
m_arAsteroids[i] = arTemp;
if (arTemp.rcAsteroid.X < -87)
{
m_arAsteroids.RemoveAt(i);
--i;
m_nScore += 10;
}
e.Graphics.DrawImage(m_bmPlanets, arTemp.rcAsteroid,
87 * arTemp.nBitmap, 0, 87, 87,
GraphicsUnit.Pixel, iattrib);
}
请记住,增加分数会增加添加行星的速度,从而使游戏难度增加.(Remember that incrementing the score adds to the speed in which planets are added, making the game increase in difficulty.)
船(The Ship)
船是非常基本的东西-我们创建两个变量来存储船在船上移动的值(The ship is pretty basic stuff - we create two variables to store the values in which to move the ship in the) x
和(and) y
方向,并将它们设置为(direction, and set them to) 1
要么(or) -1
当按下箭头时,释放时返回0.可以轻松更改代码,以便多次按下即可加速/减速船舶.船的(when an arrow is pressed, and back to 0 when it is released. The code could easily be changed so that multiple presses accelerate/decelerate the ship. The ship’s) rect
存储,并通过这些值进行更改,然后用于绘制它.(is stored, and changed by these values, then used to draw it.)
下一步是什么 ?(What’s Next ?)
该代码仍然不使用任何时间同步代码,因为在我的700 MHz机器上,全速运行仍然可以.下一步是检查碰撞,我们将以像素完美的方式进行工作,同时希望保持可玩的速度,并在下次决定编写动作游戏时使用C ++和DirectX.随着添加了更多行星,游戏肯定会放慢速度,并且如果我们关闭颜色键控,效果会更好一些(尽管看起来很糟糕).如果我们使用(The code still does not use any time sync code, because it still is just OK at full speed on my 700 MHz machine. The idea is next to check for collision, which we will make work in a pixel perfect manner, while hopefully retaining a playable speed, and to use C++ and DirectX next time we decide to write an action game. The game definitely slows down as more planets are added, and it is a little better (although it looks awful) if we turn off colour keying. Some speed increase may be possible if we were to use the) MakeTransparent
功能并恢复为(更大)位图资源,而不是jpg.(function and revert to a (larger) bitmap resource instead of jpg.)
历史(History)
- 17(17)日(th)2002年4月:初始版本(April, 2002: Initial version)
执照(License)
本文没有附带任何明确的许可,但可能在文章文本或下载文件本身中包含使用条款.如有疑问,请通过下面的讨论区与作者联系.(This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.) 可以找到作者可能使用的许可证列表(A list of licenses authors might use can be found) 这里(here) .(.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# Windows .NET .NET1.0 Visual-Studio Dev 新闻 翻译