C#小行星(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/c-asteroids-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 5 分钟阅读 - 2333 个词 阅读量 0C#小行星(译文)
原文地址:https://www.codeproject.com/Articles/7428/C-Asteroids
原文作者:Howard Uman
译文由本站 robot-v1.0 翻译
前言
An article on writing a simple game in C#
有关使用C#编写简单游戏的文章
介绍(Introduction)
这是我自己对小行星的实现.它是基于模拟矢量的,并使用GDI进行图形处理.游戏本身是相当面向对象的,应该易于理解.代码中还有一些问题,希望一些人可以帮助解决.(This is my own implementation of Asteroids. It is simulated vector based and uses the GDI for graphics. The game itself is fairly object-oriented and should be easy for someone to interpret. There are some issues remaining in the code which I hope some people could help resolve.)
背景(Background)
我很久以前最初在Windows 3.1上的VB中编写了Asteroids游戏.这实际上是该版本的端口.该版本只有基本的游戏玩法,但没有声音.(I originally wrote an Asteroids game in VB on Windows 3.1 a very long time ago. This is essentially a port of that version. That version had just basic gameplay, but no sounds.)
使用代码(Using the Code)
由于此游戏是基于矢量的,因此它是基于很大的空间.所有对象均在此较大的空间中表示,然后按比例缩小到显示器的分辨率.这似乎工作得很好,并且可以在任何大小的窗口中缩放同一游戏.游戏场拥有所有对象,并且基本上通过循环绘制每个对象.(Since this game is vector based, it is based on a very large space. All objects are represented in this larger space and then scaled down to the resolution of the display. This seems to work fairly well and allows the same game to be scaled in a window of any size. The playfield owns all the objects and basically loops through to draw each one.) 游戏具有精美的标题屏幕显示和暂停功能.实现暂停是微不足道的-我不知道为什么所有游戏都没有包含这种功能.(The game features a fancy title screen display and pause. Implementing pause was trivial - I don’t know why all games don’t incorporate such a feature.) 声音通过DirectX播放.图形是使用GDI绘制的. DirectX或其他某种形式的显示可能会改善很多情况.(Sounds are played through DirectX. The graphics are drawn using the GDI. DirectX or some other form of display would likely improve things quite a bit.) 每个对象都负责自己.这对于动画来说是微不足道的,因为该对象实际上是免费处理的.在某些独特的情况下,事物的绘制有些不同.例如,太空船负责绘制推力.(Each object is in charge of itself. This makes it trivial for animation, since the object essentially handles this for free. There are a few unique situations where things are drawn a little bit differently. The space ship, for example, is responsible for drawing its thrust.) 推力,子弹和爆炸产生令人惊讶的良好效果.这是通过在绘制时随机循环浏览颜色列表来完成的.这会产生令人眼花effect乱的效果.(A surprisingly nice effect is generated for the thrust, bullets and explosions. This is done by randomly cycling through a list of colors while drawing. This creates a dazzling effect.) 碰撞检测目前是微不足道的.由于小行星都是圆形的(请参见下文),因此仅检查距小行星中心的任何点都是微不足道的.当不明飞行物出现(见下文)并且小行星具有独特的形状时,此方法将行不通-但最好查看两个物体是否足够靠近以保证进行更彻底的检查.(Collision detection is currently trivial. Since the asteroids are all round (see below), it’s trivial to just check the distance of any point from the center of the asteroid. When UFOs come (see below) and asteroids get unique shapes, this method won’t work - but will be good to see if two objects are close enough to warrant a more complete check.) 对象旋转:(Object rotation:)
protected void Rotate(double degrees)
{
double radiansAdjust = degrees * 0.0174532925;
radians += radiansAdjust/FPS;
double SinVal = Math.Sin(radians);
double CosVal = Math.Cos(radians);
pointsTransformed.Clear();
Point ptTransformed = new Point(0,0);
for (int i=0; i<points.Count; i++)
{
Point pt = ((Point)points[i]);
ptTransformed.X = (int)(pt.X * CosVal + pt.Y * SinVal);
ptTransformed.Y = (int)(pt.X * SinVal - pt.Y * CosVal);
pointsTransformed.Add(ptTransformed);
}
}
绘制对象(在这种情况下,(Drawing an object (in this case,) ship
):():)
public new void Draw(ScreenCanvas sc, int iPictX, int iPictY)
{
switch (state)
{
case SHIP_STATE.ALIVE:
base.Draw(sc, iPictX, iPictY);
if (bThrustOn)
{
//We have points transformed
//so... we know where the bottom of the ship is
ArrayList alPoly = new ArrayList();
alPoly.Capacity = 3;
alPoly.Add(pointsTransformed[iPointThrust1]);
alPoly.Add(pointsTransformed[iPointThrust2]);
int iThrustSize = rndGen.Next(200) + 100; random thrust effect
alPoly.Add(new Point((((Point)pointsTransformed[iPointThrust1]).X +
((Point)pointsTransformed[iPointThrust2]).X) / 2 +
(int)(iThrustSize*Math.Sin(radians)),
(((Point)pointsTransformed[iPointThrust1]).Y +
((Point)pointsTransformed[iPointThrust2]).Y) / 2 +
(int)(-iThrustSize*Math.Cos(radians))));
//Draw thrust directly to ScreenCanvas
//it's not really part of the ship object
DrawPolyToSC(alPoly, sc, iPictX, iPictY, GetRandomFireColor());
}
break;
}
兴趣点(Points of Interest)
控制项:(Controls:)
-
推力-向上箭头(Thrust - up arrow)
-
向右旋转-向右箭头(Rotate right - right arrow)
-
向左旋转-左箭头(Rotate left - left arrow)
-
超空间-向下箭头(重新进入时有爆炸的危险)(Hyperspace - down arrow (there is a risk of explosion on re-entry))
-
射击-空格键(Shoot - space bar)
-
暂停-P键.(Pause - P key.) 这个游戏有些令人讨厌或不完整的事情.他们是:(There are several things that are a bit annoying or incomplete with this game. They are:)
-
有时,游戏会完全冻结.我认为这是由于声音.(Sometimes, the game completely freezes. I think this is due to sound.)
-
游戏似乎可以在1024x768以上的分辨率下正常工作.我确信这是由于我愚蠢的绘画和页面翻转算法造成的.令人惊奇的是,这款游戏在这方面的表现不比我的旧VB版本好.(The game seems to work fine up to resolutions above 1024x768. I am sure this is due to my goofy drawing and page-flipping algorithm. It’s pretty amazing that this game performs no better than my old VB version in this regard.)
-
有时,游戏在加速时会缓慢爬行.释放推力就可以了.(Sometimes, the game slows to a crawl while thrusting. Releasing thrust and it’s fine.)
-
有时,游戏关闭时会引发异常.(Sometimes, there is an exception thrown at shutdown of the game.)
-
还没有UFO-我需要有关智能子弹射击逻辑的一些帮助.(No UFOs - yet. I need some help on smart bullet shooting logic.)
-
小行星都是圆形的.它们会旋转.我想要像拱廊一样颠簸的小行星.这应该是微不足道的,但是它将使碰撞检测更加困难.(Asteroids are all round. They do rotate. I’d like to get bumpy asteroids like the arcade. This should be fairly trivial, but it will make the collision detection a little bit harder.)
-
当对象越过屏幕边缘时,对象会跳跃.这可能会导致看起来像应该发生碰撞的物体消失并且不会发生碰撞的可能性,除非物体发生翻转.(Objects jump when going over edge of the screen. This could create the possibility where something that looks like it should have collided will disappear and there won’t be a collision unless it occurs where the object flipped to.)
-
声音在目录中-我不知道如何将它们嵌入可执行文件中,这会更好.(Sounds are in a directory - I couldn’t figure out how to embed them into the executable, which would be so much nicer.)
历史(History)
- 15(15)日(th)2004年6月-初始版本(June, 2004 - Initial release)
执照(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.1 Visual-Studio VS.NET2003 Dev 新闻 翻译