This repository was archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBall.java
More file actions
executable file
·85 lines (69 loc) · 1.49 KB
/
Ball.java
File metadata and controls
executable file
·85 lines (69 loc) · 1.49 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
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
import acm.graphics.*;
import java.awt.Color;
public class Ball extends GImage implements Commons{
double Vx = 0;
double Vy = 0;
Color type;
boolean revx = true;
boolean revy = true;
public Ball(String name, Color initial){
super(name);
type = initial;
resetState();
}
public void resetState(){
if(type==Color.BLUE){
setLocation(25, Commons.Screen_Height/2-getHeight()/2);
}else{
setLocation(Commons.Screen_Width-45, Commons.Screen_Height/2-getHeight()/2);
}
}
public void initVelocity(double x, double y){
Vx = x;
Vy = y;
}
public void move(){
move(Vx, Vy);
}
public void paddleCollisionRel(double offset){
Vy = Vy += offset;
if(Math.abs(Vy)>Math.abs(Vx)*2){
if(Vy<0){
Vy = -Math.abs(Vx);
}else{
Vy = Math.abs(Vx);
}
}
}
public void paddleCollisionCalc(double offset){
Vy += offset;
if(Math.abs(Vy) >= Commons.Ball_Velocity){
if(Vy<0){
Vy = -(Commons.Ball_Velocity-.5);
}else{
Vy = Commons.Ball_Velocity-.5;
}
}
if(Vx<0){
Vx = -Math.sqrt(Math.pow(Commons.Ball_Velocity, 2)-Math.pow(Vy, 2));
}else{
Vx = Math.sqrt(Math.pow(Commons.Ball_Velocity, 2)-Math.pow(Vy, 2));
}
}
public void setXvel(double x){
Vx = x;
}
public void setYvel(double y){
Vy = y;
}
public void reverseYvel(){
if (revy) {
this.Vy = -this.Vy;
}
}
public void reverseXvel(){
if (revx) {
this.Vx = -this.Vx;
}
}
}