弾丸を発射

Space キーで弾丸を発射します。
C言語(Windows)でも 弾丸を発射 を作成しています。
Java Applet で 弾丸の発射と爆発 が楽しめます。

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

同心円状に発射

  1. ソースコードです。
    @"c:\data\test\Tama_R.png" が弾丸の画像です。
    /***************************************/
    /*★ 同心円状に弾丸を発射    前田 稔 ★*/
    /***************************************/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Collections;
    
    // 弾丸を発射するクラス
    public class SHOT
    {
        public float X, Y;      //弾の座標
        public float DX, DY;    //弾の移動量
    
        // Constructor で弾丸を登録
        public SHOT(float x, float y, float dx, float dy)
        {
            X= x;
            Y= y;
            DX= dx;
            DY= dy;
        }
    
        // 座標の移動(Rectangle を超えると return true)
        public bool Move(Rectangle rect)
        {   X += DX;
            Y += DY;
            if (X<rect.Left || X>rect.Right || Y<rect.Top || Y>rect.Bottom) return true;
            return false;
        }
    }
    
    public class MyForm : Form
    {
        public  static Bitmap  Bmp; //弾丸の画像
        public  ArrayList array;    //弾丸を登録
        private Timer timer1;
        private System.ComponentModel.IContainer components;
        Rectangle rect = new Rectangle(20,20,540,380);  //弾丸の描画範囲
    
        // 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
            BackColor = SystemColors.AppWorkspace;
            //BackColor = SystemColors.ControlLight;
            this.Width = 640;
            this.Height = 480;
            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            Paint += new PaintEventHandler(MyHandler);
            array = new ArrayList();
            timer1.Start();
        }
    
        // 画像を描画
        private void MyHandler(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            foreach(SHOT wk in array)
            {   g.DrawImage(Bmp,wk.X,wk.Y);
            }
        }
    
        // タイマー割り込み
        private void timer1_Tick(object sender, EventArgs e)
        {
            SHOT wk;
            for(int i=0; i<array.Count; i++)
            {
                wk = (SHOT)array[i];
                if (wk.Move(rect))  array.RemoveAt(i);
            }
            Invalidate();
        }
    
        // キータイプ
        protected override void OnKeyDown(KeyEventArgs e)
        {
            float dx, dy;
            if (e.KeyCode== Keys.Space)
            {
                for(int i=0; i<10; i++)
                {   dx = (float)(Math.Sin(i*36.0/180.0*Math.PI));
                    dy = (float)(Math.Cos(i*36.0/180.0*Math.PI));
                    array.Add(new SHOT(300,200,dx*2f,dy*2f));
                }
            }
        }
    }
    
    class form01
    {
        public static void Main()
        {
            // Load Image
            try
            {   MyForm.Bmp= new Bitmap(@"c:\data\test\Tama_R.png");  }
            catch
            {   MessageBox.Show("イメージが取得できません", "Error");
                return;
            }
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    
  2. SHOT が一個の弾丸を制御するクラスです。
    X, Y が弾の座標で、DX, DY が弾の移動量(飛ぶ方向)です。
    Move() メソッドで弾丸を移動します。
    弾丸が rect の外に出ると true をリターンして、弾丸を削除します。
    public class SHOT
    {
        public float X, Y;      //弾の座標
        public float DX, DY;    //弾の移動量
    
        // Constructor で弾丸を登録
        public SHOT(float x, float y, float dx, float dy)
        {
            X= x;
            Y= y;
            DX= dx;
            DY= dy;
        }
    
        // 座標の移動(Rectangle を超えると return true)
        public bool Move(Rectangle rect)
        {   X += DX;
            Y += DY;
            if (X<rect.Left || X>rect.Right || Y<rect.Top || Y>rect.Bottom) return true;
            return false;
        }
    }
    
  3. ArrayList で、同心円状に発射する弾丸を登録します。
        public  ArrayList array;    //弾丸を登録
        array = new ArrayList();
    
        array.Add(new SHOT(300,200,dx*2f,dy*2f));
        if (wk.Move(rect))  array.RemoveAt(i);
        
  4. @"c:\data\test\Tama_R.png" が弾丸の画像で、Space キーを押すと同心円状に広がるように弾を発射します。
        // キータイプ
        protected override void OnKeyDown(KeyEventArgs e)
        {
            float dx, dy;
            if (e.KeyCode== Keys.Space)
            {
                for(int i=0; i<10; i++)
                {   dx = (float)(Math.Sin(i*36.0/180.0*Math.PI));
                    dy = (float)(Math.Cos(i*36.0/180.0*Math.PI));
                    array.Add(new SHOT(300,200,dx*2f,dy*2f));
                }
            }
        }
        
  5. タイマー割り込みで弾丸を移動します。
    rect の外に出ると弾丸を削除します。
        Rectangle rect = new Rectangle(20,20,540,380);  //弾丸の描画範囲
    
        private void timer1_Tick(object sender, EventArgs e)
        {
            SHOT wk;
            for(int i=0; i<array.Count; i++)
            {
                wk = (SHOT)array[i];
                if (wk.Move(rect))  array.RemoveAt(i);
            }
            Invalidate();
        }
        

弾丸を連射

  1. ソースコードです。
    @"c:\data\test\Tama_R.png" が弾丸の画像で、@"c:\data\test\ship.png" がシップの画像です。
    C言語(Windows)でも 弾を連射 を作成しています。
    /*********************************/
    /*★ シップから連射    前田 稔 ★*/
    /*********************************/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Collections;
    using System.Runtime.InteropServices;
    
    // 弾丸を発射するクラス
    public class SHOT
    {
        ★「同心円状に発射」と同じです
    }
    
    public class MyForm : Form
    {
        [DllImport("User32.dll")]
        static extern short GetAsyncKeyState(int vKey);
    
        public  static Bitmap  Bmp; //弾丸の画像
        public  static Bitmap  Ship;//シップの画像
        public  ArrayList array;    //弾丸を登録
        private Timer timer1;
        private System.ComponentModel.IContainer components;
        Rectangle rect = new Rectangle(20,20,540,380);
        int     xp,yp;              //シップの座標
        public  int cnt;            //弾丸の連射間隔を設定
    
        // 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
            BackColor = SystemColors.AppWorkspace;
            //BackColor = SystemColors.ControlLight;
            this.Width = 640;
            this.Height = 480;
            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            Paint += new PaintEventHandler(MyHandler);
            array = new ArrayList();
            xp = 320;
            yp = 350;
            timer1.Start();
        }
    
        // 画像を描画
        private void MyHandler(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawImage(Ship,xp,yp);
            foreach(SHOT Shot in array)
            {   g.DrawImage(Bmp,Shot.X,Shot.Y);
            }
        }
    
        // タイマー割り込み
        private void timer1_Tick(object sender, EventArgs e)
        {
            SHOT shot;
            cnt++;
            if (GetAsyncKeyState(0x1B)!=0)  Application.Exit();
            if (GetAsyncKeyState(0x25)!=0)  xp -= 2;
            if (GetAsyncKeyState(0x26)!=0)  yp -= 2;
            if (GetAsyncKeyState(0x27)!=0)  xp += 2;
            if (GetAsyncKeyState(0x28)!=0)  yp += 2;
            if (GetAsyncKeyState(0x20)!=0 && cnt > 20)
            {   array.Add(new SHOT(xp+25, yp, 0, -2f));
                cnt = 0;
            }
            for(int i=0; i<array.Count; i++)
            {
                shot = (SHOT)array[i];
                if (shot.Move(rect))    array.RemoveAt(i);
            }
            Invalidate();
        }
    }
    
    class form01
    {
        public static void Main()
        {
            // Load Image
            try
            {   MyForm.Bmp= new Bitmap(@"c:\data\test\Tama_R.png");
                MyForm.Ship= new Bitmap(@"c:\data\test\ship.png");
            }
            catch
            {   MessageBox.Show("イメージが取得できません", "Error");
                return;
            }
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    
  2. 弾丸を制御する SHOT Class は「弾丸を発射」を参照して下さい。
    シップの移動は キーをセンス を参照して下さい。
  3. タイマ割り込みで Space キーの押し下げを検出して弾丸を連射します。
    毎回弾丸を登録すると棒状に繋がるので、適当な間隔(cnt>20)を置いて弾を登録します。
        public  int cnt;            //弾丸の連射間隔を設定
    
            cnt++;
            if (GetAsyncKeyState(0x20)!=0 && cnt > 20)
            {   array.Add(new SHOT(xp+25, yp, 0, -2f));
                cnt = 0;
            }
        

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