2017-06-19 102 views
0

我不完全知道如何去這個問題,但是想象一下,一些2D遊戲這樣 Some 2d game 早晚這傢伙是要達到的形式結束(如果表單ISN」重繪/擴展),那麼當人們到達當前形式的中間時,我如何重繪背景,這樣我們的角色就可以在意義上走向永遠。想想那些可以散步或飛行無限的2d跑步遊戲,如飛揚的小鳥或飛行包兜風。此外,只有在角色移動時纔會更改表格大小。如何增加窗體的寬度

+1

您不會增加表格寬度,您會開始將背景移動到左側而不是右側的播放器圖像。 –

+0

,我該怎麼做? – Jamisco

+0

O,那似乎....容易 – Jamisco

回答

1

下面是一個粗略的方法來做到這一點,希望它有助於你的發展。基本上只需創建一個與您的窗體一樣高的圖片框,寬度爲兩倍,將背景圖片加載到圖片框中,然後在計時器中將圖片移動到左側。您需要非常寬的圖像,除非您使最後一個「幀」與第一個「幀」相匹配,否則它在轉場時會顯得有點不平。

更改從左側減去的量將控制滾動的速度,因此我將該變量命名爲「速度」。

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; 
     } 
    } 
}