时间:2019-03-17 标签:c#pictureboxinheritanceforcardgame

发布于 2024-08-05 04:04:37 字数 379 浏览 4 评论 0原文

我正在为 OOP 简介论文编写纸牌游戏。游戏本身符合规格,但我现在玩它是为了自己的满足和学习。

我有一个卡片类,其中包含图像、其等级和套件。在 GUI 上,我使用图片框来显示存储在各个卡片中的图像(存储在牌组类的卡片数组中),因此,

cardPictureBox1.Image = card1.show();
cardPictureBox2.Image = card2.show();
cardPictureBox3.Image = card3.show();
...
etc

是否可以使卡片类继承 PictureBox 控件,那么什么是 -屏幕“实际上”是卡片类的一个实例(而不是保存其图像值的盒子),这将大大减少人们为获取卡片其他相关信息而必须跳过的圈数。

I'm woking on a card game for my Intro to OOP paper. The game itself meets the specifications but I am now playing around with it for my own gratification and learning.

I have a card class that contains and image, its rank and suite. On the GUI I use picture boxes to display the image stored in the respective cards (which are stored in a card array in the deck class) so,

cardPictureBox1.Image = card1.show();
cardPictureBox2.Image = card2.show();
cardPictureBox3.Image = card3.show();
...
etc

Would it be possible to make the card class an inherit the PictureBox control so what is on-screen is "actually" an instance of the card calss (rather than a box that hold the image value of it) which would dramaticall reduce he amount of hoops one would have to leap through to attain the cards other relevant info.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转角预定愛 2024-08-12 04:04:37

您可以创建一个继承自 PictureBox(而不是继承自 UserControl)的 UserControl(命名为 Card 或 ucCard 或其他名称),而不是使用 Card 类。在 C# 中执行此操作的最简单方法是添加具有所需名称的 UserControl,然后在代码中将顶行从 更改

public partial class ucCard : UserControl

public partial class ucCard : PictureBox

您的 ucCard 控件将具有 PictureBox 的所有属性(包括 Image,您将在其中存储卡的位图)。当您构建项目时,编译器将在引用 AutoScaleMode 的行上吐出 - 只需删除该行并重建即可。

然后,您可以添加卡片所需的任何其他属性和方法,例如卡片正面和背面的花色和排名以及位图(背面可以是静态的,以便所有卡片都共享它),也许还可以添加 Flip() 方法在正面和背面图像之间切换。

就 OOP 而言,神圣三位一体中被遗忘的部分似乎是封装。在这种情况下,由于卡片是 ​​UI 中与用户交互的视觉元素,因此将其封装为 UserControl 是非常有意义的。正如您已经注意到的,这将使应用程序更简单,您的生活更轻松。

Instead of having a Card class, you can create a UserControl (named Card or ucCard or whatever) that inherits from PictureBox (instead of inheriting from UserControl). The easiest way to do this in C# is to add a UserControl with your desired name, and then in the code change the top line from

public partial class ucCard : UserControl

to

public partial class ucCard : PictureBox

Your ucCard control will then have all the properties of a PictureBox (including Image, where you would store the card's Bitmap). When you build your project, the compiler will barf on a line that references AutoScaleMode - just delete this line and rebuild.

You can then add any additional properties and methods the cards require, like suit and rank and Bitmaps for the front and back of the card (the back could be static so that all cards would share it), and maybe a Flip() method to switch between the front and back images.

As far as OOP goes, the forgotten part of the holy trinity seems to be Encapsulation. In this case, since a card is a visual element in the UI that the user interacts with, it makes perfect sense to encapsulate it as a UserControl. As you've already noticed, this will make the application simpler and your life easier.

梦纸 2024-08-12 04:04:37

可以办卡吗
类继承PictureBox
控制所以屏幕上显示的是
“实际上”是该卡的一个实例
类(而不是一个容纳
它的图像值)这将
大大减少了圈的数量
一个人必须跳到
获取卡片的其他相关信息。

是的,但是你愿意吗?不会。它将您的业务逻辑和模型与 GUI 层紧密耦合。

如果你正在写一篇关于 OOP 的论文,你应该鼓励良好的编程习惯并阻止偷工减料。最好创建一个快速的 MVC,这样只要您对模型进行更改,您的 GUI 就会自动更新。

Would it be possible to make the card
class an inherit the PictureBox
control so what is on-screen is
"actually" an instance of the card
calss (rather than a box that hold the
image value of it) which would
dramaticall reduce he amount of hoops
one would have to leap through to
attain the cards other relevant info.

Yes, but would you want to? No. It couples your business logic and model very tightly to your GUI layer.

If you're writing a paper on OOP, you should encourage good programming habits and discourage cutting corners. It would be better for you to whip a quick MVC, so that your GUI automagically updates itself whenever you make a change to your model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文