- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
Latest commit
114 lines (98 loc) · 3.34 KB
/
Copy pathGame.java
File metadata and controls
114 lines (98 loc) · 3.34 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
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
importjava.awt.*;
importjava.awt.event.*;
//import java.io.File;
importjava.net.MalformedURLException;
importjava.net.URL;
importjava.applet.*;
importjavax.swing.*;
publicclassGameextendsJComponentimplementsActionListener, MouseMotionListener, KeyListener {
privatefinalintWIDTH = 800, HEIGHT = 600;
privatefinalintB_WIDTH = 30;
privateintball1, ball2;
privateintrec = 0;
privateintball2move, ball1move;
publicintscore = 0, scoreNext = 0;
privateintBest;
publicbooleanyouLose, out;
protectedvoidpaintComponent(Graphicsg) {
ColorGOLD = newColor(255,215,0); //paints all the parts if the program and the new ball
g.setColor(Color.white);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.black);
g.fillRect(0, 550, WIDTH, 50);
g.setColor(GOLD);
g.fillRect(rec, 500, 100, 20);
g.setColor(Color.black);
g.fillOval(ball1, ball2, B_WIDTH, B_WIDTH);
g.setColor(GOLD);
g.setFont(newFont("Arial", 8, 50));
g.drawString(String.valueOf("Score: " + score), 29 / 1 - 15, 80);
g.setColor(Color.black);
g.setFont(newFont("Arial", 8, 50));
if (youLose) {
g.drawString(String.valueOf(" Best Score :" + Best), 50 / 1 - 15, 200);
}
}
publicvoidnewball(intball1, intball2, intball1move, intball2move) {
ball1 = B_WIDTH; //creates a new ball
ball2 = B_WIDTH;
ball1move = 5;
ball2move = 7;
return;
}
publicDimensiongetPreferredSize() { //sets the dimensions for the screen
returnnewDimension(WIDTH, HEIGHT);
}
publicvoidactionPerformed(ActionEvente) {
ball1 = ball1 + ball1move; // the balls movements off the walls
ball2 = ball2 + ball2move;
if (ball1 >= rec && ball1 <= rec + 100 && ball2 >= 475) {
ball2move = -7;
score++;
}
if (ball2 >= 700 ) { //when the ball misses rec
score = 0;
ball2 = B_WIDTH;
youLose = true;
out = true;
}
if (ball2 <= 0) {
ball2move = 7;
}
if (ball1 >= 775) {
ball1move = -5;
}
if (ball1 <= 0) {
ball1move = 5;
}
//presents the scores
if (score > Best) {
Best = score;
try { //plays the sound every time the ball scores
Stringurl = "/*add the one1.wav file path here*/"; //add the file path the one1.wav here
PlaySound.myPlay(url);
System.out.println("df");
youLose = false;
} catch (InterruptedExceptionie) {
System.out.println(ie);
}
}
repaint();
}
//used to follow the mouse
publicvoidmouseMoved(MouseEvente) {
rec = e.getX() - 50;
repaint();
} //other necessary ones that do nothing
publicvoidkeyPressed(KeyEventarg0) {
// TODO Auto-generated method stub
}
publicvoidkeyReleased(KeyEventsp) {
// TODO Auto-generated method stub
}
publicvoidmouseDragged(MouseEventarg0) {
// TODO Auto-generated method stub
}
publicvoidkeyTyped(KeyEventarg0) {
}
}