-
Notifications
You must be signed in to change notification settings - Fork 0
/
DunkShotRunner
193 lines (155 loc) · 6.32 KB
/
DunkShotRunner
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.company;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class DunkShotRunner {
static double power;
static double powerFactor = 0.04;
static double distance;
static double basketX;
static double basketY;
static double xChange = 0;
static double yChange = 0;
static double theta;
static double xStarts;
static double yStarts;
static double xDirection = 0;
static double yDirection = 0;
static Timer repainter;
static JPanel p;
static JButton reset;
static JButton quit;
//Directions: Drag in any direction to launch ball in the opposite direction of the drag, like pulling a sling shot.
//the further the mouse is dragged, the more power the launch will have and the further the ball will go.
//The goal is to get it into the hoop as many times as you can.
//NOTE: Sometimes when resetting, the hoops and score take a while to disappear. Not sure why.
public static void main(String[] args) {
JFrame f = new JFrame("DunkShot");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 900);
f.setResizable(false);
DrawingComponent game = new DrawingComponent();
f.getContentPane().setBackground(new Color(86, 211, 240));
f.add(game);
class CanvasListener implements MouseListener{
public void mouseClicked(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
public void mousePressed(MouseEvent arg0) {
xStarts = arg0.getX();
yStarts = arg0.getY();
basketX = game.getHoopX();
basketY = game.getHoopY();
}
public void mouseReleased(MouseEvent arg0) {
if(!isInMotion() & !game.getRealgined()) {
calculateTheta2(arg0.getX(), arg0.getY(), xStarts, yStarts);
power = powerFactor * distance;
xChange = xDirection * power * Math.cos(theta);
yChange = yDirection * power * Math.sin(theta);
}
}
}
class shotListener implements MouseMotionListener{
public void mouseDragged(MouseEvent arg0) {
// System.out.print("\ndragged: " + arg0.getX());
distance = Math.sqrt(Math.pow(arg0.getX() - basketX, 2) + Math.pow(arg0.getY() - basketY, 2));
}
public void mouseMoved(MouseEvent arg0) {}
}
CanvasListener cListen = new CanvasListener();
shotListener sListen = new shotListener();
f.addMouseListener(cListen);
f.addMouseMotionListener(sListen);
f.setVisible(true);
class ResetListener implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
game.reset();
yChange = 0;
xChange = 0;
f.remove(p);
f.revalidate();
f.repaint();
repainter.restart();
}
}
class StopListener implements ActionListener{
public void actionPerformed(ActionEvent e) {f.dispose();}
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e){
if(game.getBallY() < 900) {
if(game.isOut()){xChange *= -1;}
game.moveBall2(xChange, yChange);
if(game.isInHoop2()){game.realign();}
game.revalidate();
game.repaint();
} else {restart();}
}
public void restart() {
repainter.stop();
p = new JPanel();
reset = new JButton("Your Score: " + game.getScore() + "\n \n \n Restart?");
reset.addActionListener(new ResetListener());
reset.setFont(new Font("Arial", Font.PLAIN, 40));
reset.setBackground(new Color(184, 152, 180));
try{
Image resetBack = ImageIO.read(getClass().getResource("pierreJackson.jpg"));
reset.setIcon(new ImageIcon(resetBack));
}catch (Exception ex) {}
quit = new JButton("Quit");
quit.addActionListener(new StopListener());
quit.setFont(new Font("Arial", Font.PLAIN, 40));
quit.setBackground(new Color(184, 139, 99));
try{
Image quitBack= ImageIO.read(getClass().getResource("tiredGordon.jpg"));
quit.setIcon(new ImageIcon(quitBack));
}catch (Exception ex) { }
p.setLayout(new GridLayout(2,1));
p.add(reset);
p.add(quit);
f.add(p);
f.revalidate();
f.repaint();
}
}
repainter = new Timer(17, new TimerListener());
repainter.start();
}
public static void setInitalChanges(double newXChange, double newYChange) {
xChange = newXChange;
yChange = newYChange;
}
public static void decreaseY() {yChange -= 0.5;}
public static void calculateTheta(double xRelease, double yRelease) {
//don't forget to figure out directions
double xChanges = Math.abs(xRelease - basketX);
double yChanges = Math.abs(basketY - yRelease);
theta = Math.atan(yChanges/xChanges);
}
//if drag does not start at basket
public static void calculateTheta2(double xRelease, double yRelease, double xStart, double yStart) {
//don't forget to figure out directions
if(xRelease - xStart < 0){
xDirection = 1;
} else if(xRelease - xStart > 0){
xDirection = -1;
}
if(yStart - yRelease < 0){
yDirection = 1;
}else if(yStart - yRelease > 0){
yDirection = -1;
}
double xChanges = Math.abs(xRelease - xStart);
double yChanges = Math.abs(yStart - yRelease);
theta = Math.atan(yChanges/xChanges);
}
public static boolean isInMotion() {
if(yChange == 0 && xChange == 0){
return false;
}else return true;
}
}