java - 我怎样才能让我的角色在java中跳跃?

标签 java

我在 youtube 上观看了 thenewboston 的教程,所以现在我正在创作“Bucky's Land”。这段代码目前允许我左右移动我的角色。它根据玩家的移动方式改变玩家的侧面。我是 java 的初学者,我想知道如何让我的角色跳跃。任何帮助,将不胜感激!

package javagame;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Play extends BasicGameState{

Animation bucky, movingUp, movingDown, movingLeft, movingRight;
Image worldMap;
boolean quit = false;
int[] duration = {200,200};
float buckyPositionX = 0;
float buckyPositionY = 0;
float shiftX = buckyPositionX + 320;
float shiftY = buckyPositionY + 160;
boolean jumping = false; 
float verticalSpeed = 0.0f;

public Play(int state){
}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
   worldMap = new Image("res/world.png");
      Image[] walkUp = {new Image("res/buckysBack.png"), new Image("res/buckysBack.png")}; 
      Image[] walkDown = {new Image("res/buckysFront.png"), new Image("res/buckysFront.png")};
      Image[] walkLeft = {new Image("res/buckysLeft.png"), new Image("res/buckysLeft.png")};
      Image[] walkRight = {new Image("res/buckysRight.png"), new Image("res/buckysRight.png")};

      movingUp = new Animation(walkUp, duration, false);
      movingDown = new Animation(walkDown, duration, false);
      movingLeft = new Animation(walkLeft, duration, false);
      movingRight = new Animation(walkRight, duration, false);
      bucky = movingDown; 
  }

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
   worldMap.draw(buckyPositionX, buckyPositionY);
   bucky.draw(shiftX, shiftY);
   g.drawString("Buckys X: "+buckyPositionX+"\nBuckys Y: "+buckyPositionY, 400, 20);

   if(quit==true){
         g.drawString("Resume (R)", 250, 100);
         g.drawString("Main Menu (M)", 250, 150);
         g.drawString("Quit Game (Q)", 250, 200);
         if(quit==false){
            g.clear();
         }
    }
 }

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
   Input input = gc.getInput();
   if(input.isKeyDown(Input.KEY_UP)){
         bucky = movingUp; //change bucky to up image
         buckyPositionY += delta * .1f; //increase the Y coordinates of bucky (move him up)
         if(buckyPositionY>162){
            buckyPositionY -= delta * .1f; //dont let him keep going up if he reaches the top
         }
      }
      if(input.isKeyDown(Input.KEY_DOWN)){
         bucky = movingDown;
         buckyPositionY -= delta * .1f;
         if(buckyPositionY<-600){
            buckyPositionY += delta * .1f;
         }
      }
      if(input.isKeyDown(Input.KEY_LEFT)){
         bucky = movingLeft;
         buckyPositionX += delta * .1f;
         if(buckyPositionX>324){
            buckyPositionX -= delta * .1f;
         }
      }
      if(input.isKeyDown(Input.KEY_RIGHT)){
         bucky = movingRight;
         buckyPositionX -= delta * .1f;
         if(buckyPositionX<-840){
            buckyPositionX += delta * .1f;
         }
      }
        //when they hit escape
      if(quit==true){
         if(input.isKeyDown(Input.KEY_R)){
            quit = false;
         }
         if(input.isKeyDown(Input.KEY_M)){
            sbg.enterState(0);
            try{
               Thread.sleep(250);
            }catch(InterruptedException e){
               e.printStackTrace();
            }
         }
         if(input.isKeyDown(Input.KEY_Q)){
            System.exit(0);

         }
     }
}

public int getID(){
  return 1;
    }
}

最佳答案

为此你需要一些物理学知识。这个想法是,例如,当有人按下空格键时,您将角色的速度设置为例如 5 up。每秒从中减去 1 并将其添加到角色的高度,直到他再次撞到地面。您可能还想将速度限制为 (5,-5)

关于java - 我怎样才能让我的角色在java中跳跃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11764331/

相关文章:

java - JAVA中的正则表达式最多一个点

java - 使用 Office-365-SDK-for-Java 获取文件

java - 如何将哈希表向下转换为抽象类对象

java - 通过js调用访问小程序中的智能卡 keystore

java - 流 mp3/asx/pls/m3u webradio android

java - 如何让用户不断地将元素输入到JList中

java - 打开默认 Java 信任库的最便携方法是什么?

java - 如何将数据传递到 linkki-framework 中的表?

java - 顶级 Activity 必须位于自定义包之外吗?

java - 使 native 代码访问java方法和数据成员