forked from yashthipsay/ball_breakout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGamePlay.java
189 lines (149 loc) · 4.91 KB
/
GamePlay.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
package Brick_Breaker.src.demogame;
import java.util.*;
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 java.security.Key;
import java.util.Timer;
import javax.swing.Timer.*;
import javax.swing.JPanel;
public class GamePlay extends JPanel implements ActionListener, KeyListener{
private boolean play = false;
private int score=0;
private int totalBrick = 21;
private Timer timer;
private int delay=8;
private int ballposX=120;
private int ballposY=350;
private int ballXdir=-1;
private int ballYdir = 2;
private int playerX=350;
private MapGenerator map;
public GamePlay(){
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(true);
javax.swing.Timer timer = new javax.swing.Timer(delay, this);
timer.start();
map = new MapGenerator(3, 7);
}
public void paint(Graphics g){
g.setColor(Color.black);
g.fillRect(1, 1, 692, 592);
g.setColor(Color.yellow);
g.fillRect(0, 0, 692, 3);
g.fillRect(0, 3, 3, 592);
g.fillRect(691, 3, 3, 592);
g.setColor(Color.green);
g.fillRect(playerX, 550, 100, 8);
map.draw((Graphics2D )g);
g.setColor(Color.red);
g.fillOval(ballposX, ballposY, 20, 20);
g.setColor(Color.green);
g.setFont(new Font("serif", Font.BOLD, 20));
g.drawString("Score: "+score, 550, 30);
//gameover
if(ballposY>=570){
play=false;
ballXdir=0;
ballYdir=0;
g.setColor(Color.green);
g.setFont(new Font("serif", Font.BOLD, 40));
g.drawString("GameOver!! Score: " + score, 200, 300);
g.setFont(new Font("serif", Font.BOLD, 30));
g.drawString("Press Enter to restart " + score, 230, 350);
}
}
private void moveLeft(){
play=true;
playerX-=20;
}
private void moveRight(){
play=true;
playerX+=20;
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_LEFT){
if(playerX<=0)
playerX=0;
moveLeft();
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
if(playerX>=600)
playerX=600;
moveRight();
}
if(e.getKeyCode()==KeyEvent.VK_ENTER){
if(!play){
score=0;
totalBrick=21;
ballposX=120;
ballposY = 350;
ballXdir=-1;
ballYdir=-2;
playerX=320;
map = new MapGenerator(3, 7);
}
}
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
if(play){
if(ballposX<=0 || ballposX>=670){
ballXdir= -ballXdir;
}
if(ballposY<=0){
ballYdir= -ballYdir;
}
Rectangle ballRect = new Rectangle(ballposX, ballposY, 20, 20);
Rectangle paddleRect = new Rectangle(playerX, 550, 100, 8);
if(ballRect.intersects(paddleRect)){
ballYdir=-ballYdir;
}
A: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 width=map.brickWidth;
int height=map.brickHeight;
int brickposX=80+j*width;
int brickposY = 50+i*height;
Rectangle brickRect=new Rectangle(brickposX, brickposY, width, height);
if(ballRect.intersects(brickRect)){
map.setBrick(0, i, j);
totalBrick--;
score+= 5;
if(ballposX+19<=brickposX || ballposX+1>=brickposX+width){
ballXdir=-ballXdir;
}
else{
ballYdir=-ballYdir;
}
break A;
}
}
}
}
ballposX+=ballXdir;
ballposY-=ballYdir;
}
repaint();
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'keyTyped'");
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'keyReleased'");
}
}