rendered paste bodyusing System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Threading;
namespace CannonBallGame
{
class Cannon
{
private Panel box;
private float angle;
private int velocity;
private int height;
private int current = 0;
//private static int xSize = 10;
//private static int ySize = 10;
private SizeF pSize = new SizeF(5.0f, 5.0f);
private float currentX = 0;
private float currentY;
PointF pt = new PointF();
Matrix xform = new Matrix();
public Cannon(Panel p, int z, int x, int y)
{
box = p;
angle = z * (float)(Math.PI/180);
velocity = x;
height = y;
currentY = height;
xform.Scale(1.0f, -1.0f);
xform.Translate(0, -box.Height);
}
/**
* Use code below to check if projectile has hit a target
* if(projectile.IntersectsWith(target)){
* score++;
* cancel projectile animation;
* remove the target from window;
* }
**/
//// Below follows projectile trajectory calculations
//// Convert IA to radian, radian = degrees * (PI/180)
//angle = Convert.ToInt32( (IA * (Math.PI/180)) );
//// Find X and Y values at time current
//int currentX = ((velocity * Math.Cos(angle) * current)) / 2;
//int currentY = (((velocity * Math.Sin(angle) * current) - (.5 * 9.8 * (current * current))) / 2);
public void draw()
{
Graphics g = box.CreateGraphics();
g.Transform = xform;
Brush blackBrush = new SolidBrush(Color.Black);
pt = new PointF(height, 0);
RectangleF rectA = new RectangleF(pt, pSize);
g.FillEllipse(blackBrush, rectA);
}
public void move()
{
Graphics g = box.CreateGraphics();
g.Transform = xform;
Brush blackBrush = new SolidBrush(Color.Black);
Brush bgBrush = new SolidBrush(box.BackColor);
// Visually remove "old" projectile by drawing over it with background color
RectangleF rect1 = new RectangleF(pt, pSize);
g.FillEllipse(bgBrush, rect1);
//current++;
//float distance = (float)(((velocity * Math.Cos(angle) * current)) / 9.8f) *
// (float)(velocity * Math.Sin(angle) +
// Math.Sqrt(((velocity * Math.Sin(angle)) *
// (velocity * Math.Sin(angle))) + (2 * 9.8f * height)));
//currentX = distance;
// Find X and Y values at time current
currentX = (float)((velocity * Math.Cos(angle) * current)) / 2.0f;
currentY = (float)(((velocity * Math.Sin(angle) * current) - (.5f * 9.8f * (current * current))) / 2.0f);
//float heights = (float)((currentX * Math.Tan(angle)) - (((9.8f * currentX) *
// (9.8f * currentX)) / (2 * ((velocity * Math.Cos(angle)) *
// (velocity * Math.Cos(angle))))) + height);
//currentY = -heights;
PointF tempPT = new PointF(currentX, -currentY);
pt = tempPT;
// Draw the projectile in its new position
RectangleF rect2 = new RectangleF(pt, pSize);
g.FillEllipse(blackBrush, rect2);
}
public void fire()
{
draw();
pt = new PointF(currentX, -currentY);
while (currentX < box.Size.Width)
{
current = current + 2;
move();
Thread.Sleep(100);
}
Graphics g = box.CreateGraphics();
Brush bgBrush = new SolidBrush(box.BackColor);
RectangleF rectB = new RectangleF(pt, pSize);
g.FillEllipse(bgBrush, rectB);
}
}
}