forked from srinathr91/TestJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
244 lines (172 loc) · 6.19 KB
/
Main.java
File metadata and controls
244 lines (172 loc) · 6.19 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package stickerball_game;
/*
* * Please Visit us at www.codemiles.com *
* This Program was Developed by www.codemiles.com forums Team
* * Please Don't Remove This Comment *
*/
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.applet.*;
import java.awt.*;
/**
*
* @author Mohamed Eldib
*/
public class Main extends Applet implements Runnable ,MouseMotionListener,MouseListener {
/** Creates a new instance of Main */
private final int UP = 38;
private final int DOWN = 40;
private Player myPlayer;
private Computer myComp;
private Ball myBall;
private Image dbImage;
private Image GameName;
private AudioClip kicknoise;
private Graphics dbg;
private Thread th;
private boolean GAME_STARTED;
private boolean GAME_OPTION;
private boolean GAME_OVER;
private int CScore=0;
private int PScore=0 ;
private int TopScore=5;
public void init() {
GAME_OPTION=true;
GAME_STARTED=false;
GAME_OVER=false;
CScore=0;
PScore=0;
TopScore=5;
myPlayer=new Player(20,125,10,50,Color.GREEN.darker());
myComp=new Computer(370,124,10,50,Color.RED);
myBall=new Ball(8,200,150,2,-2,Color.blue,this);
addMouseMotionListener(this);
addMouseListener(this);
kicknoise = getAudioClip(getCodeBase(), "hit.au");
GameName= ((Applet) this).getImage(((Applet) this).getCodeBase(), "images/gamename.gif");
}
public void start() {
}
public void stop() {
th.interrupt();
}
public void paint(Graphics g) {
if(GAME_OPTION) {
Image codemiles= ((Applet) this).getImage(((Applet) this).getCodeBase(), "images/codemiles.gif");
g.setColor(Color.GRAY);
g.fillRect(0,0,400,400);
g.setColor(Color.white);
g.drawString("Press Mouse Click To Start Ball Sticker",100,100);
g.drawImage(codemiles,150,150,this);
g.drawString("Visit us at << http://codemiles.com/ >> for more online games",35,250);
g.drawImage( GameName,100,275,this);
} else if(GAME_STARTED) {
g.fillRect(0,0,400,300);
myPlayer.DrawStrick(g);
myComp.DrawStrick(g);
myBall.DrawBall(g);
g.setColor(Color.YELLOW);
g.drawLine(25,32,375,32);
g.drawLine(25,292,375,292);
g.setColor(Color.GRAY);
g.fillRect(400,300,400,100);
g.setColor(Color.BLACK);
g.drawString("Player Score :"+Integer.toString(PScore),50,350);
g.drawString("Computer Score :"+Integer.toString(CScore),250,350);
}
}
/**
* @param args the command line arguments
*/
public void update(Graphics g) {
if (dbImage == null) {
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}
dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
dbg.setColor(getForeground());
paint(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true) {
repaint();
myBall.move();
myComp.ComputerMove(myBall);
int whosgoal = myBall.wheresBall();
if (whosgoal != 0) {
if(whosgoal==1)
CScore++;
else
PScore++;
if(PScore==5) {
GAME_STARTED=false;
GAME_OVER=GAME_OPTION=true;
th.interrupt();
repaint();
}else if(CScore==5) {
GAME_STARTED=false;
GAME_OVER=GAME_OPTION=true;
th.interrupt();
repaint();
}
myBall.x= 200;
myBall.vx = 3;
myBall.vy = -3;
}
if (myBall.vx < 0) {
myBall.PCollision(myPlayer,kicknoise);
} else if (myBall.vx > 0) {
myBall.CCollision(myComp,kicknoise);
}
try {
Thread.sleep(15);
} catch (InterruptedException ex) {
break;
}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
int y=e.getY();
if(y<250&&y>25)
myPlayer.MoveByMouse(y);
}
public void mouseClicked(MouseEvent e) {
if(!GAME_STARTED) {
GAME_STARTED=true;
GAME_OPTION=false;
CScore=0;
PScore=0;
TopScore=5;
myBall.vx = 3;
myBall.vy = -3;
th=new Thread(this);
th.start();
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
/*
* * Please Visit us at www.codemiles.com *
* This Program was Developed by www.codemiles.com forums Team
* * Please Don't Remove This Comment *
*/