Không có gì tuyệt vời hơn là luyện tập với ví dụ thực tế. Nào cùng nhau thử thách bản thân với trò chơi huyền thoại: Game Caro.
Code file Form1.designer.cs
namespace GameCaro
{
partial class Form1
{
private System.ComponentModel.IContainer components = null ;
protected override void Dispose (bool disposing)
{
if (disposing && (components != null ))
{
components.Dispose();
}
base .Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent ()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof (Form1));
this .pnlChessBoard = new System.Windows.Forms.Panel();
this .panel2 = new System.Windows.Forms.Panel();
this .pctbAvatar = new System.Windows.Forms.PictureBox();
this .panel3 = new System.Windows.Forms.Panel();
this .btnLAN = new System.Windows.Forms.Button();
this .txbIP = new System.Windows.Forms.TextBox();
this .pctbMark = new System.Windows.Forms.PictureBox();
this .prcbCoolDown = new System.Windows.Forms.ProgressBar();
this .txbPlayerName = new System.Windows.Forms.TextBox();
this .label1 = new System.Windows.Forms.Label();
this .panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this .pctbAvatar)).BeginInit();
this .panel3.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this .pctbMark)).BeginInit();
this .SuspendLayout();
this .pnlChessBoard.BackColor = System.Drawing.SystemColors.Control;
this .pnlChessBoard.Location = new System.Drawing.Point(12 , 12 );
this .pnlChessBoard.Name = "pnlChessBoard" ;
this .pnlChessBoard.Size = new System.Drawing.Size(594 , 520 );
this .pnlChessBoard.TabIndex = 0 ;
this .panel2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this .panel2.Controls.Add(this .pctbAvatar);
this .panel2.Location = new System.Drawing.Point(612 , 12 );
this .panel2.Name = "panel2" ;
this .panel2.Size = new System.Drawing.Size(269 , 269 );
this .panel2.TabIndex = 1 ;
this .pctbAvatar.BackColor = System.Drawing.SystemColors.Control;
this .pctbAvatar.BackgroundImage = global::GameCaro.Properties.Resources.caro;
this .pctbAvatar.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this .pctbAvatar.Location = new System.Drawing.Point(3 , 3 );
this .pctbAvatar.Name = "pctbAvatar" ;
this .pctbAvatar.Size = new System.Drawing.Size(263 , 263 );
this .pctbAvatar.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this .pctbAvatar.TabIndex = 0 ;
this .pctbAvatar.TabStop = false ;
this .panel3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this .panel3.BackColor = System.Drawing.SystemColors.Control;
this .panel3.Controls.Add(this .label1);
this .panel3.Controls.Add(this .btnLAN);
this .panel3.Controls.Add(this .txbIP);
this .panel3.Controls.Add(this .pctbMark);
this .panel3.Controls.Add(this .prcbCoolDown);
this .panel3.Controls.Add(this .txbPlayerName);
this .panel3.Location = new System.Drawing.Point(612 , 287 );
this .panel3.Name = "panel3" ;
this .panel3.Size = new System.Drawing.Size(269 , 214 );
this .panel3.TabIndex = 2 ;
this .btnLAN.Location = new System.Drawing.Point(3 , 84 );
this .btnLAN.Name = "btnLAN" ;
this .btnLAN.Size = new System.Drawing.Size(140 , 23 );
this .btnLAN.TabIndex = 4 ;
this .btnLAN.Text = "LAN" ;
this .btnLAN.UseVisualStyleBackColor = true ;
this .txbIP.Location = new System.Drawing.Point(3 , 58 );
this .txbIP.Name = "txbIP" ;
this .txbIP.Size = new System.Drawing.Size(140 , 20 );
this .txbIP.TabIndex = 3 ;
this .txbIP.Text = "127.0.0.1" ;
this .pctbMark.BackColor = System.Drawing.SystemColors.Control;
this .pctbMark.Location = new System.Drawing.Point(149 , 3 );
this .pctbMark.Name = "pctbMark" ;
this .pctbMark.Size = new System.Drawing.Size(117 , 104 );
this .pctbMark.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this .pctbMark.TabIndex = 2 ;
this .pctbMark.TabStop = false ;
this .prcbCoolDown.Location = new System.Drawing.Point(3 , 29 );
this .prcbCoolDown.Name = "prcbCoolDown" ;
this .prcbCoolDown.Size = new System.Drawing.Size(140 , 23 );
this .prcbCoolDown.TabIndex = 1 ;
this .txbPlayerName.Location = new System.Drawing.Point(3 , 3 );
this .txbPlayerName.Name = "txbPlayerName" ;
this .txbPlayerName.ReadOnly = true ;
this .txbPlayerName.Size = new System.Drawing.Size(140 , 20 );
this .txbPlayerName.TabIndex = 0 ;
this .label1.AutoSize = true ;
this .label1.Font = new System.Drawing.Font("Elephant" , 20.25 F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte )(0 )));
this .label1.Location = new System.Drawing.Point(3 , 150 );
this .label1.Name = "label1" ;
this .label1.Size = new System.Drawing.Size(254 , 35 );
this .label1.TabIndex = 5 ;
this .label1.Text = "5 in a line to win" ;
this .AutoScaleDimensions = new System.Drawing.SizeF(6 F, 13 F);
this .AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this .ClientSize = new System.Drawing.Size(893 , 549 );
this .Controls.Add(this .panel3);
this .Controls.Add(this .panel2);
this .Controls.Add(this .pnlChessBoard);
this .Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon" )));
this .Name = "Form1" ;
this .Text = "Game Caro LAN" ;
this .panel2.ResumeLayout(false );
((System.ComponentModel.ISupportInitialize)(this .pctbAvatar)).EndInit();
this .panel3.ResumeLayout(false );
this .panel3.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this .pctbMark)).EndInit();
this .ResumeLayout(false );
}
#endregion
private System.Windows.Forms.Panel pnlChessBoard;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.PictureBox pctbAvatar;
private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.Button btnLAN;
private System.Windows.Forms.TextBox txbIP;
private System.Windows.Forms.PictureBox pctbMark;
private System.Windows.Forms.ProgressBar prcbCoolDown;
private System.Windows.Forms.TextBox txbPlayerName;
private System.Windows.Forms.Label label1;
}
}
Code file Cons.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameCaro
{
public class Cons
{
public static int CHESS_WIDTH = 30 ;
public static int CHESS_HEIGHT = 30 ;
public static int CHESS_BOARD_WIDTH = 20 ;
public static int CHESS_BOARD_HEIGHT = 17 ;
}
}
Code ChessBoardManager.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GameCaro
{
public class ChessBoardManager
{
#region Properties
private Panel chessBoard;
public Panel ChessBoard
{
get { return chessBoard; }
set { chessBoard = value ; }
}
private List<Player> player;
public List<Player> Player
{
get { return player; }
set { player = value ; }
}
private int currentPlayer;
public int CurrentPlayer
{
get { return currentPlayer; }
set { currentPlayer = value ; }
}
private TextBox playerName;
public TextBox PlayerName
{
get { return playerName; }
set { playerName = value ; }
}
private PictureBox playerMark;
public PictureBox PlayerMark
{
get { return playerMark; }
set { playerMark = value ; }
}
#endregion
#region Initialize
public ChessBoardManager (Panel chessBoard, TextBox playerName, PictureBox mark)
{
this .ChessBoard = chessBoard;
this .PlayerName = playerName;
this .PlayerMark = mark;
this .Player = new List<Player>()
{
new Player("HowKteam" , Image.FromFile(Application.StartupPath + "\\Resources\\P1.png" )),
new Player("Education" , Image.FromFile(Application.StartupPath + "\\Resources\\P2.png" ))
};
CurrentPlayer = 0 ;
ChangePlayer();
}
#endregion
#region Methods
public void DrawChessBoard ()
{
Button oldButton = new Button() { Width = 0 , Location = new Point(0 , 0 ) };
for (int i = 0 ; i < Cons.CHESS_BOARD_HEIGHT; i++)
{
for (int j = 0 ; j < Cons.CHESS_BOARD_WIDTH; j++)
{
Button btn = new Button()
{
Width = Cons.CHESS_WIDTH,
Height = Cons.CHESS_HEIGHT,
Location = new Point(oldButton.Location.X + oldButton.Width, oldButton.Location.Y),
BackgroundImageLayout = ImageLayout.Stretch
};
btn.Click += btn_Click;
ChessBoard.Controls.Add(btn);
oldButton = btn;
}
oldButton.Location = new Point(0 , oldButton.Location.Y + Cons.CHESS_HEIGHT);
oldButton.Width = 0 ;
oldButton.Height = 0 ;
}
}
void btn_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn.BackgroundImage != null )
return ;
Mark(btn);
ChangePlayer();
}
private void Mark (Button btn)
{
btn.BackgroundImage = Player[CurrentPlayer].Mark;
CurrentPlayer = CurrentPlayer == 1 ? 0 : 1 ;
}
private void ChangePlayer ()
{
PlayerName.Text = Player[CurrentPlayer].Name;
PlayerMark.Image = Player[CurrentPlayer].Mark;
}
#endregion
}
}
Code Form1.cs
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;
namespace GameCaro
{
public partial class Form1 : Form
{
#region Properties
ChessBoardManager ChessBoard;
#endregion
public Form1 ()
{
InitializeComponent();
ChessBoard = new ChessBoardManager(pnlChessBoard, txbPlayerName, pctbMark);
ChessBoard.DrawChessBoard();
}
}
}
Code Player.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameCaro
{
public class Player
{
private string name;
public string Name
{
get { return name; }
set { name = value ; }
}
private Image mark;
public Image Mark
{
get { return mark; }
set { mark = value ; }
}
public Player (string name, Image mark)
{
this .Name = name;
this .Mark = mark;
}
}
}
Bài sau chúng ta sẽ cùng nhau xử lý thắng thua.
Đừng quên: “Luyện tập – Thử thách – Không ngại khó ”
Tải xuống
Tài liệu
Nhằm phục vụ mục đích học tập Offline của cộng đồng, Kteam hỗ trợ tính năng lưu trữ nội dung bài học Xử lý đổi người chơi Game Caro C# Winform dưới dạng file PDF trong link bên dưới.
Ngoài ra, bạn cũng có thể tìm thấy các tài liệu được đóng góp từ cộng đồng ở mục TÀI LIỆU trên thư viện Howkteam.com
Đừng quên like và share để ủng hộ Kteam và tác giả nhé!
Project
Nếu việc thực hành theo hướng dẫn không diễn ra suôn sẻ như mong muốn. Bạn cũng có thể tải xuống PROJECT THAM KHẢO ở link bên dưới!
Thảo luận
Nếu bạn có bất kỳ khó khăn hay thắc mắc gì về khóa học, đừng ngần ngại đặt câu hỏi trong phần BÌNH LUẬN bên dưới hoặc trong mục HỎI & ĐÁP trên thư viện Howkteam.com để nhận được sự hỗ trợ từ cộng đồng.
Error 1 'WindowsFormsApplication1.Player.Mark' is inaccessible due to its protection level D:\Trang Sĩ Luân\Mini Game\WindowsFormsApplication1\WindowsFormsApplication1\ChessBoardManager.cs 78 49 WindowsFormsApplication1
em bị lỗi này fix sao vậy ạ
e bị lỗi phần btn.Click += Btn_Click ạ, hàm ủy thác của e là kiểu private static
System.IO.FileNotFoundException: 'C:\Users\Admin\source\repos\GameCaro\GameCaro\bin\Debug\Resoursces\p1.png'
anh oi em bi loi nhu nay, a giup em sua voi a, em cam on anh
Bài hay quá, tks a. A nói rõ cái phần Button btn = sender as Button thì tốt quá :3
Anh ơi, Cái sender của C# thì tương đương cái gì bên Java ạ, tại em không học ngôn ngữ C# nên không hiểu hoạt động của sender để có thể suy qua java.