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("picTa ble.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.CenterImag e;
this.picTable.TabIndex = 0;
this.picTable.TabStop = false;
this.picTable.Paint += new
System.Windows.Forms.PaintEventHandler(this.picTab le_Paint);
this.picTable.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.picTab le_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.CenterScree n;
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.picTab le_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;
}
}
}
}