H
hstagni
I am begining to code C#... and I am trying to make a ball moving at
the screen.(Actually I was trying to make a pong but first I want to
make a ball moving at screen)
I did it and it worked well, but the ball is blinking while moving. I
tried to put the following lines of code inside my Form's constructor:
/**************TESTING...Double buffer********************/
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
/********************************************************/
Now, my ball does not blink but the animation is too much slower. I
will show you my code, so that you can test it WITH and WITHOUT the
above lines. Can you help me ?
(I heard about overriding the onPaintBackground method but it didnt
work either)
/*************************START HERE*************************/
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());
}
}
/
**********************************************************************/
the screen.(Actually I was trying to make a pong but first I want to
make a ball moving at screen)
I did it and it worked well, but the ball is blinking while moving. I
tried to put the following lines of code inside my Form's constructor:
/**************TESTING...Double buffer********************/
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
/********************************************************/
Now, my ball does not blink but the animation is too much slower. I
will show you my code, so that you can test it WITH and WITHOUT the
above lines. Can you help me ?
(I heard about overriding the onPaintBackground method but it didnt
work either)
/*************************START HERE*************************/
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());
}
}
/
**********************************************************************/