VS2022C#winform编程跨类使用控件操作方法
namespace DemoGo{
/// <summary>
/// 游戏类
/// </summary>
internal class Game
{
private const int GameWidth = 1583;
private const int GameHeight = 961;
//手牌和牌堆
private List<string> deck;
private Player player1;
private Player player2;
/// <summary>
/// 绘制当前游戏的场景
/// </summary>
/// <param name="g">绘图句柄</param>
/// <param name="size">游戏区域尺寸</param>
public void Draw(Graphics g,Size size)
{
//定义空白的图
Image img = new Bitmap(GameWidth, GameHeight);
Graphics _g=Graphics.FromImage(img);
Rectangle rec = new Rectangle(new Point(0, 0), new Size(GameWidth, GameHeight));
_g.DrawImage(Properties.Resources.背景图片, rec);
// 绘制曹操的武器
Rectangle weaponRect1 = new Rectangle(new Point(626, 358), new Size(263, 48));
_g.DrawImage(Properties.Resources.卡槽, weaponRect1);
// 绘制张飞的武器
Rectangle weaponRect2 = new Rectangle(new Point(1128, 586), new Size(263, 48));
_g.DrawImage(Properties.Resources.卡槽, weaponRect2);
//绘制曹操
Rectangle caocaoRect = new Rectangle(new Point(626, 35), new Size(263, 327));
_g.DrawImage(Properties.Resources.曹操, caocaoRect);
//绘制张飞
Rectangle zhangfeiRect = new Rectangle(new Point(1128, 634), new Size(263, 327));
_g.DrawImage(Properties.Resources.张飞, zhangfeiRect);
// 绘制曹操的血量
Rectangle healthRect1 = new Rectangle(new Point(626, 262), new Size(22, 100));
_g.DrawImage(Properties.Resources._4血4, healthRect1);
// 绘制张飞的血量
Rectangle healthRect2 = new Rectangle(new Point(1128, 855), new Size(22, 100));
_g.DrawImage(Properties.Resources._4血4, healthRect2);
//绘制中央卡槽
Rectangle centerRect = new Rectangle(new Point(675, 421), new Size(165, 233));
_g.DrawImage(Properties.Resources.卡槽, centerRect);
//绘制七个张飞手牌卡槽
for (int i = 0; i < 7; i++)
{
Rectangle cardRect = new Rectangle(new Point(i * 161, 725), new Size(165, 233));
_g.DrawImage(Properties.Resources.卡槽, cardRect);
}
// 绘制玩家手牌
for (int i = 0; i < player1.Hand.Count; i++)
{
string cardName = player1.Hand;
Image cardImage = Properties.Resources.ResourceManager.GetObject(cardName) as Image;
Rectangle cardRect = new Rectangle(new Point(i * 161, 725), new Size(165, 233));
_g.DrawImage(cardImage, cardRect);
}
g.DrawImage(img, new Rectangle(new Point(0, 0), size));
}
public Game()
{
// 创建并洗牌
deck = CreateAndShuffleDeck();
// 创建玩家
player1 = new Player("张飞", 4, "咆哮", int.MaxValue, "无");
player2 = new Player("曹操", 4, "奸雄", 1, "无");
}
public void StartGame()
{
DrawCards(player1, 4);
DrawCards(player2, 4);
}
private List<string> CreateAndShuffleDeck()
{
List<string> deck = new List<string>
{
// 5张桃:基本牌,出牌阶段,你可以使用一张桃,回复1点体力
"桃","桃", "桃","桃", "桃",
// 20张杀,不区分红色和黑色:基本牌,出牌阶段限一次,对敌方角色使用,若其不打出闪响应,则受到1点伤害
"杀", "杀", "杀","杀", "杀", "杀","杀", "杀", "杀","杀", "杀", "杀","杀", "杀", "杀","杀", "杀", "杀","杀", "杀",
// 10张闪:基本牌,在回合外抵消杀对你造成的效果
"闪", "闪", "闪","闪", "闪", "闪","闪", "闪", "闪","闪",
// 1张南蛮入侵:锦囊牌,出牌阶段,对敌方角色使用,若其不打出杀响应,则受到1点伤害
"南蛮入侵",
// 1张万箭齐发:锦囊牌,出牌阶段,对敌方角色使用,若其不打出闪响应,则受到1点伤害
"万箭齐发",
// 2张无中生有:锦囊牌,出牌阶段,你摸2张牌
"无中生有","无中生有",
// 2张决斗:锦囊牌,出牌阶段,敌方和你依次打出杀,先不能打出杀的角色受到1点伤害
"决斗","决斗",
// 3张过河拆桥:锦囊牌,出牌阶段,弃置敌方的一张牌
"过河拆桥","过河拆桥","过河拆桥",
// 3张顺手牵羊:锦囊牌,出牌阶段,获得敌方的一张牌
"顺手牵羊","顺手牵羊","顺手牵羊",
// 3张无懈可击:锦囊牌,抵消一张锦囊牌的效果
"无懈可击","无懈可击","无懈可击",
//1张诸葛连弩:武器牌,你可以无限出杀
"诸葛连弩",
// 1张贯石斧:武器牌,当敌方使用闪响应你的杀时,你可以弃2张牌强制对其造成伤害
"贯石斧"
};
// 洗牌
Shuffle(deck);
return deck;
}
private void Shuffle<T>(List<T> list)
{
Random random = new Random();
int n = list.Count;
// 使用Fisher-Yates算法洗牌,即通过迭代数组中的每个元素,将其与数组中随机位置的元素交换,可以确保每个元素在交换过程中都有均等的机会出现在任何位置
while (n > 1)
{
n--;
int k = random.Next(n + 1);
T value = list;
list = list;
list = value;
}
}
private void DrawCards(Player player, int numCards)
{
for (int i = 0; i < numCards; i++)
{
if (deck.Any())
{
string dealtCard = deck.Last();
deck.RemoveAt(deck.Count - 1);
player.Hand.Add(dealtCard);
}
else
{
deck = CreateAndShuffleDeck();
}
}
}
}
}
using static DemoGo.ResizeHelper;
namespace DemoGo
{
public partial class FrmMain : Form
{
private ResizeHelper resizeHelper;
private Sound sound;
//游戏对象说明
private Game _game;
public FrmMain()
{
InitializeComponent();
resizeHelper = new ResizeHelper();
resizeHelper.controlInfoDict = new Dictionary<Control, ControlInfo>();
sound = new Sound();
}
private void FrmMain_Load(object sender, EventArgs e)
{
//初始化游戏类
_game = new Game();
resizeHelper.X = this.Width;
resizeHelper.Y = this.Height;
resizeHelper.StoreControlInfo(this);
}
private void FrmMain_Resize(object sender, EventArgs e)
{
double newx = (this.Width) / resizeHelper.X;
double newy = (this.Height) / resizeHelper.Y;
resizeHelper.SetControls(newx, newy, this);
}
private void gameArea_Paint(object sender, PaintEventArgs e)
{
//游戏已经初始化了
if (_game != null)
{
_game.Draw(e.Graphics, this.gameArea.Size);
}
}
private void btnAboutMe_Click(object sender, EventArgs e)
{
FrmAboutMe frm = new FrmAboutMe();
frm.ShowDialog();
}
private void btnIntroduction_Click(object sender, EventArgs e)
{
FrmAboutGame frm = new FrmAboutGame();
frm.ShowDialog();
}
private void btnPlay_Click(object sender, EventArgs e)
{
sound.Play();
}
private void btnPause_Click(object sender, EventArgs e)
{
sound.Pause();
}
private void btnSwitch_Click(object sender, EventArgs e)
{
sound.SwitchTrack();
}
private void btnStartGame_Click(object sender, EventArgs e)
{
//重置游戏
_game = new Game(); ;
this.gameArea.Invalidate();
//开始新的游戏
_game.StartGame();
}
}
}
本人大二学生,正在编写三国杀大作业。我在FrmMain的设计界面放了一个textbox控件叫txtPrompt,用来显示游戏过程中的提示信息,现在想在Game类中实现如下效果:点击任何一张用Draw方法实时绘制出来的图片,能在txtPrompt中显示出该图片的相关信息,此处的相关信息要完全可以自由定义。根据老师要求,要尽可能减少对原有代码的改动,还要把游戏相关内容都放在Game类中,FrmMain类中的内容越少越好,这两个类在一个命名空间下,但不在同一个文件中,已有代码如上,请问应该如何处理?
页:
[1]