public void Restart() { enableTarget = false; preEnableTarget = false; ball = new EllipseObject(new PointF(size.X / 2.0f, size.Y / 2.0f), 20.0f); sumRect = 0; changeTarget = 5; startFlag = false; finishFlag = false; frame = 0; for (int i = 0; i < (float)blockWH.X; i++) { for (int j = 0; j < (float)blockWH.Y; j++) { blocks[i * blockWH.Y + j].color = Color.FromArgb(255, 160, 160, 160); blocks[i * blockWH.Y + j].target = new PointF( size.X * ((float)i + 0.5f) / (float)blockWH.X, size.Y * ((float)j + (i % 2 == 0 ? 0.25f : 0.75f)) / (float)blockWH.Y ); blocks[i * blockWH.Y + j].Position = new PointF( size.X * 0.5f, size.Y * 0.5f ); blocks[i * blockWH.Y + j].targetSize = new PointF(40.0f, 40.0f); blocks[i * blockWH.Y + j].Radian = (float)(i * blockWH.Y + j) * (float)(Math.PI) * 0.01f; blocks[i * blockWH.Y + j].targetRadian = blocks[i * blockWH.Y + j].Radian; } } }
public World(PointF size) { this.size = size; AddRectObject(new RectObject( new PointF( -100.0f, size.Y * 0.5f ), new PointF( 200.0f, size.Y ) )); AddRectObject(new RectObject( new PointF( size.X + 100.0f, size.Y * 0.5f ), new PointF( 200.0f, size.Y ) )); AddRectObject(new RectObject( new PointF( size.X * 0.5f, -100.0f ), new PointF( size.X, 200.0f ) )); AddRectObject(new RectObject( new PointF( size.X * 0.5f, size.Y + 100.0f ), new PointF( size.X, 200.0f ) )); RectObject block = new RectObject( new PointF(size.X / 2.0f, size.Y / 2.0f), new PointF(300.0f, 100.0f), 0.0f, 0.4f ); blocks.Add(block); block.targetRadian += 1.0f; AddRectObject(block); ball = new EllipseObject(new PointF(size.X / 2.0f, size.Y / 8.0f), 0.0f); enableTarget = false; }
public void Render(Graphics g) { g.DrawString( "スペースキーで当たり判定表示/非表示\nGキーで重力考慮/無視\nRキーで矩形回転/停止\nスクロールでボール半径変更\nクリックでボール位置移動", new Font("Ubuntu", 18), new SolidBrush(Color.FromArgb(128, 30, 30, 30)), new PointF(20.0f, 20.0f) ); foreach (RectObject rect in rects) { rect.Render(g); if (view) { rect.RenderHitArea(g, ball.Position, ball.Radius); } } ball.Render(g); PointF pos = mouse; PointF v = new PointF( ball.Position.X - mouse.X, ball.Position.Y - mouse.Y ); if (!gr) { foreach (RectObject rect in rects) { if (rect.Hit(mouse, ball.Radius) != 0) { (pos, v) = rect.GetReflection(mouse, ball.Position, ball.Radius); } } Pen p = new Pen(Color.FromArgb(255, 255, 0, 0), 3.0f); g.DrawEllipse(p, pos.X - ball.Radius, pos.Y - ball.Radius, ball.Radius * 2.0f, ball.Radius * 2.0f); g.DrawLine(p, ball.Position, pos); g.DrawLine(p, pos, new PointF(pos.X + v.X, pos.Y + v.Y)); } else { EllipseObject ball_ = new EllipseObject(ball.Position, ball.Radius, 0.8f); ball_.Velocity = new PointF(v.X * -0.1f, v.Y * -0.1f); ball_.Friction = 0.9f; PointF pos_ = ball_.Position; int len = 100; for (int i = 0; i < len; i++) { ball_.Update(); ball_.Acceleration = new PointF(0.0f, 0.7f); ball_.Friction = 0.99f; Pen p = new Pen(Color.FromArgb(255 - 255 * i / len, 255, 0, 0), 3.0f); foreach (RectObject rect in rects) { if (rect.Hit(ball_.Position, ball_.Radius) != 0) { (ball_.Position, ball_.Velocity) = rect.GetReflection(ball_.Position, pos_, ball_.Radius); ball.Velocity = new PointF(ball.Velocity.X * 0.9f, ball.Velocity.Y * 0.9f); g.DrawEllipse( p, ball_.Position.X - ball_.Radius, ball_.Position.Y - ball_.Radius, ball_.Radius * 2.0f, ball_.Radius * 2.0f ); } } if (ball_.Position.X - ball_.Radius < 0.0f) { ball_.Position = new PointF(ball_.Radius, ball_.Position.Y); } if (ball_.Position.X + ball_.Radius > size.X) { ball_.Position = new PointF(size.X - ball_.Radius, ball_.Position.Y); } if (ball_.Position.Y - ball_.Radius < 0.0f) { ball_.Position = new PointF(ball_.Position.X, ball_.Radius); } if (ball_.Position.Y + ball_.Radius > size.Y) { ball_.Position = new PointF(ball_.Position.X, size.Y - ball_.Radius); } g.DrawLine(p, pos_, ball_.Position); pos_ = ball_.Position; } } }