java applet游戏跳跃bug

时间:2011-12-04 15:13:49

标签: 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 , jump_count, jump_move;
    double jumptime = 0;
    public boolean right, left, up, jump = true, start, grounded, grav = true;
    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 && jump == true){
            jump_count++;
            if (jump_count >= 100){
                jump_count = 0;
            }
            if (jump_count >= 0 && jump_count <= 25){
                //jump_move = 1;
                y = y - 5;
            }
            if (jump_count >= 25 && jump_count <= 50){
                //jump_move = 2;
                y = y - 5;
            }
            if (jump_count >= 50 && jump_count <= 75){
                //jump_move = 3;
                y = y - 5;
            }
            if (jump_count >= 75 && jump_count <= 100){
                //jump_move = 0;
                y = y - 5;
                jump = false;
            }
        }
        /////GRAVITY//////
        if (grav == true){y = y + gravity;}
        ///////Collision Dectection/////
        if(x > 790){//right of screen stop
            x = 10;
        }if(x < 10){// left stop
            x = 789;
        }if(y > 350){
            //y = y - 3;
            grav = false;
            jump = true;
        }else{grav = 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 :(得分:2)

玩家在哪里陷入困境?

如果它靠近地面,请确保y坐标正好在地面上。

在这段代码上:

}if(y > 350){
        //y = y - 3;
        grav = false;
        jump = true;
}else{grav = true;}

添加

}if(y > 350){
        //y = y - 3;
        grav = false;
        jump = true;

        //I'm on the ground all right
        y = 350;

}else{grav = true;}