Java 爪哇在棋盘上丢了我的骰子
我想写一个垄断游戏。下面你会看到我的游戏代码。首先,我做了游戏,我让代币穿过棋盘。然后我加入骰子。我在我的棋盘上看到了骰子。然后我想添加一个掷骰子按钮,当我掷骰子时,我的代币正在移动,但我再也看不到我的骰子了。现在,我仍然看不到我知道的一切,在网上找不到任何东西。有人能帮忙吗?如果您需要我的项目中的任何其他类,只需对其进行注释,我将在一分钟内添加它Java 爪哇在棋盘上丢了我的骰子,java,swing,jpanel,Java,Swing,Jpanel,我想写一个垄断游戏。下面你会看到我的游戏代码。首先,我做了游戏,我让代币穿过棋盘。然后我加入骰子。我在我的棋盘上看到了骰子。然后我想添加一个掷骰子按钮,当我掷骰子时,我的代币正在移动,但我再也看不到我的骰子了。现在,我仍然看不到我知道的一切,在网上找不到任何东西。有人能帮忙吗?如果您需要我的项目中的任何其他类,只需对其进行注释,我将在一分钟内添加它 public class Game { Player p1; Player p2; int posx = 619; int posy = 619;
public class Game {
Player p1;
Player p2;
int posx = 619;
int posy = 619;
int posx1 = 619;
int posy1 = 619;
int d1;
int d2;
int turn;
Card[] cards;
public Game(JTextArea textArea, JButton btnRollDice) {
cards = new Card[40];
initCards();
p1 = new Player("Onurcan", 1);
p2 = new Player("Batoç", 2);
play(textArea, btnRollDice);
}
public void play(JTextArea textArea, JButton button) {
Game g = this;
turn = p1.getPnumber();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Dice d = new Dice();
d1 = d.getDice1();
d2 = d.getDice2();
if (turn == p1.getPnumber()) {
textArea.append(p1.getPname() + "'s dice is " + d.getDice()
+ "\n");
Round r = new Round(p1, d, g);
r.changePosition(p1, d, g);
textArea.append(p1.getPname() + " welcome to "
+ g.getCards()[p1.getPosition()].getName() + "\n");
textArea.append(p1.getPname() + " your balance is "
+ p1.getBalance() + "\n");
turn = p2.getPnumber();
} else if (turn == p2.getPnumber()) {
textArea.append(p2.getPname() + "'s dice is " + d.getDice()
+ "\n");
Round r = new Round(p2, d, g);
r.changePosition(p2, d, g);
textArea.append(p2.getPname() + " welcome to "
+ g.getCards()[p2.getPosition()].getName() + "\n");
textArea.append(p2.getPname() + " your balance is "
+ p2.getBalance() + "\n");
turn = p1.getPnumber();
}
}
});
}
public class Board extends JPanel {
private ImageIcon image;
private JLabel label;
private JLabel dlabel;
private JLabel dlabel2;
public Board(Game g) {
setLayout(null);
image = new ImageIcon("board.jpg");
label = new JLabel(image);
label.setSize(700, 700);
dlabel2 = displayDice(g, g.getD2());
dlabel2.setSize(50, 50);
dlabel2.setLocation(351, 350);
dlabel = displayDice(g, g.getD1());
dlabel.setSize(50, 50);
dlabel.setLocation(299, 350);
g.getP1().getToken().setSize(24, 24);
g.getP1().getToken().setLocation(g.getPosx(), g.getPosy());
add(g.getP1().getToken());
g.getP2().getToken().setSize(24, 24);
g.getP2().getToken().setLocation(g.getPosx1(), g.getPosy1());
add(g.getP2().getToken());
add(dlabel);
add(dlabel2);
add(label);
}
public JLabel displayDice(Game g, int d) {
image = new ImageIcon("dice"+d+".jpg");
dlabel = new JLabel(image);
return dlabel;
}
}
}
}
}
}为什么每次点击按钮都要创建一个新的
骰子呢?因为我在掷骰子。你的建议是什么?你的代码不能运行。我们缺少了一些对解决您的问题很重要的课程,如玩家和卡片。帮助我们将您的代码缩减到尽可能小的Java应用程序来执行。除非我能运行代码,看看会发生什么,否则我不会处理成百上千行的代码。我加了所有的大副,有人能帮我吗。告诉我一点方法?只有一个骰子,每次你需要一个值时就掷它<代码>骰子d=新骰子()代码>int value=d.roll()
。。。骰子是一个对象,所以你不必每次需要值时都创建一个新的骰子!无论如何,我们不能帮你更多,因为你的代码中缺少部分。。。
public class Token extends JPanel {
ImageIcon image;
JLabel token;
public Token(int pnumber) {
if (pnumber == 1) {
image = new ImageIcon("hat.png");
token = new JLabel(image);
}
if (pnumber == 2) {
image = new ImageIcon("shoe.jpeg");
token = new JLabel(image);
}
add(token);
}
public class Dice {
int dice;
int dice1;
int dice2;
public Dice() {
Random rand = new Random();
Random rand2 = new Random();
dice1 = rand.nextInt(6);
dice2 = rand2.nextInt(6);
dice1++;
dice2++;
dice = dice1 + dice2;
}
public int getDice() {
return dice;
}
public int getDice1() {
return dice1;
}
public int getDice2() {
return dice2;
}
import javax.swing.JPanel;
public class Player {
String pname;
int balance;
int pnumber;
int position;
JPanel token;
public Player(String name, int p) {
pname = name;
balance = 1500;
position = 0;
pnumber = p;
token = new Token(pnumber);
}
public JPanel getToken() {
return token;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public int getPnumber() {
return pnumber;
}
public String getPname() {
return pname;
}
public class Card {
String name;
int price;
int rent;
int rent1;
int rent2;
int rent3;
int rent4;
int renth;
String colorgroup;
public Card(String n, int p, int r, int r1, int r2, int r3, int r4, int rh, String cg) {
name = n;
price = p;
rent = r;
rent1 = r1;
rent2 = r2;
rent3 = r3;
rent4 = r4;
renth = rh;
colorgroup = cg;
}
public Card(String n, String cg) {
name = n;
colorgroup = cg;
}
public Card(String n, int p, String cg) {
name = n;
price = p;
colorgroup = cg;
}
public Card(String n, int p, int r, int r1, int r2, int r3, String cg) {
name = n;
price = p;
rent = r;
rent1 = r1;
rent2 = r2;
rent3 = r3;
colorgroup = cg;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public int getRent() {
return rent;
}
public int getRent1() {
return rent1;
}
public int getRent2() {
return rent2;
}
public int getRent3() {
return rent3;
}
public int getRent4() {
return rent4;
}
public int getRenth() {
return renth;
}
public String getColorgroup() {
return colorgroup;
}