diff --git a/out/MazeGame.jar b/out/MazeGame.jar new file mode 100644 index 0000000000000000000000000000000000000000..8ff2c27a0bc21f529c6b44057b553f0ecae57cbd Binary files /dev/null and b/out/MazeGame.jar differ diff --git a/src/GameFrame.java b/src/GameFrame.java new file mode 100644 index 0000000000000000000000000000000000000000..68b715daf4c5029f53dc6cfb7a64f62ff24511a8 --- /dev/null +++ b/src/GameFrame.java @@ -0,0 +1,71 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class GameFrame extends JFrame implements ActionListener { + + private static final long serialVersionUID = 1L; + GamePanel game; + JPanel score; + JPanel start; + JButton resetButton; + JButton startGame; + + public GameFrame(int width, int height, String title) { + + this.setTitle(title); + this.setSize(width, height); + this.setResizable(false); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setLocationRelativeTo(null); + + + resetButton = new JButton(); + resetButton.setText("Reset"); + resetButton.setSize(100,30); + resetButton.addActionListener(this); + resetButton.setFocusable(false); + + startGame = new JButton(); + startGame.setText("Start"); + startGame.setSize(200,200); + startGame.setLocation(250,250); + startGame.addActionListener(this); + startGame.setFocusable(false); + + score = new JPanel(); + score.setBackground(Color.black); + score.add(resetButton); + score.setFocusable(true); + + start = new JPanel(); + start.setBackground(Color.black); + start.setSize(getWidth(),getHeight()); + start.setLocation(0,0); + start.add(startGame); + + this.add(start); + this.setVisible(true); + } + + @Override + public void actionPerformed(ActionEvent e) { + if(e.getSource() == resetButton){ + this.remove(game); + game = new GamePanel(); + this.add(game); + SwingUtilities.updateComponentTreeUI(this); + game.requestFocus(); + } + if(e.getSource() == startGame){ + this.remove(start); + game = new GamePanel(); + this.add(score,BorderLayout.NORTH); + this.add(game); + SwingUtilities.updateComponentTreeUI(this); + game.requestFocus(); + } + } + +} diff --git a/src/GameObject.java b/src/GameObject.java new file mode 100644 index 0000000000000000000000000000000000000000..fe33303516c33875a89a0c9b7661562bb073e5c9 --- /dev/null +++ b/src/GameObject.java @@ -0,0 +1,19 @@ +import java.awt.*; +import java.awt.event.*; + +public class GameObject extends Rectangle{ + + Color color; + + GameObject(int x, int y, int width, int height, Color color){ + this.x=x; + this.y=y; + this.width=width; + this.height=height; + this.color=color; + } + public void draw(Graphics g) { + g.setColor(this.color); + g.fillRect(this.x, this.y, this.width, this.height); + } +} diff --git a/src/GamePanel.java b/src/GamePanel.java new file mode 100644 index 0000000000000000000000000000000000000000..364208a64c1ec59f132f4607baa8e35d295872e0 --- /dev/null +++ b/src/GamePanel.java @@ -0,0 +1,138 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.util.concurrent.TimeUnit; + +public class GamePanel extends JPanel implements ActionListener, KeyListener { + + private static final long serialVersionUID = 1L; + + Timer timer = new Timer(5, this); + int x = 0, y = 0, velX = 0, velY = 0, veldX = 0, veldY = 0; + int playerSize = 22; + int movmentSpeed = 4; + int faildRounds; + + ImageIcon ggwp; + + private Walls finishWall; + private Walls walls; + private boolean play = true; + + public GamePanel() { + timer.start(); + addKeyListener(this); + setFocusable(true); + setFocusTraversalKeysEnabled(false); + walls = new Walls(); + ggwp = new ImageIcon("res\\ggwp.png"); + this.setSize(817,840); + this.setLocation(100,0); + this.requestFocus(true); + } + + public void paintComponent(Graphics g) { + super.paintComponent(g); + + g.setColor(Color.black); + g.fillRect(0,0,getWidth(),getWidth()); + + //Walls + walls.draw(this,g); + + //Player + if(play) { + g.setColor(Color.red); + g.fillOval(x, y, playerSize, playerSize); + } + if(walls.wallsCollision(x,y,playerSize,playerSize)){ + x = 0; + y=0; + } + if(walls.finishWallsCollision(x,y,playerSize,playerSize)){ + play=false; + } + if(!play) { + gameOver(g); + } + + } + + public void gameOver(Graphics g) { + if(!play){ + g.setColor(Color.black); + g.fillRect(0,0,getWidth(),getHeight()); + ggwp.paintIcon(this,g,280,250); + faildRounds++; + } + } + public int getFailedRounds(){ + return faildRounds; + } + + @Override + public void keyPressed(KeyEvent e) { + int c = e.getKeyCode(); + if(play) { + if (c == KeyEvent.VK_LEFT || c == KeyEvent.VK_A) + veldX = -movmentSpeed; + if (c == KeyEvent.VK_RIGHT || c == KeyEvent.VK_D) + velX = movmentSpeed; + if (c == KeyEvent.VK_UP || c == KeyEvent.VK_W) + veldY = -movmentSpeed; + if (c == KeyEvent.VK_DOWN || c == KeyEvent.VK_S) + velY = movmentSpeed; + } + + } + + @Override + public void keyReleased(KeyEvent e) { + int c = e.getKeyCode(); + if(play) { + if (c == KeyEvent.VK_LEFT || c == KeyEvent.VK_A) + veldX = 0; + if (c == KeyEvent.VK_RIGHT || c == KeyEvent.VK_D) + velX = 0; + if (c == KeyEvent.VK_UP || c == KeyEvent.VK_W) + veldY = 0; + if (c == KeyEvent.VK_DOWN || c == KeyEvent.VK_S) + velY = 0; + } + + } + + @Override + public void actionPerformed(ActionEvent e) { + + if (x < 0) { + velX = 0; + x = 0; + } + if (x > getWidth() - playerSize) { + velX = 0; + x = getWidth() - playerSize; + } + if (y < 0) { + velY = 0; + y = 0; + } + if (y > getHeight() - playerSize) { + velY = 0; + y = getHeight() - playerSize; + } + + x += velX + veldX; + y += velY + veldY; + repaint(); + + } + + @Override + public void keyTyped(KeyEvent e) { + } + +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..0ff7a49131db36d4f7ea94f9c6183073136df59e --- /dev/null +++ b/src/Main.java @@ -0,0 +1,9 @@ +public class Main { + + public static void main(String[] args) { + + new GameFrame(820, 875, "Maze Game"); + + } + +} diff --git a/src/Walls.java b/src/Walls.java new file mode 100644 index 0000000000000000000000000000000000000000..6596f44942067a574cf3786212643ca951248a8c --- /dev/null +++ b/src/Walls.java @@ -0,0 +1,119 @@ +import java.awt.*; + +public class Walls { + public int unit_size = 50; + + int[] wallXPos = { + //upper wall + 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 + //left wall + ,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + //down wall + 1,2,3,4,5,6,7,8,9,10,11,12,13, + //right wall + 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, + //rest + 1,1,1,1, + 2,2,2,2, + 3,3,3,3,3, + 4,4,4,4,4,4, + 5,5,5,5,5,5,5,5,5, + 6,6,6,6, + 7,7,7,7,7,7,7, + 8,8,8,8,8,8, + 9,9,9,9,9,9,9,9, + 10,10,10,10,10, + 11,11,11,11,11,11,11, + 12,12,12,12, + 13,13,13, + 14,14,14 + + }; + int[] wallYPos = { + //upper wall + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + //left wall + 2,3,4,5,6,7,8,9,10,11,12,13,14,15, + //down wall + 15,15,15,15,15,15,15,15,15,15,15,15,15, + //right wall + 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, + //rest + //1 + 3,8,9,12, + //2 + 1,3,6,12, + //3 + 3,6,7,10,14, + //4 + 2,3,7,8,10,12, + //5 + 2,3,4,8,9,10,11,12,13, + //6 + 4,8,9,13, + //7 + 1,2,4,6,7,8,11, + //8 + 4,8,11,12,13,14, + //9 + 1,2,4,5,6,8,10,11, + //10 + 1,4,8,11,13, + //11 + 4,6,7,8,9,11,13, + //12 + 8,11,13,4, + //13 + 8,11,13, + //14 + 3,8,13 + }; + + + public Walls() { + + } + + + public void draw(Component c, Graphics g) { + + for(int i=0; i< wallXPos.length;i++) + { + g.setColor(Color.GRAY); + g.fillRect(wallXPos[i]*unit_size,wallYPos[i]*unit_size,unit_size,unit_size); + } + g.setColor(Color.yellow); + g.fillRect(14*unit_size,15*unit_size,2*unit_size,unit_size); + } + + public boolean wallsCollision(int x, int y,int width,int height) + { + boolean collided = false; + for(int i=0; i< wallXPos.length;i++) + { + if(new Rectangle(x, y, width, height).intersects(new Rectangle(wallXPos[i]*unit_size, wallYPos[i]*unit_size, 50, 50))) + { + collided = true; + break; + } + } + + return collided; + } + + public boolean finishWallsCollision(int x, int y,int width,int height) + { + boolean finishCollided = false; + if(new Rectangle(x, y, width, height).intersects(new Rectangle(14*unit_size, 15*unit_size, 50, 50))) + { + finishCollided = true; + } + + return finishCollided; + } + + + + + +}