java applet游戏跳跃

时间:2011-12-03 23:13:03

标签: java applet

我对Java很陌生,目前我正在制作一个超级肉食男孩,但我无法弄清楚如何让他跳跃。

这是我到目前为止的代码,有谁能形容我如何让他跳?感谢。

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;

public class meatboy extends Applet implements KeyListener{
    public int x = 10, y = 300, gravity = 3;
    public int xs = 380, ys = 230;
    double jumptime = 0;
    public boolean right, left, up, jump, start, grounded;
    public void init(){
        setSize(800,400);
        setBackground(Color.black);
        addKeyListener(this);
        /////Movement//////
        Timer t = new Timer();
        t.schedule(new TimerTask(){public void run(){
        if (start == true){
        if (right == true){
            x = x + 3;
        }if (left == true){
            x = x - 3;
        }if(up == true && grounded == true){
            while(jumptime < 50){
                y--;
                jumptime = jumptime + 0.5;
            }
            grounded = false;
            jumptime = 0;
        }
        /////GRAVITY//////
        y = y + gravity;
        ///////Collision Dectection/////
        if(x > 790){//right of screen stop
            x = 10;
        }if(x < 10){// left stop
            x = 789;
        }if(y > 352){
            y = y - 3;
            grounded = true;
        }
        ////////////End of collision///////////
        repaint();
        }}},10,10);


        ///////movement end////////
        }
    public void paint(Graphics g){
        ////////CREATES MEATBOY///////////////
        if (start == false){
            x = 10; 
            y = 300;
            g.setColor(Color.DARK_GRAY);
            g.fillRect(0, 0, 800, 400);
            g.setColor(Color.white);
            g.drawString("MeatBoy 4K", 358, 180);
            g.drawString("Press Z to start!!!!", 350, 200);
            g.setColor(Color.RED);
            g.fillRect(xs, ys, 16, 16);//meatboys body
            g.fillRect(xs + 16, ys + 6, 4, 4);//arm
            g.fillRect(xs - 4, ys + 6, 4, 4);//arm
            g.fillRect(xs, ys + 12, 4, 6);//leg
            g.fillRect(xs + 12, ys + 12, 4, 6);//leg
            g.setColor(Color.black);
            g.fillRect(xs + 2, ys + 2, 5, 5);//eye
            g.fillRect(xs + 10, ys + 2, 5, 5);//eye

        }
        if (start == true){
        g.setColor(Color.RED);
        g.fillRect(x, y, 16, 16);//meatboys body
        g.fillRect(x + 16, y + 6, 4, 4);//arm
        g.fillRect(x - 4, y + 6, 4, 4);//arm
        g.fillRect(x, y + 12, 4, 6);//leg
        g.fillRect(x + 12, y + 12, 4, 6);//leg
        g.setColor(Color.black);
        g.fillRect(x + 2, y + 2, 5, 5);//eye
        g.fillRect(x + 10, y + 2, 5, 5);//eye
        ///////////END OF MEATBOY//////////////////
        ////////Creates Floor///////////////////
        g.setColor(Color.GRAY);
        g.fillRect(0, 370, 800, 30);
        }
    }
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_Z){
            //right = true;
            start = true;
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT){
            //right = true;
            right = true;
        }if (e.getKeyCode() == KeyEvent.VK_LEFT){
            left = true;
        }if (e.getKeyCode() == KeyEvent.VK_UP){
            up = true;
        }if (e.getKeyCode() == KeyEvent.VK_SPACE){
            up = true;
        }
    }
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT){
            //right = true;
            right = false;
        }if (e.getKeyCode() == KeyEvent.VK_LEFT){
            left = false;
        }if (e.getKeyCode() == KeyEvent.VK_UP){
            up = false;
        }if (e.getKeyCode() == KeyEvent.VK_SPACE){
            up = false;
        }
    }
    public void keyTyped(KeyEvent e) {
    }

}

1 个答案:

答案 0 :(得分:1)

当你跳跃时,你的垂直变化为1(向上),然而,你的引力垂直变化为-3(向下),如果太远,你将垂直位置重置为地面。

你需要做的是在“跳跃”时补偿重力,并采用一种方法

while(jumptime < 50){
    y -= 1 + gravity;
    jumptime = jumptime + 0.5;
}