none
Figuren bewegen sich nicht (WinForms) RRS feed

  • Frage

  • Hallo Leute,

    folgender Code soll, ohne Prüfung auf Korrektheit, Figuren bewegen. Es bewegt sich aber nix. Warum? Der relevante Code dürfte in diesem Fall auf die  Methode PickOrDropPiece(MouseEventArgs e) beschränken. Ich poste dennoch das ganze Projekt mitsamt aller Klassen

    Form1.cs

    using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace ChessGUI { public partial class Form1 : Form {

    public Form1() { InitializeComponent(); pictureBox1.MouseDown += PictureBox1_MouseDown_1; } private Board Board { get; set; } private Piece CurrentPiece { get; set; } private Dictionary<Piece, Bitmap> PieceBitmaps { get; set; } private int TileWidth { get; set; } private int TileHeight { get; set; } private void Form1_Load(object sender, EventArgs e) { InitializeGame(); DrawGame(); } private void InitializeGame() { TileWidth = 64; TileHeight = 64; Board = new Board(); PieceBitmaps = new Dictionary<Piece, Bitmap>(); PieceBitmaps.Add(new Piece(PieceType.Pawn, PieceColor.Black), new Bitmap("pawnblack.png")); PieceBitmaps.Add(new Piece(PieceType.Pawn, PieceColor.White), new Bitmap("pawnwhite.png")); } private void DrawGame() { var tileSize = new Size(TileWidth, TileHeight); Bitmap bitmap = CreateBoard(tileSize); DrawPieces(bitmap); pictureBox1.Image = bitmap; } private Bitmap CreateBoard(Size tileSize) { int tileWidth = tileSize.Width; int tileHeight = tileSize.Height; var bitmap = new Bitmap(tileWidth * 8, tileHeight * 8); using (Graphics graphics = Graphics.FromImage(bitmap)) { for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { Brush brush = (x % 2 == 0 && y % 2 == 0) || (x % 2 != 0 && y % 2 != 0) ? Brushes.Black : Brushes.White; graphics.FillRectangle(brush, new Rectangle(x * tileWidth, y * tileHeight, tileWidth, tileHeight)); } } } return bitmap; } private void DrawPieces(Bitmap bitmap) { using (Graphics graphics = Graphics.FromImage(bitmap)) { Board board = Board; for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { Piece piece = board.GetPiece(x, y); if (piece != null) { Bitmap bitmap1 = PieceBitmaps[piece]; graphics.DrawImageUnscaled(bitmap1, new Point(x * TileWidth, y * TileHeight)); } } } } } private void PickOrDropPiece(MouseEventArgs e) { Point location = e.Location; int x = location.X / TileWidth; int y = location.Y / TileHeight; bool pickOrDrop = CurrentPiece == null; if (pickOrDrop) { // Pick a piece Piece piece = Board.GetPiece(x, y); Board.SetPiece(x, y, null); if (piece != null) { label1.Text = string.Format("You picked a {0} {1} at location {2},{3}", piece.Color, piece.Type, x,y); } else { label1.Text = "Nothing there !"; } CurrentPiece = piece; } else { // Drop picked piece Board.SetPiece(x, y, CurrentPiece); label1.Text = string.Format("You dropped a {0} {1} at location {2},{3}", CurrentPiece.Color,CurrentPiece.Type, x,y); CurrentPiece = null; } } private void PictureBox1_MouseDown_1(object sender, MouseEventArgs e) { PickOrDropPiece(e); DrawGame(); } } }


    Board.cs

    namespace ChessGUI {
        public class Board {
            private readonly Piece[] _pieces;
    
            public Board() {
                _pieces = new Piece[8 * 8];
                PopulatePieces();
            }
    
            public Piece GetPiece(int x, int y) {
                int i = y * 8 + x;
                return _pieces[i];
            }
    
            public void SetPiece(int x, int y, Piece piece) {
                int i = y * 8 + x;
                _pieces[i] = piece;
            }
    
            private void PopulatePieces() {
                for (int i = 0; i < 8; i++) {
                    SetPiece(i, 1, new Piece(PieceType.Pawn, PieceColor.Black));
                    SetPiece(i, 6, new Piece(PieceType.Pawn, PieceColor.White));
                }
            }
        }
    }

    Piece.cs

    namespace ChessGUI {
        public enum PieceType {
            Pawn
        }
    
        public enum PieceColor {
            Black,
            White
        }
        public class Piece {
            private readonly PieceColor _color;
            private readonly PieceType _type;
    
            public Piece(PieceType type, PieceColor color) {
                _type = type;
                _color = color;
            }
    
            public PieceType Type {
                get { return _type; }
            }
    
            public PieceColor Color {
                get { return _color; }
            }
    
            protected bool Equals(Piece other) {
                return _color == other._color && _type == other._type;
            }
    
            public override bool Equals(object obj) {
                if (ReferenceEquals(null, obj)) return false;
                if (ReferenceEquals(this, obj)) return true;
                if (obj.GetType() != GetType()) return false;
                return Equals((Piece)obj);
            }
    
            public override int GetHashCode() {
                unchecked {
                    return ((int)_color * 397) ^ (int)_type;
                }
            }
    
            public static bool operator ==(Piece left, Piece right) {
                return Equals(left, right);
            }
    
            public static bool operator !=(Piece left, Piece right) {
                return !Equals(left, right);
            }
        }
    }

    Form1.Designer.cs

    namespace ChessGUI {
        partial class Form1 {
            /// <summary>
            /// Erforderliche Designervariable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Verwendete Ressourcen bereinigen.
            /// </summary>
            /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
            protected override void Dispose(bool disposing) {
                if (disposing && (components != null)) {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Vom Windows Form-Designer generierter Code
    
            /// <summary>
            /// Erforderliche Methode für die Designerunterstützung.
            /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
            /// </summary>
            private void InitializeComponent() {
                this.pictureBox1 = new System.Windows.Forms.PictureBox();
                this.label1 = new System.Windows.Forms.Label();
                ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
                this.SuspendLayout();
                // 
                // pictureBox1
                // 
                this.pictureBox1.Location = new System.Drawing.Point(198, 23);
                this.pictureBox1.Name = "pictureBox1";
                this.pictureBox1.Size = new System.Drawing.Size(600, 600);
                this.pictureBox1.TabIndex = 0;
                this.pictureBox1.TabStop = false;
                this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseDown_1);
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(12, 610);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(35, 13);
                this.label1.TabIndex = 1;
                this.label1.Text = "label1";
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(923, 633);
                this.Controls.Add(this.label1);
                this.Controls.Add(this.pictureBox1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.PictureBox pictureBox1;
            private System.Windows.Forms.Label label1;
        }
    }





    • Bearbeitet tklustig Sonntag, 23. Juni 2019 20:05
    Sonntag, 23. Juni 2019 19:55

Antworten

  • Mit der "globalen" boolschen Variable haveTouched klappt's

            private void PickPiece(MouseEventArgs e) {
                Point location = e.Location;
                int x = location.X / Feldbreite;
                int y = location.Y / Feldhoehe;
                bool pickOrDrop = CurrentPiece == null;
                if (pickOrDrop) {
                    // Pick a piece
                    Piece piece = Board.GetPiece(x, y);
                    Board.SetPiece(x, y, null);
                    if (piece != null) {
                        haveTouched = true;
                        lblZug.Text = string.Format("You picked a {0} {1} at location {2},{3}", piece.Color, piece.Type, x + 1, y + 1);
                    } else {
                        lblZug.Text = "Nothing there !";
                        haveTouched = false;
                    }
                    CurrentPiece = piece;
                }
            }
    
            private void DropPiece(MouseEventArgs e) {
                //Drop, if piece is available
                if (haveTouched) {
                    Point location = e.Location;
                    int x = location.X / Feldbreite;
                    int y = location.Y / Feldhoehe;
                    Board.SetPiece(x, y, CurrentPiece);
                    lblZug.Text = string.Format("You dropped a {0} {1} at location {2},{3}", CurrentPiece.Color, CurrentPiece.Type, x + 1, y + 1);
                    CurrentPiece = null;
                }
            }


    • Bearbeitet tklustig Sonntag, 23. Juni 2019 21:00
    • Als Antwort markiert tklustig Sonntag, 23. Juni 2019 21:01
    Sonntag, 23. Juni 2019 21:00