在Java中跳起来后,如何使播放器缓慢地向下移动

时间:2019-01-21 04:18:00

标签: java animation jpanel

我一直在做滚动背景游戏,但是现在我正在尝试使角色跳跃,但是我似乎无法使玩家平稳地跳跃然后掉回地面。我的播放器跳了起来,但是立即传送回地面,使其无法越过障碍。任何帮助,将不胜感激。谢谢!

这是代码。

   /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.geom.Rectangle2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;



    /**
     *
     * @author kids
     */
    public class game extends javax.swing.JFrame {
    private PaintSurface canvas;
        int x_position = 0;
        int y_position = 580;
        int x_speed = 7;

    int x_pos = 0;
    int y_pos = 0;
    int x_pos2 = -1200;
    int x_position2 = -1200;

    int cloudOnex = 30;
    int cloudOney = 70;
    int cloud2x = -1150;
    int cloud2y = 70;
    int cloud3x = 700;
    int cloud3y = 70;
    int cloud4x = -600;
    int cloud4y = 70;
    int cloud5x = 450;
    int cloud5y = 70;
    int playerx =400;
    int playery = 540;
    int enemy_posX = 500;
    int enemy_posY = 540;
    private BufferedImage image;
    int defaultPosition = 540;


        /**
         * Creates new form game
         */
        public game() {

              JPanel btnPanel = new JPanel(new FlowLayout());

            JButton btnUp = new JButton("Move Up ");
            btnPanel.add(btnUp);
            btnUp.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    playery += 10;

                    canvas.repaint();
                    requestFocus(); // change the focus to JFrame to receive KeyEvent
                }
            });


            Container cp = getContentPane();

            cp.setLayout(new BorderLayout());

            // "super" JFrame fires KeyEvent
            addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent evt) {

                    switch (evt.getKeyCode()) {

                        case KeyEvent.VK_UP:
                            playery -= 90;

                            repaint();
                            break;
                        case KeyEvent.VK_DOWN:
                            playery += 22;
                            repaint();
                            break;

                    }
                }
            });

     this.setTitle("Scrolling Game");
            this.setSize(1200, 650);
            // super.setBackground(Color.YELLOW);
            this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
            this.add(new PaintSurface(), BorderLayout.CENTER);
            this.setVisible(true);
            canvas = new PaintSurface();
            this.add(canvas, BorderLayout.CENTER);




            //settings for the form, handling things such as exiting and size
            Timer timer = new Timer(10, e -> {
                canvas.movement();
                 canvas.falling();
               canvas.check();

                canvas.repaint();
            });
            timer.start();
        }

        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE)
            );

            pack();
        }// </editor-fold>                        



         class PaintSurface extends JComponent {

            public void paintComponent(Graphics g) {
                super.paintComponent(g);

                Graphics2D g2 = (Graphics2D) g;

                Rectangle2D background = new Rectangle2D.Float(x_pos, y_pos, 1200, 650);
                g2.setColor(Color.GRAY);
                g2.fill(background);

                Rectangle2D ground = new Rectangle2D.Float(x_position, y_position, 1200, 30);
                g2.setColor(Color.darkGray);
                g2.fill(ground);

                Rectangle2D cloudOne = new Rectangle2D.Float(cloudOnex, cloudOney, 70, 70);
                g2.setColor(Color.WHITE);
                g2.fill(cloudOne);

                 Rectangle2D cloudThree = new Rectangle2D.Float(cloud3x, cloud3y, 70, 70);
                g2.setColor(Color.WHITE);
                g2.fill(cloudThree);

               Rectangle2D enemy = new Rectangle2D.Float(enemy_posX, enemy_posY, 40,40);
                g2.setColor(Color.PINK);
                g2.fill(enemy);



                Rectangle2D background2 = new Rectangle2D.Float(x_pos2, y_pos, 1200, 650);
                g2.setColor(Color.CYAN);
                g2.fill(background2);

                Rectangle2D ground2 = new Rectangle2D.Float(x_position2, y_position, 1200, 30);
                g2.setColor(Color.GREEN);
                g2.fill(ground2);

               Rectangle2D cloudTwo = new Rectangle2D.Float(cloud2x, cloud2y, 70, 70);
                g2.setColor(Color.WHITE);
                g2.fill(cloudTwo);

                 Rectangle2D cloudFour = new Rectangle2D.Float(cloud4x, cloud4y, 70, 70);
                g2.setColor(Color.WHITE);
                g2.fill(cloudFour);

                Rectangle2D player = new Rectangle2D.Float(playerx, playery,40,40);
                g2.setColor(Color.red);
                g2.fill(player);





                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            /**
             * @param args the command line arguments
             */
        }

          public void movement(){
                        enemy_posX +=10;

              cloudOnex += 10;
              cloud2x += 10;
              cloud3x += 10;
              cloud4x += 10;
              x_position += 10;
              x_pos += 10;
              x_position2 += 10;
              x_pos2 += 10;




                     try { Thread.sleep(20); }   /* this will pause for 50 milliseconds */
                     catch (InterruptedException e) { System.err.println("sleep exception"); }


            }
          public void falling(){


                 while (playery != defaultPosition) {
//it adds  90 because that is the distance it goes up every jump
//default position is the spot where it rests when it is not jumping
                      playery += 90;
                      repaint();

                 } 
             // }

              } 

             public void check() {
                 if (x_pos == 1200) {
                     x_pos = -1200;
                     //x_position = -1200;
                     repaint();
                 }
                 if (x_pos2 == 1200) {
                     x_pos2 = -1200;
                     // x_position2 = -1200;
                     repaint();

                 }
                 if (x_position == 1200) {
                     x_position = -1200;
                     repaint();
                 }
                 if (x_position2 == 1200) {
                     x_position2 = -1200;
                     repaint();

                 }
                 if (cloudOnex == 1200) {
                     cloudOnex = -1150;
                     repaint();

                 }
                 if (cloud2x == 1200) {
                     cloud2x = -1150;
                     repaint();

                 }
                 if (cloud3x == 1200) {
                     cloud3x = -200;
                     repaint();

                 }
                 if (cloud4x == 1200) {
                     cloud4x = 0;
                     repaint();

                 }

         if (enemy_posX == 1200){
                  enemy_posX = 0;
                  repaint();
         }
            }



         }

        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(game.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(game.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(game.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(game.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>

            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new game().setVisible(true);
                }
            });
        }

        // Variables declaration - do not modify                     
        // End of variables declaration                   
    }

处理播放器回退的位置是这样:

while (playery != defaultPosition) {
//it adds  90 because that is the distance it goes up every jump
//default position is the spot where it rests when it is not jumping
                      playery += 90;
                      repaint();

                 } 

我怀疑它会立即跳回去,因为它像我将其设置为90一样将其设置回90,而不是逐渐增加播放器y,但是当我这样做时,它冻结了。

0 个答案:

没有答案