M
M
Maxim K2022-02-15 22:35:37
C++ / C#
Maxim K, 2022-02-15 22:35:37

How to fix - I'm trying to move the bitmap across the field?

Hello. In general, I load the map / level:

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            var lines = File.ReadAllLines(@"C:\Users\MAKSIM\source\repos\LoadLevel\WindowsFormsApp5\Level\level1.txt");
            width = lines[0].Length;
            height = lines.Length;

            for (int j = 0; j < height; j++)
            {
                for (int i = 0; i < width; i++)
                {
                    switch (lines[j][i])
                    {
                        case 'x':
                            g.DrawImage(image1, i * 32, j * 32, 32, 32);
                            coordBoll[i, j] = 1;
                            break;
                        case 'b':
                            g.DrawImage(image2, i * 32, j * 32, 32, 32);
                            coordBoll[i, j] = 2;
                            break;
                        case ' ':
                            g.DrawImage(image3, i * 32, j * 32, 32, 32);
                            coordBoll[i, j] = 0;
                            break;
                        case 'p':
                            g.DrawImage(image4, i * 32, j * 32, 32, 32);
                            coordBoll[i, j] = 3;
                            break;
                        case 's':
                            g.DrawImage(image5, i * 32, j * 32, 32, 32); posSX = i; posSY = j;
                            coordBoll[i, j] = 4;
                            break;
                    }
                }
            }
        }


Further, I try to move bitmap. The player himself moves successfully (image5). When colliding with (image2), you need to move image2 left/right/up/down - depending on the buttons pressed. I check the collision condition like this:
case Keys.Left:
                    image5 = new Bitmap(@"C:\Users\MAKSIM\source\repos\LoadLevel\WindowsFormsApp5\Image\left.png");
                    g.DrawImage(image3, posSX * 32, posSY * 32);
                    g.DrawImage(image5, posSX * 32 - 32, posSY * 32);
                    posSX -= 1;

                    if (coordBoll[posSX, posSY] == 2)
                    {                     
                        for (int j = 0; j < height; j++)
                        {
                            for (int i = 0; i < width; i++)
                            {
                                g.DrawImage(image2, posSX * 32 - 32, posSY * 32);
                            }
                        }
                    }

                    g.DrawImage(image2, posSX * 32 - 32, posSY * 32);
                    break;


But every time you press the button, another image2 is drawn. How to fix it??? I do not ask you to rewrite the entire code, if there are any methodological instructions, I will gladly read or simple examples where you can move one object to another and so that they move further together. THANK YOU

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Myclass, 2022-02-15
@Myclass

case Keys.Left:
                    image5 = new Bitmap(@"C:\Users\MAKSIM\source\repos\LoadLevel\WindowsFormsApp5\Image\left.png");
                    g.DrawImage(image3, posSX * 32, posSY * 32);
                    g.DrawImage(image5, posSX * 32 - 32, posSY * 32);
                    posSX -= 1;

                    if (coordBoll[posSX, posSY] == 2)
                    {                     
                        for (int j = 0; j < height; j++)
                        {
                            for (int i = 0; i < width; i++)
                            {
                                g.DrawImage(image2, posSX * 32 - 32, posSY * 32);
                            }
                        }
                    }

                    g.DrawImage(image2, posSX * 32 - 32, posSY * 32); // а зачем в условии эта последняя строчка с прорисованием image2 вновь....
                    break;

and why in the condition this last line with drawing image2 again ....
And one more thing. I didn't understand these two nested loops at all. Because they don't affect anything. Why are they?
And further. Of course, I am not a specialist in games, but I would keep a grid with coordinates in my head (memory) and the drawing of elements would be independent of the number of pixels in one cell (32), so that I could always scale.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question