Question: To make a rectangle jump we need the following variables //rectangle to represent player Rectangle player; //the lower limit of how far the player can

To make a rectangle jump we need the following variables

//rectangle to represent player Rectangle player; //the lower limit of how far the player can fall i.e. //the ground, this can be the top of a rectangle. int yLimit; //flag to tell if player is currently jumping and //therefore not able to jump again till no longer //jumping. bool jumping; //vars that control how a player jumps //upper controls how many pixels are moved up at a time //gravity controls how fast the player drops. adjust //these values to control how the player jumps e.g. //fast and hard, or like in space int upper, gravity; //timer that carries out the jumping movement Timer jumpTimer;

Inside the form load we need to do the following to setup jumping capabilities private void Form1_Load(object sender, EventArgs e) { //sets the ylimit yLimit = this.Height - 40; //makes you not jumping to start jumping=false; //sets up the player player = new Rectangle(20, yLimit, 20, 20); //events for drawing to screen and keydown events this.DoubleBuffered = true; this.Paint += new PaintEventHandler(Form1_Paint); this.KeyDown += new KeyEventHandler(Form1_KeyDown); //sets up jump timer jumpTimer = new Timer(); jumpTimer.Interval = 1000 / 60; jumpTimer.Tick += new EventHandler(jumpTimer_Tick); jumpTimer.Start(); this.Invalidate();

} Inside the Jump Timer Tick we need to do the following: //jump timer method void jumpTimer_Tick(object sender, EventArgs e) { //refreshes the screen this.Invalidate(); //if you are jumping if (jumping) { //if you are not yet at the ylimit i.e. you are //still falling if (player.Bottom <= yLimit) { //continue with jump jumping = true; //move player up by the upper value player.Y -= upper; //move player down by the gravity value player.Y += gravity; //if you still have a positive upper value //reduce the upper value by 1 to slowly //reach the peak of your jump if (upper > 0) { --upper; } //if you hit the yLimit if (player.Bottom >= yLimit) { //stop jumping jumping = false; //set the bottom of the player to the //yLimit player.Y = yLimit - player.Height; //force upper to 0 upper = 0;//force gravity to 0 gravity = 0; } }

The following code triggers a jump when the spacebar is pushed and you are not in the process of jumping //event handler for keydown events void Form1_KeyDown(object sender, KeyEventArgs e) { //when space key is pushed if (e.KeyCode == Keys.Space) { //if not already jumping if (!jumping) { //start jumping jumping = true; //set an upper value upper = 14; //set a gavity value gravity = 3; } }

The following code triggers a jump when the spacebar is pushed and you are not in the process of jumping //event handler for keydown events void Form1_KeyDown(object sender, KeyEventArgs e) { //when space key is pushed if (e.KeyCode == Keys.Space) { //if not already jumping if (!jumping) { //start jumping jumping = true; //set an upper value upper = 14; //set a gavity value gravity = 3; } } The following code triggers a jump when the spacebar is pushed and you are not in the process of jumping //event handler for keydown events void Form1_KeyDown(object sender, KeyEventArgs e) { //when space key is pushed if (e.KeyCode == Keys.Space) { //if not already jumping if (!jumping) { //start jumping jumping = true; //set an upper value upper = 14; //set a gavity value gravity = 3; } }

The following code triggers a jump when the spacebar is pushed and you are not in the process of jumping //event handler for keydown events void Form1_KeyDown(object sender, KeyEventArgs e) { //when space key is pushed if (e.KeyCode == Keys.Space) { //if not already jumping if (!jumping) { //start jumping jumping = true; //set an upper value upper = 14; //set a gavity value gravity = 3; } } }

Use the above code to create a jumping rectangle with a picture on it, create a second rectangle that move back and forth across the bottom (yLimit) and allow the jumping rectangle to move left and right too. The jumping box must avoid a collision with the second rectangle, if they hit a message should be shown that you died and end the program.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!