前田稔(Maeda Minoru)の超初心者のプログラム入門
/*★ Enemy に向かって連射 前田 稔 ★*/ using System; using System.Drawing; using System.Windows.Forms; using System.Collections; using System.Runtime.InteropServices; public class MyForm : Form { [DllImport("User32.dll")] static extern short GetAsyncKeyState(int vKey); SHOOT Shoot; //弾丸を一括管理するクラス ANIME Enemy; //アニメーションクラス private Timer timer1; private System.ComponentModel.IContainer components; Rectangle rect1 = new Rectangle(20,20,500,380); Rectangle rect2 = new Rectangle(20,20,540,380); 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; this.Width = 640; this.Height = 480; SetStyle(ControlStyles.DoubleBuffer, true); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); Shoot = new SHOOT(@"c:\data\test\tama4.png",32,32); Enemy = new ANIME(@"c:\data\test\Ene5.png",80,96); Enemy.Set(0,50,10,2,0); Paint += new PaintEventHandler(MyHandler); timer1.Start(); } // 画像を描画 private void MyHandler(object sender, PaintEventArgs e) { Graphics g = e.Graphics; if (Enemy.Bmp == null) Application.Exit(); Enemy.View(g); Shoot.View(g); } // タイマー割り込み private void timer1_Tick(object sender, EventArgs e) { short kw; if (GetAsyncKeyState(0x1B)!=0) Application.Exit(); cnt++; Shoot.Move(rect2); kw = GetAsyncKeyState(0x20); //空白 if (kw!=0 && cnt>30) { Shoot.Add(0, 300, 400, 0, -1.5f); cnt = 0; } Enemy.Loop(60000000); Enemy.Bound(rect1); Invalidate(); } } //☆ 弾丸を一括管理するクラス public class SHOOT { //★「 弾丸を一括管理」のページを参照してください。 } //☆ アニメーションクラス class ANIME { //★「アニメーションクラス」のページを参照してください。 } class form01 { public static void Main() { MyForm mf = new MyForm(); Application.Run(mf); } } |
SHOOT Shoot; //弾丸を一括管理するクラス ANIME Enemy; //アニメーションクラス Rectangle rect1 = new Rectangle(20,20,500,380); Rectangle rect2 = new Rectangle(20,20,540,380); |
Shoot = new SHOOT(@"c:\data\test\tama4.png",32,32); Enemy = new ANIME(@"c:\data\test\Ene5.png",80,96); Enemy.Set(0,50,10,2,0); Paint += new PaintEventHandler(MyHandler); timer1.Start(); |
// 画像を描画 private void MyHandler(object sender, PaintEventArgs e) { Graphics g = e.Graphics; if (Enemy.Bmp == null) Application.Exit(); Enemy.View(g); Shoot.View(g); } |
private void timer1_Tick(object sender, EventArgs e) { short kw; if (GetAsyncKeyState(0x1B)!=0) Application.Exit(); cnt++; Shoot.Move(rect2); kw = GetAsyncKeyState(0x20); //空白 if (kw!=0 && cnt>30) { Shoot.Add(0, 300, 400, 0, -1.5f); cnt = 0; } Enemy.Loop(60000000); Enemy.Bound(rect1); Invalidate(); } |
/*********************************/ /*★ 当り判定と爆発 前田 稔 ★*/ /*********************************/ using System; using System.Drawing; using System.Windows.Forms; using System.Collections; using System.Runtime.InteropServices; public class MyForm : Form { [DllImport("User32.dll")] static extern short GetAsyncKeyState(int vKey); IMAGE Ship; //シップを描画するクラス SHOOT Shoot; //弾丸を一括管理するクラス ANIME Enemy; //Enemy を描画するクラス ANIME Bomb; //爆発アニメーション private Timer timer1; private System.ComponentModel.IContainer components; Rectangle rect1 = new Rectangle(20,20,500,380); Rectangle rect2 = new Rectangle(20,20,540,380); int xp,yp; //シップの座標 int cnt; //弾丸の連射間隔を設定 System.Media.SoundPlayer player; // 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; this.Width = 640; this.Height = 480; SetStyle(ControlStyles.DoubleBuffer, true); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); Ship = new IMAGE(@"c:\data\test\ship.png"); xp = 300; yp = 360; Shoot = new SHOOT(@"c:\data\test\tama4.png",32,32); Enemy = new ANIME(@"c:\data\test\Ene5.png",80,96); Enemy.Set(0,50,10,2,0); Bomb = new ANIME(@"c:\data\test\Bomb.gif",128,128); player = new System.Media.SoundPlayer(@"c:\data\test\bomb.wav"); Paint += new PaintEventHandler(MyHandler); timer1.Start(); } // 画像を描画 private void MyHandler(object sender, PaintEventArgs e) { Graphics g = e.Graphics; if (Enemy.Bmp == null) Application.Exit(); Ship.View(g, xp, yp); Enemy.View(g); Shoot.View(g); Bomb.View(g); } // タイマー割り込み private void timer1_Tick(object sender, EventArgs e) { if (GetAsyncKeyState(0x1B)!=0) Application.Exit(); cnt++; Shoot.Move(rect2); // 弾丸と Enemy との当り判定 if (Shoot.Hit(Enemy.X,Enemy.Y,50)) { //ヒットしたとき player.Play(); Bomb.Set(0,Enemy.X,Enemy.Y); Enemy.Set(0,50,10,2,0); //Enemy を左端に移動 } 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>30) { Shoot.Add(0, xp + 20, yp, 0, -1.5f); cnt = 0; } Bomb.Next(1000000); Enemy.Loop(60000000); Enemy.Bound(rect1); Invalidate(); } } //☆ 画像を描画するクラス class IMAGE { //★「 画像を描画するクラス」のページを参照してください。 } //☆ 弾丸を一括管理するクラス public class SHOOT { //★「 弾丸を一括管理」のページを参照してください。 } //☆ アニメーションクラス class ANIME { //★「アニメーションクラス」のページを参照してください。 } class form01 { public static void Main() { MyForm mf = new MyForm(); Application.Run(mf); } } |
IMAGE Ship; //シップを描画するクラス SHOOT Shoot; //弾丸を一括管理するクラス ANIME Enemy; //Enemy を描画するクラス ANIME Bomb; //爆発アニメーション |
private void MyHandler(object sender, PaintEventArgs e) { Graphics g = e.Graphics; if (Enemy.Bmp == null) Application.Exit(); Ship.View(g, xp, yp); Enemy.View(g); Shoot.View(g); Bomb.View(g); } |
private void timer1_Tick(object sender, EventArgs e) { if (GetAsyncKeyState(0x1B)!=0) Application.Exit(); cnt++; Shoot.Move(rect2); // 弾丸と Enemy との当り判定 if (Shoot.Hit(Enemy.X,Enemy.Y,50)) { //ヒットしたとき player.Play(); Bomb.Set(0,Enemy.X,Enemy.Y); Enemy.Set(0,50,10,2,0); //Enemy を左端に移動 } 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>30) { Shoot.Add(0, xp + 20, yp, 0, -1.5f); cnt = 0; } Bomb.Next(1000000); Enemy.Loop(60000000); Enemy.Bound(rect1); Invalidate(); } |