JAVA-----------Brick-breaker(Modified)
1.Class - MapGenerator --
package Brick_Breaker;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
public class MapGenerator {
public int[][] map;
public int brickwid;
public int brickhei;
public MapGenerator(int row , int col) {
map = new int[row][col];
for(int i = 0 ; i < row ; i++) {
for(int j = 0 ; j < col ; j++) {
map[i][j] = 1;
}
}
brickwid = 540/col;
brickhei = 150/row;
}
public void draw(Graphics2D g) {
for(int i = 0 ; i < map.length ; i++) {
for(int j = 0 ; j < map[0].length ; j++) {
if(map[i][j]> 0) {
g.setColor(Color.white);
g.fillRect(j*brickwid + 80, i*brickhei + 50, brickwid, brickhei);
g.setStroke(new BasicStroke(3));
g.setColor(Color.black);
g.drawRect(j*brickwid + 80, i*brickhei + 50, brickwid, brickhei);
}
}
}
}
public void setBrickvalue(int value , int row , int col) {
map[row][col] = value;
}
}
2.Class - GamePlay --
package Brick_Breaker;
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.JPanel;
import javax.swing.Timer;
public class GamePlay extends JPanel implements KeyListener, ActionListener{
private boolean play = false;
private int score = 0;
private int totalbricks = 49;
private Timer timer;
private int delay = 8;
private int playerx = 310;
private int ballposx1 = (int)(Math.random()*686) + 5;
private int ballposy1 = 350;
//extra--------------start
private int ballposx2 = (int)(Math.random()*686) + 5;
private int ballposy2 = 320;
private int ballposx3 = (int)(Math.random()*686) + 5;
private int ballposy3 = 300;
private int ballxdir1 = -1;
private int ballydir1 = -1;
private int ballxdir2 = -1;
private int ballydir2 = -2;
private int ballxdir3 = -1;
private int ballydir3 = -3;
//extra-----------end
private MapGenerator map;
public GamePlay() {
map = new MapGenerator(7 ,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.red);
g.fillRect(0, 0, 3, 592);
g.fillRect(0, 0, 692, 3);
g.fillRect(692, 0, 3, 592);
//scores
g.setColor(Color.white);
g.setFont(new Font("serif" , Font.BOLD , 25));
g.drawString(""+score , 592 , 30);
//paddle
g.setColor(Color.green);
g.fillRect(playerx, 550, 100, 8);
//ball
//ball --> 1
g.setColor(Color.red);
g.fillOval(ballposx1, ballposy1, 20, 20);
//ball --> 2
g.setColor(Color.yellow);
g.fillOval(ballposx2, ballposy2, 20, 20);
//ball --> 3
g.setColor(Color.blue);
g.fillOval(ballposx3, ballposy3, 20, 20);
if(totalbricks <= 0) {
play = false;
ballxdir1 = 0;
ballydir1 = 0;
ballxdir2 = 0;
ballydir2 = 0;
ballxdir3 = 0;
ballydir3 = 0;
g.setColor(Color.red);
g.setFont(new Font("serif" , Font.BOLD , 30));
g.drawString("You Won, Score: " + score , 260 , 300);
g.setFont(new Font("serif" , Font.BOLD , 20));
g.drawString("Press Enter to restart" , 230 , 350);
}
if(ballposy1 > 570 || ballposy2 > 570 || ballposy3 > 570) {
play = false;
ballxdir1 = 0;
ballydir1 = 0;
ballxdir2 = 0;
ballydir2 = 0;
ballxdir3 = 0;
ballydir3 = 0;
g.setColor(Color.red);
g.setFont(new Font("serif" , Font.BOLD , 30));
g.drawString("Game Over, scores: " + score , 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) {
//ball --> 1
if(ballxdir1 > 0) {
if(new Rectangle(ballposx1 , ballposy1 , 20 , 20).intersects(new Rectangle(playerx , 550 , 25 , 8))) {
ballydir1 = -ballydir1;
ballxdir1 = -ballxdir1;
}
else if(new Rectangle(ballposx1 , ballposy1 , 20 , 20).intersects(new Rectangle(playerx + 25 , 550 , 100 , 8)))
ballydir1 = -ballydir1;
}
else {
if(new Rectangle(ballposx1 , ballposy1 , 20 , 20).intersects(new Rectangle(playerx , 550 , 75 , 8))) {
ballydir1 = -ballydir1;
}
else if(new Rectangle(ballposx1 , ballposy1 , 20 , 20).intersects(new Rectangle(playerx + 75 , 550 , 100 , 8))) {
ballydir1 = -ballydir1;
ballxdir1 = -ballxdir1;
}
}
//extra---------------------start
//ball --> 2
if(ballxdir2 > 0) {
if(new Rectangle(ballposx2 , ballposy2 , 20 , 20).intersects(new Rectangle(playerx , 550 , 25 , 8))) {
ballydir2 = -ballydir2;
ballxdir2 = -ballxdir2;
}
else if(new Rectangle(ballposx2 , ballposy2 , 20 , 20).intersects(new Rectangle(playerx + 25 , 550 , 100 , 8)))
ballydir2 = -ballydir2;
}
else {
if(new Rectangle(ballposx2 , ballposy2 , 20 , 20).intersects(new Rectangle(playerx , 550 , 75 , 8))) {
ballydir2 = -ballydir2;
}
else if(new Rectangle(ballposx2 , ballposy2 , 20 , 20).intersects(new Rectangle(playerx + 75 , 550 , 100 , 8))) {
ballydir2 = -ballydir2;
ballxdir2 = -ballxdir2;
}
}
//ball ---> 3
if(ballxdir3 > 0) {
if(new Rectangle(ballposx3 , ballposy3 , 20 , 20).intersects(new Rectangle(playerx , 550 , 25 , 8))) {
ballydir3 = -ballydir3;
ballxdir3 = -ballxdir3;
}
else if(new Rectangle(ballposx3 , ballposy3 , 20 , 20).intersects(new Rectangle(playerx + 25 , 550 , 100 , 8)))
ballydir3 = -ballydir3;
}
else {
if(new Rectangle(ballposx3 , ballposy3 , 20 , 20).intersects(new Rectangle(playerx , 550 , 75 , 8))) {
ballydir3 = -ballydir3;
}
else if(new Rectangle(ballposx3 , ballposy3 , 20 , 20).intersects(new Rectangle(playerx + 75 , 550 , 100 , 8))) {
ballydir3 = -ballydir3;
ballxdir3 = -ballxdir3;
}
}
//extra------------------------end
x:
for(int i = 0 ; i < map.map.length ; i++) {
for(int j = 0 ; j < map.map[0].length ; j++) {
if(map.map[i][j] > 0) {
int brickx = j*map.brickwid + 80;
int bricky = i*map.brickhei + 50;
int brickwid = map.brickwid;
int brickhei = map.brickhei;
Rectangle rect = new Rectangle(brickx, bricky , brickwid , brickhei);
Rectangle ballrect1 = new Rectangle(ballposx1 , ballposy1 , 20 , 20);
Rectangle ballrect2 = new Rectangle(ballposx2 , ballposy2 , 20 , 20);
Rectangle ballrect3 = new Rectangle(ballposx3 , ballposy3 , 20 , 20);
Rectangle brickrect = rect;
if(ballrect1.intersects(brickrect)) {
map.setBrickvalue(0 , i , j);
totalbricks--;
score += 5;
//ball --> 1
if(ballposx1 + 19 <= brickrect.x || ballposx1 + 1 >= brickrect.x + brickrect.width) {
ballxdir1 = -ballxdir1;
}
else
ballydir1 = -ballydir1;
break x;
}
if(ballrect2.intersects(brickrect)) {
map.setBrickvalue(0 , i , j);
totalbricks--;
score += 5;
//ball --> 2
if(ballposx2 + 19 <= brickrect.x || ballposx2 + 1 >= brickrect.x + brickrect.width) {
ballxdir2 = -ballxdir2;
}
else
ballydir2 = -ballydir2;
break x;
}
if(ballrect3.intersects(brickrect)) {
map.setBrickvalue(0 , i , j);
totalbricks--;
score += 5;
//ball --> 3
if(ballposx3 + 19 <= brickrect.x || ballposx3 + 1 >= brickrect.x + brickrect.width) {
ballxdir3 = -ballxdir3;
}
else
ballydir3 = -ballydir3;
break x;
}
}
}
}
//ball --> 1
ballposx1 += ballxdir1;
ballposy1 += ballydir1;
if(ballposx1 < 0)
ballxdir1 = -ballxdir1;
if(ballposy1 < 0)
ballydir1 = -ballydir1;
if(ballposx1 > 670)
ballxdir1 = -ballxdir1;
//ball --> 2
ballposx2 += ballxdir2;
ballposy2 += ballydir2;
if(ballposx2 < 0)
ballxdir2 = -ballxdir2;
if(ballposy2 < 0)
ballydir2 = -ballydir2;
if(ballposx2 > 670)
ballxdir2 = -ballxdir2;
//ball --> 3
ballposx3 += ballxdir3;
ballposy3 += ballydir3;
if(ballposx3 < 0)
ballxdir3 = -ballxdir3;
if(ballposy3 < 0)
ballydir3 = -ballydir3;
if(ballposx3 > 670)
ballxdir3 = -ballxdir3;
}
repaint();
}
@Override
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
if(!play) {
play = true;
playerx = 310;
//ball --> 1
ballposx1 = 120;
ballposy1 = 350;
ballxdir1 = -1;
ballydir1 = -1;
//ball --> 2
ballposx2 = 120;
ballposy2 = 320;
ballxdir2 = -1;
ballydir2 = -2;
//ball --> 3
ballposx3 = 120;
ballposy3 = 300;
ballxdir3 = -1;
ballydir3 = -3;
score = 0;
totalbricks = 49;
map = new MapGenerator(7,7);
repaint();
}
}
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();
}
}
public void moveleft() {
play = true;
playerx -= 20;
}
public void moveright() {
play = true;
playerx += 20;
}
}
Comments
Post a Comment