I am starting a blackjack game using c# graphics. here is my starting code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
}
public static List<T> Shuffle<T>(List<T> list)
{
Random rnd = new Random(DateTime.Now.Millisecond);
var randomised = list.Select(item => new { item, order = rnd.Next() })
.OrderBy(x => x.order)
.Select(x => x.item)
.ToList();
return randomised;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Bitmap bitmap_ace_club = new Bitmap("c01.bmp", true);
Bitmap bitmap_two_club = new Bitmap("c02.bmp", true);
Bitmap bitmap_three_club = new Bitmap("c03.bmp", true);
Bitmap bitmap_four_club = new Bitmap("c04.bmp", true);
Bitmap bitmap_five_club = new Bitmap("c05.bmp", true);
Bitmap bitmap_six_club = new Bitmap("c06.bmp", true);
Bitmap bitmap_seven_club = new Bitmap("c07.bmp", true);
Bitmap bitmap_eight_club = new Bitmap("c08.bmp", true);
Bitmap bitmap_nine_club = new Bitmap("c09.bmp", true);
Bitmap bitmap_ten_club = new Bitmap("c10.bmp", true);
Bitmap bitmap_jack_club = new Bitmap("c11.bmp", true);
Bitmap bitmap_queen_club = new Bitmap("c12.bmp", true);
Bitmap bitmap_king_club = new Bitmap("c13.bmp", true);
Bitmap bitmap_ace_diamond = new Bitmap("d01.bmp", true);
Bitmap bitmap_two_diamond = new Bitmap("d02.bmp", true);
Bitmap bitmap_three_diamond = new Bitmap("d03.bmp", true);
Bitmap bitmap_four_diamond = new Bitmap("d04.bmp", true);
Bitmap bitmap_five_diamond = new Bitmap("d05.bmp", true);
Bitmap bitmap_six_diamond = new Bitmap("d06.bmp", true);
Bitmap bitmap_seven_diamond = new Bitmap("d07.bmp", true);
Bitmap bitmap_eight_diamond = new Bitmap("d08.bmp", true);
Bitmap bitmap_nine_diamond = new Bitmap("d09.bmp", true);
Bitmap bitmap_ten_diamond = new Bitmap("d10.bmp", true);
Bitmap bitmap_jack_diamond = new Bitmap("d11.bmp", true);
Bitmap bitmap_queen_diamond = new Bitmap("d12.bmp", true);
Bitmap bitmap_king_diamond = new Bitmap("d13.bmp", true);
Bitmap bitmap_ace_heart = new Bitmap("h01.bmp", true);
Bitmap bitmap_two_heart = new Bitmap("h02.bmp", true);
Bitmap bitmap_three_heart = new Bitmap("h03.bmp", true);
Bitmap bitmap_four_heart = new Bitmap("h04.bmp", true);
Bitmap bitmap_five_heart = new Bitmap("h05.bmp", true);
Bitmap bitmap_six_heart = new Bitmap("h06.bmp", true);
Bitmap bitmap_seven_heart = new Bitmap("h07.bmp", true);
Bitmap bitmap_eight_heart = new Bitmap("h08.bmp", true);
Bitmap bitmap_nine_heart = new Bitmap("h09.bmp", true);
Bitmap bitmap_ten_heart = new Bitmap("h10.bmp", true);
Bitmap bitmap_jack_heart = new Bitmap("h11.bmp", true);
Bitmap bitmap_queen_heart = new Bitmap("h12.bmp", true);
Bitmap bitmap_king_heart = new Bitmap("h13.bmp", true);
Bitmap bitmap_ace_spade = new Bitmap("s01.bmp", true);
Bitmap bitmap_two_spade = new Bitmap("s02.bmp", true);
Bitmap bitmap_three_spade = new Bitmap("s03.bmp", true);
Bitmap bitmap_four_spade = new Bitmap("s04.bmp", true);
Bitmap bitmap_five_spade = new Bitmap("s05.bmp", true);
Bitmap bitmap_six_spade = new Bitmap("s06.bmp", true);
Bitmap bitmap_seven_spade = new Bitmap("s07.bmp", true);
Bitmap bitmap_eight_spade = new Bitmap("s08.bmp", true);
Bitmap bitmap_nine_spade = new Bitmap("s09.bmp", true);
Bitmap bitmap_ten_spade = new Bitmap("s10.bmp", true);
Bitmap bitmap_jack_spade = new Bitmap("s11.bmp", true);
Bitmap bitmap_queen_spade = new Bitmap("s12.bmp", true);
Bitmap bitmap_king_spade = new Bitmap("s13.bmp", true);
List<int> cards = new List<int>(52);
for (int i = 0; i < 52; i++)
{
cards.Add(i);
}
cards = Shuffle(cards);
if (e.Button == MouseButtons.Left)
{
Graphics g1 = this.CreateGraphics();
g1.DrawImage(bitmap_king_spade, 360, 20);
g1.Dispose();
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
}
}
}
Initial suggestions off the top of my head.
Don't use just int for the cards. Have a Card class containing index from 1-52, also the suit and value.
Have the bitmaps in a List or array, then get the right one for the Card by the card's index. You can't refer to them easily as individual objects.
Make the bitmap list a field or property. Don't initialise them in MouseDown, but in form1_Load() or wherever.
Do your drawing in Paint()
Use the using() pattern so you don't have to remember to dispose().
Good luck.
PS: when submitting code, use the "C, C++, C# ..." dropdown option to format it nicely.