在简单的Java游戏中的重力系统

时间:2015-04-13 16:23:34

标签: java gravity

我和一个朋友开始制作一个简单的Java游戏,我的工作是创建一个具有可以在平台上跳转的玩家块的JFrame。我可以通过扩展Block来制作播放器类。

我已经达到了可以画出玩家的程度,但是它只会向上移动并且不会再回落,并且应该出现在地板上的区块不会出现。这是我的主要方法,BlockPlayer类。

package michael.s.game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MichaelSGame {

/**
 * @param args the command line arguments
 */
static JFrame frame = new JFrame("Lab 54");
static JPanel panel;
static int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
static int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
static Player player = new Player(5,5,height - 20,width/2,0,0,Color.BLUE);
static Block floor = new Block(10,width,width,height - 20,Color.GREEN){
    @Override
    public void update() {
        if(isTouching(player))
                player.vy=0;
    }
};
public static void main(String[] args) {   


    panel = new JPanel(){
        //@Override
        public void paint(Graphics g)
        {
            super.paint(g);
            g.setColor(Color.GREEN);
            floor.paint(g);
            player.paint(g);


        }
    };        
    panel.setBackground(Color.white);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setBounds(0,0,width,height);
    frame.setUndecorated(true);
    KeyListener controls = new KeyListener(){
      @Override
      public void keyTyped(KeyEvent e) {

      }

      @Override
      public void keyPressed(KeyEvent e) {
         if(e.getKeyCode() == KeyEvent.VK_SPACE)
         {
             player.jump();
         }
         if(e.getKeyCode() == KeyEvent.VK_LEFT){
            player.left();
         }
         if(e.getKeyCode() == KeyEvent.VK_RIGHT)
         {
            player.right();
         }
      }

      @Override
      public void keyReleased(KeyEvent e) 
      {
         if(e.getKeyCode() == KeyEvent.VK_LEFT)
         {
             player.stop();
         }
         if(e.getKeyCode() == KeyEvent.VK_RIGHT)
         {
             player.stop();
         }
         if(e.getKeyCode() == KeyEvent.VK_SPACE)
         {
             player.stop();   
         }


      }
  };
    frame.add(panel);
    panel.setFocusable(true);
    panel.addKeyListener(controls);
    panel.addKeyListener(new KeyAdapter(){
        @Override
        public void keyPressed(KeyEvent e){
            if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
                System.exit(0);
        }
    });        
    frame.setVisible(true);
    new Thread(new Runnable(){
        @Override
        public void run() {
            try{
              while(true){
                  floor.update();
                  player.update();
                  panel.repaint();
                  Thread.currentThread().sleep(10);
              }
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }

    }).start();
}
}

///////////////////////////////

package michael.s.game;
import java.awt.Color;
import java.awt.Graphics;
/**
 *
 * @author David
 */
public abstract class Block {
    protected int ht;
    protected int wt;
    protected double x;
    protected double y;
    protected Color color;
    public Block(int ht, int wt, double x, double y, Color color)
    {
        this.ht = ht;
        this.wt = wt;
        this.x = x;
        this.y = y;
        this.color = color;
    }
    public boolean isTouching(Block other)
    {
       return x+wt >= other.getX() && other.getX() + other.getWt() >= x && y+ht >= other.getY() && other.getY() + other.getHt() >= y; 
    }
    public int getX()
    {
        return (int) x;
    }
    public int getY()
    {
        return (int) y;
    }
    public int getHt()
    {
        return this.ht;
    }
    public int getWt()
    {
        return this.wt;
    }
    public void paint(Graphics g)
    {
        g.setColor(color);
        g.fillRect(getX(), getY(), wt, ht);
    }

    public abstract void update();
}

/////////////////

/*
 * 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.
 */
package michael.s.game;

import java.awt.Color;

/**
 *
 * @author David
 */
public class Player extends Block 
{
    public double vx;
    public double vy;
    public double GRAVITY;
    public Player(int ht, int wt, double x, double y, double vx, double vy, Color color)
    {
        super(ht,wt,x,y, color);
    }

    @Override
    public void update() 
    {
        vy+= GRAVITY;
        x+=vx;
        y+=vy;
    }
    public void left()
    {
        vx = -5;
    }
    public void right()
    {
        vx = 5;
    }
    public void jump()
    {
        vy = -10;
    }
    public void stop()
    {
        vy = 0;
    }


}

我怎样才能让重力在这里发挥作用,让实际上出现?

我需要帮助添加重力功能,让地板显示自己,让玩家在玩家击中时停止。

0 个答案:

没有答案