C # imagebox inheritance for card game
I am tying a card game for my OOP post. The game itself is up to spec, but now I play with it for my own fun and learning.
I have a map class that contains both an image, its rank and a set. In the GUI, I use fields to display the image stored in the respective cards (which are stored in an array of cards in the deck class), so
cardPictureBox1.Image = card1.show(); cardPictureBox2.Image = card2.show(); cardPictureBox3.Image = card3.show(); ... etc
Is it possible for the card class to inherit the PictureBox control so that there is "actually" an instance of the card calss on the screen (and not the field that stores its value), which would drastically reduce the number of hoops it would have to skip to get maps of other relevant information.
source to share
Instead of a Card class, you can create a UserControl (called 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 the 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 now have all the properties of the PictureBox (including the image in which you will store the bitmap of the card). When you build your project, the compiler will depend on the line that refers to AutoScaleMode - just remove that line and rebuild.
Then you can add any additional properties and methods that the cards require, such as suit and rank and bitmaps for the front and back of the card (the back can be static for all cards to share) and possibly a flip () to toggle between the front and back images.
When it comes to OOP, the forgotten part of the holy trinity seems to be encapsulation. In this case, since the map is a visual element in the user interface that the user interacts with, it makes sense to encapsulate it as a UserControl. As you may have noticed, this will simplify the application and make your life easier.
source to share
Is it possible to make the map class inherit from PictureBox so that the screen "actually" is an instance of the map (and not the box in which the value of the image), which drama reduces the number of hoops one would need to go to get the map for other relevant information.
Yes, but want to? Not. It combines your business logic and is very tightly coupled to your GUI.
If you are writing an OOP document, you must encourage good programming habits and discourage sharp corners. It would be better if you kicked out fast MVC so that your GUI is automatically updated every time you make changes to your model.
source to share