pool and c#

Started by
3 comments, last by Pseudonym 17 years ago
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 }
Advertisement
You didn't actually ask a question. [smile]

Also, it would help if you use source tags (read FAQ for tag info).
my question is how do I animate the cue ball, it has something to do with a for loop
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.
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.

This topic is closed to new replies.

Advertisement