Advertisement
Guest User

DailyProgrammer179

a guest
Sep 11th, 2014
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.57 KB | None | 0 0
  1. package rogueliketraveler;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.awt.image.*;
  6. import javax.swing.*;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.Random;
  10. import javax.imageio.ImageIO;
  11. import javax.swing.filechooser.FileNameExtensionFilter;
  12.  
  13. public class RogueLikeTraveler extends JPanel implements KeyListener{
  14.     public static void main(String[] args) {
  15.         JFrame f = new JFrame();
  16.         RogueLikeTraveler r = new RogueLikeTraveler();
  17.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.         f.setFocusable(true);
  19.         f.add(r);
  20.         f.addKeyListener(r);
  21.         f.pack();
  22.         f.setLocationRelativeTo(null);
  23.         f.setVisible(true);
  24.        
  25.     }
  26.     char[][] board;
  27.     final char PLAYER, WALL, COIN;
  28.     int moves, points;
  29.     BufferedImage imgPlayer, imgWall, imgCoin, map;
  30.     Point playerPos;
  31.  
  32.     public RogueLikeTraveler() {
  33.         PLAYER = '@';
  34.         WALL = '#';
  35.         COIN = '%';
  36.         moves = 100;
  37.         try {
  38.             imgPlayer = ImageIO.read(this.getClass().getResource("graphics/player.png"));
  39.             imgWall = ImageIO.read(this.getClass().getResource("graphics/wall.png"));
  40.             imgCoin = ImageIO.read(this.getClass().getResource("graphics/coin.png"));
  41.             map = ImageIO.read(this.getClass().getResource("graphics/Levels/first.png"));
  42.             parsePicToBoard(map);
  43.         }catch(IOException e) {
  44.             e.printStackTrace();
  45.         }
  46.         setPreferredSize(new Dimension(600, 700));
  47.     }
  48.     public void parsePicToBoard(BufferedImage pic) {
  49.         board = new char[20][20];
  50.         for (int i = 0; i < 20;i++) {
  51.             for (int j = 0; j < 20; j++) {
  52.                 if (new Color(pic.getRGB(i, j)).equals(Color.GREEN))
  53.                     board[i][j] = COIN;
  54.                 else if (new Color(pic.getRGB(i, j)).equals(Color.WHITE)){
  55.                     playerPos = new Point(i, j);
  56.                     board[i][j] = PLAYER;
  57.                 }
  58.                 else if (new Color(pic.getRGB(i, j)).equals( Color.RED))
  59.                     board[i][j] = WALL;
  60.                 else
  61.                     board[i][j] = ' ';
  62.             }
  63.         }
  64.     }
  65.     public void step(int move) {
  66.         Point toMoveTo = null;
  67.         if (move == KeyEvent.VK_RIGHT)
  68.             toMoveTo = new Point(playerPos.x + 1, playerPos.y);
  69.         else if (move == KeyEvent.VK_LEFT)
  70.             toMoveTo = new Point(playerPos.x - 1, playerPos.y);
  71.         else if (move == KeyEvent.VK_DOWN)
  72.             toMoveTo = new Point(playerPos.x, playerPos.y + 1);
  73.         else if (move == KeyEvent.VK_UP)
  74.             toMoveTo = new Point(playerPos.x, playerPos.y - 1);
  75.        
  76.         if (toMoveTo == null || board[toMoveTo.x][toMoveTo.y] == WALL) return;
  77.         else if (board[toMoveTo.x][toMoveTo.y] == COIN) {
  78.             points += 100;
  79.             spawnRandomCoin();
  80.         }
  81.         movePlayer(toMoveTo);
  82.     }
  83.     public void movePlayer(Point toMoveTo) {
  84.         board[playerPos.x][playerPos.y] = ' ';
  85.         playerPos = toMoveTo;
  86.         board[playerPos.x][playerPos.y] = PLAYER;
  87.         moves--;
  88.     }
  89.     public void spawnRandomCoin() {
  90.         Random r = new Random();
  91.         int x = r.nextInt(20), y = r.nextInt(20);
  92.         while (board[x][y] != ' ') {
  93.             x = r.nextInt(20);
  94.             y = r.nextInt(20);
  95.         }
  96.         board[x][y] = COIN;
  97.     }
  98.     public void reset() {
  99.         moves = 100;
  100.         points = 0;
  101.         try {
  102.             parsePicToBoard(map);
  103.         } catch (Exception e) {}
  104.     }
  105.  
  106.     public void paintComponent(Graphics g) {
  107.         g.setColor(Color.BLACK);
  108.         g.fillRect(0, 0, board.length * 30, getHeight());
  109.         for (int i = 0; i < board.length; i++) {
  110.             for (int j = 0; j < board.length; j++) {
  111.                 if (board[i][j] == PLAYER) g.drawImage(imgPlayer, i * 30, j * 30, null);
  112.                 else if (board[i][j] == WALL) g.drawImage(imgWall, i * 30, j * 30, null);
  113.                 else if (board[i][j] == COIN) g.drawImage(imgCoin, i * 30, j * 30, null);
  114.             }
  115.         }
  116.         g.setColor(Color.WHITE);
  117.         g.drawString("Moves: " + moves, 20, 650);
  118.         g.drawString("Points: " + points, 100, 650);
  119.         g.setColor(Color.WHITE);
  120.         g.drawString("Press I to import your own level! Make sure the image file is 20x20", 20, 670);
  121.         g.drawString("Each pixel represents something: Red = Wall, Green = Coin, White = Player", 20, 685);
  122.         g.setColor(Color.RED);
  123.         if (moves == 0)
  124.             g.drawString("DONE! press R to play again!", 200, 650);
  125.     }
  126.  
  127.     public void keyPressed(KeyEvent e) {
  128.         if (moves == 0) {
  129.             if (e.getKeyCode() == KeyEvent.VK_R)
  130.                 reset();
  131.         }
  132.         else if (e.getKeyCode() == KeyEvent.VK_I) {
  133.             try{
  134.                 JFileChooser chooser = new JFileChooser();
  135.                 chooser.setFileFilter(new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif", "png"));
  136.                 int returnVal = chooser.showOpenDialog(this);
  137.                 if(returnVal == JFileChooser.APPROVE_OPTION) {
  138.                     String path = chooser.getSelectedFile().getPath();
  139.                     map = ImageIO.read(new File(path));
  140.                     reset();
  141.             }
  142.         } catch (Exception ex) { System.exit(0); }
  143.         }
  144.         else {
  145.             step(e.getKeyCode());
  146.             if (new Random().nextInt(100) < 5) spawnRandomCoin();
  147.         }
  148.         repaint();
  149.     }
  150.     public void keyTyped(KeyEvent e) {}
  151.     public void keyReleased(KeyEvent e) {}
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement