对于Java游戏,我想将玩家对象沿指定的方向移动指定的距离。尽管-使用Java Random API-在板上随机移动播放器效果很好,但我无法找到任何合适的方法将播放器沿X和Y方向移动指定距离。在下面的代码中,我尝试了一些代码(使用//进行注释),但是它们都无法正常工作。 包装高;
有没有可行的解决方案?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class mov {
int distance=50;
private MoveModel MoveModel;
private Showvisible showvisible;
public mov() {
MoveModel = new MoveModel();
showvisible = new Showvisible(MoveModel);
showvisible.getButton().addActionListener(e-> movePlayer());
//showvisible.getButton3(à.addActionListener(e))
}
//move player to a random position (works fine)
private void movePlayer() {
final Random rnd = new Random();
MoveModel.setPlayerX(rnd.nextInt(100));
MoveModel.setPlayerY(rnd.nextInt(100));
showvisible.refresh();
}
//Move player in the x direction to the East (doesn't work)
private void movePlayer2(int distance) {
MoveModel.setPlayerX(+50);
// MoveModel.setPlayerY(distance++);
//MoveModel.setPlayerX(distance);
//MoveModel.setPlayerX(playerX+distance); //Many variations tried, none of them works
showvisible.refresh();
}
public static void main(String[] args) {
new mov();
}
}
class Showvisible {
private final static int GAP = 2;
MoveModel MoveModel;
private MainPanel mainPanel;
Showvisible(MoveModel MoveModel){
this.MoveModel = MoveModel;
createAndShowGUI();
}
void refresh() {
mainPanel.repaint();
}
private void createAndShowGUI() {
JFrame board = new JFrame("Single Player Game");
board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new MainPanel();
board.add(mainPanel);
board.pack();
board.setVisible(true);
}
JButton getButton() { return mainPanel.getButton(); }
JButton getButton2() { return mainPanel.getButton(); }
class MainPanel extends JPanel {
private BottomPanel bPanel;
MainPanel() {
setLayout(new BorderLayout(GAP,GAP));
add(new TopPanel(), BorderLayout.PAGE_START);
add(new BoardPanel(), BorderLayout.CENTER);
bPanel = new BottomPanel();
add(bPanel, BorderLayout.PAGE_START);
}
JButton getButton() { return bPanel.getButton(); }
JButton getButton2() { return bPanel.getButton2(); }
}
class TopPanel extends JPanel {
TopPanel() {
setLayout(new FlowLayout(FlowLayout.CENTER));
add(new JLabel("This is an amazing game "));
}
}
class BoardPanel extends JPanel {
Player player;
BoardPanel() {
setBorder(BorderFactory.createLineBorder(Color.BLACK, GAP));
GridLayout layout = new GridLayout(MoveModel.getBoardRows(),
MoveModel.getBoardCols());
setLayout(layout);
for (int i = 0; i <MoveModel.getBoardRows(); i++) {
for (int j = 0; j < MoveModel.getBoardCols(); j++) {
add(new Tile());
}
}
player = new Player();
player.setBounds(new Rectangle(1000,1000, 1000,1000));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
player.paint(g);
}
}
class Tile extends JLabel {
Tile() {
setPreferredSize(new Dimension(MoveModel.getSquareSize(), MoveModel.getSquareSize()));
setBorder(BorderFactory.createLineBorder(Color.ORANGE, GAP));
}
}
class Player extends JLabel{
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(MoveModel.getPlayerX(), MoveModel.getPlayerY(), MoveModel.getPlayerSize(),MoveModel.getPlayerSize());
}
}
class BottomPanel extends JPanel {
JButton button = new JButton("Random Move");
JButton button2 =new JButton("move East");
BottomPanel(){
add(button);
add(button2);
}
JButton getButton() { return button; }
JButton getButton2() { return button2; }
}
}
class MoveModel{
private int boardRows = 9, boardCols = 9, squareSize = 50;
int playerX = 0;
private int playerY = 0;
private int playerSize =25;
int getPlayerX() { return playerX; }
void setPlayerX(int playerX) { this.playerX = playerX; }
int getPlayerY() {return playerY; }
void setPlayerY(int playerY) { this.playerY = playerY; }
int getPlayerSize() {return playerSize; }
int getBoardRows() {return boardRows; }
int getBoardCols() {return boardCols; }
int getSquareSize() {return squareSize; }
答案 0 :(得分:1)
在Showvisible
中进行更改
JButton getButton2() { return mainPanel.getButton(); }
到
JButton getButton2() { return mainPanel.getButton2(); }
在Mov
构造函数中(不要对class name使用mov
),将一个操作侦听器分配给按钮:
showvisible.getButton2().addActionListener(e-> movePlayerX(50));
并将此方法添加到Mov
中:
//Move player in the x direction to the East (positive) or west (negative
private void movePlayerX(final int distance) {
moveModel.setPlayerX(moveModel.getPlayerX() + distance);//get current x and add to it
showvisible.refresh();
}
要增加X,请使用getPlayerX()
来获取当前位置并将其添加。
完整的修改后的代码可以从here