So I'm trying to build a simple "game" application, that now has the user paddle and later on I'll add the shooting animations.
What I did was create a class for the paddle and I did the game loop (I'm not sure if I did it right) in the OnPaint event.
My problem is the animation isn't smooth, it flicks each time it draws again.
This is the code I did:
Paddle class:
[SOURCE]
class Paddle
{
private int x, y;
private bool in_area;
//Ctor
public Paddle(int x, int y)
{
this.x = x;
this.y = y;
this.in_area = true;
}
//Properties
public int X
{
get
{
return this.x;
}
set
{
if (value >= 0)
{
this.x = value;
}
}
}
public int Y
{
get
{
return this.y;
}
set
{
if (value >= 0)
{
this.y = value;
}
}
}
public bool InArea
{
get
{
return this.in_area;
}
}
/* Functions */
/* void DrawPaddle() - Draws the paddle on the screen. Called when rendering is on.
* void MoveRight() - Moves the paddle right. Called when Right button is pressed.
* void MoveLeft() - Moves the paddle left. Called when Left buttong is pressed.
* bool CheckCollideWalls() - Checks if paddle is whitin the form's area.
*/
public void DrawPaddle(Graphics g)
{
SolidBrush PaddleBrush = new SolidBrush(Color.Blue);
Rectangle PaddleRect = new Rectangle();
PaddleRect.X = this.x;
PaddleRect.Y = this.y;
PaddleRect.Width = 80;
PaddleRect.Height = 20;
g.FillRectangle(PaddleBrush, PaddleRect);
}
public void MoveLeft()
{
if (this.in_area)
{
this.x -= 3;
}
}
public void MoveRight()
{
if (this.in_area)
{
this.x += 3;
}
}
public bool CheckCollideWalls()
{
return false;
}
}
[/SOURCE]
And the game loop (Correct me if I did it wrong, please):
[SOURCE]
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
UserPaddle.DrawPaddle(g);
Application.DoEvents();
Invalidate();
}
[/SOURCE]
If you also need the key press and down events, I'll write them down.
Thanks in advance,
Portishead.