Jumping is an integral part of many different types of games and can be achieved quite easily in Blitz3D. Whether it’s your character jumping over obstacles, hurdles, or bad guys, the process of creating a bounce is fairly easy. I called this section “Simple Jumping” because it creates a code that will have an object jump only when a key is pressed on the keyboard. We haven’t added any extra parameters. For example, the shape in this example doesn’t have to be on the ground in order to jump; it can actually jump again midway through the first jump.
In order for jumping to work in this example, we need to have gravity already created on our object. Refer back to the previous section for information on creating gravity. Open the file called demo13-02.bb, which is the same file we used in the last section, with just a higher level of gravity (set at 0.1). The higher the level of gravity, the faster our object will fall back to the ground after it has jumped.
The actual process of jumping is just a matter of changing the object’s position on the y axis.
While Not KeyDown( ESC_KEY ) x#=0 y#=0 z#=0 If KeyDown(LEFT_KEY)=True Then x#=-0.1 If KeyDown(RIGHT_KEY)=True Then x#=0.1 If KeyDown(DOWN_KEY)=True Then y#=-0.1 If KeyDown(UP_KEY)=True Then y#=0.1 If KeyDown(Z_KEY)=True Then z#=-0.1 If KeyDown(A_KEY)=True Then z#=0.1 If KeyDown (SPACE_BAR)=True Then y#=0.5 MoveEntity sphere,x#,y#,z# TranslateEntity sphere, 0,-0.1,0 UpdateWorld RenderWorld Flip Wend End
Run the program now and hit the space bar once. You should notice that your ball jumps in the air and then floats back down. Now press the left and right arrow keys or the A or Z keys to move the sphere, and then press the space bar to see the object jump as it moves (see Figure 13.3).
You can change how fast the object floats down by changing the gravity level. Let’s take a look at the code we used to create the jump:
If KeyDown (SPACE_BAR)=True Then y#=0.5
What this says is that when the space bar is pressed, the character’s position should go up by 0.5 units along the y axis. We had already set up the code for movement (MoveEntity sphere,x#,y#,z#), so we only needed to add this one line of code to create the jump.
18.119.122.125