球可以在Jumper Game中双跳

时间:2016-03-03 09:55:31

标签: java user-interface game-physics

我写了一个游戏,它有一个只能跳跃的球(就像在大多数跳投游戏中一样)以避免传入障碍物(仅限于我尚未整合障碍物部分)。现在,它只是一个可以跳跃的球。问题是,当球在回落的路上时,它可以跳跃,导致双跳。我只希望我的球一次跳一次。我该如何解决这个问题?

主要

import javax.swing.JFrame;

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        View view = new View();
        Jumper jumper = new Jumper();
        Model model = new Model(view, jumper);
        view.setJumper(jumper);

        frame.setSize(500, 460);
        frame.getContentPane().add(view);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

查看

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class View extends JPanel {
    private Jumper jumper;
    private Rectangle bounds;
    private Model model;

    public View() {
        setBackground(Color.BLACK);
        bounds = new Rectangle(0, 0, 400, 400);

        addKeyBinding("space.pressed", KeyEvent.VK_SPACE, new MoveAction(true, Key.SPACE), false);
        addKeyBinding("space.released", KeyEvent.VK_SPACE, new MoveAction(false, Key.SPACE), true);
    }

    public void addKeyBinding(String name, int keyEvent, AbstractAction action, boolean pressed) {
        InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = getActionMap();

        inputMap.put(KeyStroke.getKeyStroke(keyEvent, 0, pressed), name);
        actionMap.put(name, action);
    }

    public void setJumper(Jumper jumper) {
        this.jumper = jumper;
    }

    public Rectangle getViewBounds() {
        return bounds;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        jumper.paint(g);
    }

    public void setModel(Model model) {
        this.model = model;
    }
}

跳线

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;

public class Jumper {
    private Point location;
    private Dimension dimension;
    private Graphics graphics;

    public Jumper() {
        location = new Point(250, 250);
        dimension = new Dimension(20, 20);
    }

    public Dimension getSize() {
        return dimension;
    }

    public void setLocation(Point location) {
        this.location = location;
    }

    public void callPaint() {
        paint(graphics);
    }

    public void paint(Graphics g) {
        graphics = g;
        g.setColor(Color.GREEN);
        g.fillOval(location.x, location.y, dimension.width, dimension.height);
    }

    public Point getLocation() {
        return location;
    }
}

模型

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Set;

import javax.swing.Timer;

public class Model {

    private boolean canBePressed, jumping;
    private int ballX, ballY;
    private int jumpCounter;
    private Thread thread;
    private Jumper jumper;
    private View view;
    private Timer timer1;
    private Point point;
    static Set<Key> key;

    public Model(View view, Jumper jumper) {
        this.view = view;
        view.setModel(this);
        canBePressed = false;
        jumping = false;
        key = new HashSet<Key>(3);
        this.jumper = jumper;
        ballX = 250;
        ballY = 0;
        thread = new Thread();

        timer1 = new Timer(5, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                update();
                view.repaint();
            }

        });
        timer1.start();
    }

    public void update() {

        if (jumping) {
            // Only enter when space is pressed
            if (jumpCounter <= 100) {
                ballY--;
                jumpCounter++;
            } else {
                jumping = false;
            }
        } else {

            if (canBePressed && !key.isEmpty()) {
                // Start jumping (set variable to true)
                jumpCounter = 0;
                jumping = true;
                canBePressed = false;
            }

            if (jumper.getLocation().y + jumper.getSize().height > view.getViewBounds().height) {
                // Make the ball unable to jump
                canBePressed = true;
            } else {
                ballY++;
            }

        }

        //Set location of ball
        point = new Point(ballX, ballY);
        jumper.setLocation(point);
        point = null;
    }

}

关键

public enum Key {
    SPACE
}

MoveAction

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

public class MoveAction extends AbstractAction {

    private boolean pressed;
    private Key direction;

    public MoveAction(boolean pressed, Key direction) {
        this.pressed = pressed;
        this.direction = direction;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (pressed) {
            if (Model.key.size() < 1)
                Model.key.add(direction);
        } else {
            Model.key.remove(direction);
        }

    }

}

0 个答案:

没有答案