-
Notifications
You must be signed in to change notification settings - Fork 0
/
Football.java
141 lines (102 loc) · 3.1 KB
/
Football.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
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Line2D;
import javax.swing.JPanel;
public class Football extends Thread{
private static final int XSIZE = 10;
private static final int YSIZE = 10;
private JPanel panel;
private InfoPanel infoPanel;
private int x;
private int y;
private int dx;
private int dy;
Graphics2D g2;
private Color backgroundColor;
private Dimension dimension;
private Player player;
private Goal goal;
private boolean hit;
private boolean kicked;
SoundManager soundManager;
public Football(JPanel p,InfoPanel infoPanel,Player player,Goal goal) {
this.goal = goal;
this.panel = p;
this.infoPanel = infoPanel;
this.player = player;
this.hit = false;
this.kicked = false;
Graphics g = panel.getGraphics ();
g2 = (Graphics2D) g;
backgroundColor = panel.getBackground ();
this.soundManager = SoundManager.getInstance();
dimension = panel.getSize();
x = player.getx();
y = player.gety() - 10;
dx = 0;
dy = -10;
this.infoPanel.displayInfo();
}
public Rectangle2D.Double getBoundingRectangle() {
return new Rectangle2D.Double (x, y, XSIZE, YSIZE);
}
public void draw () {
Graphics g = panel.getGraphics ();
g2 = (Graphics2D) g;
g2.setColor (new Color(255, 255, 255));
g2.fill (new Ellipse2D.Double (x, y, XSIZE, YSIZE));
}
public void erase () {
Graphics g = panel.getGraphics ();
g2 = (Graphics2D) g;
g2.setColor (backgroundColor);
g2.fill (new Ellipse2D.Double (x, y, XSIZE, YSIZE));
}
public void move () {
if (!panel.isVisible ()) {return;}
y+= dy;
Rectangle2D.Double goalRect = goal.getBoundingRectangle();
Rectangle2D.Double myRect = this.getBoundingRectangle();
if(myRect.intersects(goalRect)){
soundManager.playSound("goal", false);
this.hit = true;
y = -5;
infoPanel.incrementPoints(10);
infoPanel.incrementHits();
infoPanel.displayInfo();
}
//missed the ball
if ((y <0)&&hit==false&&kicked==false) {
soundManager.playSound("missed", false);
infoPanel.incrementMisses();
infoPanel.decreasePoints(10);
infoPanel.displayInfo();
kicked = true;
}
}
public void run () {
try {
draw ();
while (true) { // loop forever
erase();
move ();
draw();
sleep (50); // increase value to slow down ball
}
}
catch(InterruptedException e) {
System.out.println(e +" bro idk nah");
}
}
public int gety(){
return this.y;
}
public void sety(int y){
this.y = y;
}
}