Hello all..
Been having some more trouble with the hit lines in my Air Hockey
game.
I have some hit lines drawn (and loaded into an array, Lines) around
the borders of my table to repel the puck backwards when hit. However,
when i pass it the x and y position (puck_x and puck_y, respectively)
of the image of the puck on screen, it will not detect it touching a
hit line.
However, if i use that IsInLine() in my MouseMove() method and pass it
the e.X and e.Y mouse positions - it will recognise when the mouse
cursor touches the line. I'm guessing it's something to do with the
puck_x and puck_y positions i'm passing to the IsInLine() method?
Take a look at the source code excerpt (these are the four important
methods)below if you've a chance. Any suggestions or comments are very
much appreciated!
Thanks.
Brian
---------------------------------------------------------------------------
private void gameDraw(Graphics g)
{
// image of puck is loaded and drawn
Image image = Image.FromFile("Puck.png");
g.DrawImage(image, puck_x, puck_y, 35, 35);
// redraws hit lines
foreach(Line l in Lines)
{
l.DrawLine(g, Color.FromArgb(192,192,192));
}
}
private void gameLoop(Object sender, EventArgs e)
{
foreach(Line l in Lines)
l.IsInLine(new Point(puck_x, puck_y));
puck_x += x_vel;
puck_y += y_vel;
//Called so the form will repaint itself.
Invalidate();
}
public bool IsInLine(Point pnt)
{
Pen p = new Pen(Color.Black,PenWidth);
GraphicsPath pth=new GraphicsPath();
pth.AddLine(StartPoint, EndPoint);
pth.Widen(p);
p.Dispose();
if(pth.IsVisible(pnt))
{
this.hit=true;
return true;
}
else
{
this.hit=false;
return false;
}
}
public void DrawLine(Graphics g,Color c, int x_vel, int y_vel)
{
Pen p=new Pen(c,PenWidth);
if(hit)
x_vel *= -1;
y_vel *= -1;
g.DrawLine(p,StartPoint,EndPoint);
p.Dispose();
}