001:import java.applet.Applet;
002:import java.awt.*;
003:import java.util.*;
004:import java.awt.event.*;
005:
006:public class Blockkuzusi extends java.applet.Applet
007: implements Runnable, MouseMotionListener,MouseListener{
008: int speed=50;
009: int num=5;
010: int bx,by;
011: int rx=50,ry=250;
012: int ballWidth=10, ballHeight10;
013: int racketWidth=20,racketHeight=3;
014: int margin=5;
015: int score;
016: int playWidth = 250 , playHeight = 280;
017: int meterWidth = 70 , meterHeight =250;
018: int width = 330, height = 300;
019:
020: int block[]=new int[600];
021: int x[]= new int[600];
022: int y[]= new int[600];
023: int blockWidth=10,blockHeight=3;
024:
025: boolean loop=true;
026: Thread kicker=null;
027:
028: Dimension d;
029: Image offs;
030: Graphics grf;
031:
032: public void init(){
033: int i,j;
034: int k;
035: int yy;
036:
037: /*ブロックの位置(x、y座標)の設定*/
038: k=0;
039: for(i=0; i<30; i++){
040: yy=i*(blockHeight+1)+margin*3;
041: for(j=0; j<20; j++){
042: x[k]=j*(blockWidth+1)+margin+2;
043: y[k]=yy;
044: block[k]=1;
045: k=k+1;
046: }
047: }
048:
049: /*オフスクリーンの設定*/
050: offs = createImage( width, height);
051: grf=offs.getGraphics();
052: /*ボールの初期値の設定*/
053: bx=margin+(int)(Math.random()*(float)(width/2-(margin*2+ballWidth+1)));
054: by=130;
055:
056: /*マウスモーション・リスナーとして自分自身を登録*/
057: addMouseMotionListener(this);
058:
059: /*マウス・リスナーとして自分自身を登録*/
060: addMouseListener(this);
061:
062: }
063:
064: public void paint(Graphics g){
065: update(g);
066: }
067:
068: public void update(Graphics g){
069: int i;
070: /* バックをレッドで塗る */
071: grf.setColor(Color.red);
072: grf.fillRect(0,0,width, height);
073: grf.setColor(Color.gray);
074: grf.fillRect(margin,margin, playWidth-margin*2, playHeight);
075: grf.setColor(Color.gray);
076: grf.fillRect(margin+playWidth,margin, meterWidth , meterHeight);
077:
078: /*ラケットを描く*/
079:grf.setColor(Color.blue);
080:grf.fillRect(rx,ry,racketWidth,racketHeight);
081:
082: /*ブロックを描く*/
083:for(i=0; i<600; i++){
084: grf.setColor(Color.blue);
085:
086: if(block[i] == 1){
087: /*ブロックがあればブロックを描く*/
088: grf.fillRect(x[i],y[i],blockWidth,blockHeight);
089: }
090:}
091:
092:/*ボールを描く*/
093:if(score<3000){
094:grf.setColor(Color.green);
095:grf.fillOval(bx,by,ballWidth,ballHeight);
096:grf.setColor(Color.blue);
097:grf.drawOval(bx,by,ballWidth,ballHeight);
098:}
099:if(3000<=score & score<5000){
100:grf.setColor(Color.orange);
101:grf.fillOval(bx,by,ballWidth,ballHeight);
102:grf.setColor(Color.black);
103:grf.drawOval(bx,by,ballWidth,ballHeight);
104:}
105:if(5000<=score & score <=6000){
106:grf.setColor(Color.red);
107:grf.fillOval(bx,by,ballWidth,ballHeight);
108:grf.setColor(Color.black);
109:grf.drawOval(bx,by,ballWidth,ballHeight);
110:}
111:
112: /* 点数、残りボール数の表示 */
113: grf.setColor(Color.blue);
114: grf.drawString("Score ", 260, 21);
115: grf.drawString(" "+score, 265, 38);
116: grf.drawRect( 261, 25, 50, 15);
117: grf.drawString("Boals ", 260, 61);
118: grf.drawString(" "+num, 260, 78);
119:if(num<0){
120: grf.setColor(Color.red);
121: grf.drawString("GAME OVER!",85,168);
122:}
123:if(score==600){
124: grf.setColor(Color.red);
125: grf.drawString("PERFECT!",85,168);
126:}
127:if(!loop ){
128: /*RESTARTボタンの描画*/
129: grf.setColor(Color.black);
130: grf.fillRect(88,120,65,25);
131: grf.setColor(Color.red);
132: grf.drawString("RESTAT!",95,138);
133: grf.setColor(Color.red);
134: grf.drawLine(88,120,153,120);
135: grf.drawLine(88,120,88,145);
136: grf.setColor(Color.red);
137: grf.drawLine(88,145,153,145);
138: grf.drawLine(153,145,153,120);
139:}
140:
141:/*オフスクリーンのイメージを一挙に実際の表示領域に描く*/
142:g.drawImage(offs,0,0,this);
143:
144:}
145:
146:public void mouseDragged(MouseEvent e){
147:/*マウスが押されてドラッグされた*/
148:}
149:
150:public void mouseMoved(MouseEvent e){
151:/*マウスが移動した*/
152:
153: rx=e.getX();
154:
155:/*ラケットがコートを出ないための処理*/
156:if(rx< margin){
157: rx=margin;
158: }
159: if(rx+racketWidth> playWidth -margin){
160: rx= playWidth -margin-racketWidth;
161:}
162:
163:repaint();
164:}
165:
166:public void start(){
167: if(kicker==null){
168: /*スレッドを実行される*/
169: kicker=new Thread(this);
170: kicker.start();
171: }
172:}
173:
174:public void stop(){
175: /*スレッドを止める*/
176: kicker=null;
177:}
178:
179:public void run(){
180: int dx=4,dy=4;
181: int i;
182:
183: /*実行中のスレッドをチェック*/
184: Thread thisThread =Thread.currentThread();
185:
186: /*繰り返し*/
187: while(loop & kicker==thisThread){
188:
189: /*ラケットが当たったをきの処理*/
190: if(by+ballHeight>=ry & by +ballHeight<=ry+racketHeight &
191: bx+ballWidth>=rx & bx <=rx+racketWidth){
192: /*ラケットに当たったら上へ返す*/
193: dy=-4;
194: if(bx < rx || bx+ballWidth>rx+racketWidth){
195: /*ラケットの端に当たったとき*/
196: if(dx==0){
197: /*垂直にきたボール*/
198: if(bx< rx){
199: /*左端に当たったら左斜めに返す*/
200: dx=-4;
201: }
202: if(bx+ballWidth>rx+racketWidth){
203: /*右端に当たったら右斜め上に返す*/
204: dx=+4;
205: }
206: }else{
207: /*斜めにきたボールは垂直に返す*/
208: dx=2;
209: }
210: }
211: }
212:/*左端、右端、上端に来たときの処理*/
213:if(bx< 0+margin){
214: /*左端にきたら反転*/
215: dx=4;
216:}
217:if(bx+ballWidth >playWidth-margin){
218: /*右端に来たら反転*/
219: dx=-4;
220:}
221:
222:if(by < 0+margin){
223: /*上端に来たら反転*/
224: dy=4;
225: }
226:
227: /*ラケットの下に行った時の処理*/
228:if(by+ballHeight>playHeight-margin){
229: /*下端に来たらボールを初期値へ*/
230: bx=margin+(int)(Math.random()*(float)(playWidth/2-(margin*2+ballWidth+1)));
231: by=130;
232: num=num-1;
233:}
234:
235:/*ブロックに当たったときの処理*/
236:for(i=0; i< 600; i++){
237: if(block[i]==1){
238: if(by+ballHeight >= y[i] & by <= y[i]+blockHeight &bx+ballWidth >= x[i] & bx <= x[i]+blockWidth){
239: /*ブロックに当たったら反転*/
240: dy=-dy;
241: score=score+10;
242: /*スピードアップさせる*/
243: if(1000 <=score & score < 2000)speed=40;
244: if(2000 <=score & score < 4000)speed=30;
245: if(4000 <=score & score < 5000)speed=20;
246: if(5000 <=score )speed=10;
247: block[i]=0;
248:
249: }
250: }
251: }
252:}
253:/*ゲーム終了判定*/
254:if(num< 0 || score == 6000){
255: loop=false;
256: }
257:
258: bx=bx+dx;
259: by=by+dy;
260:
261: repaint();
262:
263: try{
264: Thread.sleep(speed);
265: }catch(InterruptedException e){}
266: }
267:}
268:
269:public void mousePressed(MouseEvent e){
270: int ix,iy;
271:
272: /*マウスが押された座標を得る*/
273: ix=e.getX();
274: iy=e.getY();
275:
276: if(ix>88 & ix< 153 & iy>120 & iy<145){
277: /*マウスの座標(ix,iy)がRESTARTボタン内だったらゲーム再スタート*/
278: stop();
279: gameStart();
280: repaint();
281: }
282:}
283:
284:public void mouseReleased(MouseEvent e){
285:/*マウスが離れたら*/
286:}
287:
288:public void mouseClicked(MouseEvent e){
289:/*マウスボタンがクリックされた*/
290:}
291:
292:public void mouseEntered(MouseEvent e){
293:/*マウスが入ってきた*/
294:}
295:
296:public void mouseExited(MouseEvent e){
297:/*マウスが出て行った*/
298:}
299:
300:public void gameStart(){
301: int i;
302: /*変数初期化*/
303: num=3;
304: score=0;
305: speed=40;
306: for(i=0; i<600; i++){
307: block[i]=1;
308: }
309: bx=margin+(int)(Math.random()*(float)(width/2-(margin*2+ballWidth+1)));
310: by=130;
311: loop=true;
312: start();
313:}
314:}
|