How to create scrolling tiles in Visual Studio C# Forms - c#

I want to make a tiles based scrolling game in Visual Studio C# using forms. I know its not the best platform for this but those are set parameters. I suppose the easiest way to think of the end program is like pokemon 2d top down world scrolling.
I can create a 2d array of picture boxes and allocated images to them based on a text 2d array of tile ids. I can scroll the picture boxes and when they reach a certain place, jump back to the original location and display a shifted tile.
My issue is adding controls, it only adds a column rather than the full grid. I have tried using tables but with the same problem.
Has anyone done this type of large world scroller using VSC# and forms? Are there any tutorials or suggestions? Thanks.
EDIT - Code added'
public Form1()
{
InitializeComponent();
TableLayoutPanel wholescreen = new TableLayoutPanel();
wholescreen.Location = new System.Drawing.Point(0,0);
wholescreen.Size = new System.Drawing.Size(200,200);
wholescreen.RowCount = 2;
wholescreen.ColumnCount = 2;
Controls.Add(wholescreen);
PictureBox item = new PictureBox();
item.Size = new System.Drawing.Size(50, 50);
item.ImageLocation = "C:\\Users\\i.price\\Documents\\1.png";
for (int x = 0; x < 2; x++)
{
for (int y = 0; y < 2; y++)
{
item.Left = x * 50;
item.Top = y * 50;
wholescreen.Controls.Add(item,x,y);
}
}
}
'
another way i tried....'
int WIDTH = 3;
int HEIGHT = 3;
PictureBox[] grid = new PictureBox[9];
//PictureBox[,] grid = new PictureBox[3, 3];
//int[,] level1 = new int[2, 2] { { 1, 2 }, { 3, 4 } };
int playerx = 0;
public Form1()
{
InitializeComponent();
int y = 0;
int x = 0;
for (int cntr = 0; cntr < HEIGHT*WIDTH; cntr++)
{
if ((cntr % HEIGHT) == 0)
{
x++;
y = 0;
}
grid[cntr] = new PictureBox();
grid[cntr].Left = x * 50;
grid[cntr].Top = y * 50;
grid[cntr].ImageLocation = "C:\\Users\\i.price\\Documents\\1.png";
Controls.Add(grid[cntr]);
}
}
'

I think you are just creating one square and moving it around. Try...
private void Method2()
{
TableLayoutPanel wholescreen = new TableLayoutPanel();
wholescreen.BackColor = Color.AliceBlue;
wholescreen.Location = new System.Drawing.Point(0, 0);
wholescreen.Size = new System.Drawing.Size(200, 200);
wholescreen.RowCount = 2;
wholescreen.ColumnCount = 2;
Controls.Add(wholescreen);
PictureBox item;
// item.ImageLocation = "C:\\Users\\i.price\\Documents\\1.png";
for (int x = 0; x < 2; x++)
{
for (int y = 0; y < 2; y++)
{
item = new PictureBox();
item.Size = new System.Drawing.Size(50, 50);
item.BackColor = Color.Blue;
//item.Left = 0;
//item.Top = 0;
wholescreen.Controls.Add(item, x, y);
}
}
}

Related

which button is pressed in dynamically created button 2d array

I have a 2d array of LEDButton : Button.
I want to find out the index [x,y] of each buttons the user clicks.
I am new to Windows Forms and not used to working outside of a console so these GUI objects are very unfamiliar to me.
private void Form1_Load(object sender, EventArgs e)
{
LEDButton[,] leds = new LEDButton[11, 11];
for (int x = 0; x < leds.GetUpperBound(0); x++)
{
listBox1.Items.Add("x = " + x);
for (int y = 0; y < leds.GetUpperBound(1); y++)
{
leds[x, y] = new LEDButton()
{
Name = String.Format("Button{0}{1}", x, y),
TabIndex = 40 * x + y,
Location = new Point(40 * y + 50, 40 * x + 50)
};
leds[x, y].pointx = x;
leds[x, y].pointy = y;
}
}
// add buttons to controls
for (int x = 0; x < leds.GetUpperBound(0); x++)
{
for (int y = 0; y < leds.GetUpperBound(1); y++)
{
Controls.Add(leds[x, y]);
leds[x, y].Click += Form1_Click;
}
}
public class LEDButton : Button
{
public const int LEDWidth = 20;
public const int LEDHeight = 20;
public int pointx = 0;
public int pointy = 0;
public LEDButton()
{
BackColor = Color.FromArgb(0, 64, 0);
ForeColor = Color.Black;
FlatStyle = FlatStyle.Flat;
Size = new Size(LEDWidth, LEDHeight);
UseVisualStyleBackColor = false;
this.Click += LEDButton_Click; //throws error
}
}
I think I found my answer with the help of Lars.
Code should be
private void Form1_Click(object? sender, EventArgs e)
{
LEDButton btn = sender as LEDButton;
listBox2.Items.Add(btn.Name);
}

Dynamicly created panels location distance

I'm beginner in programming and I want to create a little "game" with Panels.
(Later maybe I'll change for PictureBox, but for now it's OK)
Code:
private void Form1_Load(object sender, EventArgs e)
{
int size = 20;
int quantity = 10;
Random rnd = new Random();
for (int y = 0; y < quantity; y++)
{
for (int x = 0; x < quantity; x++)
{
Color randomColor = Color.FromArgb(
rnd.Next(256), rnd.Next(256), rnd.Next(256)
);
Panel panel = new Panel
{
Size = new Size(size, size),
Location = new Point(x * size, y * size),
BorderStyle = BorderStyle.FixedSingle,
BackColor = randomColor
};
Controls.Add(panel);
//panel.Click += Panel_Click;
}
}
}
I have two questions:
How can I set 5 pixel distance to each Panel from each other?
Should I place these panel creation in the constructor? I see people prefer that.
You have to include additional 5 pixels padding into Location:
...
int padding = 5;
...
Location = new Point(x * (size + padding), y * (size + padding))
...
Let's extract a method:
private void CreateGameField() {
int size = 20;
int padding = 5;
int quantity = 10;
Random rnd = new Random();
for (int y = 0; y < quantity; ++y)
for (int x = 0; x < quantity; ++x)
new Panel() {
Size = new Size(size, size),
Location = new Point(x * (size + padding), y * (size + padding)),
BorderStyle = BorderStyle.FixedSingle,
BackColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256)),
Parent = this, // instead of Controls.Add(panel);
};
}
Then
private void Form1_Load(object sender, EventArgs e) {
CreateGameField();
}
FromLoad event handler is a good place to create the game field; if you want you can place CreateGameField() in the constructor, but put it after InitializeComponent():
public Form1() {
InitializeComponent();
CreateGameField();
}

Drawing bounding box using pixel data of image c#

I am trying to draw a bounding box using pixel data. I want to generate a bounding box like in the attached image. I tried to draw a line graph and find box coordinates for each character.
I am confused in implementation. Can any one kindly guide me?
my code is as follows
private void button1_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(filename);
GetBitmap(bmp);
bmp.Save(#"C:\Users\harinath\Desktop\New folder (2)\hello.jpg");
List<System.Drawing.Point> bl = new List<System.Drawing.Point>();
List<System.Drawing.Point> wt = new List<System.Drawing.Point>();
List<System.Drawing.Point> bt = new List<System.Drawing.Point>();
int whiteColor = 0;
int blackColor = 0;
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
Color color = bmp.GetPixel(x, y);
if (color.ToArgb() == Color.White.ToArgb())
{
wt.Add(new System.Drawing.Point(x, y));
bt.Add(new System.Drawing.Point(x, 0));
}
else if (color.ToArgb() == Color.Black.ToArgb())
{
bl.Add(new System.Drawing.Point(x, y));
bt.Add(new System.Drawing.Point(x, y));
}
}
}
//int count = bt.Count;
//// Point ab = new Point();
//List<int> xlist = new List<int>();
//List<int> ylist = new List<int>();
//for (int i = 0; i < count; i++)
//{
// var a=bt[i].X;
// var b =bt[i].Y;
// if (b > 0) { }
//}
var series = new Series("test");
chart1.ChartAreas[0].AxisX.Maximum = bmp.Width;
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = bmp.Height;
chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisX.Interval = 5;
chart1.ChartAreas[0].AxisY.Interval = 2;
foreach (System.Drawing.Point ls in bt)
{
series.Points.AddXY(ls.X, ls.Y);
series.ChartType = SeriesChartType.Line;
}
chart1.Series.Add(series);
chart1.SaveImage(#"C:\Users\harinath\Desktop\New folder (2)\chart1.png", ChartImageFormat.Png);
//textBox1.Text = whiteColor.ToString();
//textBox2.Text = blackColor.ToString();
bmp.Save(#"C:\Users\harinath\Desktop\New folder (2)\hello.jpg");
}

Aligning labels in array in C# to form a grid

I have created an array of black labels and have displayed them on a picture box. Unfortunately, I am not able to line them up directly at each intersection of the black lines. How can I do this?
InitializeComponent();
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
// create 361 labels, set their properties
for (int i = 0; i < 361; i++)
{
board[i] = new Label();
board[i].Parent = pictureBox1;
board[i].Location = new Point(x, y);
board[i].Name = "label" + i;
board[i].Text = "0";
board[i].BackColor = Color.Black;
//set size of labels
board[i].Size = new Size(31,31);
}
// set the position of the label
foreach (Label i in board)
{
//set distance between labels
if (x >= 1024)
{
x = pictureBox1.Location.X;
y += 52;
}
else
{
x += 52;
}
this.Controls.Add(i);
i.BringToFront();
i.Location = new Point(x, y);
}
as far as i have understand your question, and saw your code.
you are creating labels at same
Location(x,y) where x = 100 and y = 0
in the next loop
foreach (Label i in board)
{
if (x >= 1024)
{
x = 0;
y += i.Height + 55;
}
else if (y >= 1024)
{
y = 0;
x += i.Width + 55;
}
}
None of your condition will become true, because your x = 100 and y = 0
so location will remain same and all labels will be at same location
if you want to display a chess grid see this method
Chess Grid in Winforms
if you want to display labels on intersection of lines then lets modify your Code
x = PictureBox1.Location.X + 55;
y = pictureBox1.Location.Y + 55;
for (int i = 0; i < 361; i++)
{
board[i] = new Label();
board[i].Parent = pictureBox1;
board[i].Location = new Point(x,y);
board[i].Name = "label" + i;
board[i].Text = "0";
board[i].BackColor = Color.Black;
board[i].Size = new Size(55,55); //Define size of label according to your choice
if(x >= 1024)
{
x = PictureBox1.Location.X + 55; //Start position
y += 55; // Step to next line
}
else
x += 55; //jump to next horizontal box
}
I hope this helps

Items not appearing inside a canvas c#/silverlight

I am just experimenting with Silverlight however I've run into an issue with a Canvas element. I am trying to generate a grid of rectangle based off a multidimensional array but when I run the code, the canvas has no elements. I wonder if you could offer some guidance for this?
public void generate(Rectangle[, ,] worldData)
{
int locx = 0, locy = 0;
Rectangle currentBlock = new Rectangle();
int z = 0;
for (int x = 0; x < worldData.GetLength(1); x++)
{
for (int y = 0; y < worldData.GetLength(2); y++)
{
currentBlock = worldData[z, x, y];
Canvas.SetTop(currentBlock, locy);
Canvas.SetLeft(currentBlock, locx);
locy = locy + 32;
gridDisplayCanvas.Children.Add(currentBlock);
}
locx = locx + 32;
locy = 0;
}
}
I figured out what is causing the issue, the images where being referenced with a \ but silverlight expects a \

Categories

Resources