Reputation: 9
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
Upvotes: 0
Views: 87
Reputation: 51565
I could not get your code to compile.
Here's my best guess as to what your GUI looks like when it starts up.
Here's what the GUI looks like after I've eaten a pellet.
Here's what the GUI looks like after I've eaten more pellets.
Do not use static variables.
Do not use so many global variables. You created a model, so pass the model to the controller classes.
Why did you make the score variable a double
? An int
seems like it would work.
You had the right idea. You created a Packman
class to hold a packman and a Point
class to hold a pellet. I renamed your Point
class to Pellet
because there's a java.awt.Point
class that is useful for this code.
I saved the center point of the packman and the center point of each pellet. You'll see in the drawing code how I adjust the center point to draw an oval.
I created a GameModel
class to hold a java.util.List
of Pellet
instances, and a Packman
instance. The code to check for collisions is also included in this class.
The collision detection is simple. If the center of the pellet and the center of the packman are close enough together, then the packman eats the pellet.
I started the Swing application with a call to the SwingUtilities
invokeLater
method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.
I created a JFrame
, a score JPanel
, and a drawing JPanel
. I didn't extend a JFrame
. I used a JFrame
. I don't care how large the JFrame
is, so I used the pack
method to pack the components on the JFrame.
The score JPanel
uses a FlowLayout
to place the JLabel
and JTextField
in the JPanel
.
The drawing JPanel
extends JPanel
so I can override the paintComponent
method. It's much easier to draw on a drawing JPanel
. You don't have to take the rest of the GUI into account. All the drawing Panel
does is draw the packman and the pellets. Moving the packman and checking for collisions happen in the controller classes.
I used KeyBindings rather than a KeyListener
. Generally, key bindings are more reliable than a key listener. It's too easy for the component to lose focus and for the keys to stop working. Sure, the key bindings code looks scary at first glance. I have to look up the format every time I want to use them, which is why I provided the link. But in the long run key bindings are better.
I coded indirect key actions. The key presses just change the Direction
enum
in the Packman
class. The java.swing.Timer
ActionListener
actually moves the packman. This makes the motion much smoother.
If you want to make the packman move faster or slower, adjust the distance
parameter in the Packman
move
method.
For some reason I can't determine, the left and right arrow keys don't respond. I coded the WSAD keys as a substitute. I know the code works because it works with the WSAD keys. If I ever figure out why the left and right arrow keys are unresponsive, I'll update this answer.
I have a NavigationAction
class to listen for the key bindings and a MotionListener
class to move the packman.
The NavigationAction
class indirectly updates the Direction
enum
in the Packman
class.
The MotionListener
class moves the packman and checks for collisions. Since these methods are in other classes, the actionPerformed method is short and easy to understand.
Here's the complete, formatted, runnable code. I made all the classes inner classes so I could post the code as one large block.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.BorderFactory;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class PackmanGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new PackmanGUI());
}
private DrawingPanel drawingPanel;
private final GameModel model;
private JFrame frame;
private JPanel scorePanel;
private JTextField scoreField;
private Timer timer;
public PackmanGUI() {
this.model = new GameModel();
}
@Override
public void run() {
frame = new JFrame("Packman");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
scorePanel = createScorePanel();
frame.add(scorePanel, BorderLayout.BEFORE_FIRST_LINE);
drawingPanel = new DrawingPanel(model);
frame.add(drawingPanel, BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
setKeyBindings();
timer = new Timer(40, new MotionListener(this, model));
timer.start();
}
private void setKeyBindings() {
String up = "up";
String down = "down";
String left = "left";
String right = "right";
String pressed = " pressed";
String released = " released";
InputMap inputMap = drawingPanel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = drawingPanel.getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, false), up + pressed);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, false), down + pressed);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), left + pressed);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), right + pressed);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, true), up + released);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, true), down + released);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true), left + released);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), right + released);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), up + pressed);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), down + pressed);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), left + pressed);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), right + pressed);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), up + released);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), down + released);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true), left + released);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), right + released);
Packman packman = model.getPackman();
actionMap.put(up + pressed, new NavigationAction(Direction.UP, packman));
actionMap.put(down + pressed, new NavigationAction(Direction.DOWN, packman));
actionMap.put(left + pressed, new NavigationAction(Direction.LEFT, packman));
actionMap.put(right + pressed, new NavigationAction(Direction.RIGHT, packman));
actionMap.put(up + released, new NavigationAction(Direction.NONE, packman));
actionMap.put(down + released, new NavigationAction(Direction.NONE, packman));
actionMap.put(left + released, new NavigationAction(Direction.NONE, packman));
actionMap.put(right + released, new NavigationAction(Direction.NONE, packman));
}
private JPanel createScorePanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBackground(Color.YELLOW);
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
Font font = panel.getFont().deriveFont(Font.BOLD, 36f);
JLabel label = new JLabel("Score:");
label.setFont(font);
panel.add(label);
scoreField = new JTextField(10);
scoreField.setBackground(Color.YELLOW);
scoreField.setEditable(false);
scoreField.setFont(font);
scoreField.setHorizontalAlignment(JTextField.TRAILING);
updateScore(model.getPackman().getScore());
panel.add(scoreField);
return panel;
}
public void updateScore(double score) {
scoreField.setText(Double.toString(score));
}
public JFrame getFrame() {
return frame;
}
public void repaint() {
drawingPanel.repaint();
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
private final GameModel model;
public DrawingPanel(GameModel model) {
this.model = model;
this.setBackground(Color.BLACK);
this.setPreferredSize(new Dimension(800, 800));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Packman packman = model.getPackman();
Point point = packman.getCenterPoint();
// Draw body
int x = point.x - 25;
int y = point.y - 25;
g.setColor(Color.YELLOW);
g.fillArc(x, y, 50, 50, 30, 300);
// Draw eye
x = point.x;
y = point.y - 18;
g.setColor(Color.BLACK);
g.fillOval(x, y, 8, 8);
// Draw pellets
for (Pellet pellet : model.getPellets()) {
g.setColor(pellet.getColor());
point = pellet.getPoint();
x = point.x - 10;
y = point.y - 10;
g.fillOval(x, y, 20, 20);
}
}
}
public interface KeyDirectionActionHandler {
public void keyDirectionActionPerformed(Direction direction);
}
public class NavigationAction extends AbstractAction {
private static final long serialVersionUID = 1L;
private Direction direction;
private KeyDirectionActionHandler keyDirectionActionHandler;
public NavigationAction(Direction direction,
KeyDirectionActionHandler keyDirectionActionHandler) {
this.direction = direction;
this.keyDirectionActionHandler = keyDirectionActionHandler;
}
@Override
public void actionPerformed(ActionEvent event) {
keyDirectionActionHandler.keyDirectionActionPerformed(direction);
}
}
public enum Direction {
NONE, UP, DOWN, RIGHT, LEFT;
}
public class MotionListener implements ActionListener {
private final PackmanGUI frame;
private final GameModel model;
public MotionListener(PackmanGUI frame, GameModel model) {
this.frame = frame;
this.model = model;
}
@Override
public void actionPerformed(ActionEvent event) {
model.getPackman().move();
model.checkCollisions();
frame.updateScore(model.getPackman().getScore());
frame.repaint();
}
}
public class GameModel {
private final List<Pellet> pellets;
private final Packman packman;
public GameModel() {
this.packman = new Packman(0, new Point(400, 400));
this.pellets = new ArrayList<>();
inicialization();
}
public void inicialization() {
int x = 600;
int y = 600;
for (int i = 0; i < 4; i++) {
Pellet pellet = new Pellet(new Point(x, y), 20, Color.WHITE);
y -= 100;
pellets.add(pellet);
}
for (int i = 0; i < 4; i++) {
Pellet pellet = new Pellet(new Point(x, y), 20, Color.WHITE);
x -= 100;
pellets.add(pellet);
}
}
public void checkCollisions() {
Point packmanPoint = packman.getCenterPoint();
for (int index = 0; index < pellets.size(); index++) {
Pellet pellet = pellets.get(index);
Point pelletPoint = pellet.getPoint();
// Collision detection
int deltaX = packmanPoint.x - pelletPoint.x;
int deltaY = packmanPoint.y - pelletPoint.y;
double distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
if (distance < 20) {
pellets.remove(pellet);
packman.incrementScore(pellet.getValue());
}
}
}
public List<Pellet> getPellets() {
return pellets;
}
public Packman getPackman() {
return packman;
}
}
public class Packman implements KeyDirectionActionHandler {
private double score;
private Direction direction;
private Point centerPoint;
public Packman(double score, Point centerPoint) {
this.score = score;
this.centerPoint = centerPoint;
this.direction = Direction.NONE;
}
public double getScore() {
return score;
}
public void incrementScore(double score) {
this.score += score;
}
public Point getCenterPoint() {
return centerPoint;
}
public void setCenterPoint(Point centerPoint) {
this.centerPoint = centerPoint;
}
public void move() {
int distance = 6;
int deltaX = 0;
int deltaY = 0;
switch (direction) {
case NONE:
deltaX = 0;
deltaY = 0;
break;
case UP:
deltaX = 0;
deltaY = -distance;
break;
case DOWN:
deltaX = 0;
deltaY = distance;
break;
case LEFT:
deltaX = -distance;
deltaY = 0;
break;
case RIGHT:
deltaX = distance;
deltaY = 0;
}
int x = centerPoint.x + deltaX;
int y = centerPoint.y + deltaY;
this.centerPoint = new Point(x, y);
}
@Override
public void keyDirectionActionPerformed(Direction direction) {
this.direction = direction;
}
}
public class Pellet {
private double value;
private Color color;
private Point point;
public Pellet(Point point, double value, Color color) {
this.point = point;
this.value = value;
this.color = color;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
}
}
Upvotes: 0
Reputation: 9
pacman after what changed direction
public class Hra extends JFrame {
Timer timer;
private JPanel contentPane;
PackMan packman ;
Points point ;
boolean check;
ArrayList<Point> points = new ArrayList<Point>();
static int x =900;
static int y=600;
Color packCol = Color.BLACK;
Color pointCol = Color.WHITE;
static Direction vysledek = new Direction(null);
static int newX = 900;
static int newY =200;
static int xVel = 1;
static int yVel = 1;
static int count = 1;
static JLabel gifLabel = new JLabel(new ImageIcon(//path of Image));
/**
* Launch the application.
*/
public static void main(String[] args) {
Game frame = new Game();
frame.setVisible(true);
frame.getContentPane().add(gifLabel);
gifLabel.setLocation(packman.getCoordinatesX(),packman.getCoordinatesY()-38);
}
public void inicialization() {
for (int i = 0; i < 4; i++) {
Point point = new Point(x, y, 20, pointCol);
y -=100;
points.add(point);
}
for (int i = 0; i < 4; i++) {
Point p = new Point(newX, newY, 20, pointCol);
newX-=100;
points.add(p);
}
}
public GAME() {
inicialization();
packman = new PackMan(0, 900, 900,packCol);
point = new point(900,800,20,pointCol);
check = false;
timer = new Timer(10,this);
timer.start();
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
count = 1;
repaint();
chechCollision();
break;
case 38: //up
count =2;
repaint();
chechCollision();
break;
case 39://right
count = 3;
repaint();
chechCollision();
break;
case 40://down
count = 4;
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());
for (int i = 0; i < points.size(); i++) {
g.drawOval(points.get(i).getPointX(), points.get(i).getPointY(), 50, 50);
g.fillOval(points.get(i).getPointX(), points.get(i).getPointY(), 50, 50);
}
}
}
public void checkCollision() {
if (packman.getCoordinatesX() == point.getPointX() && packman.getCoordinatesY() == point.getPointY()) {
packman.setScore(packman.getScore() + point.getValueOfPoint());
lblNewLabel.setText("Score:" + packman.getScore() );
points(i);
repaint();
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (count == 1) {
packman.setCoordinatesX(packman.getCoordinates() - xVel);
gifLabel.setLocation(packman.getCoordinatesX(), packman.getCoordinatesY()-38);
repaint();
chechCollision();
}
if (count ==2) {
packman.setCoordinatesY(packman.getCoordinatesY() - yVel);
gifLabel.setLocation(packman.getCoordinatesX(), packman.getCoordinatesY()-38);
repaint();
chechCollision();
}
if (count ==3) {
packman.setCoordinatesX(packman.getSouradniceX() + xRychlost);
gifLabel.setLocation(packman.getSouradniceX(), packman.getSouradniceY()-38);
repaint();
chechCollision();
}
if (count ==4) {
packman.setCoordinatesY(packman.getCoordinatesY() + yVel);
gifLabel.setLocation(packman.getSouradniceX(), packman.getCoordinatesY()-38);
repaint();
chechCollision();
}
}
public class Direction {
private Directions directions;
private int index;
public Smery getDirection() {
return directions;
}
public void setSmery(Directions directions) {
this.directions = directions;
}
public directions(Directions directions) {
super();
this.directions = directions;
}
public enum Smery {
doprava,doleva,nahoru,dolu;
}
Upvotes: 0
Reputation: 9
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();
points.remove(i);
}
Upvotes: 0