Jamisco
Jamisco

Reputation: 1862

how to increase form width

I'm not exactly sure how to go about this, But imagine some 2d game like this Some 2d game sooner or later this guy is going to reach the end of the form(if the form isn't redrawn/ extended),hence how do I Redraw the background when the guys reaches the middle of the current form so our character can in sense, walk for ever. Think of those 2d running games like flappy bird or jetpack joyride where you can walk or fly for infinity. Additionally, the form size is only altered when the character moves.

Upvotes: 0

Views: 81

Answers (1)

Rufus L
Rufus L

Reputation: 37020

Here is a crude way to do it, hopefully it helps get you going. Basically just create a picture box as tall as your form and twice as wide, load your background image into it, and then, in a timer, move the picture to the left. You'll need a very wide image, and it will look a little jerky at the transition unless you make the last "frame" match the first one.

Changing the amount that you subtract from the left will control how fast it scrolls, so I named that variable "speed".

public partial class Form1 : Form
{
    private int scrollSpeed = 10;
    Timer timer = new Timer();
    private PictureBox backgroundPictureBox;

    private void Form1_Load(object sender, EventArgs e)
    {
        Width = 1000;
        Height = 1000;

        backgroundPictureBox = new PictureBox
        {
            BackgroundImageLayout = ImageLayout.Stretch,
            Height = this.Height,
            Image = Image.FromFile(@"f:\Public\Temp\tmp.png"),
            Left = 0,
            SizeMode = PictureBoxSizeMode.StretchImage,
            Visible = true,
            Width = this.Width * 2
        };
        Controls.Add(backgroundPictureBox);

        timer.Interval = 1;
        timer.Tick += Timer_Tick;
        timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        if (backgroundPictureBox.Left < (Width * -1))
        {
            backgroundPictureBox.Left = 0;
        }
        else
        {
            backgroundPictureBox.Left -= scrollSpeed;
        }
    }
}

Upvotes: 1

Related Questions