c# blackjack graphics

posted in phil67rpg's Blog
Published February 24, 2019
Advertisement

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)
        {

        }
    }
}

 

Previous Entry c# console snake game
Next Entry c# bug invaders
0 likes 3 comments

Comments

duke_meister

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.

 

February 24, 2019 08:16 AM
phil67rpg

thanks duke but I think am going to work on a console based blackjack game.

February 26, 2019 06:16 PM
8Observer8

Use modern shader OpenGL 3+ instead of GDI+. In this case you will learn how to use C#/OpenGL with writing these simple games. It is more interesting to understand how shaders works. If the shaders in complicated you to start and you afraid them you can start with deprecated/legacy OpenGL 1. It is the excellent book to start from scratch: C# Game Programming. Do not spend your time on GDI+. We are humans and we have very short life. Start with shaders and you can use them in Unity soon, because GLSL and HLSL are very similar. It is very important to study how to write shaders. You can use GLSL with Vulkan too.

You need only understand:

  • how vertex and fragment shaders work
  • basic of linear algebra (scalar product, vector product, how to multiply matrix on vector)
  • how to load textures
  • how move/rotate/scale rectangles with textures using matrices from OpenTK Math Library

I can help you. You can ask me any questions about shader OpenGL with version 3+. You can find a lot of very simple tutorials and books about how shaders works in the Internet. It is not very complicated as you can think. It seems complicated in the first look.

I began a new project special for you. You can continue it. I wrote only how to draw a few images on the window. This is WinForms + shader OpenGL 3.1: Blackjack_WinFormsOpenGL31CSharp.zip

Blackjack_TwoCards.png.ef5226f5078b74398a9d7885b556a3c6.png

February 27, 2019 07:13 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

asteroids wars

2701 views

plane game

3444 views

rgg platformer

2081 views

win 32 pong

2562 views

bug invaders

2231 views

c# book

2510 views

c# bug invaders

2168 views

c# console snake game

23953 views
Advertisement