Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

winforms - I've been trying to use a class to create images of obstacles and nothing happens when I run it

I'm trying to create a simple game and I wanted to use classes to have multiple obstacles to spawn and move from right to left. For now I was only focused on actually creating the obstcales anywhere on the screen but nothing seems to be working.

public class Obstacles
        {
            public Point postion;
            public Size size;
            public System.Drawing.Bitmap image;

            public PictureBox createObstacle()
            {
                PictureBox rock = new PictureBox();
                rock.Location = postion;
                rock.Size = size;
                rock.Image = image;
                rock.SizeMode = PictureBoxSizeMode.StretchImage;
                return rock;
            }
        }
            Obstacles obstacles = new Obstacles();

            obstacles.postion = new Point(500, 200);
            obstacles.size = new Size(50, 50);
            obstacles.image = Properties.Resources.pixil_frame_0;

            obstacles.createObstacle();

Can someone let me know if theres something wrong with my method or how I call the class? I've created a blank picturebox in the form and named it rock still to no avail.

question from:https://stackoverflow.com/questions/65937788/ive-been-trying-to-use-a-class-to-create-images-of-obstacles-and-nothing-happen

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Rati,

Assuming you are using WinForms for your project. You will need to return the PictureBox object you create in the createObstacle method and add it to the Controls collection in the form.

For example,

public Form1()
{
    InitializeComponent();

    Obstacles obstacles = new Obstacles();

    obstacles.postion = new Point(500, 200);
    obstacles.size = new Size(50, 50);
    obstacles.image = Properties.Resources.pixil_frame_0;

    var obstacle = obstacles.createObstacle();
    
    Controls.Add(obstacle);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...