Repository navigation
Expand file tree
/
Copy pathBall.java
More file actions
53 lines (49 loc) · 1.51 KB
/
Copy pathBall.java
File metadata and controls
53 lines (49 loc) · 1.51 KB
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
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Ball extends MovingObject implements WindowInfo{
public final static double INITIAL_SPEED = 550;
public final static double RADIUS = 15;
private Random rand = new Random();
public Color ballColor = Color.ORANGE;
public Ball() {
this.setPosition(new Pair(WindowInfo.WINDOW_WIDTH/2, WindowInfo.WINDOW_HEIGHT/2));
int initDirection = rand.nextInt(2); //randomly determine initial direction => left or right
// TO THE RIGHT
if (initDirection == 0) {
this.setVelocity(new Pair(INITIAL_SPEED, 0.0));
}
// TO THE LEFT
else {
this.setVelocity(new Pair(-INITIAL_SPEED, 0.0));
}
}
//if l => left side lost
public char whereDied() {
if(this.getPosition().x < 0) {
return 'l';
}
else if(this.getPosition().x > WindowInfo.WINDOW_WIDTH) {
return 'r';
}
else{
return ' ';
}
}
public void startOver() {
if(this.whereDied() == 'l') {
this.setPosition(new Pair(WindowInfo.WINDOW_WIDTH/2, WindowInfo.WINDOW_HEIGHT/2));
this.setVelocity(new Pair(-INITIAL_SPEED, 0.0));
}
if(this.whereDied() == 'r') {
this.setPosition(new Pair(WindowInfo.WINDOW_WIDTH/2, WindowInfo.WINDOW_HEIGHT/2));
this.setVelocity(new Pair(INITIAL_SPEED, 0.0));
}
}
public void drawBall(Graphics g) {
//drawing the ball
//this.x and this.y are the center coordinates
g.setColor(this.ballColor);
g.fillOval((int)(this.getPosition().x - RADIUS), (int)(this.getPosition().y - RADIUS), (int)RADIUS*2, (int)RADIUS*2);
}
}