Skip to content

Commit eff7f66

Browse files
committed
added brick breaker game using java
1 parent 86bd4db commit eff7f66

File tree

11 files changed

+310
-0
lines changed

11 files changed

+310
-0
lines changed

Brick-Breaker-Game/pom.xml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.mycompany</groupId>
5+
<artifactId>Brick</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>15</maven.compiler.source>
11+
<maven.compiler.target>15</maven.compiler.target>
12+
</properties>
13+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.mycompany.brick;
7+
8+
import javax.swing.JPanel;
9+
import javax.swing.Timer;
10+
import java.awt.Graphics2D;
11+
import java.awt.Rectangle;
12+
import java.awt.Graphics;
13+
import java.awt.Color;
14+
import java.awt.Font;
15+
import java.awt.event.ActionEvent;
16+
import java.awt.event.ActionListener;
17+
import java.awt.event.KeyEvent;
18+
import java.awt.event.KeyListener;
19+
20+
/**
21+
*
22+
* @author chinm
23+
*/
24+
public class GamePlay extends JPanel implements KeyListener, ActionListener {
25+
26+
private boolean play = false;
27+
private int score = 0;
28+
private int totalbricks = 21;
29+
private Timer Timer;
30+
private int delay = 8;
31+
private int playerX = 310;
32+
private int ballposX = 120;
33+
private int ballposY = 350;
34+
private int ballXdir = -1;
35+
private int ballYdir = -2;
36+
private MapGenerator map;
37+
38+
public GamePlay() {
39+
map = new MapGenerator(3, 7);
40+
addKeyListener(this);
41+
setFocusable(true);
42+
setFocusTraversalKeysEnabled(false);
43+
Timer = new Timer(delay, this);
44+
Timer.start();
45+
}
46+
47+
public void paint(Graphics g) {
48+
g.setColor(Color.black);
49+
g.fillRect(1, 1, 692, 592);
50+
51+
map.draw((Graphics2D) g);
52+
53+
g.setColor(Color.yellow);
54+
g.fillRect(0, 0, 3, 592);
55+
g.fillRect(0, 0, 692, 3);
56+
g.fillRect(691, 0, 3, 592);
57+
58+
g.setColor(Color.white);
59+
g.setFont(new Font("serif", Font.BOLD, 25));
60+
g.drawString("" + score, 590, 30);
61+
62+
g.setColor(Color.yellow);
63+
g.fillRect(playerX, 550, 100, 8);
64+
65+
//ball
66+
g.setColor(Color.GREEN);
67+
g.fillOval(ballposX, ballposY, 20, 20);
68+
69+
if (ballposY > 570) {
70+
play = false;
71+
ballXdir = 0;
72+
ballYdir = 0;
73+
g.setColor(Color.red);
74+
g.setFont(new Font("serif", Font.BOLD, 30));
75+
g.drawString(" Game Over Score: " + score, 190, 300);
76+
77+
g.setFont(new Font("serif", Font.BOLD, 30));
78+
g.drawString(" Press Enter to Restart", 190, 340);
79+
}
80+
if(totalbricks == 0){
81+
play = false;
82+
ballYdir = -2;
83+
ballXdir = -1;
84+
g.setColor(Color.red);
85+
g.setFont(new Font("serif",Font.BOLD,30));
86+
g.drawString(" Game Over: "+score,190,300);
87+
88+
g.setFont(new Font("serif", Font.BOLD, 30));
89+
g.drawString(" Press Enter to Restart", 190, 340);
90+
91+
92+
}
93+
94+
g.dispose();
95+
96+
97+
}
98+
99+
@Override
100+
public void actionPerformed(ActionEvent e) {
101+
Timer.start();
102+
103+
if (play) {
104+
if (new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX, 550, 100, 8))) {
105+
ballYdir = -ballYdir;
106+
}
107+
108+
A:
109+
for (int i = 0; i < map.map.length; i++) {
110+
for (int j = 0; j < map.map[0].length; j++) {
111+
if (map.map[i][j] > 0) {
112+
int brickX = j * map.bricksWidth + 80;
113+
int brickY = i * map.bricksHeight + 50;
114+
int bricksWidth = map.bricksWidth;
115+
int bricksHeight = map.bricksHeight;
116+
117+
Rectangle rect = new Rectangle(brickX, brickY, bricksWidth, bricksHeight);
118+
Rectangle ballrect = new Rectangle(ballposX, ballposY, 20, 20);
119+
Rectangle brickrect = rect;
120+
121+
if (ballrect.intersects(brickrect)) {
122+
map.setBricksValue(0, i, j);
123+
totalbricks--;
124+
score += 5;
125+
if (ballposX + 19 <= brickrect.x || ballposX + 1 >= brickrect.x + bricksWidth) {
126+
ballXdir = -ballXdir;
127+
} else {
128+
ballYdir = -ballYdir;
129+
}
130+
break A;
131+
}
132+
}
133+
134+
135+
}
136+
}
137+
138+
139+
ballposX += ballXdir;
140+
ballposY += ballYdir;
141+
if (ballposX < 0) {
142+
ballXdir = -ballXdir;
143+
}
144+
if (ballposY < 0) {
145+
ballYdir = -ballYdir;
146+
}
147+
if (ballposX > 670) {
148+
ballXdir = -ballXdir;
149+
}
150+
}
151+
repaint();
152+
}
153+
154+
@Override
155+
public void keyTyped(KeyEvent e) {
156+
157+
}
158+
159+
160+
@Override
161+
public void keyReleased(KeyEvent e) {
162+
163+
}
164+
165+
@Override
166+
public void keyPressed(KeyEvent e) {
167+
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
168+
if (playerX >= 600) {
169+
playerX = 600;
170+
} else {
171+
moveRight();
172+
}
173+
}
174+
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
175+
if (playerX < 10) {
176+
playerX = 10;
177+
} else {
178+
moveLeft();
179+
}
180+
}
181+
182+
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
183+
if (!play) {
184+
ballposX = 120;
185+
ballposY = 350;
186+
ballXdir = -1;
187+
ballYdir = -2;
188+
score = 0;
189+
playerX = 310;
190+
totalbricks = 21;
191+
map = new MapGenerator(3, 7);
192+
193+
repaint();
194+
}
195+
}
196+
197+
198+
}
199+
200+
public void moveRight ()
201+
{
202+
play = true;
203+
playerX += 20;
204+
}
205+
public void moveLeft ()
206+
{
207+
play = true;
208+
playerX -= 20;
209+
}
210+
211+
212+
213+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.mycompany.brick;
7+
8+
import java.awt.BasicStroke;
9+
import java.awt.Color;
10+
import java.awt.Graphics2D;
11+
12+
/**
13+
*
14+
* @author chinm
15+
*/
16+
public class MapGenerator {
17+
public int map[][];
18+
public int bricksWidth;
19+
public int bricksHeight;
20+
public MapGenerator(int row , int col){
21+
map = new int[row][col];
22+
for (int[] map1 : map) {
23+
for (int j = 0; j < map[0].length; j++) {
24+
map1[j] = 1;
25+
}
26+
}
27+
bricksWidth = 540/col;
28+
bricksHeight = 150/row;
29+
}
30+
public void draw(Graphics2D g) {
31+
for (int i = 0; i < map.length; i++) {
32+
for (int j = 0; j < map[0].length; j++) {
33+
if (map[i][j] > 0) {
34+
g.setColor(Color.red);
35+
g.fillRect(j * bricksWidth + 80, i * bricksHeight + 50, bricksWidth, bricksHeight);
36+
37+
g.setStroke(new BasicStroke(3));
38+
g.setColor(Color.black);
39+
g.drawRect(j * bricksWidth + 80, i * bricksHeight + 50, bricksWidth, bricksHeight);
40+
41+
}
42+
}
43+
44+
}
45+
}
46+
public void setBricksValue(int value,int row,int col)
47+
{
48+
map[row][col] = value;
49+
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.mycompany.brick;
7+
8+
import javax.swing.JFrame;
9+
10+
/**
11+
*
12+
* @author chinm
13+
*/
14+
public class MyApp {
15+
public static void main(String[] args) {
16+
JFrame obj = new JFrame();
17+
GamePlay gameplay = new GamePlay();
18+
obj.setBounds(10,10,700,600);
19+
obj.setTitle("BrickBreaker");
20+
obj.setResizable(false);
21+
obj.setVisible(true);
22+
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
23+
obj.add(gameplay);
24+
}
25+
26+
}

Brick-Breaker-Game/target/classes/.netbeans_automatic_build

Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
com\mycompany\brick\MapGenerator.class
2+
com\mycompany\brick\MyApp.class
3+
com\mycompany\brick\GamePlay.class
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
C:\Users\chinm\OneDrive\Documents\NetBeansProjects\Brick\src\main\java\com\mycompany\brick\MyApp.java
2+
C:\Users\chinm\OneDrive\Documents\NetBeansProjects\Brick\src\main\java\com\mycompany\brick\GamePlay.java
3+
C:\Users\chinm\OneDrive\Documents\NetBeansProjects\Brick\src\main\java\com\mycompany\brick\MapGenerator.java

Brick-Breaker-Game/target/test-classes/.netbeans_automatic_build

Whitespace-only changes.

0 commit comments

Comments
 (0)