-
Notifications
You must be signed in to change notification settings - Fork 0
/
GamePlay.java
157 lines (126 loc) · 3.87 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
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
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 int score = 0;
private int brickY = 7;
private int brickX = 5;
private MapGenerator map;
private Timer timer;
private int playerX = 310;
private int ballPosX = 120;
private int ballPosY = 350;
private int ballXdir = -1;
private int ballYdir = -2;
private boolean play = false;
private boolean pause = false;
private int delay = 8;
private int totalBricks;
public GamePlay () {
map = new MapGenerator(brickX,brickY);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(delay, this);
timer.start();
}
public void paint(Graphics graphic) {
//Background
graphic.setColor(Color.black);
graphic.fillRect(1,1,692,592);
//Drawing map
totalBricks = map.draw((Graphics2D) graphic);
System.out.println(totalBricks);
//Setting Borders
graphic.setColor(Color.black);
graphic.fillRect(0,0,3,592);
graphic.fillRect(0,0,692,3);
graphic.fillRect(691,0,3,592);
//score counter
graphic.setColor(Color.white);
graphic.setFont(new Font("serif", Font.BOLD, 25));
graphic.drawString(""+ score,590,30);
//The Ball
graphic.setColor(Color.white);
graphic.fillOval(ballPosX,ballPosY,20,20);
//Paddle
graphic.setColor(Color.cyan);
graphic.fillRect(playerX,550,100,8);
//Game Check
if(totalBricks <= 0){
play = false;
ballXdir = 0;
ballYdir = 0;
graphic.setColor(Color.white);
graphic.setFont(new Font("serif",Font.BOLD,30));
graphic.drawString("You Won!",260,300);
graphic.setColor(Color.white);
graphic.setFont(new Font("serif",Font.BOLD,30));
graphic.drawString("Press (Enter) to restart",230,350);
}
if(ballPosY > 570) {
play = false;
ballXdir = 0;
ballYdir = 0;
graphic.setColor(Color.red);
graphic.setFont(new Font("serif",Font.BOLD,30));
graphic.drawString("Game Over, score: " + score,190,350);
graphic.setColor(Color.white);
graphic.setFont(new Font("serif",Font.BOLD,30));
graphic.drawString("Press (Enter) to restart",230,350);
}
graphic.dispose();
}
@Override
public void keyPressed(KeyEvent event) {
//
System.out.print("-key pressed");
if(event.getKeyCode()== KeyEvent.VK_RIGHT)
{
System.out.print(" right-");
pause = false;
if(playerX >= 600){
playerX = 600;
} else {
moveRight();
}
}
//
if(event.getKeyCode()== KeyEvent.VK_LEFT)
{
System.out.print(" left-");
pause = false;
if(playerX < 10){
playerX = 10;
} else {
moveRight();
}
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void moveRight(){
play = true;
playerX += 20;
}
public void moveLeft(){
play = true;
playerX -= 20;
}
@Override
public void actionPerformed(ActionEvent event) {
}
}