Thanks all.... game now all seems fixed...

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

[EDIT: Sorry for posting this initially as a new thread but i'm
posting via Google (terribly buggy!) as my account for reading
NewsGroups doesn't support this particular C-Sharp newsgroup]

Just to let you all know, got the issues with the puck straddling the
the hit-line's (jerking).

The error was that i was forgetting to add on the width of the image.

Let me elaborate... the puck is an image (puck.png) drawn and is set
at 35 pixels (height and width).

When the puck bounced at the top, it just read the puck_x which was
fine as that was the beginning of the image (the 0,0 in height and
width, in other words).

But when the puck wanted to bounce at the bottom, puck_y had to react
at the opposite end of the image (the 35,35 of height and width) and
was bouncing too late. It only reacted when it senses that puck_x was
at the bottom of the table, as opposed to puck_y.

So, i added on the dimension of the radius of the image (35) to the
puck_y to enable it actually takes the dimensions of the image into
account when bouncing at the bottom.

Probably could've worded that better but quite tough to explain.

Again, thanks all for the advice and suggestions.

PS - Cheers Jason for your advice regarding the smooth bouncing. Will
be starting into user interaction next week, so your useful advice
will come into it then.

PPS - Here's the entire GameDraw() method's updated code, for those of
you interested:

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);

// create a rectangle from the image of the puck
puckRect = new Rectangle(puck_x, puck_y, 35, 35);

// create a region out of the form (and exclude the table-top)
formRect = new Rectangle(0, 0, 983, 683);
formRgn = new Region(formRect);
formRgn.Exclude(pth);

// intersect the two regions (table-top and the puck)
intersection = new Region(pth);
intersection.Intersect(puckRect);

// if the puck is visible outside the table-top region, return true!
boundaryHit = formRgn.IsVisible(puckRect);

// if boundary hit, check whether it was hit in a goal
if(boundaryHit == true)
{

// if puck hits either player's goal, increment the opponent's score
if(puck_y <= 76 && (puck_x >= 341 && puck_x <= 454))
{
IncrementPlayer1Score();
}

else if(puck_y + 35 >= 595 && (puck_x >= 333 && puck_x <= 461))
{
IncrementPlayer2Score();
}

// if puck hits the top or bottom of the table, bounce the puck back
in opposite direction!
if(puck_y <= 76 || puck_y + 35 >= 595)
{
y_vel*=-1;
Sound.PlaySoundContinue(@"bounce.wav");
}

else
{
x_vel*=-1;
Sound.PlaySoundContinue(@"bounce.wav");
}
}
}
 
[OT]

If you want to get to this newsgroup without google.

Open Outlook Express, and add the news server "msnews.microsoft.com", which
is available to all for free.
This is done under the menu Tools->Accounts...


Hope this helps

Publicjoe
C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Snippets at http://www.publicjoe.f9.co.uk/csharp/snip/snippets.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html


Brian said:
[EDIT: Sorry for posting this initially as a new thread but i'm
posting via Google (terribly buggy!) as my account for reading
NewsGroups doesn't support this particular C-Sharp newsgroup]

Just to let you all know, got the issues with the puck straddling the
the hit-line's (jerking).

The error was that i was forgetting to add on the width of the image.

Let me elaborate... the puck is an image (puck.png) drawn and is set
at 35 pixels (height and width).

When the puck bounced at the top, it just read the puck_x which was
fine as that was the beginning of the image (the 0,0 in height and
width, in other words).

But when the puck wanted to bounce at the bottom, puck_y had to react
at the opposite end of the image (the 35,35 of height and width) and
was bouncing too late. It only reacted when it senses that puck_x was
at the bottom of the table, as opposed to puck_y.

So, i added on the dimension of the radius of the image (35) to the
puck_y to enable it actually takes the dimensions of the image into
account when bouncing at the bottom.

Probably could've worded that better but quite tough to explain.

Again, thanks all for the advice and suggestions.

PS - Cheers Jason for your advice regarding the smooth bouncing. Will
be starting into user interaction next week, so your useful advice
will come into it then.

PPS - Here's the entire GameDraw() method's updated code, for those of
you interested:

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);

// create a rectangle from the image of the puck
puckRect = new Rectangle(puck_x, puck_y, 35, 35);

// create a region out of the form (and exclude the table-top)
formRect = new Rectangle(0, 0, 983, 683);
formRgn = new Region(formRect);
formRgn.Exclude(pth);

// intersect the two regions (table-top and the puck)
intersection = new Region(pth);
intersection.Intersect(puckRect);

// if the puck is visible outside the table-top region, return true!
boundaryHit = formRgn.IsVisible(puckRect);

// if boundary hit, check whether it was hit in a goal
if(boundaryHit == true)
{

// if puck hits either player's goal, increment the opponent's score
if(puck_y <= 76 && (puck_x >= 341 && puck_x <= 454))
{
IncrementPlayer1Score();
}

else if(puck_y + 35 >= 595 && (puck_x >= 333 && puck_x <= 461))
{
IncrementPlayer2Score();
}

// if puck hits the top or bottom of the table, bounce the puck back
in opposite direction!
if(puck_y <= 76 || puck_y + 35 >= 595)
{
y_vel*=-1;
Sound.PlaySoundContinue(@"bounce.wav");
}

else
{
x_vel*=-1;
Sound.PlaySoundContinue(@"bounce.wav");
}
}
}
 
As an optimization Brian you should consider cacheing the puck image instead
of loading it from file every draw cycle. Probably a good place to do this
is in the OnLoad of the game form.

--
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:
[EDIT: Sorry for posting this initially as a new thread but i'm
posting via Google (terribly buggy!) as my account for reading
NewsGroups doesn't support this particular C-Sharp newsgroup]

Just to let you all know, got the issues with the puck straddling the
the hit-line's (jerking).

The error was that i was forgetting to add on the width of the image.

Let me elaborate... the puck is an image (puck.png) drawn and is set
at 35 pixels (height and width).

When the puck bounced at the top, it just read the puck_x which was
fine as that was the beginning of the image (the 0,0 in height and
width, in other words).

But when the puck wanted to bounce at the bottom, puck_y had to react
at the opposite end of the image (the 35,35 of height and width) and
was bouncing too late. It only reacted when it senses that puck_x was
at the bottom of the table, as opposed to puck_y.

So, i added on the dimension of the radius of the image (35) to the
puck_y to enable it actually takes the dimensions of the image into
account when bouncing at the bottom.

Probably could've worded that better but quite tough to explain.

Again, thanks all for the advice and suggestions.

PS - Cheers Jason for your advice regarding the smooth bouncing. Will
be starting into user interaction next week, so your useful advice
will come into it then.

PPS - Here's the entire GameDraw() method's updated code, for those of
you interested:

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);

// create a rectangle from the image of the puck
puckRect = new Rectangle(puck_x, puck_y, 35, 35);

// create a region out of the form (and exclude the table-top)
formRect = new Rectangle(0, 0, 983, 683);
formRgn = new Region(formRect);
formRgn.Exclude(pth);

// intersect the two regions (table-top and the puck)
intersection = new Region(pth);
intersection.Intersect(puckRect);

// if the puck is visible outside the table-top region, return true!
boundaryHit = formRgn.IsVisible(puckRect);

// if boundary hit, check whether it was hit in a goal
if(boundaryHit == true)
{

// if puck hits either player's goal, increment the opponent's score
if(puck_y <= 76 && (puck_x >= 341 && puck_x <= 454))
{
IncrementPlayer1Score();
}

else if(puck_y + 35 >= 595 && (puck_x >= 333 && puck_x <= 461))
{
IncrementPlayer2Score();
}

// if puck hits the top or bottom of the table, bounce the puck back
in opposite direction!
if(puck_y <= 76 || puck_y + 35 >= 595)
{
y_vel*=-1;
Sound.PlaySoundContinue(@"bounce.wav");
}

else
{
x_vel*=-1;
Sound.PlaySoundContinue(@"bounce.wav");
}
}
}
 
Thanks Mike.. been looking for a free newsgroup's account that i can use in
OE.

And cheers for your advice also, Bob. I never even considered the effect
that re-drawing that puck every time would have.

Much appreciated!
 
Back
Top