Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

WinForms Jitter

$
0
0
So, the final "lab" in Head First C# is to do a Space Invaders clone in WinForms. So far, all I've done is draw a black background and animate the player ship, moving left when someone hits the left key or right with the right key. That's it.

However, right off the bat, I'm hitting an issue with a lot of jitter when moving the ship left and right, and I'm not sure why. Right now, I have it set up so with each tick of the timer, it checks if the left or right key is pressed and, if so, moves the playerShip object left or right and then calls Form1.Invalidate(). I'd think this would result in a reasonably smooth animation, but not so much. Any ideas on what I'm doing wrong? Oh, and I've set Form1 to be double buffered.

Here's some relevant code.

Form1:

    public partial class Form1 : Form
    {
        Game game;
        Renderer renderer;

        public Form1()
        {
            InitializeComponent();
            game = new Game(this.ClientRectangle);
            renderer = new Renderer(this, game);
        }

        List<Keys> keysPressed = new List<Keys>();

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Q)
                Application.Exit();
            if (game.GameOver)
            {
                if (e.KeyCode == Keys.S)
                {
                    //code to reset the game and restart timers
                    return;
                }
            }
            if (e.KeyCode == Keys.Space)
                game.FireShot();
            if (keysPressed.Contains(e.KeyCode))
                keysPressed.Remove(e.KeyCode);
            keysPressed.Add(e.KeyCode);
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (keysPressed.Contains(e.KeyCode))
                keysPressed.Remove(e.KeyCode);
        }

        private void gameTimer_Tick(object sender, EventArgs e)
        {
            game.Go();
            foreach (Keys key in keysPressed)
            {
                if (key == Keys.Left)
                {
                    game.MovePlayer(Direction.Left);
                }
                else if (key == Keys.Right)
                {
                    game.MovePlayer(Direction.Right);
                }
            }
            this.Invalidate();
        }

        private void animationTimer_Tick(object sender, EventArgs e)
        {
            //Does Nothing
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            renderer.Render(e.Graphics);
        }
    }





The Renderer Class:

    class Renderer
    {
        Form form;
        Game game;
        Bitmap playerImage;
        Point testPoint = new Point(300, 300);

        public Renderer(Form form, Game game)
        {
            this.form = form;
            this.game = game;
        }

        public void InitializeImages()
        {
            playerImage = Properties.Resources.player;
        }

        public void Render(Graphics g)
        {
            g.FillRectangle(Brushes.Black, form.ClientRectangle);
            g.DrawImageUnscaled(Properties.Resources.player, game.PlayerLocation);
        }
    }


The PlayerShip class (note: Game Class code is not shown, as at this point it just pretty much exposes the playerShip Location to the Renderer and passes the keypresses to the playerShip so it can move itself)

    class PlayerShip
    {
        private Point location;
        public Point Location {get { return location; }}

        Rectangle area;
        public bool Alive {get; private set;}
        const int speed = 5;

        public PlayerShip(Rectangle area)
        {
            Alive = true;
            this.area = area;
            location = new Point(area.Width / 2, area.Height - 50);
        }

        internal void Move(Direction direction)
        {
            if (direction == Direction.Left)
            {
                if (location.X <= 0)
                    return;
                location.X = location.X - speed;
            }
            else if (direction == Direction.Right)
            {
                if (location.X >= area.Width - Properties.Resources.player.Width)
                    return;
                location.X = location.X + speed;
            }
        }
    }

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>