using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Input; namespace animation { public partial class PlayerSprite : PictureBox { // constants controlling movement characteristics private static double SPEED = 1.0; private static double INITIAL_JUMP_SPEED = -3.0; // need variables to keep track of movement state private double velocityY = 0; // these properties make much more sense to have here on the playersprite than on the form public bool CmdLeft { get; set; } public bool CmdRight { get; set; } public bool CmdJump { get; set; } public PlayerSprite() { InitializeComponent(); CmdRight = false; CmdLeft = false; CmdJump = false; } public void UpdateBehavior(Form1 form) { // get the current position to work with (could be a Vector2) int x, y; x = Location.X; y = Location.Y; // simple handling of left-right movement to start // in this version, you can still control horizontal movement in the air (not good) if (CmdLeft) { x = x - (int)(SPEED * Form1.FRAME_TIME); } if (CmdRight) { x = x + (int)(SPEED * Form1.FRAME_TIME); } // simple handling of jump - should update to keep a grounded variable // in this version, holding space makes you keep going up (not good) if (CmdJump) { // give instantaneous upward velocity velocityY = INITIAL_JUMP_SPEED; } // check for landing (at or below ground, and moving downwards) if (y >= 400 && velocityY > 0) { y = 400; velocityY = 0; } // check for in the air else if (y < 400) { // update vertical velocity according to gravity velocityY = velocityY + (Form1.ACCEL_GRAVITY * Form1.FRAME_TIME); } // update vertical position y = y + (int)(velocityY * Form1.FRAME_TIME); // update the position of the sprite Location = new Point(x, y); } } }