-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
208 lines (188 loc) · 4.93 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package BrickBreaker;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class Main {
static JPanel p1;
public static void main(String[] args) {
JFrame frame=new JFrame();
Panels pe=new Panels();
//p1=new JPanel();
//p1.setBounds(10, 10, 50, 50);
//p1.setBackground(Color.black);
//frame.add(p1);
frame.add(pe);
frame.setBounds(10,10,700,600);
frame.setTitle("Brick Breaker");
// frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
//Panal p=new Panal();
frame.setResizable(false);
//frame.add(p);
}
}
class Panels extends JPanel implements ActionListener,KeyListener {
private boolean play=false;
private int score=0;
private int totalbrick=27;
private Timer timer;
private int ballpointx=120;
private int ballpointy=350;
private int ballxdir=-1;
private int ballydir=-2;
private int playerx=350;
private int delay=-100;
private bricks b1;
private boolean gameover=false;
public Panels(){
b1=new bricks(3,9);
addKeyListener(this);
setFocusable(true);
this.setFocusTraversalKeysEnabled(false);
timer=new Timer(delay,this);
timer.start();
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(1, 1, 692, 592);
//draw bricks
b1.draw((Graphics2D)g);
//boarder
g.setColor(Color.yellow);
g.fillRect(0, 0, 3, 592);
g.fillRect(0, 0, 692, 3);
g.fillRect(682, 0, 3, 592);
//SCORE
g.setColor(Color.white);
g.setFont(new Font("Arial",Font.BOLD,25));
g.drawString(""+score, 590, 30);
//creat paddle
g.setColor(Color.GREEN);
g.fillRect(playerx, 550, 100, 10);
//ball
g.setColor(Color.yellow);
g.fillOval(ballpointx, ballpointy, 20, 20);
//
if(ballpointy>=555) {
play=false;
ballxdir=0;
ballydir=0;
g.setColor(Color.RED);
g.setFont(new Font("Arial",Font.BOLD,25));
g.drawString("GameOver !! Score is :"+score, 250, 250);
g.setColor(Color.WHITE);
g.drawString("Press Enter To Restart", 250, 280);
}
if(totalbrick<=0) {
play=false;
ballxdir=0;
ballydir=0;
g.setColor(Color.green);
g.setFont(new Font("Arial",Font.BOLD,25));
g.drawString("Congratulations you Completed The Game", 230, 250);
g.drawString("Press Enter To Restart", 230, 280);
}
g.dispose();
}
@Override
public void actionPerformed(ActionEvent e) {
//if ball intersect with paddle
timer.start();
if(play){
if(new Rectangle(ballpointx,ballpointy,20,20).intersects(new Rectangle(playerx,550,100,8))) {
ballydir=-ballydir;
}
//if ball touch the bricks
A: for(int i=0;i<b1.map.length;i++) {
for(int j=0;j<b1.map[0].length;j++) {
if(b1.map[i][j]>0) {
int brickx=j*b1.brickwidth+80;
int bricky=i*b1.brickhight+50;
int brickwidth=b1.brickwidth;
int brickhight=b1.brickhight;
Rectangle rect=new Rectangle(brickx,bricky,brickwidth,brickhight);
Rectangle ballrect=new Rectangle(ballpointx,ballpointy,20,20);
Rectangle brickrect=rect;
if(ballrect.intersects(brickrect)) {
b1.setbrickvalue(0, i, j);
totalbrick--;
score+=5;
if(ballpointx+19<=brickrect.x||ballpointx+1>=brickrect.width) {
ballxdir=-ballxdir;
}
else{
ballydir=-ballydir;
}
break A;
}
}
}
}
}
if(play) {
ballpointx+=ballxdir;
ballpointy+=ballydir;
if(ballpointx<0) {
ballxdir=-ballxdir;
}
if(ballpointy<0) {
ballydir=-ballydir;
}
if(ballpointx>670) {
ballxdir=-ballxdir;
}
}
repaint();
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
if(playerx>=596) {
playerx=596;
}
else {
moveright();
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT) {
if(playerx<=3) {
playerx=3;
}
else {
moveleft();
}
}
if(e.getKeyCode()==KeyEvent.VK_ENTER) {
if(!play){
score=0;
totalbrick=27;
ballpointx=120;
ballpointy=350;
ballxdir=-1;
ballydir=-2;
playerx=320;
b1=new bricks(3,9);
}
}
}
public void moveright() {
play=true;
playerx+=20;
}
public void moveleft() {
play=true;
playerx-=20;
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}