using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace chongtu
{
public partial class Form1 : Form
{
PointD pos = new PointD(0,0);
PointD mv = new PointD(0,0);
PointD mpos = new PointD(0,0);
PointD velosity = new PointD(0.5,0.5);
bool bClick = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SetStyle(ControlStyles.OptimizedDoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint, true);
this.ClientSize = new Size(400,400);
this.FormBorderStyle = FormBorderStyle.FixedSingle;
Thread thread = new Thread(new ThreadStart(ThreadOn));
thread.Start();
}
private void ThreadOn()
{
int before = Environment.TickCount;
int frame = 0;
int FPS = 60;
while (!IsDisposed)
{
int now = Environment.TickCount;
int progress = now -before;
int ideal = (int)(frame * (1000.0f / FPS));
if(ideal > progress) Thread.Sleep(ideal - progress);
Invalidate();
frame++;
if (progress > 1000)
{
frame = 0;
before = now;
}
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
pos += mv;
if(pos.X > 400 - 32 && mv.X > 0)
mv.X = -mv.X;
if(pos.X < 0 && mv.X <0)
mv.X = -mv.X;
if(pos.Y > 400 - 32 && mv.Y >0)
mv.Y = -mv.Y;
if(pos.Y < 0 && mv.Y < 0)
mv.Y = -mv.Y;
Cricle(e.Graphics,
pos.X,
pos.Y,
Color.Red);
if(bClick)
{
line(e.Graphics,
new PointD(pos.X + 16,pos.Y + 16),
mpos,
Color.Blue);
}
}
private void Cricle(Graphics g, double x, double y, Color col)
{
SolidBrush brush = new SolidBrush(col);
g.FillEllipse(brush, (float)x, (float)y, 32, 32);
}
private void line(Graphics g, PointD p1, PointD p2, Color col)
{
Pen pen = new Pen(col,3);
g.DrawLine(pen, (float)p1.X,(float)p1.Y,
(float)p2.X,(float)p2.Y);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
bClick = true;
mpos = new PointD(e.X,e.Y);
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if(bClick)
{
mpos = new PointD(e.X,e.Y);
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
bClick = false;
double xdif = mpos.X - pos.X + 10;
double ydif = mpos.Y - pos.Y + 10;
if(xdif == 0)
{
mv.X = 0;
mv.Y = ydif;
}
else if(ydif == 0)
{
mv.X = xdif;
mv.Y = 0;
}
else
{
mv.X = xdif;
mv.Y = ydif;
}
}
}
}
}
class PointD
{
public double X;
public double Y;
public PointD(double x,double y)
{
X = x;
Y = y;
}
public static PointD operator+ (PointD pos1,PointD pos2)
{
PointD pos = new PointD(0,0);
pos.X = pos1.X + pos2.X;
pos.Y = pos1.Y + pos2.Y;
return pos;
}
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
老师的
-------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace collision
{
public partial class Form1 : Form
{
// ボールの座標
PointD pos = new PointD(0, 0);
// ボールの速度
PointD mv = new PointD(0, 0);
// マウスの座標
PointD mpos = new PointD(0, 0);
// ボールの加速度
PointD velosity = new PointD(0.5, 0.5);
// マウスクリックフラグ
bool bClick = false;
// 次のボール
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SetStyle(ControlStyles.OptimizedDoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint, true);
this.ClientSize = new Size(400, 400);
this.FormBorderStyle = FormBorderStyle.FixedSingle;
Thread thread = new Thread(new ThreadStart(ThreadOn));
thread.Start();
}
private void ThreadOn()
{
int before = Environment.TickCount;
int frame = 0;
int FPS = 60;
while (!IsDisposed)
{
int now = Environment.TickCount;
int progress = now - before;
int ideal = (int)(frame * (1000.0f / FPS));
if (ideal > progress) Thread.Sleep(ideal - progress);
frame++;
Invalidate();
if (progress > 1000)
{
frame = 0;
before = now;
}
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
// 座標の更新
pos += mv;
if (pos.X > 400 - 32 && mv.X > 0)
mv.X = -mv.X;
if (pos.X < 0 && mv.X < 0)
mv.X = -mv.X;
if (pos.Y > 400 - 32 && mv.Y > 0)
mv.Y = -mv.Y;
if (pos.Y < 0 && mv.Y < 0)
mv.Y = -mv.Y;
if (mv.X != 0)
{
if (mv.X > 0)
{
mv.X -= velosity.X;
if (mv.X <= 0)
mv.X = 0;
}
else
{
mv.X += velosity.X;
if (mv.X >= 0)
mv.X = 0;
}
}
if (mv.Y != 0)
{
if (mv.Y > 0)
{
mv.Y -= velosity.Y;
if (mv.Y <= 0)
mv.Y = 0;
}
else
{
mv.Y += velosity.Y;
if (mv.Y >= 0)
mv.Y = 0;
}
}
// 円の描画
circle(e.Graphics,
pos.X,
pos.Y,
Color.Red);
// 直線の描画
if (bClick)
{
line(e.Graphics,
new PointD(pos.X + 16, pos.Y + 16),
mpos,
Color.Blue);
}
}
// 円の描画
private void circle(Graphics g, double x, double y, Color col)
{
SolidBrush brush = new SolidBrush(col);
g.FillEllipse(brush, (float)x, (float)y, 32, 32);
}
// 直線の描画
private void line(Graphics g, PointD p1, PointD p2, Color col)
{
Pen pen = new Pen(col, 3);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot;
g.DrawLine(pen, (float)p1.X, (float)p1.Y,
(float)p2.X, (float)p2.Y);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
bClick = true;
mpos = new PointD(e.X, e.Y);
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (bClick)
{
mpos = new PointD(e.X, e.Y);
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
bClick = false;
double xdif = mpos.X - pos.X - 16.0;
double ydif = mpos.Y - pos.Y - 16.0;
xdif /= 5.0;
ydif /= 5.0;
if( xdif != 0 || ydif != 0 )
{
mv.X = xdif;
mv.Y = ydif;
double dif =
Math.Sqrt(xdif * xdif + ydif * ydif);
velosity.X = Math.Abs(xdif * 0.5 / dif);
velosity.Y = Math.Abs(ydif * 0.5 / dif);
}
}
}
}
// 引数がDoubleのクラスを生成
class PointD
{
public double X;
public double Y;
public PointD(double x,double y)
{
X = x;
Y = y;
}
public static PointD operator+ (PointD pos1, PointD pos2)
{
PointD pos = new PointD(0,0);
pos.X = pos1.X + pos2.X;
pos.Y = pos1.Y + pos2.Y;
return pos;
}
}
class Ball
{
PointD pos = new PointD(0,0);
PointD mv = new PointD(0,0);
PointD velosity = new PointD(0.5,0.5);
Color col;
public Ball(Color col)
{
this.col = col;
}
public PointF bpos
{
set { pos.X = ((PointF)value).X; pos.Y = ((PointF)value).Y; }
get { PointF pf = new PointF((float)pos.X, (float)pos.Y); return pf; }
}
public PointF mvs
{
set { mv.X = ((PointF)value).X; mv.Y = ((PointF)value).Y; }
get { PointF pf = new PointF((float)mv.X, (float)mv.Y); return pf; }
}
public PointF vel
{
set { velosity.X = ((PointF)value).X; velosity.Y = ((PointF)value).Y; }
get { PointF pf = new PointF((float)velosity.X, (float)velosity.Y); return pf; }
}
public void setNewPos()
{
pos += mv;
if (mv.X != 0)
{
if (mv.X > 0)
{
mv.X -= velosity.X;
if (mv.X < 0)
mv.X = 0;
}
else if (mv.X < 0)
{
mv.X += velosity.X;
if (mv.X > 0)
mv.X = 0;
}
}
if (mv.Y != 0)
{
if (mv.Y > 0)
{
mv.Y -= velosity.Y;
if (mv.Y < 0)
mv.Y = 0;
}
else if (mv.Y < 0)
{
mv.Y += velosity.Y;
if (mv.Y > 0)
mv.Y = 0;
}
}
if ((pos.X > 400 - 32 && mv.X > 0) || (pos.X < 0 && mv.X < 0))
mv.X = -mv.X;
if ((pos.Y > 400 - 32 && mv.Y > 0) || (pos.Y < 0 && mv.Y < 0))
mv.Y = -mv.Y;
}
// 円の描画
private void circle(Graphics g)
{
SolidBrush brush = new SolidBrush(col);
g.FillEllipse(brush, bpos.X, bpos.Y, 32, 32);
}
}
}