2012-08-01 110 views
2

我在YouTube上觀看新波士頓的教程,所以現在我正在創建「Bucky's Land」。這段代碼目前可以讓我的角色左右移動,上下移動。它根據他如何移動來改變玩家的側面。我是java的初學者,我想知道如何讓我的角色跳躍。任何幫助,將不勝感激!如何讓我的角色在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; 
    } 
} 
+0

我不知道該怎麼做 – huntingGirl 2012-08-01 17:33:06

+1

@Zoop它看起來像OP是在學習階段,所以像「你有什麼嘗試」的評論贏得'在這種情況下幫助。 – 2012-08-01 17:42:04

+2

[此鏈接](http://stackoverflow.com/q/11491404/1065197)可以幫助您獲得答案。 – 2012-08-01 17:44:30

回答

1

你需要一點物理知識。這個想法是,例如當有人按空格鍵時,你將字符的速度設置爲例如5。每秒你減去1並將其加到角色的高度,直到他再次擊中地面。你可能也想限制速度到(5,-5)

+0

當然,另一種選擇是將跳躍狀態設置爲5,然後每秒遞減1,並在跳躍狀態大於0時將字符繪製爲「跳躍」。 – 2012-08-01 17:25:29

+0

嘿,我是14歲,我盡我所能 - 但你能給我多一點幫助嗎?就像一個示例代碼或一段代碼來啓動我? – huntingGirl 2012-08-01 17:30:19

+0

umm。如果沒有真正理解你正在做什麼樣的遊戲,很困難。我建議你谷歌更多的教程。 – 2012-08-01 17:53:23