H
hstagni
Im starting to code C# and I tried to make a simple animation of a
ball moving in the screen(i will transform it later in a pong game).
My program worked fine, but the ball was blinking while moving. So, I
tried to put this lines of code into form constructor, cause I saw on
a forum that this would prevent the background to be redrawed every
time:
/**************TESTING...************************/
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
/********************************************************/
My ball stopped blinking, but the animation is slower now. WHY???????
I heard of overriding OnPaintBackground method, but it just destroys
my hole program. [Can you also give me some tips based on my code?]
HERE IS THE CODE:
/******************************************************************/
using System;
using System.Windows.Forms;
using System.Drawing;
class Ball
{
public int xPos, yPos;
int xVet, yVet;
public Color currColor;
public Ball(int xIniPos, int yIniPos)
{
this.xPos = xIniPos;
this.yPos = xIniPos;
this.xVet = 1;
this.yVet = 3;
currColor = Color.Red;
}
public void NextPositionAndVector()
{
xPos += xVet;
yPos += yVet;
if (yPos<=0 || yPos>=500){
yVet = -yVet;
}
if (xPos<=0 || xPos>=500)
xVet = -xVet;
}
}
public class MainForm : Form
{
Timer ballTimer = new Timer();
Ball myBall = new Ball(100, 100);
public MainForm(){
this.Bounds = Screen.PrimaryScreen.Bounds;
this.ResumeLayout(true);
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
Cursor.Hide();
this.BackColor = Color.Black;
ballTimer.Interval = 1;
/**************TESTING...Double buffer********************/
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
/********************************************************/
this.Show();
this.KeyDown += new KeyEventHandler(MainFormKeyDown);
ballTimer.Tick += new EventHandler(ballTimer_OnTick);
ballTimer.Start();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.FillEllipse(new SolidBrush(myBall.currColor), myBall.xPos,
myBall.yPos, 40, 40);
}
// protected override void OnPaintBackground(PaintEventArgs e)
// {
// }
public void ballTimer_OnTick(object Sender, EventArgs e)
{
myBall.NextPositionAndVector();
this.Invalidate();
}
public void MainFormKeyDown(object Sender, KeyEventArgs e)
{
this.Close();
}
}
public class App
{
public static void Main(){
Application.Run(new MainForm());
}
}
/*****************************************************************/
ball moving in the screen(i will transform it later in a pong game).
My program worked fine, but the ball was blinking while moving. So, I
tried to put this lines of code into form constructor, cause I saw on
a forum that this would prevent the background to be redrawed every
time:
/**************TESTING...************************/
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
/********************************************************/
My ball stopped blinking, but the animation is slower now. WHY???????
I heard of overriding OnPaintBackground method, but it just destroys
my hole program. [Can you also give me some tips based on my code?]
HERE IS THE CODE:
/******************************************************************/
using System;
using System.Windows.Forms;
using System.Drawing;
class Ball
{
public int xPos, yPos;
int xVet, yVet;
public Color currColor;
public Ball(int xIniPos, int yIniPos)
{
this.xPos = xIniPos;
this.yPos = xIniPos;
this.xVet = 1;
this.yVet = 3;
currColor = Color.Red;
}
public void NextPositionAndVector()
{
xPos += xVet;
yPos += yVet;
if (yPos<=0 || yPos>=500){
yVet = -yVet;
}
if (xPos<=0 || xPos>=500)
xVet = -xVet;
}
}
public class MainForm : Form
{
Timer ballTimer = new Timer();
Ball myBall = new Ball(100, 100);
public MainForm(){
this.Bounds = Screen.PrimaryScreen.Bounds;
this.ResumeLayout(true);
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
Cursor.Hide();
this.BackColor = Color.Black;
ballTimer.Interval = 1;
/**************TESTING...Double buffer********************/
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
/********************************************************/
this.Show();
this.KeyDown += new KeyEventHandler(MainFormKeyDown);
ballTimer.Tick += new EventHandler(ballTimer_OnTick);
ballTimer.Start();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.FillEllipse(new SolidBrush(myBall.currColor), myBall.xPos,
myBall.yPos, 40, 40);
}
// protected override void OnPaintBackground(PaintEventArgs e)
// {
// }
public void ballTimer_OnTick(object Sender, EventArgs e)
{
myBall.NextPositionAndVector();
this.Invalidate();
}
public void MainFormKeyDown(object Sender, KeyEventArgs e)
{
this.Close();
}
}
public class App
{
public static void Main(){
Application.Run(new MainForm());
}
}
/*****************************************************************/