Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.ImageIcon;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- public class Push extends JPanel
- {
- private static final long serialVersionUID = -3178471650891130615L;
- public static void main(String[] args)
- {
- final JFrame frame = new JFrame("PUSH");
- final Push game = new Push();
- frame.add(game);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.pack();
- frame.setLocation(50, 50);
- frame.setVisible(true);
- frame.setResizable(false);
- final Timer timer = new Timer(35, new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- {
- if (game.act())
- game.nextLevel();
- frame.validate();
- frame.repaint();
- }
- }});
- final KeyListener keys = new KeyListener(){
- public void keyPressed(KeyEvent arg0)
- {
- switch (arg0.getKeyCode()){
- case KeyEvent.VK_LEFT :
- case KeyEvent.VK_RIGHT :
- case KeyEvent.VK_DOWN :
- case KeyEvent.VK_UP :
- if (game.canMove())
- game.moveDir(arg0.getKeyCode());
- break;
- case KeyEvent.VK_ESCAPE :
- System.out.println("Game over");
- System.exit(0);
- break;
- case KeyEvent.VK_HOME :
- timer.stop();
- game.setLevel();
- timer.start();
- break;
- }
- }
- public void keyReleased(KeyEvent arg0) {}
- public void keyTyped(KeyEvent arg0) {}
- };
- timer.start();
- frame.addKeyListener(keys);
- }
- final Image BALL = new ImageIcon(this.getClass().getResource("Ball.png")).getImage();
- private BufferedImage PALETTE;{
- try {
- PALETTE = ImageIO.read(this.getClass().getResource("Blockpalette.png"));
- } catch (IOException e) {
- }};
- static final Dimension DEFAULTSIZE = new Dimension(170,100);
- private byte level, r, c, vx, vy, vxl, vyl;
- private boolean m, won;
- static Image[] blocks = new Image[13];
- private byte[][] map = new byte[8][16];
- public Push()
- {
- setPreferredSize(DEFAULTSIZE);
- for (int i = 0; i < blocks.length; i++)
- blocks[i] = PALETTE.getSubimage(10 * i, 0, 10, 10);
- level = 1;
- m = true;
- won = false;
- setLevel();
- }
- public Image ball()
- {
- return BALL;
- }
- public byte level()
- {
- return level;
- }
- public void nextLevel()
- {
- level++;
- setLevel();
- }
- public boolean canMove()
- {
- return m;
- }
- private void setLevel()
- {
- r = c = 2;
- vx = vy = vxl = vyl = 0;
- m = true;
- won = false;
- for (int i = 0; i < 8; i++)
- for (int j = 0; j < 16; j++)
- map[i][j] = STORAGE[level-1][i][j];
- }
- public void paint(Graphics g)
- {
- Graphics2D g2 = (Graphics2D) g;
- for (int i = 0; i < 8; i++)
- for (int j = 0; j < 16; j++)
- g2.drawImage(blocks[map[i][j]], 10 * j, 10 * i, null);
- g2.drawImage(BALL, 10 * c, 10 * r, null);
- }
- public void moveDir(int key)
- {
- switch (key){
- case KeyEvent.VK_LEFT :
- vx = -1;
- vy = 0;
- break;
- case KeyEvent.VK_RIGHT :
- vx = 1;
- vy = 0;
- break;
- case KeyEvent.VK_DOWN :
- vx = 0;
- vy = 1;
- break;
- case KeyEvent.VK_UP :
- vx = 0;
- vy = -1;
- break;
- }
- m = false;
- }
- public boolean act()
- {
- if (won)
- return true;
- if (vx == 0)
- vertical();
- else if (vy == 0)
- horizontal();
- if (!m && vx == 0 && vy == 0)
- {
- switch (map[r][c]){
- case 1 :
- won = true;
- break;
- case 6 :
- r -= vxl;
- c -= vyl;
- if (vxl != 0)
- {
- vyl = (byte) (vxl * -1);
- vxl = 0;
- }
- else if (vyl != 0)
- {
- vxl = (byte) (vyl * -1);
- vyl = 0;
- }
- break;
- case 9 :
- vxl = 1;
- vyl = 0;
- c += 2;
- break;
- case 10 :
- vxl = -1;
- vyl = 0;
- c -= 2;
- break;
- case 11 :
- vxl = 0;
- vyl = -1;
- r -= 2;
- break;
- case 12 :
- vxl = 0;
- vyl = 1;
- r -= 2;
- break;
- case 13 :
- vxl = 0;
- vyl = 0;
- r = c = 2;
- break;
- case 0 :
- case 2 :
- case 3 :
- case 4 :
- case 7 :
- case 8 :
- m = true;
- break;
- case 5 :
- m = true;
- map[r][c] = 3;
- break;
- }
- }
- return false;
- }
- private void horizontal()
- {
- c += vx;
- switch (map[r][c]){
- case 1 :
- won = true;
- break;
- case 6 :
- case 9 :
- case 10 :
- case 11 :
- case 12 :
- case 13 :
- vxl = vx;
- vyl = vy;
- vx = 0;
- m = false;
- break;
- case 4 :
- vx = 0;
- vxl = 0;
- vyl = 0;
- m = true;
- break;
- case 2 :
- case 3 :
- case 7 :
- c -= vx;
- vx = 0;
- vxl = 0;
- vyl = 0;
- m = true;
- break;
- case 5 :
- map[r][c] = 2;
- break;
- }
- }
- private void vertical()
- {
- r += vy;
- switch (map[r][c]){
- case 1 :
- won = true;
- break;
- case 6 :
- case 9 :
- case 10 :
- case 11 :
- case 12 :
- case 13 :
- vxl = vx;
- vyl = vy;
- vy = 0;
- m = false;
- break;
- case 4 :
- vy = 0;
- vxl = 0;
- vyl = 0;
- m = true;
- break;
- case 2 :
- case 3 :
- case 8 :
- r -= vy;
- vy = 0;
- vxl = 0;
- vyl = 0;
- m = true;
- break;
- case 5 :
- map[r][c] = 2;
- break;
- }
- }
- static final byte[][][] STORAGE = {
- //Level 1
- {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,5,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,6,0,8,0,1,0,2},
- {2,0,0,0,6,0,0,4,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,3,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
- //Level 2
- {{2,2,2,2,2,2,2,2,2,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,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
- //Level 13
- {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
- //Level 14
- {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
- //Level 15
- {{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
- {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}},
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement