0

In the approach below, everything works fine, the logic is processed well by the program, but... It loads my images "with background". The "board" image is displayed, followed by the "pawns" images. And these pawns load with the background (despite setting pawn.BackColor = Color.Transparent;)

    public partial class Form1 : Form
    {

        /////fields
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitializeBoard();
            InitializeDice();
            InitializeWhosTurnLabel();
            InitializePawns();
            RunGameLoop();
        }

        private void InitializeBoard()
        {
            board.ImageLocation = @"C:\..\board.jpg";
            board.SizeMode = PictureBoxSizeMode.StretchImage;
            board.Size = new Size(500, 500);
            board.Location = new Point(0, 0);
            this.Controls.Add(board);
        }

        private void InitializeDice()
        {
            dice.Size = new Size(150, 150);
            dice.Location = new Point(550, 200);
            this.Controls.Add(dice);
            diceCount = random.Next(1, 7);
        }

        private void InitializeWhosTurnLabel()
        {
            whosTurn.Size = new Size(500, 250);
            whosTurn.Location = new Point(550, 10);
            this.Controls.Add(whosTurn);
        }

        private void InitializePawns()
        {
            allPawns.Add(LoadPawns("blue", 72, 130, 360, 415));
            allPawns.Add(LoadPawns("yellow", 418, 360, 360, 415));
            allPawns.Add(LoadPawns("green", 418, 360, 70, 125));
            allPawns.Add(LoadPawns("red", 72, 130, 70, 125));

            // Dodaj pionki do Controls formularza
            foreach (var pawns in allPawns)
            {
                foreach (Pawn pawn in pawns)
                {
                    this.Controls.Add(pawn);
                    pawn.Click += Pawn_Click; 
                    pawn.BringToFront();
                }
            }
        }

        private List<Pawn> LoadPawns(string color, int x1, int x2, int y1, int y2)
        {
            List<Pawn> pawns = new List<Pawn>();
            Image image = Image.FromFile($@"C:\..\{color}_pawn.png");

            
            for (int i = 0; i < 4; i++)
            {
                Point location;
                switch (i)
                {
                    case 0:
                        location = new Point(x1, y1);
                        break;
                    case 1:
                        location = new Point(x2, y1);
                        break;
                    case 2:
                        location = new Point(x1, y2);
                        break;
                    case 3:
                        location = new Point(x2, y2);
                        break;
                    default:
                        throw new Exception("Invalid pawn index");
                }
                Pawn pawn = new Pawn(image, color, location);
                
                pawns.Add(pawn);
            }

            return pawns;  
        }

        private void Pawn_Click(object sender, EventArgs e)
        {
            Pawn selectedPawn = (Pawn)sender;

...
        }

In this approach, the images actually load ok, but the entire program logic falls apart. Additionally, when I click pawn, nothing happens...

namespace ludo_game
{
    public partial class Form1 : Form
    {

        /////fields

        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitializeBoard();
            InitializeDice();
            InitializeWhosTurnLabel();
            InitializePawns();
            RunGameLoop();
        }

        private void InitializeBoard()
        {
            board.Image = Image.FromFile(@"C:\..\board.jpg");
            board.SizeMode = PictureBoxSizeMode.StretchImage;
            board.Size = new Size(500, 500);
            board.Location = new Point(0, 0);
            board.Paint += new PaintEventHandler(Board_Paint);
            this.Controls.Add(board);
        }

        private void InitializeDice()
        {
            dice.Size = new Size(150, 150);
            dice.Location = new Point(550, 200);
            this.Controls.Add(dice);
            diceCount = random.Next(1, 7);
        }

        private void InitializeWhosTurnLabel()
        {
            whosTurn.Size = new Size(500, 250);
            whosTurn.Location = new Point(550, 10);
            this.Controls.Add(whosTurn);
        }

        private void InitializePawns()
        {
            allPawns.Add(LoadPawns("blue", 72, 130, 360, 415));
            allPawns.Add(LoadPawns("yellow", 418, 360, 360, 415));
            allPawns.Add(LoadPawns("green", 418, 360, 70, 125));
            allPawns.Add(LoadPawns("red", 72, 130, 70, 125));

            board.Invalidate(); 
        }

        private List<Pawn> LoadPawns(string color, int x1, int x2, int y1, int y2)
        {
            List<Pawn> pawns = new List<Pawn>();
            Image image = Image.FromFile($@"C:\..\{color}_pawn.png");

            
            for (int i = 0; i < 4; i++)
            {
                Point location;
                switch (i)
                {
                    case 0:
                        location = new Point(x1, y1);
                        break;
                    case 1:
                        location = new Point(x2, y1);
                        break;
                    case 2:
                        location = new Point(x1, y2);
                        break;
                    case 3:
                        location = new Point(x2, y2);
                        break;
                    default:
                        throw new Exception("Invalid pawn index");
                }
                Pawn pawn = new Pawn(image, color, location);
                pawns.Add(pawn);
            }

            return pawns;  
        }

        private void Board_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            foreach (var pawnList in allPawns)
            {
                foreach (var pawn in pawnList)
                {
                    g.DrawImage(pawn.Image, new Rectangle(pawn.Location, new Size(10, 16)));
                }
            }
        }

        private void Pawn_Click(object sender, EventArgs e)
        {
            Pawn selectedPawn = (Pawn)sender;

...
        }

How can I combine these two approaches? Or fix any of them?

1
  • Please add the part of Pawn definition in the code.
    – Fei Xu
    Commented Jul 11 at 1:21

1 Answer 1

0

Here are some suggestions for improving your code:

  1. Define some objects used in the instance in the field section of the class like this:

    // Fields
     private PictureBox board;
     private PictureBox dice;
     private Label whosTurn;
     private List<List<Pawn>> allPawns = new List<List<Pawn>>();
     private Random random = new Random();
     private int diceCount;
     private System.ComponentModel.IContainer components = null;
    
  2. Make initialization operations correctly in the InitializeComponent method or in the constructor of the class as following:

private void InitializeBoard()
{
    board = new PictureBox();
    board.Image = Image.FromFile($"board.png");
    board.SizeMode = PictureBoxSizeMode.StretchImage;
    board.Size = new Size(500, 500);
    board.Location = new Point(0, 0);
    board.Paint += new PaintEventHandler(Board_Paint);
    this.Controls.Add(board);
}

private void InitializeDice()
{
    dice = new PictureBox(); 
    dice.Size = new Size(150, 150);
    dice.Location = new Point(550, 200);
    this.Controls.Add(dice);
    diceCount = random.Next(1, 7);
}

private void InitializeWhosTurnLabel()
{
    whosTurn = new Label();
    whosTurn.Size = new Size(500, 250);
    whosTurn.Location = new Point(550, 10);
    this.Controls.Add(whosTurn);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.