-
Notifications
You must be signed in to change notification settings - Fork 0
/
GamePanel.java
177 lines (146 loc) · 4.36 KB
/
GamePanel.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
import java.awt.*;
import java.awt.event.*;
import java.security.PublicKey;
import java.util.*;
import javax.swing.*;
public class GamePanel extends JPanel implements Runnable {
static final int GAME_WIDTH = 1000;
static final int GAME_HEIGHT = (int)(GAME_WIDTH * (0.5555));
static final Dimension SCREEN_SIZE = new Dimension(GAME_WIDTH,GAME_HEIGHT);
static final int BALLS_DIAMETER = 20;
static final int PADDLE_WIDTH = 25;
static final int PADDLE_HEIGHT = 100;
Thread gameThread;
Image image;
Graphics graphics;
Random random;
Paddle paddle1;
Paddle paddle2;
Balls ball;
Score score;
GamePanel() {
newPaddles();
newBall();
score = new Score(GAME_WIDTH,GAME_HEIGHT);
this.setFocusable(true);
this.addKeyListener(new AL());
this.setPreferredSize(SCREEN_SIZE);
gameThread = new Thread(this);
gameThread.start();
}
public void newBall() {
random = new Random();
ball = new Balls((GAME_WIDTH/2)-(BALLS_DIAMETER/2),random.nextInt(GAME_HEIGHT-BALLS_DIAMETER),BALLS_DIAMETER,BALLS_DIAMETER);
}
public void newPaddles() {
paddle1 = new Paddle(0,(GAME_HEIGHT/2)-(PADDLE_HEIGHT/2),PADDLE_WIDTH,PADDLE_HEIGHT,1);
paddle2 = new Paddle(GAME_WIDTH-PADDLE_WIDTH,(GAME_HEIGHT/2)-(PADDLE_HEIGHT/2),PADDLE_WIDTH,PADDLE_HEIGHT,2);
}
public void paint(Graphics g) {
image = createImage(getWidth(),getHeight());
graphics = image.getGraphics();
draw(graphics);
g.drawImage(image,0,0,this);
}
public void draw(Graphics g) {
paddle1.draw(g);
paddle2.draw(g);
ball.draw(g);
score.draw(g);
}
public void move() {
paddle1.move();
paddle2.move();
ball.move();
}
public void checkCollision() {
// bounce the ball off top and bottom window edges
if(ball.y <= 0)
ball.setYDirection(-ball.yVelocity);
if(ball.y >= GAME_HEIGHT-BALLS_DIAMETER)
ball.setYDirection(-ball.yVelocity);
// bounce ball off paddles
// paddle 1
if(ball.intersects(paddle1)) {
ball.xVelocity = Math.abs(ball.xVelocity);
ball.xVelocity++; // for more difficulty xball_speed_++
if(ball.yVelocity>0)
ball.yVelocity++; //for more difficulty yball_speed_++
else
ball.yVelocity--;
ball.setXDirection(ball.xVelocity);
ball.setYDirection(ball.yVelocity);
}
// paddle 2
if(ball.intersects(paddle2)) {
ball.xVelocity = Math.abs(ball.xVelocity);
ball.xVelocity++; // for more difficulty xball_speed_++
if(ball.yVelocity>0)
ball.yVelocity++; //for more difficulty yball_speed_++
else
ball.yVelocity--;
ball.setXDirection(-ball.xVelocity);
ball.setYDirection(ball.yVelocity);
}
// check if paddles still in game window
//paddle 1 (x,y) testing
if(paddle1.y <= 0)
paddle1.y = 0;
if(paddle1.y >= GAME_HEIGHT-PADDLE_HEIGHT)
paddle1.y = GAME_HEIGHT-PADDLE_HEIGHT;
if(paddle1.x <= 0)
paddle1.x = 0;
if(paddle1.x >= (GAME_WIDTH / 2)-PADDLE_WIDTH*2)
paddle1.x = (GAME_WIDTH / 2)-PADDLE_WIDTH*2;
//paddle 2 (x,y) testing
if(paddle2.y <= 0)
paddle2.y = 0;
if(paddle2.y >= GAME_HEIGHT-PADDLE_HEIGHT)
paddle2.y = GAME_HEIGHT-PADDLE_HEIGHT;
if(paddle2.x <= (GAME_WIDTH / 2)+PADDLE_WIDTH)
paddle2.x = (GAME_WIDTH / 2)+PADDLE_WIDTH;
if(paddle2.x >= GAME_WIDTH-PADDLE_WIDTH)
paddle2.x = GAME_WIDTH-PADDLE_WIDTH;
// give a player +1 point and create a ball and paddles
if(ball.x <= 0) {
score.player2++;
newPaddles();
newBall();
//System.out.println("Player 2 : "+score.player2);
}
if(ball.x >= GAME_WIDTH-BALLS_DIAMETER) {
score.player1++;
newPaddles();
newBall();
//System.out.println("Player 1 : "+score.player1);
}
}
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000/amountOfTicks;
double delta = 0;
while(true) {
long now = System.nanoTime();
delta += (now -lastTime)/ns;
lastTime = now;
if (delta >= 1) {
move();
checkCollision();
repaint();
delta--;
System.out.println("game is running ; )");
}
}
}
public class AL extends KeyAdapter {
public void keyPressed(KeyEvent e) {
paddle1.keyPressed(e);
paddle2.keyPressed(e);
}
public void keyReleased(KeyEvent e) {
paddle1.keyReleased(e);
paddle2.keyReleased(e);
}
}
}