高低纸牌游戏(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/hilow-card-game-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 5 分钟阅读 - 2053 个词 阅读量 0高低纸牌游戏(译文)
原文地址:https://www.codeproject.com/Articles/8758/HiLow-Card-Game
原文作者:Mike Kitchen
译文由本站 robot-v1.0 翻译
前言
An article on creating a simple card game with C#, using updated card.dll wrapper class
有关使用更新的card.dll包装器类使用C#创建简单的纸牌游戏的文章
介绍(Introduction)
这是纸牌游戏的简单版本(This is a simple version of the card game) HiLow
其中包括高分表.(that includes a high score table.)
背景(Background)
2003年,当我开始使用C#编程时,我对写纸牌游戏感兴趣.一段时间后,我发现了足够的信息来编写包装器类以使用(In 2003, when I started to program with C#, I was interested in writing a card game. After some time, I found enough information to write a wrapper class to use the)**cards.dll(cards.dll)**Windows附带的.这促使我写了一篇有关DLL的文章(that ships with Windows. This prompted me to write an article about the DLL that can be found) 这里(here) .从那时起,我一直在逐步改进它,以至于感觉到它现在处于可以重新提交的良好状态.如果您想查看我如何去上现在的课程的历史,可以在(. Since then, I have been gradually improving it to a point where I feel it is now in a good enough state to be resubmitted. If you want to see the history of how I got to where the class is now, you can see this on) 我的网站(my website) .(.)
高低游戏(HiLow Game)
游戏仅基于猜测下一张随机纸牌是否高于或低于当前显示的纸牌.每场比赛花费10分,如果您猜对了四张正确的牌,您将赢得20分.玩了一段时间后,猜测四张正确的牌并不像看起来那样容易.(The game is simply based on guessing whether the next random card is higher or lower than the currently shown card. Each game costs 10 points, and if you guess four correct cards, you win 20 points. After playing for a while, guessing four correct cards is not as easy as it seems.) 如果您猜错了四张正确的牌或输了游戏,则"(When you have either guessed four correct cards or you have lost the game, then the “)再玩一遍(Play Again)出现"“按钮.(” button appears.)
包装类(The Wrapper Class)
第一次更改(The first change to the)**cards.dll(cards.dll)**包装器类是它现在实现(wrapper class is that it now implements) IDisposable
.这是为了确保正确清理图形设备上下文,因为该类现在具有其自己的图形表面可以借鉴.(. This is to make sure that the graphics device context is cleaned up properly, as the class now has its own graphics surface to draw upon.)
/// <summary>
/// .NET Graphics surface used for drawing.
/// </summary>
private Graphics graphicsSurface;
/// <summary>
/// Win32 HDC surface use for Win32 drawing.
/// </summary>
private IntPtr graphicsDC;
为了现在使用该类,我们称(In order to now use the class, we call) Begin
在抽签之前,以及(before any cards are drawn, and) End
当我们完成抽签后.(when we have finished drawing our cards.)
protected override void OnPaint(PaintEventArgs e)
{
// Allocate graphics device context
cardDrawing.Begin( e.Graphics );
// Do Card drawing
. . .
// Release graphics device context
cardDrawing.End();
// Draw anything else
base.OnPaint(e);
}
另外,我们需要确保卡片的(Also, we need to ensure that the cards') Dispose
从窗体中调用方法.(method is called from the form.)
if( cardDrawing != null )
{
cardDrawing.Dispose();
}
新的功能(New Features)
该类的一个不错的新功能之一就是可以绘制任意角度旋转的卡片.因此,为了绘制从卡片左上角起逆时针旋转90度的红心之王,我们将使用以下代码行:(One of the nice new features of the class is the ability to draw a card rotated at any angle. So in order to draw the King of Hearts rotated at 90 degrees anti-clockwise from the top-left corner of the card, we would use the following line of code:)
cardDrawing.DrawRotatedCard( new Point(120,120), 90,
Card.ToCardIndex( CardSuit.Hearts, CardRank.King ) );
哪里(where) cardDrawing
是(is an instance of the) Card
类.(class.)
一个简单的甲板课(A Simple Deck Class)
该应用程序使用一个简单的类来表示一副纸牌.我们不需要一个卡数组,而只需要一个整数数组,因为(This application uses a simple class to represent a deck of cards. Rather than having an array of cards, we only need an array of integers, because the) Card
全班只抽牌,没有别的.(class only draws cards and nothing else.)
public class Deck
{
private int[] CardArray = new int[52];
/// <summary>
/// Initializes deck with the 52 integers.
/// </summary>
public Deck()
{
// Deck uses RankCollated cards 0 - 51
for( int i = 0; i < 52; i++ )
{
CardArray[i] = i;
}
}
}
这使得执行随机排序程序变得很容易,该程序本质上是随机的.(This makes it easy to implement a shuffle routine, which is very random in nature.)
/// <summary>
/// Randomly rearrange integers
/// </summary>
public void Shuffle()
{
int[] newArray = new int[52];
bool[] used = new bool[52];
for( int j = 0; j < 52; j++ )
{
used[j] = false;
}
Random rnd = new Random();
int iCount = 0;
int iNum;
while( iCount < 52 )
{
iNum = rnd.Next( 0, 52 ); // between 0 and 51
if( used[iNum] == false )
{
newArray[iCount] = iNum;
used[iNum] = true;
iCount++;
}
}
// Load original array with shuffled array
CardArray = newArray;
}
最后,访问存储在数组中的当前整数以知道要绘制哪张卡.(Finally, the current integer stored in our array is accessed to know which card to draw.)
/// <summary>
/// Obtains a card number from the deck.
/// </summary>
public int GetCard( int arrayNum )
{
if (arrayNum >= 0 && arrayNum <= 51)
return CardArray[arrayNum];
else
throw (new System.ArgumentOutOfRangeException("arrayNum", arrayNum,
"Value must be between 0 and 51."));
}
重用代码(Reusing Code)
在制作这款游戏时,我决定要一张高分桌.与其说是重新发明轮子,不如说我将(Whilst making this game, I decided that I wanted a high score table. Rather than re-inventing the wheel so to speak, I have incorporated the) HighScore
我上课(class from my) Yahtzee(Yahtzee) 游戏之前提交过.(game submitted previously.)
玩的开心!(Enjoy the game!)
历史(History)
- 10(10)日(th)2007年5月-.NET 2.0的更新源代码(May, 2007 - Updated source code for .NET 2.0)
- 14(14)日(th)2007年5月-更新的文章发布到了Code Project(May, 2007 - Updated article posted to Code Project)
执照(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# Win2K WinXP Windows .NET .NET1.0 GDI+ Visual-Studio Dev 新闻 翻译