背景のスクロール

ゲームの背景画像をスクロールします。
Java でも 背景画像のスクロール を作成しています。

前田稔(Maeda Minoru)の超初心者のプログラム入門

プログラムの説明

  1. ソースコードです。
    @"c:\data\test\utyu.jpg" が 1280*480 の背景画像です。
    /***************************************/
    /*★ 背景画像をスクロール    前田 稔 ★*/
    /***************************************/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class MyForm : Form
    {
        public  static Bitmap  Back;
        public  int Pos;
        private Timer timer1;
        private System.ComponentModel.IContainer components;
    
        // Constructor
        public MyForm()
        {
            // timer1
            components = new System.ComponentModel.Container();
            timer1 = new System.Windows.Forms.Timer(this.components);
            SuspendLayout();
            timer1.Interval = 15;
            timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // MyForm
            this.Width = 640;
            this.Height = 480;
            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            Paint += new PaintEventHandler(MyHandler);
            Pos = 0;
            timer1.Start();
        }
    
        // 画像を描画
        private void MyHandler(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawImage(Back,new Rectangle(0,0,640,480),
                new Rectangle(Pos,0,640,480),GraphicsUnit.Pixel);
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
            Pos =(Pos+1)%640;
            Invalidate();
        }
    }
    
    class form01
    {
        public static void Main()
        {
            // Load Image
            try
            {   MyForm.Back= new Bitmap(@"c:\data\test\utyu.jpg");  }
            catch
            {   MessageBox.Show("イメージが取得できません", "Error");
                return;
            }
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    
  2. utyu.jpg はウインドウサイズ(640*480)に合わせた画像が横に二枚並んだ 1280*480 の画像です。
    FPS(frames per second)を15ミリ秒に設定して、Pos=(Pos+1)%640; で横にスクロールします。
    Pos の値は「0~639」を繰り返すのですが、639 から 0 に戻る時に背景が連続するように画像を作成して下さい。
            timer1 = new System.Windows.Forms.Timer(this.components);
            SuspendLayout();
            timer1.Interval = 15;
            timer1.Tick += new System.EventHandler(this.timer1_Tick);
    
        private void timer1_Tick(object sender, EventArgs e)
        {
            Pos =(Pos+1)%640;
            Invalidate();
        }
        
  3. MyHandler() ではチラツキを押さえるためにダブルバッファを使っています。
    ダブルバッファの説明は Double Buffer で描画する を参照して下さい。
            // MyForm
            this.Width = 640;
            this.Height = 480;
            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    
        // 画像を描画
        private void MyHandler(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawImage(Back,new Rectangle(0,0,640,480),
                new Rectangle(Pos,0,640,480),GraphicsUnit.Pixel);
        }
        
  4. 同様の手法で、背景を下方向にスクロールして下さい。
    また、ウインドウと同じサイズの画像を用いて、左右(上下)に二分割して描画する方法もあります。
    一工夫必要ですが、挑戦してみて下さい。

前田稔(Maeda Minoru)の超初心者のプログラム入門