My game is similar to Packman. My problem is, that if Packman will eat the point, no one disappear respectively every point except the first one will change colour like background. I know a I have it in method, but It did what I want when I draw just one point. I just want to clear the point which Packman ate. I created the window in WindowBuilder (I just wanted to try it), I hope it won't be a problem.
public class Hra extends JFrame {
private JPanel contentPane;
PackMan packman ;
Points point ;
boolean check;
ArrayList<Body> points = new ArrayList<Body>();
static int x =900;
static int y=600;
Color packCol = Color.BLACK;
Color pointCol = Color.WHITE;
/**
* Launch the application.
*/
public static void main(String[] args) {
Game frame = new Game();
frame.setVisible(true);
}
public void inicialization() {
for (int i = 0; i < 4; i++) {
Point point = new Point(x, y, 20, pointCol);
x +=100;
points.add(point);
}
}
public GAME() {
inicialization();
packman = new PackMan(0, 900, 900,packCol);
point = new point(900,800,20,pointCol);
check = false;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
getContentPane().setBackground(Color.YELLOW);
JLabel lblNewLabel = new JLabel("Score:" + packman.getScore());
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
.addContainerGap(309, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
.addContainerGap(229, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);
this.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
switch(e.getKeyCode()) {
case 37: //left
packman.setCoordinatesX(packman.getCoordinatesX()-10);
repaint();
chechCollision();
break;
case 38: //up
packman.setCoordinatesY(packman.getCoordinatesY()-10);
repaint();
chechCollision();
break;
case 39://right
packman.setCoordinatesX(packman.getCoordinatesX()+10);
repaint();
chechCollision();
break;
case 40://down
packman.setCoordinatesY(packman.getCoordinatesY()+10);
chechCollision();
repaint();
break;
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
});
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.fillRect(packman.getCoordinatesX(), packman.getCoordinatesY(), 50, 50);
g.setColor(point.getColor());
g.fillRect(point.getPointX(), body.getPointY(), 50, 50);
g.setColor(Color.BLACK);
for(int i =0;i<points.size();i++){
if (kontrola) {
g.clearRect(points.get(i).getPointX(), points.get(i).getPointY(), 50, 50);
}
}
for (int i = 0; i < bodiky.size(); i++) {
g.fillRect(points.get(i).getPointX(), points.get(i).getPointY(), 50, 50);
g.setColor(points.get(i).getColor());
}
}
}
public void checkCollision() {
if (packman.getCoordinatesX() == point.getPointX() && packman.getCoordinatesY() == point.getPointY()) {
packman.setScore(packman.getScore() + point.getValueOfPoint());
lblNewLabel.setText("Score:" + packman.getScore() );
check = true;
point.setColor(Color.YELLOW);
point.setValueOfPoint(0);
repaint();
}
}
}
}
public class Point{
private Color color;
private int pointX;
private int pointY;
private double valueofPoint;
public int getCoordinatesPointX() {
return pointX;
}
public Point(int pointX, int pointY, double valueofPoint,Color color) {
super();
this.pointX = pointX;
this.pointY = pointY;
this.valueofPoint= valueofPoint;
this.color = color;
}
public void sePointX(int pointX) {
this.pointX = pointX;
}
public int getPointY() {
return pointY;
}
public void setPointY(int pointY) {
this.pointY = pointY;
}
public double getValueofPoint() {
return valueofPoint;
}
public void setValueofPoint(double valueofPoint) {
this.valueofPoint = valueofPoint;
}
public void setColor(Color color){
this.color = color;
}
public Color getColor(){
return color;
}
}
public class PackMan {
private double score;
private int coordinatesX;
private int coordinatesY;
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public int getCoordinatesX() {
return coordinatesX;
}
public void setCoordinatesX(int coordinatesX) {
this.coordinatesX = coordinatesX;
}
public int getCoordinatesY() {
return coordinatesY;
}
public void setCoordinatesY(int coordinatesY) {
this.coordinatesY = coordinatesY;
}
public PackMan(double score, int coordinatesX, int coordinatesY) {
super();
this.score = score;
this.coordinatesX = coordinatesX;
this.coordinatesY = coordinatesY;
}
}
[2 picture] result and problem which I described in 1