海军战斗游戏(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/navy-battle-game-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 6 分钟阅读 - 2591 个词海军战斗游戏(译文)
原文地址:https://www.codeproject.com/Articles/11122/Navy-Battle-Game
原文作者:Alex Cutovoi
译文由本站 robot-v1.0 翻译
前言
A client-server navy battle game.
客户端-服务器海军战斗游戏.
介绍(Introduction)
一个带有套接字和IO的客户端服务器应用程序,可以玩Navy Battle.(A client-server application with sockets and IO to play Navy Battle.)
在您开始阅读之前(Before you start to read)
本文是两篇系列文章的第一部分.第一部分介绍如何绘制海军战场.为了更好地理解本文,您应该了解一些套接字连接(对于第2部分),即名称空间(This article is the first part of a two-article series. This first part covers how to draw a navy battle battlefield. To better understand this article, you should know a little bit of socket connections (for the part 2), the namespace) System.Drawing
和(, and) IO
(第2部分也是如此).如果您不知道我在说什么,我建议您阅读Deitel的书" C#How to Program".但是,如果您没有钱,请参阅((for the part 2 too). If you don’t have any idea what I’m talking about, I recommend you read the Deitel’s book “C# How to Program”. But if you don’t have money, the documentation in the) MSDN(MSDN) 是很大的帮助.最重要的是您要拥有这本书并在MSDN中进行导航.(is a great help. The great deal is you to have the book and to navigate in the MSDN.)
一个简单的(?)C#游戏(A simple(?) C# game)
我想写一个简单的C#游戏,然后我想:" Navy Battle是个好主意".但是当我编码的时候,现实并不是很好.我已经多次敲过键盘,主要是当我想找到一种在两种形式的正确位置绘制" X"坐标的方法时,但应用程序完成了.(I wanted to write a simple C# game, and I thought: “Navy Battle is a good idea”. But when I was coding, the reality was not so good. I’ve hit my head many times on the keyboard, mainly when I wanted to find a way to draw the “X” coordinates in the right places in both forms, but the application is done.)
绘制战场(Drawing the Battlefield)
好吧,我们现在走.首先,我需要在其中绘制战场和坐标(当然用字母和数字表示).我怎样才能做到这一点?我创建了一个名为(Well let’s go now. First of all I’ve needed to draw a battle field and coordinates (represented with letters and numbers, of course) in it. How can I do this? I’ve created a class called) BattleField
.(.)
这些是该方法用于在我的班级中绘制战场的变量,请参见将它们及其值放在此处:(These are the variables that the method uses to draw the battle field in my class, see that I put them here with their values:)
private int iCol = 0, iRow = 0, iX = 30, iY = 80;
private const int m_iWidth = 50, m_iHeight = 50;
主要方法是这样的:(The main method is this:)
private void Draw()
{
string abc = "12345abcde";
string[,] sCoordinates = new string[2,5];
for(int i = 0 ; i <= 9 ; i++)
{
//populate the array
if(i == 5)
{
iCol = 0;
iRow += 1;
}
sCoordinates[iRow,iCol] = abc.Substring(0,1);
abc = abc.Remove(0,1);
iCol += 1;
}
float fLocal = (float)60;
float fX = (float)iX;
float fY = (float)iY;
Font font = new Font("Arial", 10);
SolidBrush sBlack = new SolidBrush(Color.Black);
Point pLocation = new Point(iX, iY);
PointF pfLocation = new PointF(fX, fY);
Pen pen = new Pen(new SolidBrush(Color.Black), 3);
//Setting the row and column to origin
iRow = 0;
iCol = 0;
pfLocation.X += 25;
pfLocation.Y = fLocal;
for(int iCount = 0 ; iCount <= 9 ; iCount++)
{
//put numbers
if(iCount <= 4)
{
m_FormGraphs.DrawString(sCoordinates[iRow,
iCol].ToString(),font, sBlack, pfLocation);
pfLocation.X += 50;
}
//put letters
else
{
if(iCount == 5)
{
pfLocation.X = 10;
pfLocation.Y += 40;
iRow +=1;
iCol = 0;
}
m_FormGraphs.DrawString(sCoordinates[iRow,
iCol].ToString(),font, sBlack, pfLocation);
pfLocation.Y += 50;
}
iCol += 1;
}
//to draw 5 squares in a row
for(int iOtherCount = 0; iOtherCount < 5 ; iOtherCount++)
{
//to draw 1 square in each column
for(int j = 0 ; j < 5 ; j++)
{
m_FormGraphs.DrawRectangle(pen, pLocation.X,
pLocation.Y, m_iWidth, m_iHeight);
pLocation.X += 50;
}
pLocation.X = iX;
pLocation.Y += 50;
}
}
绘制坐标(Drawing the coordinates)
在我的(In my) Draw
方法,将创建一个坐标为" 12345abcde"的字符串和一个2x5矩阵.该数组将循环填充.下面有三个(method, a string with coordinates “12345abcde” and a 2x5 matrix are created. The array is populated in a loop. Below there are three) float
变量,一个(variables, a) Font
和一个(and a) SolidBrush
和两个(and two) Point
对象,其中一个是(objects, and one of them is a) PointF
它被称作(that is called) pfLocation
.现在,这些是如何用来绘制战场的?如果您在下面注意到,变量(. Now, how are these used to draw the battlefield? If you notice below, the variables) iRow
和(and) iCol
设置为0,将值25加到(are set to 0, the value 25 is added to) pfLocation.X
财产和(property and) pfLocation.Y
被分配了包含在(is assigned with the value contained in the) fLocal
变量.为什么?我需要将字母放在与它们的中心对齐的前5个正方形的顶部,所以我的总和为25(variable. Why? I need to put the letters on the top of the first 5 squares aligned with their centers, so I sum 25 in my) pfLocation.X
并画出我的第一个数字.此后,我继续求和,现在值为50,因为我需要转到下一个平方的一半并画出下一个数字.字母也会发生相同的过程,但是它们是在垂直位置绘制的,但是这次(and draw my first number. After this I continue with my sum, now with the value 50, because I need to go to the half of the next square and draw the next number. The same process occurs with the letters, but they are drawn in the vertical position, but this time the) pfLocation.X
首先分配值10,(is first assigned with the value 10 and) pfLocation.Y
总和为40.仅在定义坐标时才这样做(is summed with 40. This is done only for defining a coordinate in the) PointF
的(’s) pfLocation
目的.(object.)
画正方形(Drawing the squares)
现在是时候解释平方了.看一下这段代码:(Now it’s time to explain the squares. Look at this part of the code:)
for(int iOtherCount = 0; iOtherCount < 5 ; iOtherCount++)
{
//to draw 1 square in each column
for(int j = 0 ; j < 5 ; j++)
{
m_FormGraphs.DrawRectangle(pen, pLocation.X,
pLocation.Y, m_iWidth, m_iHeight);
pLocation.X += 50;
}
pLocation.X = iX;
pLocation.Y += 50;
}
正方形的另一个循环使用(There is another loop for the squares that uses the) Point
目的(object) pLocation
.方法(. The method) DrawRectangle
我使用的有五个参数:(that I use, has five parameters: a) Pen
对象,X坐标,Y坐标,宽度和高度.为了在每次迭代中在我的表单中填充一条假想线,我将X坐标上的50求和,此过程将继续填充表单中的5条假想线.我用(object, X coordinate, Y coordinate, width and height. To fill an imaginary line in my form in each iteration, I sum 50 in X coordinate, this process continues filling 5 imaginary lines in the form. I use the) Point
具有整数的对象,用于定义绘制正方形的点.您可以使用(object with integers to define the point that my squares are drawn. You can use the) PointF
对象,但仅适用于浮点数.(object but it only works with floating point.)
谢谢(Thank you)
大家好,本系列的第一部分已经结束.在下一篇文章中,我将解释在应用程序中使用套接字连接以及如何在战场上绘制" X".如果您有任何建议,怀疑和批评,请(Guys, this first part of the series has ended. In the next article I will explain using socket connections in applications and how to draw the “X"s on the battlefield. If you have any suggestions, doubts and critics, please)传一个电子邮件给我(send me an e-mail).感谢您的耐心配合.希望您喜欢这篇文章!第二部分即将推出.我要感谢Jesse Liberty的书,Deitel家族的C#HTP,Daniel Turini的文章"我想成为一名优秀的程序员^怎么做?”. [请参阅(. Thanks for the patience. I hope that you enjoy this article! The second part is coming soon. I would like to thank Jesse Liberty for his books, Deitel family for their C# HTP, Daniel Turini for his article “I want to be a good programmer…how?” [see this article in) 丹尼尔`图里尼的巢穴(Daniel Turini’s Lair) ],MSDN,当然还有您和Code Project.(], MSDN, of course, you and Code Project.)
文章的历史(Article’s History)
- 2005年7月12日(07-12-2005)-本文第一部分的开头和结尾.(- The beginning and the end of the first part of the article.)
- 2005年7月26日(07-26-2005)-完整的软件包已发送到The Code Project.(- Complete package sent to The Code Project.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# Windows .NET .NET1.1 Visual-Studio GDI+ VS.NET2003 Dev 新闻 翻译