public World(PointF size, Point blockWH) { this.size = size; this.blockWH = blockWH; 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 ) )); for (int i = 0; i < blockWH.X; i++) { for (int j = 0; j < blockWH.Y; j++) { RectObject block = new RectObject( new PointF(size.X / 2.0f, size.Y / 2.0f), new PointF(100.0f, 100.0f), 0.0f, 0.4f ); blocks.Add(block); AddRectObject(block); } } Restart(); }
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 Update() { frame++; //foreach (RectObject rect in rects) rect.Update(); for (int i = 0; i < rects.Count() && i < frame * 3; i++) { rects[i].Update(); } foreach (RectObject block in blocks) { block.targetRadian += 0.01f; } if (finishFlag) { foreach (RectObject block in blocks) { block.targetSize = new PointF(0.0f, 0.0f); } } if (!startFlag || finishFlag) { return; } ball.Update(); 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); } if (enableTarget) { ball.Acceleration = new PointF((target.X - ball.Position.X) * 0.1f, (target.Y - ball.Position.Y) * 0.1f); ball.Friction = 0.9f; } else { ball.Acceleration = new PointF(0.0f, 0.7f); ball.Friction = 0.99f; } if (preEnableTarget == true && enableTarget == false) { changeTarget--; } preEnableTarget = enableTarget; if (enableTarget) { enableTarget = false; return; } for (int i = 0; i < rects.Count(); i++) { RectObject rect = rects[i]; if (rect.Hit(ball.Position, ball.Radius) == 0) { continue; } (PointF pos, PointF v) = rect.GetReflection( ball.Position, new PointF( ball.Position.X - ball.Velocity.X, ball.Position.Y - ball.Velocity.Y ), ball.Radius ); ball.Position = pos; ball.Velocity = v; if (blocks.Contains(rect)) { rect.Velocity = new PointF( (rect.Velocity.X - ball.Velocity.X) * 2.0f, (rect.Velocity.Y - ball.Velocity.Y) * 2.0f ); rect.color = Color.FromArgb(((int)rect.color.A - 64) < 0 ? 0 : rect.color.A - 64, rect.color.R, rect.color.G, rect.color.B); if (rect.color.A != 0) { sumRect++; } } ball.Velocity = new PointF( ball.Velocity.X * 0.9f, ball.Velocity.Y * 0.9f ); } if (changeTarget == 0 && Math.Abs(ball.Velocity.X) < 1.0f && Math.Abs(ball.Velocity.Y) < 1.0f && ball.Position.Y >= size.Y - ball.Radius) { finishFlag = true; } }
public void AddRectObject(RectObject rect) { rects.Add(rect); }
public void Update() { frame++; foreach (RectObject rect in rects) { rect.Update(); } if (!r) { foreach (RectObject block in blocks) { block.targetRadian += 0.01f; } } ball.Update(); 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); } if (enableTarget) { ball.Position = target; } if (enableTarget) { enableTarget = false; } for (int i = 0; i < rects.Count(); i++) { RectObject rect = rects[i]; if (rect.Hit(ball.Position, ball.Radius) == 0) { continue; } (PointF pos, PointF v) = rect.GetReflection( ball.Position, new PointF( ball.Position.X - ball.Velocity.X, ball.Position.Y - ball.Velocity.Y ), ball.Radius ); ball.Position = pos; ball.Velocity = v; ball.Velocity = new PointF( ball.Velocity.X * 0.9f, ball.Velocity.Y * 0.9f ); } }