【C#】WPF实现经典纸牌游戏,适合新手入门

世界杯直播365 2025-12-07 03:19:26 admin 阅读 5898

文章目录

1 纸牌类2 布局3 初始化4 事件点击牌堆拖动牌的去留

源代码

C#专栏目录(长期更新)

1 纸牌类

之所以产生这个无聊至极的念头,是因为发现Unicode中竟然有这种字符。。。

黑桃🂡 🂢 🂣 🂤 🂥 🂦 🂧 🂨 🂩 🂪 🂫 🂬 🂭 🂮红心🂱 🂲 🂳 🂴 🂵 🂶 🂷 🂸 🂹 🂺 🂻 🂼 🂽 🂾钻石🃁 🃂 🃃 🃄 🃅 🃆 🃇 🃈 🃉 🃊 🃋 🃌 🃍 🃎草花🃑 🃒 🃓 🃔 🃕 🃖 🃗 🃘 🃙 🃚 🃛 🃜 🃝 🃞

这就意味着不用任何资源就可以实现一些纸牌游戏,效果如下图所示

实现这个游戏的第一步就是新建一个纸牌类,一张扑克牌至少包含三个信息,分别是花色、颜色以及牌序,以及三合一的图案。由于我们要做一个经典纸牌,所以最好在纸牌类中包含一些游戏时需要用到的信息。

#region 常量

private static readonly Dictionary CardNames = new Dictionary{

{"Spade", new string[13]{"🂡","🂢","🂣","🂤","🂥","🂦","🂧","🂨","🂩","🂪","🂫","🂭","🂮" } },

{"Heart", new string[13]{"🂱", "🂲", "🂳", "🂴", "🂵", "🂶", "🂷", "🂸", "🂹", "🂺", "🂻", "🂽", "🂾" } },

{"Diamond", new string[13]{"🃁", "🃂", "🃃", "🃄", "🃅", "🃆", "🃇", "🃈", "🃉", "🃊", "🃋", "🃍", "🃎" } },

{"Club" , new string[13]{"🃑", "🃒", "🃓", "🃔", "🃕", "🃖", "🃗", "🃘", "🃙", "🃚", "🃛", "🃝", "🃞"}}

};

#endregion

#region 卡牌类型

private class Card

{

public Card(string name, int number, string type, bool red, int index)

{

Name =name;

Number = number;

Type = type;

Red = red;

Index = index;

}

public int Index;

public string Name;

public int Number;

public bool Red;

public string Type;

public int Region;

}

#endregion

在实现了纸牌类之后,将每个纸牌放到一个Button的Tag中,然后再 为Button添加各种事件,就能实现这个游戏了。

2 布局

由于是动态布局,所以建议使用Canvas,xaml界面十分简洁,除了一个刷新按钮,剩下的就只有画布了。

相关文章

膜派(Mopair)

不懂就问。关于旷野之息的DLC。请教前辈。

泰坦陨落2剧情多少小时完成任务?《泰坦陨落2》单人剧情时长介绍

西游记未解之谜:为什么所有妖怪都要吃唐僧肉,因为唐僧肉是一种稀有资源