我是Java编程的初学者。我正在尝试创建一个跳棋游戏,玩家可以在其中移动自己的棋子。问题是,当移动一件时,所有件都会移动它。我真的很感激一些帮助。感谢!!!
主类
package Checkers_Own;
import resources.DrawingBoard;
import resources.Timer;
public class Game {
public static void main(String[] args) {
Board[][] boards = new Board[8][8];
DrawingBoard board = new DrawingBoard(840, 640);
Timer timer = new Timer();
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
if(i % 2 == 1){
if(j % 2 == 1)boards[i][j] = new Board(true, i, j);
else boards[i][j] = new Board(false, i, j);
}
else if(i % 2 == 0){
if(j % 2 == 1)boards[i][j] = new Board(false, i, j);
else boards[i][j] = new Board(true, i, j);
}
}
}
for(int i = 5; i < 8; i++) for(int j = 0; j < 8; j++) if(!boards[j][i].isWhite) PiecesManager.add1(new Pieces(true, j, i));
for(int i = 0; i < 8; i++) for(int j = 0; j < 3; j++) if(!boards[i][j].isWhite) PiecesManager.add2(new Pieces(false, i, j));
PiecesManager.properties(board);
while(true){
board.clear();
for(int i = 0; i < 8; i++) for(int j = 0; j < 8; j++) boards[i][j].draw(board);
PiecesManager.draw(board);
PiecesManager.move();
board.repaint();
timer.pause(30);
}
}
}
件
package Checkers;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import resources.DrawingBoard;
public class Pieces implements MouseMotionListener, MouseListener{
boolean side1, changed = false;
int x, y, lastX, lastY, dx, dy;
public Pieces(boolean side1, int x, int y){
this.side1 = side1;
this.x = (x * 80) + 10;
this.y = (y * 80) + 10;
}
public void draw(DrawingBoard board){
Graphics2D g = board.getCanvas();
if(side1){
g.setColor(Color.blue);
g.fillOval(x, y, 60, 60);
}
else{
g.setColor(Color.red);
g.fillOval(x, y, 60, 60);
}
}
public Pieces moved(){
if(changed){
return this;
}
return null;
}
public void move(){
if(changed){
x += dx;
y += dy;
}
changed = false;
}
public void mousePressed(MouseEvent e) {
lastX = e.getX();
lastY = e.getY();
}
public void mouseDragged(MouseEvent e) {
int mx = e.getX();
dx = mx - lastX;
lastX = mx;
int my = e.getY();
dy = my - lastY;
lastY = my;
if(mx != x || my != y) changed = true;
}
public void mouseMoved(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
板
package Checkers_Own;
import java.awt.Color;
import java.awt.Graphics2D;
import resources.DrawingBoard;
public class Board {
boolean isWhite;
private int x, y;
public Board(boolean isWhite, int x, int y){
this.isWhite = isWhite;
this.x = x * 80;
this.y = y * 80;
}
public void draw(DrawingBoard board){
Graphics2D g = board.getCanvas();
if(isWhite){
g.setColor(Color.white);
g.fillRect(x, y, 80, 80);
}
else{
g.setColor(Color.black);
g.fillRect(x, y, 80, 80);
}
}
}
PiecesManager
package Checkers_Own;
import resources.DrawingBoard;
public class PiecesManager {
private static int size1, size2;
private static Pieces[] p1 = new Pieces[12];
private static Pieces[] p2 = new Pieces[12];
public static void draw(DrawingBoard board){
for(int i = 0; i < size1; i++) p1[i].draw(board);
for(int i = 0; i < size2; i++) p2[i].draw(board);
}
public static void move(){
for(int i = 0; i < p1.length; i++) if(p1[i].moved() == p1[i]) p1[i].move();
for(int i = 0; i < p2.length; i++) if(p2[i].moved() == p2[i]) p2[i].move();
}
public static void add1(Pieces piece){
p1[size1] = piece;
size1 ++;
}
public static void add2(Pieces piece){
p2[size2] = piece;
size2 ++;
}
public static void properties(DrawingBoard board){
for(int i = 0; i < p1.length; i++){
board.addMouseListener(p1[i]);
board.addMouseMotionListener(p1[i]);
}
for(int i = 0; i < p2.length; i++){
board.addMouseListener(p2[i]);
board.addMouseMotionListener(p2[i]);
}
}
}