using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
PictureBox pic = new PictureBox();
Bitmap[] bmp = new Bitmap[2];
Timer timer = new Timer();
Rectangle SRect;
GraphicsPath gp = new GraphicsPath();
private void Form1_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
this.ClientSize = new Size(600, 600);
pic.Dock = DockStyle.Fill;
this.Controls.Add(pic);
bmp[0] = new Bitmap(600, 600, PixelFormat.Format32bppArgb);
bmp[1] = new Bitmap(600, 600, PixelFormat.Format32bppArgb);
SRect = new Rectangle(0, 0, 600, 600);
gp.AddEllipse(0, 0, 10, 10);
pic.Click += new EventHandler(pic_Click);
timer.Interval = (int)(1.0f / 30.0f * 1000.0f);
timer.Tick += new EventHandler(timer_Tick);
timer.Enabled = true;
this.ModeSet();
}
void ModeSet()
{
this.Text = "Mode = " + Mode.ToString();
allb.Clear();
}
int Mode = 0;
void pic_Click(object sender, EventArgs e)
{
if (Mode == 0)
{
Mode = 1;
}
else if (Mode == 1)
{
Mode = 2;
}
else if (Mode == 2)
{
Mode = 0;
}
this.ModeSet();
}
int screen = 0;
int SetCount = 0;
List<Bullet> allb = new List<Bullet>();
List<Bullet> delb = new List<Bullet>();
Font stringFont = new Font(new FontFamily("メイリオ"), 20.0f);
void timer_Tick(object sender, EventArgs e)
{
if (Mode == 0)
{
}
else if (Mode == 1)
{
if (SetCount == 10)
{
this.SetBullet(15,5);
SetCount = -1;
}
SetCount++;
}
else if (Mode == 2)
{
if (SetCount == 10)
{
this.SetBullet(30,5);
SetCount = -1;
}
SetCount++;
}
using (Graphics g = Graphics.FromImage(bmp[screen]))
{
g.FillRectangle(Brushes.White, SRect);
Matrix m = new Matrix();
foreach (Bullet b in allb)
{
if (!b.Move())
{
m.Translate(b.Lx, b.Ly);
g.Transform = m;
g.FillPath(Brushes.Blue, gp);
m.Reset();
g.Transform = m;
}
else
{
delb.Add(b);
}
}
g.DrawString("Bullet = " + allb.Count.ToString(),
stringFont, Brushes.Black, 10.0f, 10.0f);
}
pic.Image = bmp[screen];
screen ^= 1;
foreach (Bullet b in delb)
{
allb.Remove(b);
}
delb.Clear();
}
void SetBullet(int Num, double Vec)
{
if (Num < 10) Num = 10;
if (Num > 360) Num = 360;
double Angle = Math.PI * (360.0 / Num) /180.0;
for (int i = 0; i < Num; i++)
{
Bullet b = new Bullet();
b.DeleteTime = 100;
b.Lx = 300;
b.Ly = 300;
b.Vx = (float)(Vec * Math.Cos(Angle * i));
b.Vy = (float)(Vec * Math.Sin(Angle * i));
//x' = x * cosθ - y * sinθ
//y' = x * sinθ + y * cosθ
//↓
//x = v * cosθ
//y = v * sinθ
allb.Add(b);
}
}
}
public class Bullet
{
public float Lx { get; set; }
public float Ly { get; set; }
public Size Size { get; set; }
public float Vx { get; set; }
public float Vy { get; set; }
public int DeleteTime { get; set; }
public bool Move()
{
this.Lx += Vx;
this.Ly += Vy;
DeleteTime--;
if (DeleteTime < 0) return true;
return false;
}
}
}