Jump to content
  • Advertisement
Sign in to follow this  
Portishead

[C#] Trying my first game

This topic is 3535 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

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.

Share this post


Link to post
Share on other sites
Advertisement

It's been a while, but as I recall this is due to the background being repainted when you call Invalidate. If that's correct, you should be able to fix it by adding this code to your Form's/Control's constructor:


this.Setstyle(Controlstyles.AllPaintingInWmPaint | Controlstyles.UserPaint, true);


(I've edited this a number of times, but GDNet seems intent on molesting the casing in that code snippet, so you can't use it verbatim :)

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!