diff --git a/F group Project evaluation 1/Shivam Bansal 1710991744/.classpath b/F group Project evaluation 1/Shivam Bansal 1710991744/.classpath new file mode 100644 index 0000000..51a8bba --- /dev/null +++ b/F group Project evaluation 1/Shivam Bansal 1710991744/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/F group Project evaluation 1/Shivam Bansal 1710991744/.project b/F group Project evaluation 1/Shivam Bansal 1710991744/.project new file mode 100644 index 0000000..e4220b0 --- /dev/null +++ b/F group Project evaluation 1/Shivam Bansal 1710991744/.project @@ -0,0 +1,17 @@ + + + Project + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/F group Project evaluation 1/Shivam Bansal 1710991744/.settings/org.eclipse.jdt.core.prefs b/F group Project evaluation 1/Shivam Bansal 1710991744/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..3a21537 --- /dev/null +++ b/F group Project evaluation 1/Shivam Bansal 1710991744/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/F group Project evaluation 1/Shivam Bansal 1710991744/bin/Gameplay.class b/F group Project evaluation 1/Shivam Bansal 1710991744/bin/Gameplay.class new file mode 100644 index 0000000..6cc7b48 Binary files /dev/null and b/F group Project evaluation 1/Shivam Bansal 1710991744/bin/Gameplay.class differ diff --git a/F group Project evaluation 1/Shivam Bansal 1710991744/bin/Main.class b/F group Project evaluation 1/Shivam Bansal 1710991744/bin/Main.class new file mode 100644 index 0000000..991a727 Binary files /dev/null and b/F group Project evaluation 1/Shivam Bansal 1710991744/bin/Main.class differ diff --git a/F group Project evaluation 1/Shivam Bansal 1710991744/bin/MapGenerator.class b/F group Project evaluation 1/Shivam Bansal 1710991744/bin/MapGenerator.class new file mode 100644 index 0000000..34ae1f8 Binary files /dev/null and b/F group Project evaluation 1/Shivam Bansal 1710991744/bin/MapGenerator.class differ diff --git a/F group Project evaluation 1/Shivam Bansal 1710991744/src/Gameplay.java b/F group Project evaluation 1/Shivam Bansal 1710991744/src/Gameplay.java new file mode 100644 index 0000000..c1eb38a --- /dev/null +++ b/F group Project evaluation 1/Shivam Bansal 1710991744/src/Gameplay.java @@ -0,0 +1,198 @@ + + +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import javax.swing.Timer; + +import javax.swing.JPanel; + +public class Gameplay extends JPanel implements KeyListener, ActionListener { + private boolean play = false; + private int score = 0; + + private int totalBricks = 21; + + private Timer timer; + private int delay = 8; + + private int playerX = 310; + + private int ballposX = 120; + private int ballposY = 350; + private int ballXdir = -1; + private int ballYdir = -2; + + private MapGenerator map; + + public Gameplay() { + map = new MapGenerator(3, 7); + addKeyListener(this); + setFocusable(true); + setFocusTraversalKeysEnabled(false); + timer = new Timer(delay, this); + timer.start(); + } + + public void paint(Graphics g) { + // background + g.setColor(Color.black); + g.fillRect(1, 1, 692, 592); + + // drawing map + map.draw((Graphics2D)g); + + // borders + g.setColor(Color.yellow);; + g.fillRect(0, 0, 3, 592); + g.fillRect(0, 0, 692, 3); + g.fillRect(691, 0, 3, 592); + //scores + g.setColor(Color.white); + g.setFont(new Font("serif", Font.BOLD, 25)); + g.drawString(""+score, 590, 30); + + // the paddle + g.setColor(Color.green); + g.fillRect(playerX, 550, 100, 8); + + // the ball + g.setColor(Color.yellow); + g.fillOval(ballposX, ballposY, 20, 20); + + if(totalBricks <= 0) { + play = false; + ballXdir = 0; + ballYdir = 0; + g.setColor(Color.red); + g.setFont(new Font("serif", Font.BOLD, 30)); + g.drawString("You Won: ", 260, 300); + + g.setFont(new Font("serif", Font.BOLD, 20)); + g.drawString("Press Enter to Restart", 230, 350); + } + + if(ballposY > 570) { + play = false; + ballXdir = 0; + ballYdir = 0; + g.setColor(Color.red); + g.setFont(new Font("serif", Font.BOLD, 30)); + g.drawString("Game Over, Scores: ", 190, 300); + + g.setFont(new Font("serif", Font.BOLD, 20)); + g.drawString("Press Enter to Restart", 230, 350); + } + + g.dispose(); + } + + @Override + public void actionPerformed(ActionEvent e) { + timer.start(); + if(play) { + if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX, 550, 100, 8))) { + ballYdir = -ballYdir; + } + + A: for(int i=0; i 0) { + int brickX = j * map.brickWidth + 80; + int brickY = i * map.brickHeight + 50; + int brickWidth = map.brickWidth; + int brickHeight = map.brickHeight; + + Rectangle rect = new Rectangle(brickX, brickY, brickWidth, brickHeight); + Rectangle ballRect = new Rectangle(ballposX, ballposY, 20, 20); + Rectangle brickRect = rect; + + if(ballRect.intersects(brickRect)) { + map.setBrickValue(0, i, j); + totalBricks--; + score += 5; + + if(ballposX + 19 <= brickRect.x || ballposX + 1 >= brickRect.x + brickRect.width) { + ballXdir = -ballXdir; + } else { + ballYdir = -ballYdir; + } + + break A; + } + } + } + } + + ballposX += ballXdir; + ballposY += ballYdir; + if(ballposX < 0) { + ballXdir = -ballXdir; + } + if(ballposY < 0) { + ballYdir = -ballYdir; + } + if(ballposX > 670 ) { + ballXdir = -ballXdir; + } + } + repaint(); + + } + + @Override + public void keyPressed(KeyEvent e) { + if(e.getKeyCode() == KeyEvent.VK_RIGHT) { + if(playerX >= 600) { + playerX = 600; + } else { + moveRight(); + } + } + if(e.getKeyCode() == KeyEvent.VK_LEFT) { + if(playerX < 10) { + playerX = 10; + } else { + moveLeft(); + } + } + if(e.getKeyCode() == KeyEvent.VK_ENTER) { + if(!play) { + play = true; + ballposX = 120; + ballposY = 350; + ballXdir = -1; + ballYdir = -2; + playerX = 310; + score = 0; + totalBricks = 21; + map = new MapGenerator(3,7); + + repaint(); + } + } + + } + public void moveRight() { + play = true; + playerX += 20; + } + public void moveLeft() { + play = true; + playerX -= 20; + } + + + @Override + public void keyReleased(KeyEvent e) {} + + @Override + public void keyTyped(KeyEvent e) {} + +} diff --git a/F group Project evaluation 1/Shivam Bansal 1710991744/src/Main.java b/F group Project evaluation 1/Shivam Bansal 1710991744/src/Main.java new file mode 100644 index 0000000..af00712 --- /dev/null +++ b/F group Project evaluation 1/Shivam Bansal 1710991744/src/Main.java @@ -0,0 +1,17 @@ + +import javax.swing.JFrame; + +public class Main { + + public static void main(String[] args) { + JFrame obj = new JFrame(); + Gameplay gameplay = new Gameplay(); + obj.setBounds(10, 10, 700, 600); + obj.setTitle("Breakout Ball"); + obj.setResizable(false); + obj.setVisible(true); + obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + obj.add(gameplay); + } + +} diff --git a/F group Project evaluation 1/Shivam Bansal 1710991744/src/MapGenerator.java b/F group Project evaluation 1/Shivam Bansal 1710991744/src/MapGenerator.java new file mode 100644 index 0000000..36edbce --- /dev/null +++ b/F group Project evaluation 1/Shivam Bansal 1710991744/src/MapGenerator.java @@ -0,0 +1,40 @@ + + +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Graphics2D; + +public class MapGenerator { + public int map[][]; + public int brickWidth; + public int brickHeight; + public MapGenerator(int row, int col) { + map = new int[row][col]; + for(int i=0; i 0) { + g.setColor(Color.white); + g.fillRect(j*brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight); + + g.setStroke(new BasicStroke(3)); + g.setColor(Color.black); + g.drawRect(j*brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight); + } + } + } + } + public void setBrickValue(int value, int row, int col) { + map[row][col] = value; + } + +}