前田稔(Maeda Minoru)の超初心者のプログラム入門
/***************************************/ /*★ 同心円状に弾丸を発射 前田 稔 ★*/ /***************************************/ 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); } } |
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 ArrayList array; //弾丸を登録 array = new ArrayList(); array.Add(new SHOT(300,200,dx*2f,dy*2f)); if (wk.Move(rect)) array.RemoveAt(i); |
// キータイプ 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)); } } } |
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(); } |
/*********************************/ /*★ シップから連射 前田 稔 ★*/ /*********************************/ 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); } } |
public int cnt; //弾丸の連射間隔を設定 cnt++; if (GetAsyncKeyState(0x20)!=0 && cnt > 20) { array.Add(new SHOT(xp+25, yp, 0, -2f)); cnt = 0; } |