Guest User

Push

a guest
May 24th, 2013
46
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.Dimension;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.Image;
  5. import java.awt.image.BufferedImage;
  6. import java.io.IOException;
  7. import javax.imageio.ImageIO;
  8. import javax.swing.ImageIcon;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.KeyEvent;
  12. import java.awt.event.KeyListener;
  13. import javax.swing.JFrame;
  14. import javax.swing.JPanel;
  15. import javax.swing.Timer;
  16.  
  17. public class Push extends JPanel
  18. {
  19.     private static final long serialVersionUID = -3178471650891130615L;
  20.     public static void main(String[] args)
  21.     {
  22.         final JFrame frame = new JFrame("PUSH");
  23.         final Push game = new Push();
  24.         frame.add(game);
  25.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  26.         frame.pack();
  27.         frame.setLocation(50, 50);
  28.         frame.setVisible(true);
  29.         frame.setResizable(false);
  30.         final Timer timer = new Timer(35, new ActionListener(){
  31.  
  32.             public void actionPerformed(ActionEvent e) {
  33.             {
  34.                 if (game.act())
  35.                     game.nextLevel();
  36.                 frame.validate();
  37.                 frame.repaint();
  38.             }
  39.             }});
  40.         final KeyListener keys = new KeyListener(){
  41.  
  42.             public void keyPressed(KeyEvent arg0)
  43.             {
  44.                     switch (arg0.getKeyCode()){
  45.                     case KeyEvent.VK_LEFT :
  46.                     case KeyEvent.VK_RIGHT :
  47.                     case KeyEvent.VK_DOWN :
  48.                     case KeyEvent.VK_UP :
  49.                         if (game.canMove())
  50.                             game.moveDir(arg0.getKeyCode());
  51.                         break;
  52.                     case KeyEvent.VK_ESCAPE :
  53.                         System.out.println("Game over");
  54.                         System.exit(0);
  55.                         break;
  56.                     case KeyEvent.VK_HOME :
  57.                         timer.stop();
  58.                         game.setLevel();
  59.                         timer.start();
  60.                         break;
  61.                     }
  62.                 }
  63.             public void keyReleased(KeyEvent arg0) {}
  64.  
  65.             public void keyTyped(KeyEvent arg0) {}
  66.            
  67.         };
  68.         timer.start();
  69.         frame.addKeyListener(keys);
  70.     }
  71.    
  72.     final Image BALL = new ImageIcon(this.getClass().getResource("Ball.png")).getImage();
  73.     private BufferedImage PALETTE;{
  74.         try {
  75.             PALETTE = ImageIO.read(this.getClass().getResource("Blockpalette.png"));
  76.         } catch (IOException e) {
  77.         }};
  78.     static final Dimension DEFAULTSIZE = new Dimension(170,100);
  79.     private byte level, r, c, vx, vy, vxl, vyl;
  80.     private boolean m, won;
  81.     static Image[] blocks = new Image[13];
  82.     private byte[][] map = new byte[8][16];
  83.     public Push()
  84.     {
  85.         setPreferredSize(DEFAULTSIZE);
  86.         for (int i = 0; i < blocks.length; i++)
  87.             blocks[i] = PALETTE.getSubimage(10 * i, 0, 10, 10);
  88.         level = 1;
  89.         m = true;
  90.         won = false;
  91.         setLevel();
  92.     }
  93.     public Image ball()
  94.     {
  95.         return BALL;
  96.     }
  97.     public byte level()
  98.     {
  99.         return level;
  100.     }
  101.     public void nextLevel()
  102.     {
  103.         level++;
  104.         setLevel();
  105.     }
  106.     public boolean canMove()
  107.     {
  108.         return m;
  109.     }
  110.     private void setLevel()
  111.     {
  112.         r = c = 2;
  113.         vx = vy = vxl = vyl = 0;
  114.         m = true;
  115.         won = false;
  116.         for (int i = 0; i < 8; i++)
  117.             for (int j = 0; j < 16; j++)
  118.                 map[i][j] = STORAGE[level-1][i][j];
  119.     }
  120.     public void paint(Graphics g)
  121.     {
  122.         Graphics2D g2 = (Graphics2D) g;
  123.         for (int i = 0; i < 8; i++)
  124.             for (int j = 0; j < 16; j++)
  125.                 g2.drawImage(blocks[map[i][j]], 10 * j, 10 * i, null);
  126.         g2.drawImage(BALL, 10 * c, 10 * r, null);
  127.     }
  128.     public void moveDir(int key)
  129.     {
  130.         switch (key){
  131.         case KeyEvent.VK_LEFT :
  132.             vx = -1;
  133.             vy = 0;
  134.             break;
  135.         case KeyEvent.VK_RIGHT :
  136.             vx = 1;
  137.             vy = 0;
  138.             break;
  139.         case KeyEvent.VK_DOWN :
  140.             vx = 0;
  141.             vy = 1;
  142.             break;
  143.         case KeyEvent.VK_UP :
  144.             vx = 0;
  145.             vy = -1;
  146.             break;
  147.         }
  148.         m = false;
  149.     }
  150.     public boolean act()
  151.     {
  152.         if (won)
  153.             return true;
  154.         if (vx == 0)
  155.             vertical();
  156.         else if (vy == 0)
  157.             horizontal();
  158.         if (!m && vx == 0 && vy == 0)
  159.         {
  160.             switch (map[r][c]){
  161.             case 1 :
  162.                 won = true;
  163.                 break;
  164.             case 6 :
  165.                 r -= vxl;
  166.                 c -= vyl;
  167.                 if (vxl != 0)
  168.                 {
  169.                     vyl = (byte) (vxl * -1);
  170.                     vxl = 0;
  171.                 }
  172.                 else if (vyl != 0)
  173.                 {
  174.                     vxl = (byte) (vyl * -1);
  175.                     vyl = 0;
  176.                 }
  177.                 break;
  178.             case 9 :
  179.                 vxl = 1;
  180.                 vyl = 0;
  181.                 c += 2;
  182.                 break;
  183.             case 10 :
  184.                 vxl = -1;
  185.                 vyl = 0;
  186.                 c -= 2;
  187.                 break;
  188.             case 11 :
  189.                 vxl = 0;
  190.                 vyl = -1;
  191.                 r -= 2;
  192.                 break;
  193.             case 12 :
  194.                 vxl = 0;
  195.                 vyl = 1;
  196.                 r -= 2;
  197.                 break;
  198.             case 13 :
  199.                 vxl = 0;
  200.                 vyl = 0;
  201.                 r = c = 2;
  202.                 break;
  203.             case 0 :
  204.             case 2 :
  205.             case 3 :
  206.             case 4 :
  207.             case 7 :
  208.             case 8 :
  209.                 m = true;
  210.                 break;
  211.             case 5 :
  212.                 m = true;
  213.                 map[r][c] = 3;
  214.                 break;
  215.             }
  216.         }
  217.         return false;
  218.     }
  219.     private void horizontal()
  220.     {
  221.         c += vx;
  222.         switch (map[r][c]){
  223.         case 1 :
  224.             won = true;
  225.             break;
  226.         case 6 :
  227.         case 9 :
  228.         case 10 :
  229.         case 11 :
  230.         case 12 :
  231.         case 13 :
  232.             vxl = vx;
  233.             vyl = vy;
  234.             vx = 0;
  235.             m = false;
  236.             break;
  237.         case 4 :
  238.             vx = 0;
  239.             vxl = 0;
  240.             vyl = 0;
  241.             m = true;
  242.             break;
  243.         case 2 :
  244.         case 3 :
  245.         case 7 :
  246.             c -= vx;
  247.             vx = 0;
  248.             vxl = 0;
  249.             vyl = 0;
  250.             m = true;
  251.             break;
  252.         case 5 :
  253.             map[r][c] = 2;
  254.             break;
  255.         }
  256.     }
  257.     private void vertical()
  258.     {
  259.         r += vy;
  260.         switch (map[r][c]){
  261.         case 1 :
  262.             won = true;
  263.             break;
  264.         case 6 :
  265.         case 9 :
  266.         case 10 :
  267.         case 11 :
  268.         case 12 :
  269.         case 13 :
  270.             vxl = vx;
  271.             vyl = vy;
  272.             vy = 0;
  273.             m = false;
  274.             break;
  275.         case 4 :
  276.             vy = 0;
  277.             vxl = 0;
  278.             vyl = 0;
  279.             m = true;
  280.             break;
  281.         case 2 :
  282.         case 3 :
  283.         case 8 :
  284.             r -= vy;
  285.             vy = 0;
  286.             vxl = 0;
  287.             vyl = 0;
  288.             m = true;
  289.             break;
  290.         case 5 :
  291.             map[r][c] = 2;
  292.             break;
  293.         }
  294.     }
  295.     static final byte[][][] STORAGE = {
  296.         //Level 1
  297.         {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
  298.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  299.         {2,0,0,0,0,0,5,0,0,0,0,0,0,0,0,2},
  300.         {2,0,0,0,0,0,0,0,0,6,0,8,0,1,0,2},
  301.         {2,0,0,0,6,0,0,4,0,0,0,0,0,0,0,2},
  302.         {2,0,0,0,0,0,0,0,0,0,3,0,0,0,0,2},
  303.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  304.         {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
  305.         //Level 2
  306.         {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
  307.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  308.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  309.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  310.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  311.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  312.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  313.         {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
  314.         //Level 3
  315.         {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
  316.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  317.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  318.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  319.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  320.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  321.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  322.         {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
  323.         //Level 4
  324.         {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
  325.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  326.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  327.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  328.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  329.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  330.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  331.         {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
  332.         //Level 5
  333.         {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
  334.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  335.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  336.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  337.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  338.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  339.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  340.         {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
  341.         //Level 6
  342.         {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
  343.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  344.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  345.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  346.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  347.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  348.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  349.         {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
  350.         //Level 7
  351.         {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
  352.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  353.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  354.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  355.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  356.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  357.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  358.         {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
  359.         //Level 8
  360.         {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
  361.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  362.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  363.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  364.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  365.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  366.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  367.         {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
  368.         //Level 9
  369.         {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
  370.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  371.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  372.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  373.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  374.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  375.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  376.         {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
  377.         //Level 10
  378.         {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
  379.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  380.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  381.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  382.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  383.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  384.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  385.         {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
  386.         //Level 11
  387.         {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
  388.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  389.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  390.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  391.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  392.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  393.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  394.         {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
  395.         //Level 12
  396.         {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
  397.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  398.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  399.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  400.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  401.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  402.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  403.         {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
  404.         //Level 13
  405.         {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
  406.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  407.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  408.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  409.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  410.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  411.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  412.         {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
  413.         //Level 14
  414.         {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
  415.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  416.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  417.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  418.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  419.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  420.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  421.         {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
  422.         //Level 15
  423.         {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
  424.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  425.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  426.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  427.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  428.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  429.         {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
  430.         {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
  431.     };
  432. }
RAW Paste Data