• Advertisement
Sign in to follow this  

pool and c#

This topic is 4045 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am programming a simple pool game using c# and gdi+, I am trying to move the cue ball,l here is some of the code private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { this.BackColor=Color.Black; Graphics g=e.Graphics; SolidBrush greenBrush=new SolidBrush(Color.Green); SolidBrush redBrush= new SolidBrush(Color.Red); Pen brownPen = new Pen (Color.Brown,20); Rectangle rect=new Rectangle(225,10,300,550); g.DrawRectangle(brownPen,rect); g.FillRectangle(greenBrush,rect); g.FillEllipse(Brushes.Red,400,50,20,20); g.FillEllipse(Brushes.Blue,380,50,20,20); g.FillEllipse(Brushes.Pink,360,50,20,20); g.FillEllipse(Brushes.Orange,340,50,20,20); g.FillEllipse(Brushes.Plum,390,70,20,20); g.FillEllipse(Brushes.Black,370,70,20,20); g.FillEllipse(Brushes.Purple,350,70,20,20); g.FillEllipse(Brushes.MistyRose,380,90,20,20); g.FillEllipse(Brushes.LightYellow,360,90,20,20); g.FillEllipse(Brushes.SkyBlue,370,110,20,20); g.FillEllipse(Brushes.White,370,500,20,20);//draw the cue ball }

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by phil67rpg
Graphics g=e.Graphics;


I'm assuming this stays the same throughout the whole program, so there shouldn't be a need to re-assign it every frame o_0.

Quote:
Original post by phil67rpg
SolidBrush greenBrush=new SolidBrush(Color.Green);
SolidBrush redBrush= new SolidBrush(Color.Red);

Pen brownPen = new Pen (Color.Brown,20);
Rectangle rect=new Rectangle(225,10,300,550);


Ouch, be careful with that, I wouldn't call that in the paint function. Your creating new objects each frame...and waisting memory.

Share this post


Link to post
Share on other sites
Quote:
Original post by phil67rpg
my question is how do I animate the cue ball, it has something to do with a for loop

It depends on how realistic you want the physics to be. The simplest approach would probably be Pong-like physics with pseudo-friction. Just run a timer that has a tick event that moves the ball based on some velocity, then the velocity is decreased. Here's an example (minus the "friction" which shouldn't be hard to implement): http://codecube.net/item.asp?cc_ItemID=144

Otherwise you will need to understand: vectors, conversation of momentum and energy, elastic collisions, etc.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement