public char getCharTo(Point location) { int x1 = location.x; int y1 = location.y; if(x1 < 0){ x1 = 0; } if(x1 > 14){ x1 = 14; } if(y1 < 0){ y1 = 0; } if(y1 > 14){ y1 = 14; } location.x = x1; location.y = y1; return currentArene[location.x][location.y]; }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionimport java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; public class PacmanDemo { public static void main(final String[] args) { Runnable gui = new Runnable() { public void run() { new MainFrame().setVisible(true); } }; SwingUtilities.invokeLater(gui); } } class MainFrame extends JFrame implements KeyListener { private JLabel pacMan; private Point location = new Point(); private Point next = new Point(); private PacmanMap map = new PacmanMap(); private Pacman pacRight, pacLeft, pacUp, pacDown; public MainFrame() { this.setTitle("Pac-Man Demo: use arrow keys to play"); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(350, 350); this.setLocationRelativeTo(null); this.addKeyListener(this); initPacMan(); } private void initPacMan() { pacRight = new Pacman("right"); pacLeft = new Pacman("left"); pacUp = new Pacman("up"); pacDown = new Pacman("down"); pacMan = new JLabel(pacLeft); //Ajout de la map et du PacMan this.setContentPane(map); map.add(pacMan); //Mise en place du Pac Man pacMan.setBounds(200, 40, 20, 20); //Mise en place de la Map map.currentArene = map.arene1x1; } public void keyPressed(final KeyEvent e) { location.x = pacMan.getX() / 20; location.y = pacMan.getY() / 20; next.x = location.x; next.y = location.y; if (e.getKeyCode() == KeyEvent.VK_UP) { pacMan.setIcon(pacUp); next.y -= 1; if (map.getCharTo(next) == ' ') { pacMan.setLocation(pacMan.getX(), pacMan.getY() - 20); } } else if (e.getKeyCode() == KeyEvent.VK_LEFT) { pacMan.setIcon(pacLeft); next.x -= 1; if (map.getCharTo(next) == ' ') { pacMan.setLocation(pacMan.getX() - 20, pacMan.getY()); } } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { pacMan.setIcon(pacRight); next.x += 1; if (map.getCharTo(next) == ' ') { pacMan.setLocation(pacMan.getX() + 20, pacMan.getY()); } } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { pacMan.setIcon(pacDown); next.y += 1; if (map.getCharTo(next) == ' ') { pacMan.setLocation(pacMan.getX(), pacMan.getY() + 20); } } } public void keyReleased(final KeyEvent e) { } public void keyTyped(final KeyEvent e) { } } class PacmanMap extends JPanel { public char[][] currentArene; public char[][] arene1x1 = { {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'}, {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'}, {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'}, {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'}, {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'}, {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'}, {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'}, {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'}, {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'}, {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'}, {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'}, {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'}, {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'}, {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'}, {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'} }; private BufferedImage image; public PacmanMap() { setLayout(null); } @Override public void paintComponent(final Graphics g) { super.paintComponent(g); g.drawImage(image, 0, 0, this); } public char getCharTo(final Point location) { return currentArene[location.x][location.y]; } } class Pacman implements Icon { private int[] xpoints; private int[] ypoints; private Polygon bouche; private Color color = Color.ORANGE; public Pacman(final String dir) { if (dir.equals("right")) { xpoints = new int[]{10, 20, 20}; ypoints = new int[]{10, 0, 20}; } else if (dir.equals("left")) { xpoints = new int[]{10, 0, 0}; ypoints = new int[]{10, 0, 20}; } else if (dir.equals("up")) { xpoints = new int[]{10, 0, 20}; ypoints = new int[]{10, 0, 0}; } else if (dir.equals("down")) { xpoints = new int[]{10, 0, 20}; ypoints = new int[]{10, 20, 20}; } bouche = new Polygon(xpoints, ypoints, 3); } public void paintIcon(final Component c, final Graphics g, final int x, final int y) { g.setColor(color); g.fillOval(0, 0, 20, 20); g.setColor(c.getBackground()); g.fillPolygon(bouche); } public int getIconWidth() { return 20; } public int getIconHeight() { return 20; } }