Hit testing lines are not working!

B

Brian

Hello all..

Am working on an Air Hockey game... have an table loaded into a
picture box. The borders of the table are slightly slanted. Am using
hit testing lines with GDI+ to manipulate the puck moving around. I
want the puck is bounce when it hits a border (specified by the
hitlines).

Retreieved some info on hit testing lines from Bob Powell's GDI+ FAQ
(very useful!) but i'm fairly new at the idea of hit testing and am
wondering what exactly is wrong with this code.

Sorry about the abundance of code but my code isn't exactly safe
enough to post a sample (so basically i'm saying, it's fairly badly
written code). I thought it might be easier if people just tried to
run this code themselves and let me know of the results.

Anu suggestions will be much appreciated!

Thanks in advance!


using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace AirHockey
{
/// <summary>
/// Summary description for Table.
/// </summary>
public class frmTable : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox picTable;
private System.Windows.Forms.Label lblPlayer2Score;
private System.Windows.Forms.Label lblPlayer1Score;
private System.Windows.Forms.Label lblPlayer2Name;
private System.Windows.Forms.Label lblPlayer1Name;
Timer gameClock;
int puck_x;
int puck_y;
int x_vel = -5;
int y_vel = -5;

private LineArray Lines=new LineArray();
Line lineLeft;
Line lineRight;
Line lineTopLeft;
Line lineTopRight;
Line lineBottomLeft;
Line lineBottomRight;
bool hitLinesDrawn = false;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public frmTable()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
///

private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(frmTable));
this.picTable = new System.Windows.Forms.PictureBox();
this.lblPlayer2Score = new System.Windows.Forms.Label();
this.lblPlayer1Score = new System.Windows.Forms.Label();
this.lblPlayer2Name = new System.Windows.Forms.Label();
this.lblPlayer1Name = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// picTable
//
this.picTable.BackColor = System.Drawing.Color.Transparent;
this.picTable.Image =
((System.Drawing.Image)(resources.GetObject("picTable.Image")));
this.picTable.Location = new System.Drawing.Point(64, -24);
this.picTable.Name = "picTable";
this.picTable.Size = new System.Drawing.Size(848, 720);
this.picTable.SizeMode =
System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.picTable.TabIndex = 0;
this.picTable.TabStop = false;
this.picTable.Paint += new
System.Windows.Forms.PaintEventHandler(this.picTable_Paint);
this.picTable.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.picTable_MouseMove);
//
// lblPlayer2Score
//
this.lblPlayer2Score.Anchor =
System.Windows.Forms.AnchorStyles.None;
this.lblPlayer2Score.BackColor = System.Drawing.Color.Black;
this.lblPlayer2Score.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPlayer2Score.Cursor = System.Windows.Forms.Cursors.Default;
this.lblPlayer2Score.FlatStyle =
System.Windows.Forms.FlatStyle.System;
this.lblPlayer2Score.Font = new System.Drawing.Font("OCR A
Extended", 48F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer2Score.ForeColor = System.Drawing.Color.Red;
this.lblPlayer2Score.ImageAlign =
System.Drawing.ContentAlignment.TopLeft;
this.lblPlayer2Score.Location = new System.Drawing.Point(16, 56);
this.lblPlayer2Score.Name = "lblPlayer2Score";
this.lblPlayer2Score.Size = new System.Drawing.Size(144, 72);
this.lblPlayer2Score.TabIndex = 1;
this.lblPlayer2Score.Text = "00";
this.lblPlayer2Score.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer1Score
//
this.lblPlayer1Score.Anchor =
System.Windows.Forms.AnchorStyles.None;
this.lblPlayer1Score.BackColor = System.Drawing.Color.Black;
this.lblPlayer1Score.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPlayer1Score.FlatStyle =
System.Windows.Forms.FlatStyle.System;
this.lblPlayer1Score.Font = new System.Drawing.Font("OCR A
Extended", 48F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer1Score.ForeColor = System.Drawing.Color.Red;
this.lblPlayer1Score.ImageAlign =
System.Drawing.ContentAlignment.TopLeft;
this.lblPlayer1Score.Location = new System.Drawing.Point(816, 544);
this.lblPlayer1Score.Name = "lblPlayer1Score";
this.lblPlayer1Score.Size = new System.Drawing.Size(144, 72);
this.lblPlayer1Score.TabIndex = 2;
this.lblPlayer1Score.Text = "00";
this.lblPlayer1Score.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer2Name
//
this.lblPlayer2Name.BackColor = System.Drawing.Color.Transparent;
this.lblPlayer2Name.FlatStyle =
System.Windows.Forms.FlatStyle.Flat;
this.lblPlayer2Name.Font = new System.Drawing.Font("Microsoft Sans
Serif", 15.75F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer2Name.ForeColor = System.Drawing.Color.Yellow;
this.lblPlayer2Name.Location = new System.Drawing.Point(16, 24);
this.lblPlayer2Name.Name = "lblPlayer2Name";
this.lblPlayer2Name.Size = new System.Drawing.Size(144, 32);
this.lblPlayer2Name.TabIndex = 3;
this.lblPlayer2Name.Text = "Player2Name";
this.lblPlayer2Name.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer1Name
//
this.lblPlayer1Name.BackColor = System.Drawing.Color.Transparent;
this.lblPlayer1Name.Font = new System.Drawing.Font("Microsoft Sans
Serif", 15.75F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer1Name.ForeColor = System.Drawing.Color.Yellow;
this.lblPlayer1Name.Location = new System.Drawing.Point(816, 512);
this.lblPlayer1Name.Name = "lblPlayer1Name";
this.lblPlayer1Name.Size = new System.Drawing.Size(144, 32);
this.lblPlayer1Name.TabIndex = 4;
this.lblPlayer1Name.Text = "Player1Name";
this.lblPlayer1Name.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.TabIndex = 5;
this.label1.Text = "label1";
//
// frmTable
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackgroundImage =
((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(973, 647);
this.Controls.Add(this.label1);
this.Controls.Add(this.lblPlayer1Name);
this.Controls.Add(this.lblPlayer2Name);
this.Controls.Add(this.lblPlayer1Score);
this.Controls.Add(this.lblPlayer2Score);
this.Controls.Add(this.picTable);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Name = "frmTable";
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Air Hockey";
this.Load += new System.EventHandler(this.frmTable_Load);
this.Closed += new System.EventHandler(this.frmTable_Closed);
this.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.picTable_MouseMove);
this.ResumeLayout(false);

}
#endregion

private void frmTable_Closed(object sender, System.EventArgs e)
{
frmMainMenu MainMenu = new frmMainMenu();
MainMenu.Show();
}

private void gameLoop(Object sender, EventArgs e)
{
puck_x += x_vel;
puck_y += y_vel;

//Called so the form will repaint itself.
Invalidate();
}

private void gameDraw(Graphics g)
{
// image of puck is loaded and drawn
Image image = Image.FromFile("C:/Documents and Settings/All
Users.WINDOWS/Documents/Brian's Documents/AirHockey/Puck.png");
g.DrawImage(image, puck_x, puck_y, 35, 35);

foreach(Line l in Lines)
{
l.DrawLine(g, Color.FromArgb(192,192,192));
}
}

public void setName(string pName)
{
lblPlayer1Name.Text = pName;
}

private void frmTable_Load(object sender, System.EventArgs e)
{
gameClock = new Timer();

//Start the game clock to control game speed
gameClock.Interval = 15;
gameClock.Tick += new EventHandler(gameLoop);
gameClock.Start();

puck_x = 200;
puck_y = 200;
}

private void picTable_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
label1.Text = e.X + " " + e.Y;

Graphics g=CreateGraphics();

if(hitLinesDrawn != true)
{
DrawHitLines(g);
}
else
{
foreach(Line l in Lines)
{
if(l.IsInLine(new Point(e.X,e.Y)))
l.DrawLine(g,Color.Red);
else
l.DrawLine(g,Color.Black);
}
}
}

private void picTable_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
gameDraw(e.Graphics);
}

private void DrawHitLines(Graphics g)
{
// set of hit lines for sides of table
lineLeft = new Line();
lineLeft.StartPoint=new Point( 164, 77 );
lineLeft.EndPoint=new Point( 84, 594 );
lineLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineLeft);

lineRight = new Line();
lineRight.StartPoint=new Point( 631, 77 );
lineRight.EndPoint=new Point( 712, 594 );
lineRight.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineRight);

// set of hit lines for top of table
lineTopLeft = new Line();
lineTopLeft.StartPoint=new Point( 166, 76 );
lineTopLeft.EndPoint=new Point( 340, 76 );
lineTopLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineTopLeft);

lineTopRight = new Line();
lineTopRight.StartPoint=new Point( 630, 76 );
lineTopRight.EndPoint=new Point( 455, 76 );
lineTopRight.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineTopRight);

// set of hit lines for top of table
lineBottomLeft = new Line();
lineBottomLeft.StartPoint=new Point( 86, 595 );
lineBottomLeft.EndPoint=new Point( 328, 595 );
lineBottomLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineBottomLeft);

lineBottomRight = new Line();
lineBottomRight.StartPoint=new Point( 711, 595 );
lineBottomRight.EndPoint=new Point( 467, 595 );
lineBottomRight.DrawLine(g, Color.Red);
Lines.Add(lineBottomRight);

hitLinesDrawn = true;
}
}

public class Line
{
public Point StartPoint;
public Point EndPoint;
public int PenWidth;

public void DrawLine(Graphics g,Color c)
{
Pen p=new Pen(c,PenWidth);
g.DrawLine(p,StartPoint,EndPoint);
p.Dispose();
}

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))
{
return true;
}
else
{
return false;
}
}
}

public class LineArray : CollectionBase
{
public void Add(Line l)
{
List.Add(l);
}

public Line this[int index]
{
get
{
return (Line)List[index];
}
set
{
List[index]=value;
}
}
}
}
 
B

Bob Powell [MVP]

Hello Brian.
The line hit-testing is working just fine. The problem lies in the fact that
you are drawing in the mousemove event and then immediately overwriting it
when the Paint comes along..

Now what did I tell my readers about using CreateGraphics??? ;-) I don't
beleive you fell foul of the #1 FAQ question!

You need to set a flag somewhere to show that the line has been detected and
then take notice of the flag when you redraw the table.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





Brian said:
Hello all..

Am working on an Air Hockey game... have an table loaded into a
picture box. The borders of the table are slightly slanted. Am using
hit testing lines with GDI+ to manipulate the puck moving around. I
want the puck is bounce when it hits a border (specified by the
hitlines).

Retreieved some info on hit testing lines from Bob Powell's GDI+ FAQ
(very useful!) but i'm fairly new at the idea of hit testing and am
wondering what exactly is wrong with this code.

Sorry about the abundance of code but my code isn't exactly safe
enough to post a sample (so basically i'm saying, it's fairly badly
written code). I thought it might be easier if people just tried to
run this code themselves and let me know of the results.

Anu suggestions will be much appreciated!

Thanks in advance!


using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace AirHockey
{
/// <summary>
/// Summary description for Table.
/// </summary>
public class frmTable : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox picTable;
private System.Windows.Forms.Label lblPlayer2Score;
private System.Windows.Forms.Label lblPlayer1Score;
private System.Windows.Forms.Label lblPlayer2Name;
private System.Windows.Forms.Label lblPlayer1Name;
Timer gameClock;
int puck_x;
int puck_y;
int x_vel = -5;
int y_vel = -5;

private LineArray Lines=new LineArray();
Line lineLeft;
Line lineRight;
Line lineTopLeft;
Line lineTopRight;
Line lineBottomLeft;
Line lineBottomRight;
bool hitLinesDrawn = false;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public frmTable()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
///

private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(frmTable));
this.picTable = new System.Windows.Forms.PictureBox();
this.lblPlayer2Score = new System.Windows.Forms.Label();
this.lblPlayer1Score = new System.Windows.Forms.Label();
this.lblPlayer2Name = new System.Windows.Forms.Label();
this.lblPlayer1Name = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// picTable
//
this.picTable.BackColor = System.Drawing.Color.Transparent;
this.picTable.Image =
((System.Drawing.Image)(resources.GetObject("picTable.Image")));
this.picTable.Location = new System.Drawing.Point(64, -24);
this.picTable.Name = "picTable";
this.picTable.Size = new System.Drawing.Size(848, 720);
this.picTable.SizeMode =
System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.picTable.TabIndex = 0;
this.picTable.TabStop = false;
this.picTable.Paint += new
System.Windows.Forms.PaintEventHandler(this.picTable_Paint);
this.picTable.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.picTable_MouseMove);
//
// lblPlayer2Score
//
this.lblPlayer2Score.Anchor =
System.Windows.Forms.AnchorStyles.None;
this.lblPlayer2Score.BackColor = System.Drawing.Color.Black;
this.lblPlayer2Score.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPlayer2Score.Cursor = System.Windows.Forms.Cursors.Default;
this.lblPlayer2Score.FlatStyle =
System.Windows.Forms.FlatStyle.System;
this.lblPlayer2Score.Font = new System.Drawing.Font("OCR A
Extended", 48F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer2Score.ForeColor = System.Drawing.Color.Red;
this.lblPlayer2Score.ImageAlign =
System.Drawing.ContentAlignment.TopLeft;
this.lblPlayer2Score.Location = new System.Drawing.Point(16, 56);
this.lblPlayer2Score.Name = "lblPlayer2Score";
this.lblPlayer2Score.Size = new System.Drawing.Size(144, 72);
this.lblPlayer2Score.TabIndex = 1;
this.lblPlayer2Score.Text = "00";
this.lblPlayer2Score.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer1Score
//
this.lblPlayer1Score.Anchor =
System.Windows.Forms.AnchorStyles.None;
this.lblPlayer1Score.BackColor = System.Drawing.Color.Black;
this.lblPlayer1Score.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPlayer1Score.FlatStyle =
System.Windows.Forms.FlatStyle.System;
this.lblPlayer1Score.Font = new System.Drawing.Font("OCR A
Extended", 48F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer1Score.ForeColor = System.Drawing.Color.Red;
this.lblPlayer1Score.ImageAlign =
System.Drawing.ContentAlignment.TopLeft;
this.lblPlayer1Score.Location = new System.Drawing.Point(816, 544);
this.lblPlayer1Score.Name = "lblPlayer1Score";
this.lblPlayer1Score.Size = new System.Drawing.Size(144, 72);
this.lblPlayer1Score.TabIndex = 2;
this.lblPlayer1Score.Text = "00";
this.lblPlayer1Score.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer2Name
//
this.lblPlayer2Name.BackColor = System.Drawing.Color.Transparent;
this.lblPlayer2Name.FlatStyle =
System.Windows.Forms.FlatStyle.Flat;
this.lblPlayer2Name.Font = new System.Drawing.Font("Microsoft Sans
Serif", 15.75F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer2Name.ForeColor = System.Drawing.Color.Yellow;
this.lblPlayer2Name.Location = new System.Drawing.Point(16, 24);
this.lblPlayer2Name.Name = "lblPlayer2Name";
this.lblPlayer2Name.Size = new System.Drawing.Size(144, 32);
this.lblPlayer2Name.TabIndex = 3;
this.lblPlayer2Name.Text = "Player2Name";
this.lblPlayer2Name.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer1Name
//
this.lblPlayer1Name.BackColor = System.Drawing.Color.Transparent;
this.lblPlayer1Name.Font = new System.Drawing.Font("Microsoft Sans
Serif", 15.75F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer1Name.ForeColor = System.Drawing.Color.Yellow;
this.lblPlayer1Name.Location = new System.Drawing.Point(816, 512);
this.lblPlayer1Name.Name = "lblPlayer1Name";
this.lblPlayer1Name.Size = new System.Drawing.Size(144, 32);
this.lblPlayer1Name.TabIndex = 4;
this.lblPlayer1Name.Text = "Player1Name";
this.lblPlayer1Name.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.TabIndex = 5;
this.label1.Text = "label1";
//
// frmTable
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackgroundImage =
((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(973, 647);
this.Controls.Add(this.label1);
this.Controls.Add(this.lblPlayer1Name);
this.Controls.Add(this.lblPlayer2Name);
this.Controls.Add(this.lblPlayer1Score);
this.Controls.Add(this.lblPlayer2Score);
this.Controls.Add(this.picTable);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Name = "frmTable";
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Air Hockey";
this.Load += new System.EventHandler(this.frmTable_Load);
this.Closed += new System.EventHandler(this.frmTable_Closed);
this.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.picTable_MouseMove);
this.ResumeLayout(false);

}
#endregion

private void frmTable_Closed(object sender, System.EventArgs e)
{
frmMainMenu MainMenu = new frmMainMenu();
MainMenu.Show();
}

private void gameLoop(Object sender, EventArgs e)
{
puck_x += x_vel;
puck_y += y_vel;

//Called so the form will repaint itself.
Invalidate();
}

private void gameDraw(Graphics g)
{
// image of puck is loaded and drawn
Image image = Image.FromFile("C:/Documents and Settings/All
Users.WINDOWS/Documents/Brian's Documents/AirHockey/Puck.png");
g.DrawImage(image, puck_x, puck_y, 35, 35);

foreach(Line l in Lines)
{
l.DrawLine(g, Color.FromArgb(192,192,192));
}
}

public void setName(string pName)
{
lblPlayer1Name.Text = pName;
}

private void frmTable_Load(object sender, System.EventArgs e)
{
gameClock = new Timer();

//Start the game clock to control game speed
gameClock.Interval = 15;
gameClock.Tick += new EventHandler(gameLoop);
gameClock.Start();

puck_x = 200;
puck_y = 200;
}

private void picTable_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
label1.Text = e.X + " " + e.Y;

Graphics g=CreateGraphics();

if(hitLinesDrawn != true)
{
DrawHitLines(g);
}
else
{
foreach(Line l in Lines)
{
if(l.IsInLine(new Point(e.X,e.Y)))
l.DrawLine(g,Color.Red);
else
l.DrawLine(g,Color.Black);
}
}
}

private void picTable_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
gameDraw(e.Graphics);
}

private void DrawHitLines(Graphics g)
{
// set of hit lines for sides of table
lineLeft = new Line();
lineLeft.StartPoint=new Point( 164, 77 );
lineLeft.EndPoint=new Point( 84, 594 );
lineLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineLeft);

lineRight = new Line();
lineRight.StartPoint=new Point( 631, 77 );
lineRight.EndPoint=new Point( 712, 594 );
lineRight.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineRight);

// set of hit lines for top of table
lineTopLeft = new Line();
lineTopLeft.StartPoint=new Point( 166, 76 );
lineTopLeft.EndPoint=new Point( 340, 76 );
lineTopLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineTopLeft);

lineTopRight = new Line();
lineTopRight.StartPoint=new Point( 630, 76 );
lineTopRight.EndPoint=new Point( 455, 76 );
lineTopRight.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineTopRight);

// set of hit lines for top of table
lineBottomLeft = new Line();
lineBottomLeft.StartPoint=new Point( 86, 595 );
lineBottomLeft.EndPoint=new Point( 328, 595 );
lineBottomLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineBottomLeft);

lineBottomRight = new Line();
lineBottomRight.StartPoint=new Point( 711, 595 );
lineBottomRight.EndPoint=new Point( 467, 595 );
lineBottomRight.DrawLine(g, Color.Red);
Lines.Add(lineBottomRight);

hitLinesDrawn = true;
}
}

public class Line
{
public Point StartPoint;
public Point EndPoint;
public int PenWidth;

public void DrawLine(Graphics g,Color c)
{
Pen p=new Pen(c,PenWidth);
g.DrawLine(p,StartPoint,EndPoint);
p.Dispose();
}

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))
{
return true;
}
else
{
return false;
}
}
}

public class LineArray : CollectionBase
{
public void Add(Line l)
{
List.Add(l);
}

public Line this[int index]
{
get
{
return (Line)List[index];
}
set
{
List[index]=value;
}
}
}
}
 
B

Bob Powell [MVP]

Below my signature is my slightly modified version of your game.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


---------------------------------------------------------------------
using System;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace AirHockey

{

/// <summary>

/// Summary description for Table.

/// </summary>

public class frmTable : System.Windows.Forms.Form

{

private System.Windows.Forms.PictureBox picTable;

private System.Windows.Forms.Label lblPlayer2Score;

private System.Windows.Forms.Label lblPlayer1Score;

private System.Windows.Forms.Label lblPlayer2Name;

private System.Windows.Forms.Label lblPlayer1Name;

private System.Windows.Forms.Label label1;

private System.Windows.Forms.MainMenu frmMainMenu;

Timer gameClock;

int puck_x;

int puck_y;

int x_vel = -5;

int y_vel = -5;

private LineArray Lines=new LineArray();

Line lineLeft;

Line lineRight;

Line lineTopLeft;

Line lineTopRight;

Line lineBottomLeft;

Line lineBottomRight;

bool hitLinesDrawn = false;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public frmTable()

{

InitializeComponent();

this.CreateLines();

}

public static void Main()

{

Application.Run(new frmTable());

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

///

private void InitializeComponent()

{

System.Resources.ResourceManager resources = new

System.Resources.ResourceManager(typeof(frmTable));

this.picTable = new System.Windows.Forms.PictureBox();

this.lblPlayer2Score = new System.Windows.Forms.Label();

this.lblPlayer1Score = new System.Windows.Forms.Label();

this.lblPlayer2Name = new System.Windows.Forms.Label();

this.lblPlayer1Name = new System.Windows.Forms.Label();

this.label1 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// picTable

//

this.picTable.BackColor = System.Drawing.Color.Transparent;

this.picTable.Image =

((System.Drawing.Image)(resources.GetObject("picTable.Image")));

this.picTable.Location = new System.Drawing.Point(64, -24);

this.picTable.Name = "picTable";

this.picTable.Size = new System.Drawing.Size(848, 720);

this.picTable.SizeMode =

System.Windows.Forms.PictureBoxSizeMode.CenterImage;

this.picTable.TabIndex = 0;

this.picTable.TabStop = false;

this.picTable.Paint += new

System.Windows.Forms.PaintEventHandler(this.picTable_Paint);

this.picTable.MouseMove += new

System.Windows.Forms.MouseEventHandler(this.picTable_MouseMove);

//

// lblPlayer2Score

//

this.lblPlayer2Score.Anchor =

System.Windows.Forms.AnchorStyles.None;

this.lblPlayer2Score.BackColor = System.Drawing.Color.Black;

this.lblPlayer2Score.BorderStyle =

System.Windows.Forms.BorderStyle.Fixed3D;

this.lblPlayer2Score.Cursor = System.Windows.Forms.Cursors.Default;

this.lblPlayer2Score.FlatStyle =

System.Windows.Forms.FlatStyle.System;

this.lblPlayer2Score.Font = new System.Drawing.Font("OCR A Extended", 48F,
System.Drawing.FontStyle.Bold,

System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.lblPlayer2Score.ForeColor = System.Drawing.Color.Red;

this.lblPlayer2Score.ImageAlign =

System.Drawing.ContentAlignment.TopLeft;

this.lblPlayer2Score.Location = new System.Drawing.Point(16, 56);

this.lblPlayer2Score.Name = "lblPlayer2Score";

this.lblPlayer2Score.Size = new System.Drawing.Size(144, 72);

this.lblPlayer2Score.TabIndex = 1;

this.lblPlayer2Score.Text = "00";

this.lblPlayer2Score.TextAlign =

System.Drawing.ContentAlignment.MiddleCenter;

//

// lblPlayer1Score

//

this.lblPlayer1Score.Anchor =

System.Windows.Forms.AnchorStyles.None;

this.lblPlayer1Score.BackColor = System.Drawing.Color.Black;

this.lblPlayer1Score.BorderStyle =

System.Windows.Forms.BorderStyle.Fixed3D;

this.lblPlayer1Score.FlatStyle =

System.Windows.Forms.FlatStyle.System;

this.lblPlayer1Score.Font = new System.Drawing.Font("OCR AExtended", 48F,
System.Drawing.FontStyle.Bold,

System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.lblPlayer1Score.ForeColor = System.Drawing.Color.Red;

this.lblPlayer1Score.ImageAlign =

System.Drawing.ContentAlignment.TopLeft;

this.lblPlayer1Score.Location = new System.Drawing.Point(816, 544);

this.lblPlayer1Score.Name = "lblPlayer1Score";

this.lblPlayer1Score.Size = new System.Drawing.Size(144, 72);

this.lblPlayer1Score.TabIndex = 2;

this.lblPlayer1Score.Text = "00";

this.lblPlayer1Score.TextAlign =

System.Drawing.ContentAlignment.MiddleCenter;

//

// lblPlayer2Name

//

this.lblPlayer2Name.BackColor = System.Drawing.Color.Transparent;

this.lblPlayer2Name.FlatStyle =

System.Windows.Forms.FlatStyle.Flat;

this.lblPlayer2Name.Font = new System.Drawing.Font("Microsoft Sans Serif",
15.75F, System.Drawing.FontStyle.Bold,

System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.lblPlayer2Name.ForeColor = System.Drawing.Color.Yellow;

this.lblPlayer2Name.Location = new System.Drawing.Point(16, 24);

this.lblPlayer2Name.Name = "lblPlayer2Name";

this.lblPlayer2Name.Size = new System.Drawing.Size(144, 32);

this.lblPlayer2Name.TabIndex = 3;

this.lblPlayer2Name.Text = "Player2Name";

this.lblPlayer2Name.TextAlign =

System.Drawing.ContentAlignment.MiddleCenter;

//

// lblPlayer1Name

//

this.lblPlayer1Name.BackColor = System.Drawing.Color.Transparent;

this.lblPlayer1Name.Font = new System.Drawing.Font("Microsoft Sans Serif",
15.75F, System.Drawing.FontStyle.Bold,

System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.lblPlayer1Name.ForeColor = System.Drawing.Color.Yellow;

this.lblPlayer1Name.Location = new System.Drawing.Point(816, 512);

this.lblPlayer1Name.Name = "lblPlayer1Name";

this.lblPlayer1Name.Size = new System.Drawing.Size(144, 32);

this.lblPlayer1Name.TabIndex = 4;

this.lblPlayer1Name.Text = "Player1Name";

this.lblPlayer1Name.TextAlign =

System.Drawing.ContentAlignment.MiddleCenter;

//

// label1

//

this.label1.Location = new System.Drawing.Point(0, 0);

this.label1.Name = "label1";

this.label1.TabIndex = 5;

this.label1.Text = "label1";

//

// frmTable

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.BackgroundImage =

((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));

this.ClientSize = new System.Drawing.Size(973, 647);

this.Controls.Add(this.label1);

this.Controls.Add(this.lblPlayer1Name);

this.Controls.Add(this.lblPlayer2Name);

this.Controls.Add(this.lblPlayer1Score);

this.Controls.Add(this.lblPlayer2Score);

this.Controls.Add(this.picTable);

this.Cursor = System.Windows.Forms.Cursors.Default;

this.FormBorderStyle =

System.Windows.Forms.FormBorderStyle.Fixed3D;

this.Name = "frmTable";

this.StartPosition =

System.Windows.Forms.FormStartPosition.CenterScreen;

this.Text = "Air Hockey";

this.Load += new System.EventHandler(this.frmTable_Load);

this.Closed += new System.EventHandler(this.frmTable_Closed);

this.MouseMove += new

System.Windows.Forms.MouseEventHandler(this.picTable_MouseMove);

this.ResumeLayout(false);

}

#endregion

private void frmTable_Closed(object sender, System.EventArgs e)

{

frmMainMenu = new MainMenu();

//MainMenu.Show();

}

private void gameLoop(Object sender, EventArgs e)

{

puck_x += x_vel;

puck_y += y_vel;



//Called so the form will repaint itself.

Invalidate();

}

private void gameDraw(Graphics g)

{

// image of puck is loaded and drawn

Image image =
Image.FromStream(this.GetType().Assembly.GetManifestResourceStream("airhocky
..Bitmap1.bmp"));

g.DrawImage(image, puck_x, puck_y, 35, 35);

foreach(Line l in Lines)

{

l.DrawLine(g, Color.FromArgb(192,192,192));

}

}

public void setName(string pName)

{

lblPlayer1Name.Text = pName;

}

private void frmTable_Load(object sender, System.EventArgs e)

{

gameClock = new Timer();

//Start the game clock to control game speed

gameClock.Interval = 20;

gameClock.Tick += new EventHandler(gameLoop);

gameClock.Start();

puck_x = 200;

puck_y = 200;

}

//modified a little bit by Bob.

private void picTable_MouseMove(object sender,

System.Windows.Forms.MouseEventArgs e)

{

label1.Text = e.X + " " + e.Y;

Graphics g=CreateGraphics();

foreach(Line l in Lines)

l.IsInLine(new Point(e.X,e.Y));

}

private void picTable_Paint(object sender,

System.Windows.Forms.PaintEventArgs e)

{

gameDraw(e.Graphics);

}

//Added by Bob to create the lines

private void CreateLines()

{

// set of hit lines for sides of table

lineLeft = new Line();

lineLeft.StartPoint=new Point( 164, 77 );

lineLeft.EndPoint=new Point( 84, 594 );

Lines.Add(lineLeft);

lineRight = new Line();

lineRight.StartPoint=new Point( 631, 77 );

lineRight.EndPoint=new Point( 712, 594 );

Lines.Add(lineRight);

// set of hit lines for top of table

lineTopLeft = new Line();

lineTopLeft.StartPoint=new Point( 166, 76 );

lineTopLeft.EndPoint=new Point( 340, 76 );

Lines.Add(lineTopLeft);

lineTopRight = new Line();

lineTopRight.StartPoint=new Point( 630, 76 );

lineTopRight.EndPoint=new Point( 455, 76 );

Lines.Add(lineTopRight);

// set of hit lines for top of table

lineBottomLeft = new Line();

lineBottomLeft.StartPoint=new Point( 86, 595 );

lineBottomLeft.EndPoint=new Point( 328, 595 );

Lines.Add(lineBottomLeft);

lineBottomRight = new Line();

lineBottomRight.StartPoint=new Point( 711, 595 );

lineBottomRight.EndPoint=new Point( 467, 595 );

Lines.Add(lineBottomRight);

}

//heavily modified by Bob

private void DrawHitLines(Graphics g)

{

foreach(Line l in Lines)

l.DrawLine(g,Color.Gray);

}

}

public class Line

{

public Point StartPoint;

public Point EndPoint;

public int PenWidth;

bool hit;

public void DrawLine(Graphics g,Color c)

{

Pen p=new Pen(c,PenWidth);

if(hit)

p.Color=Color.Red;

g.DrawLine(p,StartPoint,EndPoint);

p.Dispose();

}

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 class LineArray : CollectionBase

{

public void Add(Line l)

{

List.Add(l);

}

public Line this[int index]

{

get

{

return (Line)List[index];

}

set

{

List[index]=value;

}

}

}

}

---------------------------------------------------------------------


Brian said:
Hello all..

Am working on an Air Hockey game... have an table loaded into a
picture box. The borders of the table are slightly slanted. Am using
hit testing lines with GDI+ to manipulate the puck moving around. I
want the puck is bounce when it hits a border (specified by the
hitlines).

Retreieved some info on hit testing lines from Bob Powell's GDI+ FAQ
(very useful!) but i'm fairly new at the idea of hit testing and am
wondering what exactly is wrong with this code.

Sorry about the abundance of code but my code isn't exactly safe
enough to post a sample (so basically i'm saying, it's fairly badly
written code). I thought it might be easier if people just tried to
run this code themselves and let me know of the results.

Anu suggestions will be much appreciated!

Thanks in advance!


using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace AirHockey
{
/// <summary>
/// Summary description for Table.
/// </summary>
public class frmTable : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox picTable;
private System.Windows.Forms.Label lblPlayer2Score;
private System.Windows.Forms.Label lblPlayer1Score;
private System.Windows.Forms.Label lblPlayer2Name;
private System.Windows.Forms.Label lblPlayer1Name;
Timer gameClock;
int puck_x;
int puck_y;
int x_vel = -5;
int y_vel = -5;

private LineArray Lines=new LineArray();
Line lineLeft;
Line lineRight;
Line lineTopLeft;
Line lineTopRight;
Line lineBottomLeft;
Line lineBottomRight;
bool hitLinesDrawn = false;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public frmTable()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
///

private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(frmTable));
this.picTable = new System.Windows.Forms.PictureBox();
this.lblPlayer2Score = new System.Windows.Forms.Label();
this.lblPlayer1Score = new System.Windows.Forms.Label();
this.lblPlayer2Name = new System.Windows.Forms.Label();
this.lblPlayer1Name = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// picTable
//
this.picTable.BackColor = System.Drawing.Color.Transparent;
this.picTable.Image =
((System.Drawing.Image)(resources.GetObject("picTable.Image")));
this.picTable.Location = new System.Drawing.Point(64, -24);
this.picTable.Name = "picTable";
this.picTable.Size = new System.Drawing.Size(848, 720);
this.picTable.SizeMode =
System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.picTable.TabIndex = 0;
this.picTable.TabStop = false;
this.picTable.Paint += new
System.Windows.Forms.PaintEventHandler(this.picTable_Paint);
this.picTable.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.picTable_MouseMove);
//
// lblPlayer2Score
//
this.lblPlayer2Score.Anchor =
System.Windows.Forms.AnchorStyles.None;
this.lblPlayer2Score.BackColor = System.Drawing.Color.Black;
this.lblPlayer2Score.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPlayer2Score.Cursor = System.Windows.Forms.Cursors.Default;
this.lblPlayer2Score.FlatStyle =
System.Windows.Forms.FlatStyle.System;
this.lblPlayer2Score.Font = new System.Drawing.Font("OCR A
Extended", 48F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer2Score.ForeColor = System.Drawing.Color.Red;
this.lblPlayer2Score.ImageAlign =
System.Drawing.ContentAlignment.TopLeft;
this.lblPlayer2Score.Location = new System.Drawing.Point(16, 56);
this.lblPlayer2Score.Name = "lblPlayer2Score";
this.lblPlayer2Score.Size = new System.Drawing.Size(144, 72);
this.lblPlayer2Score.TabIndex = 1;
this.lblPlayer2Score.Text = "00";
this.lblPlayer2Score.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer1Score
//
this.lblPlayer1Score.Anchor =
System.Windows.Forms.AnchorStyles.None;
this.lblPlayer1Score.BackColor = System.Drawing.Color.Black;
this.lblPlayer1Score.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPlayer1Score.FlatStyle =
System.Windows.Forms.FlatStyle.System;
this.lblPlayer1Score.Font = new System.Drawing.Font("OCR A
Extended", 48F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer1Score.ForeColor = System.Drawing.Color.Red;
this.lblPlayer1Score.ImageAlign =
System.Drawing.ContentAlignment.TopLeft;
this.lblPlayer1Score.Location = new System.Drawing.Point(816, 544);
this.lblPlayer1Score.Name = "lblPlayer1Score";
this.lblPlayer1Score.Size = new System.Drawing.Size(144, 72);
this.lblPlayer1Score.TabIndex = 2;
this.lblPlayer1Score.Text = "00";
this.lblPlayer1Score.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer2Name
//
this.lblPlayer2Name.BackColor = System.Drawing.Color.Transparent;
this.lblPlayer2Name.FlatStyle =
System.Windows.Forms.FlatStyle.Flat;
this.lblPlayer2Name.Font = new System.Drawing.Font("Microsoft Sans
Serif", 15.75F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer2Name.ForeColor = System.Drawing.Color.Yellow;
this.lblPlayer2Name.Location = new System.Drawing.Point(16, 24);
this.lblPlayer2Name.Name = "lblPlayer2Name";
this.lblPlayer2Name.Size = new System.Drawing.Size(144, 32);
this.lblPlayer2Name.TabIndex = 3;
this.lblPlayer2Name.Text = "Player2Name";
this.lblPlayer2Name.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblPlayer1Name
//
this.lblPlayer1Name.BackColor = System.Drawing.Color.Transparent;
this.lblPlayer1Name.Font = new System.Drawing.Font("Microsoft Sans
Serif", 15.75F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblPlayer1Name.ForeColor = System.Drawing.Color.Yellow;
this.lblPlayer1Name.Location = new System.Drawing.Point(816, 512);
this.lblPlayer1Name.Name = "lblPlayer1Name";
this.lblPlayer1Name.Size = new System.Drawing.Size(144, 32);
this.lblPlayer1Name.TabIndex = 4;
this.lblPlayer1Name.Text = "Player1Name";
this.lblPlayer1Name.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.TabIndex = 5;
this.label1.Text = "label1";
//
// frmTable
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackgroundImage =
((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(973, 647);
this.Controls.Add(this.label1);
this.Controls.Add(this.lblPlayer1Name);
this.Controls.Add(this.lblPlayer2Name);
this.Controls.Add(this.lblPlayer1Score);
this.Controls.Add(this.lblPlayer2Score);
this.Controls.Add(this.picTable);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Name = "frmTable";
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Air Hockey";
this.Load += new System.EventHandler(this.frmTable_Load);
this.Closed += new System.EventHandler(this.frmTable_Closed);
this.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.picTable_MouseMove);
this.ResumeLayout(false);

}
#endregion

private void frmTable_Closed(object sender, System.EventArgs e)
{
frmMainMenu MainMenu = new frmMainMenu();
MainMenu.Show();
}

private void gameLoop(Object sender, EventArgs e)
{
puck_x += x_vel;
puck_y += y_vel;

//Called so the form will repaint itself.
Invalidate();
}

private void gameDraw(Graphics g)
{
// image of puck is loaded and drawn
Image image = Image.FromFile("C:/Documents and Settings/All
Users.WINDOWS/Documents/Brian's Documents/AirHockey/Puck.png");
g.DrawImage(image, puck_x, puck_y, 35, 35);

foreach(Line l in Lines)
{
l.DrawLine(g, Color.FromArgb(192,192,192));
}
}

public void setName(string pName)
{
lblPlayer1Name.Text = pName;
}

private void frmTable_Load(object sender, System.EventArgs e)
{
gameClock = new Timer();

//Start the game clock to control game speed
gameClock.Interval = 15;
gameClock.Tick += new EventHandler(gameLoop);
gameClock.Start();

puck_x = 200;
puck_y = 200;
}

private void picTable_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
label1.Text = e.X + " " + e.Y;

Graphics g=CreateGraphics();

if(hitLinesDrawn != true)
{
DrawHitLines(g);
}
else
{
foreach(Line l in Lines)
{
if(l.IsInLine(new Point(e.X,e.Y)))
l.DrawLine(g,Color.Red);
else
l.DrawLine(g,Color.Black);
}
}
}

private void picTable_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
gameDraw(e.Graphics);
}

private void DrawHitLines(Graphics g)
{
// set of hit lines for sides of table
lineLeft = new Line();
lineLeft.StartPoint=new Point( 164, 77 );
lineLeft.EndPoint=new Point( 84, 594 );
lineLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineLeft);

lineRight = new Line();
lineRight.StartPoint=new Point( 631, 77 );
lineRight.EndPoint=new Point( 712, 594 );
lineRight.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineRight);

// set of hit lines for top of table
lineTopLeft = new Line();
lineTopLeft.StartPoint=new Point( 166, 76 );
lineTopLeft.EndPoint=new Point( 340, 76 );
lineTopLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineTopLeft);

lineTopRight = new Line();
lineTopRight.StartPoint=new Point( 630, 76 );
lineTopRight.EndPoint=new Point( 455, 76 );
lineTopRight.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineTopRight);

// set of hit lines for top of table
lineBottomLeft = new Line();
lineBottomLeft.StartPoint=new Point( 86, 595 );
lineBottomLeft.EndPoint=new Point( 328, 595 );
lineBottomLeft.DrawLine(g, Color.FromArgb(192,192,192));
Lines.Add(lineBottomLeft);

lineBottomRight = new Line();
lineBottomRight.StartPoint=new Point( 711, 595 );
lineBottomRight.EndPoint=new Point( 467, 595 );
lineBottomRight.DrawLine(g, Color.Red);
Lines.Add(lineBottomRight);

hitLinesDrawn = true;
}
}

public class Line
{
public Point StartPoint;
public Point EndPoint;
public int PenWidth;

public void DrawLine(Graphics g,Color c)
{
Pen p=new Pen(c,PenWidth);
g.DrawLine(p,StartPoint,EndPoint);
p.Dispose();
}

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))
{
return true;
}
else
{
return false;
}
}
}

public class LineArray : CollectionBase
{
public void Add(Line l)
{
List.Add(l);
}

public Line this[int index]
{
get
{
return (Line)List[index];
}
set
{
List[index]=value;
}
}
}
}
 
B

Brian Basquille

Thank you very much Bob!

I have a feeling it was to do with the painting i was doing in the
MouseMove event. Also, painting on a Picture Box can be quite awkard.

Thanks again. Much appreciated!

By the way, your GDI+ FAQ is definantely the most useful resource i've
used, with regards to C#.
 
B

Brian Basquille

As i want the puck bounce off the walls, i'd want to have the condition to
check each of the lines in the Array in the gameLoop() method.

But the hit testing involves drawing of points using the position of the
mouse (e.X and e.Y) which is only allowed in the MouseMove method.

How do i get the position of the mouse outside of a MouseMove event handler?

private void gameLoop(Object sender, EventArgs e)
{
puck_x += x_vel;
puck_y += y_vel;

//Called so the form will repaint itself.
Invalidate();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top