K
kayz96
Guest
Hi,
Im trying to create a game of battleships Visual Studios using C# code but im not doing to well. I want to be able to move the ships around the screen when the user must choose where to place their ships, which i have don but i also want them to be able to snap to a grid i have created when the ship is over it. For the grid i have a series of loops featuring x and y coordinates but and the x coordinate changes every time the loop runs. As for the y coordinate it changes with each loop. On top of this at each point created a dynamic button has been put there. I am wondering if it is possible to implement this into the code i already have and if not what do i have to change so i can accomplish this event. This is the code i have come up with so far:
namespace Battleships___A_New_Beginning
{
public partial class Selection_Screen : Form
{
int tag1 = 0;
Point[] grid = new Point[100];
Point Move1;
Point Move2;
Point Move3;
Point Move4;
Point Move5;
int x = 0, gridCount = 0;
public Selection_Screen()
{
InitializeComponent();
grid1();
grid2();
grid3();
grid4();
grid5();
grid6();
grid7();
grid8();
grid9();
}
private void Selection_Screen_Load(object sender, EventArgs e)
{
}
private void grid1()
{
x = 30;// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
gridCount = 1;
for (int count = 0; count < 10; count++)
{
grid[count] = new Point(x, 10);
// Create a Button object
Button dynamicButton = new Button();
// Set Button properties
dynamicButton.Height = 40;
dynamicButton.Width = 40;
dynamicButton.BackColor = Color.Blue;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Location = grid[count];
dynamicButton.Text = "";
dynamicButton.Name = "btnGrid" + gridCount;
dynamicButton.Font = new Font("Georgia", 16);
dynamicButton.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton.Click += new System.EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton);
gridCount += 1;
x += 40;
}
}
private void showName(object Sender, System.EventArgs e)
{
Button TagNumber = (Button)Sender;
tag1 = Convert.ToInt32(TagNumber.Tag);
MessageBox.Show("btnGrid" + tag1);
}
private void grid2()// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
{
x = 30;
gridCount = 11;
for (int count2 = 0; count2 < 10; count2++)
{
grid[count2] = new Point(x, 50);
// Create a Button object
Button dynamicButton2 = new Button();
// Set Button properties
dynamicButton2.Height = 40;
dynamicButton2.Width = 40;
dynamicButton2.BackColor = Color.Blue;
dynamicButton2.ForeColor = Color.Blue;
dynamicButton2.Location = grid[count2];
dynamicButton2.Text = "";
dynamicButton2.Name = "btnGrid" + gridCount;
dynamicButton2.Font = new Font("Georgia", 16);
dynamicButton2.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton2.Click += new EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton2);
gridCount += 1;
x += 40;
}
}
private void grid3()// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
{
x = 30;
gridCount = 21;
for (int count = 0; count < 10; count++)
{
grid[count] = new Point(x, 90);
// Create a Button object
Button dynamicButton = new Button();
// Set Button properties
dynamicButton.Height = 40;
dynamicButton.Width = 40;
dynamicButton.BackColor = Color.Blue;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Location = grid[count];
dynamicButton.Text = "";
dynamicButton.Name = "btnGrid" + gridCount;
dynamicButton.Font = new Font("Georgia", 16);
dynamicButton.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton.Click += new EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton);
gridCount += 1;
x += 40;
}
}
private void grid4()// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
{
x = 30;
gridCount = 31;
for (int count = 0; count < 10; count++)
{
grid[count] = new Point(x, 130);
// Create a Button object
Button dynamicButton = new Button();
// Set Button properties
dynamicButton.Height = 40;
dynamicButton.Width = 40;
dynamicButton.BackColor = Color.Blue;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Location = grid[count];
dynamicButton.Text = "";
dynamicButton.Name = "btnGrid" + gridCount;
dynamicButton.Font = new Font("Georgia", 16);
dynamicButton.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton.Click += new System.EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton);
gridCount += 1;
x += 40;
}
}
private void grid5()// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
{
x = 30;
gridCount = 41;
for (int count = 0; count < 10; count++)
{
grid[count] = new Point(x, 170);
// Create a Button object
Button dynamicButton = new Button();
// Set Button properties
dynamicButton.Height = 40;
dynamicButton.Width = 40;
dynamicButton.BackColor = Color.Blue;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Location = grid[count];
dynamicButton.Text = "";
dynamicButton.Name = "btnGrid" + gridCount;
dynamicButton.Font = new Font("Georgia", 16);
dynamicButton.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton.Click += new System.EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton);
gridCount += 1;
x += 40;
}
}
private void grid6()// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
{
x = 30;
gridCount = 51;
for (int count = 0; count < 10; count++)
{
grid[count] = new Point(x, 210);
// Create a Button object
Button dynamicButton = new Button();
// Set Button properties
dynamicButton.Height = 40;
dynamicButton.Width = 40;
dynamicButton.BackColor = Color.Blue;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Location = grid[count];
dynamicButton.Text = "";
dynamicButton.Name = "btnGrid" + gridCount;
dynamicButton.Font = new Font("Georgia", 16);
dynamicButton.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton.Click += new System.EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton);
gridCount += 1;
x += 40;
}
}
private void grid7()// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
{
x = 30;
gridCount = 71;
for (int count = 0; count < 10; count++)
{
grid[count] = new Point(x, 250);
// Create a Button object
Button dynamicButton = new Button();
// Set Button properties
dynamicButton.Height = 40;
dynamicButton.Width = 40;
dynamicButton.BackColor = Color.Blue;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Location = grid[count];
dynamicButton.Text = "";
dynamicButton.Name = "btnGrid" + gridCount;
dynamicButton.Font = new Font("Georgia", 16);
dynamicButton.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton.Click += new EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton);
gridCount += 1;
x += 40;
}
}
private void grid8()// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
{
x = 30;
gridCount = 81;
for (int count = 0; count < 10; count++)
{
grid[count] = new Point(x, 290);
// Create a Button object
Button dynamicButton = new Button();
// Set Button properties
dynamicButton.Height = 40;
dynamicButton.Width = 40;
dynamicButton.BackColor = Color.Blue;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Location = grid[count];
dynamicButton.Text = "";
dynamicButton.Name = "btnGrid" + gridCount;
dynamicButton.Font = new Font("Georgia", 16);
dynamicButton.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton.Click += new EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton);
gridCount += 1;
x += 40;
}
}
private void grid9()// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
{
x = 30;
gridCount = 91;
for (int count = 0; count < 10; count++)
{
grid[count] = new Point(x, 330);
// Create a Button object
Button dynamicButton = new Button();
// Set Button properties
dynamicButton.Height = 40;
dynamicButton.Width = 40;
dynamicButton.BackColor = Color.Blue;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Location = grid[count];
dynamicButton.Text = "";
dynamicButton.Name = "btnGrid" + gridCount;
dynamicButton.Font = new Font("Georgia", 16);
dynamicButton.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton.Click += new EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton);
gridCount += 1;
x += 40;
}
}
private void picBoat1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - Move1.X;
int dy = e.Y - Move1.Y;
picBoat1.Location = new Point(picBoat1.Left + dx, picBoat1.Top + dy);
}
}
private void picBoat1_MouseDown(object sender, MouseEventArgs e)
{
Move1 = e.Location;
}
private void picBoat2_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - Move2.X;
int dy = e.Y - Move2.Y;
picBoat2.Location = new Point(picBoat2.Left + dx, picBoat2.Top + dy);
}
}
private void picBoat2_MouseDown(object sender, MouseEventArgs e)
{
Move2 = e.Location;
}
private void picBoat3_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - Move3.X;
int dy = e.Y - Move3.Y;
picBoat3.Location = new Point(picBoat3.Left + dx, picBoat3.Top + dy);
}
}
private void picBoat3_MouseDown(object sender, MouseEventArgs e)
{
Move3 = e.Location;
}
private void picBoat4_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - Move4.X;
int dy = e.Y - Move4.Y;
picBoat4.Location = new Point(picBoat4.Left + dx, picBoat4.Top + dy);
}
}
private void picBoat4_MouseDown(object sender, MouseEventArgs e)
{
Move4 = e.Location;
}
private void picBoat5_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - Move5.X;
int dy = e.Y - Move5.Y;
picBoat5.Location = new Point(picBoat5.Left + dx, picBoat5.Top + dy);
}
}
private void picBoat5_MouseDown(object sender, MouseEventArgs e)
{
Move5 = e.Location;
}
}
}
Any help would be greatly appreciated.
Thanks
Continue reading...
Im trying to create a game of battleships Visual Studios using C# code but im not doing to well. I want to be able to move the ships around the screen when the user must choose where to place their ships, which i have don but i also want them to be able to snap to a grid i have created when the ship is over it. For the grid i have a series of loops featuring x and y coordinates but and the x coordinate changes every time the loop runs. As for the y coordinate it changes with each loop. On top of this at each point created a dynamic button has been put there. I am wondering if it is possible to implement this into the code i already have and if not what do i have to change so i can accomplish this event. This is the code i have come up with so far:
namespace Battleships___A_New_Beginning
{
public partial class Selection_Screen : Form
{
int tag1 = 0;
Point[] grid = new Point[100];
Point Move1;
Point Move2;
Point Move3;
Point Move4;
Point Move5;
int x = 0, gridCount = 0;
public Selection_Screen()
{
InitializeComponent();
grid1();
grid2();
grid3();
grid4();
grid5();
grid6();
grid7();
grid8();
grid9();
}
private void Selection_Screen_Load(object sender, EventArgs e)
{
}
private void grid1()
{
x = 30;// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
gridCount = 1;
for (int count = 0; count < 10; count++)
{
grid[count] = new Point(x, 10);
// Create a Button object
Button dynamicButton = new Button();
// Set Button properties
dynamicButton.Height = 40;
dynamicButton.Width = 40;
dynamicButton.BackColor = Color.Blue;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Location = grid[count];
dynamicButton.Text = "";
dynamicButton.Name = "btnGrid" + gridCount;
dynamicButton.Font = new Font("Georgia", 16);
dynamicButton.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton.Click += new System.EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton);
gridCount += 1;
x += 40;
}
}
private void showName(object Sender, System.EventArgs e)
{
Button TagNumber = (Button)Sender;
tag1 = Convert.ToInt32(TagNumber.Tag);
MessageBox.Show("btnGrid" + tag1);
}
private void grid2()// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
{
x = 30;
gridCount = 11;
for (int count2 = 0; count2 < 10; count2++)
{
grid[count2] = new Point(x, 50);
// Create a Button object
Button dynamicButton2 = new Button();
// Set Button properties
dynamicButton2.Height = 40;
dynamicButton2.Width = 40;
dynamicButton2.BackColor = Color.Blue;
dynamicButton2.ForeColor = Color.Blue;
dynamicButton2.Location = grid[count2];
dynamicButton2.Text = "";
dynamicButton2.Name = "btnGrid" + gridCount;
dynamicButton2.Font = new Font("Georgia", 16);
dynamicButton2.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton2.Click += new EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton2);
gridCount += 1;
x += 40;
}
}
private void grid3()// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
{
x = 30;
gridCount = 21;
for (int count = 0; count < 10; count++)
{
grid[count] = new Point(x, 90);
// Create a Button object
Button dynamicButton = new Button();
// Set Button properties
dynamicButton.Height = 40;
dynamicButton.Width = 40;
dynamicButton.BackColor = Color.Blue;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Location = grid[count];
dynamicButton.Text = "";
dynamicButton.Name = "btnGrid" + gridCount;
dynamicButton.Font = new Font("Georgia", 16);
dynamicButton.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton.Click += new EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton);
gridCount += 1;
x += 40;
}
}
private void grid4()// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
{
x = 30;
gridCount = 31;
for (int count = 0; count < 10; count++)
{
grid[count] = new Point(x, 130);
// Create a Button object
Button dynamicButton = new Button();
// Set Button properties
dynamicButton.Height = 40;
dynamicButton.Width = 40;
dynamicButton.BackColor = Color.Blue;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Location = grid[count];
dynamicButton.Text = "";
dynamicButton.Name = "btnGrid" + gridCount;
dynamicButton.Font = new Font("Georgia", 16);
dynamicButton.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton.Click += new System.EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton);
gridCount += 1;
x += 40;
}
}
private void grid5()// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
{
x = 30;
gridCount = 41;
for (int count = 0; count < 10; count++)
{
grid[count] = new Point(x, 170);
// Create a Button object
Button dynamicButton = new Button();
// Set Button properties
dynamicButton.Height = 40;
dynamicButton.Width = 40;
dynamicButton.BackColor = Color.Blue;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Location = grid[count];
dynamicButton.Text = "";
dynamicButton.Name = "btnGrid" + gridCount;
dynamicButton.Font = new Font("Georgia", 16);
dynamicButton.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton.Click += new System.EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton);
gridCount += 1;
x += 40;
}
}
private void grid6()// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
{
x = 30;
gridCount = 51;
for (int count = 0; count < 10; count++)
{
grid[count] = new Point(x, 210);
// Create a Button object
Button dynamicButton = new Button();
// Set Button properties
dynamicButton.Height = 40;
dynamicButton.Width = 40;
dynamicButton.BackColor = Color.Blue;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Location = grid[count];
dynamicButton.Text = "";
dynamicButton.Name = "btnGrid" + gridCount;
dynamicButton.Font = new Font("Georgia", 16);
dynamicButton.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton.Click += new System.EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton);
gridCount += 1;
x += 40;
}
}
private void grid7()// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
{
x = 30;
gridCount = 71;
for (int count = 0; count < 10; count++)
{
grid[count] = new Point(x, 250);
// Create a Button object
Button dynamicButton = new Button();
// Set Button properties
dynamicButton.Height = 40;
dynamicButton.Width = 40;
dynamicButton.BackColor = Color.Blue;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Location = grid[count];
dynamicButton.Text = "";
dynamicButton.Name = "btnGrid" + gridCount;
dynamicButton.Font = new Font("Georgia", 16);
dynamicButton.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton.Click += new EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton);
gridCount += 1;
x += 40;
}
}
private void grid8()// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
{
x = 30;
gridCount = 81;
for (int count = 0; count < 10; count++)
{
grid[count] = new Point(x, 290);
// Create a Button object
Button dynamicButton = new Button();
// Set Button properties
dynamicButton.Height = 40;
dynamicButton.Width = 40;
dynamicButton.BackColor = Color.Blue;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Location = grid[count];
dynamicButton.Text = "";
dynamicButton.Name = "btnGrid" + gridCount;
dynamicButton.Font = new Font("Georgia", 16);
dynamicButton.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton.Click += new EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton);
gridCount += 1;
x += 40;
}
}
private void grid9()// sets a loop that runs ten times( createing ten grid points along X axis) before stoping
{
x = 30;
gridCount = 91;
for (int count = 0; count < 10; count++)
{
grid[count] = new Point(x, 330);
// Create a Button object
Button dynamicButton = new Button();
// Set Button properties
dynamicButton.Height = 40;
dynamicButton.Width = 40;
dynamicButton.BackColor = Color.Blue;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Location = grid[count];
dynamicButton.Text = "";
dynamicButton.Name = "btnGrid" + gridCount;
dynamicButton.Font = new Font("Georgia", 16);
dynamicButton.Tag = gridCount;
// Add a Button Click Event handler
dynamicButton.Click += new EventHandler(showName);
// Add Button to the Form. Placement of the Button
// will be based on the Location and Size of button
Controls.Add(dynamicButton);
gridCount += 1;
x += 40;
}
}
private void picBoat1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - Move1.X;
int dy = e.Y - Move1.Y;
picBoat1.Location = new Point(picBoat1.Left + dx, picBoat1.Top + dy);
}
}
private void picBoat1_MouseDown(object sender, MouseEventArgs e)
{
Move1 = e.Location;
}
private void picBoat2_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - Move2.X;
int dy = e.Y - Move2.Y;
picBoat2.Location = new Point(picBoat2.Left + dx, picBoat2.Top + dy);
}
}
private void picBoat2_MouseDown(object sender, MouseEventArgs e)
{
Move2 = e.Location;
}
private void picBoat3_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - Move3.X;
int dy = e.Y - Move3.Y;
picBoat3.Location = new Point(picBoat3.Left + dx, picBoat3.Top + dy);
}
}
private void picBoat3_MouseDown(object sender, MouseEventArgs e)
{
Move3 = e.Location;
}
private void picBoat4_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - Move4.X;
int dy = e.Y - Move4.Y;
picBoat4.Location = new Point(picBoat4.Left + dx, picBoat4.Top + dy);
}
}
private void picBoat4_MouseDown(object sender, MouseEventArgs e)
{
Move4 = e.Location;
}
private void picBoat5_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int dx = e.X - Move5.X;
int dy = e.Y - Move5.Y;
picBoat5.Location = new Point(picBoat5.Left + dx, picBoat5.Top + dy);
}
}
private void picBoat5_MouseDown(object sender, MouseEventArgs e)
{
Move5 = e.Location;
}
}
}
Any help would be greatly appreciated.
Thanks
Continue reading...