ファイル名「MainLoop.java」
008 | package and.roid.shooting2; |
010 | import java.util.ArrayList; |
011 | import java.util.Date; |
012 | import java.util.Random; |
014 | import android.content.Context; |
015 | import android.content.res.Resources; |
016 | import android.graphics.Bitmap; |
017 | import android.graphics.BitmapFactory; |
018 | import android.graphics.Canvas; |
019 | import android.graphics.Color; |
020 | import android.graphics.Paint; |
021 | import android.graphics.Rect; |
022 | import android.graphics.drawable.Drawable; |
023 | import android.media.MediaPlayer; |
024 | import android.util.AttributeSet; |
025 | import android.view.MotionEvent; |
026 | import android.view.SurfaceHolder; |
027 | import android.view.SurfaceView; |
029 | public class MainLoop extends SurfaceView implements SurfaceHolder.Callback,Runnable{ |
030 | private SurfaceHolder holder; |
031 | private Thread thread; |
034 | private Shooting2Activity s2a; |
036 | private float disp_w,disp_h; |
037 | private Bitmap jikibit,tamabit; |
039 | private boolean tamaflg; |
040 | private int tamatime; |
042 | private Rect tamabtn; |
044 | private MediaPlayer jitama1s,jitama2s,jitama3s; |
047 | private Drawable tekiimg; |
048 | private Bitmap tekibit; |
051 | private Bitmap[] bombit = new Bitmap[4]; |
052 | private boolean bomflg= false ; |
056 | private boolean poseflg; |
070 | private ArrayList<Object> object = new ArrayList(); |
075 | public MainLoop(Context context) { |
080 | public MainLoop(Context context, AttributeSet attrs) { |
081 | super(context, attrs); |
085 | public void init(Context context){ |
086 | holder = getHolder(); |
087 | holder.addCallback( this ); |
088 | holder.setFixedSize(getWidth(), getHeight()); |
096 | s2a = (Shooting2Activity)context; |
101 | Resources resources = context.getResources(); |
104 | Bitmap img= BitmapFactory.decodeResource(resources,and.roid.shooting2.R.drawable.jiki); |
108 | jikibit = Bitmap.createBitmap(img,0,0,img.getWidth()/4,img.getHeight()); |
109 | tamabit = Bitmap.createBitmap(img,img.getWidth()/4,0,img.getWidth()/4,img.getHeight()); |
119 | object .add( new Jiki(disp_w,disp_h)); |
120 | object . get (0).Oint(jikibit, 240, 800, 0, 0, jikibit.getWidth(), jikibit.getHeight(),0); |
126 | tamabtn = new Rect(0,0,50,50); |
129 | jitama1s = MediaPlayer.create(context,R.raw.jitama1); |
130 | jitama2s = MediaPlayer.create(context,R.raw.jitama2); |
131 | jitama3s = MediaPlayer.create(context,R.raw.jitama3); |
136 | } catch (Exception e){} |
139 | tekibit = Bitmap.createBitmap(img,img.getWidth()/4*2,0,img.getWidth()/4,img.getHeight()); |
142 | Random r = new Random( new Date().getTime()); |
143 | for ( int i=0;i<10;i++){ |
144 | int x = r.nextInt(( int ) (disp_w-50)); |
145 | int y = r.nextInt(( int ) (disp_h/2)); |
146 | object .add( new Tekiki(disp_w,disp_h)); |
147 | object . get ( object .size()-1). |
148 | Oint(tekibit, x, y, 0, 0, tekibit.getWidth(), tekibit.getHeight(),0); |
151 | img= BitmapFactory.decodeResource(resources,and.roid.shooting2.R.drawable.bom); |
155 | for ( int i=0;i<2;i++){ |
156 | for ( int j=0;j<2;j++){ |
157 | bombit[j+i*2] = Bitmap.createBitmap( |
159 | j*(img.getWidth()/2),i*(img.getHeight()/2), |
160 | img.getWidth()/2,img.getHeight()/2); |
165 | pose = new Rect(( int )disp_w-50,0,( int )disp_w,50); |
174 | Paint p = new Paint(); |
175 | p.setAntiAlias( true ); |
177 | while (thread != null ){ |
178 | c = holder.lockCanvas(); |
182 | c.drawColor(Color.BLACK); |
187 | p.setColor(Color.BLUE); |
188 | c.drawRect(tamabtn, p); |
190 | c.drawText( "tama:" + object . get (0).tamajoutai, 50, 50, p); |
192 | p.setColor(Color.RED); |
204 | for ( int i=0;i< object .size();i++){ |
205 | object . get (i).ODraw(c); |
206 | object . get (i).OMove(); |
208 | ms.Atarihantei( object ,i); |
209 | if ( object . get (i).bomsflg== true ){ |
210 | object .add( new Bom(disp_w,disp_h)); |
211 | object . get ( object .size()-1).Oint( |
212 | bombit, object . get (i).cx, object . get (i).cy, |
213 | 0, 0, bombit[0].getWidth(), bombit[0].getHeight(),0); |
218 | if ( object . get (i).Ogetdead()== true ) object .remove(i); |
224 | if (tamaflg == false ){ |
234 | holder.unlockCanvasAndPost(c); |
239 | } catch (Exception e){} |
243 | public boolean onTouchEvent(MotionEvent event ){ |
244 | int action = event .getAction(); |
245 | int x = ( int ) event .getX(); |
246 | int y = ( int ) event .getY(); |
248 | case MotionEvent.ACTION_DOWN: |
253 | if (tamaflg == true && ms.RectTap( |
254 | x, y, object . get (0).OgetTapRect()) == true ){ |
269 | if (ms.RectTap(x, y, tamabtn)== true ){ |
270 | ++ object . get (0).tamajoutai; |
271 | object . get (0).tamajoutai = ( object . get (0).tamajoutai+3)%3; |
275 | if (ms.RectTap(x, y, pose)== true ){ |
276 | if (poseflg== true ){poseflg= false ;} else {poseflg= true ;} |
279 | case MotionEvent.ACTION_UP: |
281 | case MotionEvent.ACTION_MOVE: |
282 | if (ms.RectTap(x, y, object . get (0).OgetTapRect()) == true ) object . get (0).OMove(x, y); |
300 | public void Tamajoutai(){ |
302 | if ( object . get (0).tamajoutai == 0){ |
303 | object .add( new JiTama(disp_w,disp_h)); |
304 | object . get ( object .size()-1).Oint( |
305 | tamabit, object . get (0).cx, object . get (0).cy-jikibit.getHeight(), |
306 | 0, 30, tamabit.getWidth(), tamabit.getHeight(),0); |
308 | ms.playSound(jitama1s); |
311 | if ( object . get (0).tamajoutai == 1){ |
313 | object .add( new JiTama(disp_w,disp_h)); |
314 | object . get ( object .size()-1).Oint( |
315 | tamabit, object . get (0).cx-20, object . get (0).cy-jikibit.getHeight(), |
316 | 0, 30, tamabit.getWidth(), tamabit.getHeight(),0); |
317 | object .add( new JiTama(disp_w,disp_h)); |
318 | object . get ( object .size()-1).Oint( |
319 | tamabit, object . get (0).cx+20, object . get (0).cy-jikibit.getHeight(), |
320 | 0, 30, tamabit.getWidth(), tamabit.getHeight(),0); |
322 | ms.playSound(jitama3s); |
334 | if ( object . get (0).tamajoutai == 2){ |
335 | for ( int i=0;i<10;i++){ |
337 | float rx = ( float ) (Math.sin(ms.toRadian(r))*55); |
338 | float ry = ( float ) (Math.cos(ms.toRadian(r))*55); |
339 | object .add( new JiTama(disp_w,disp_h)); |
340 | object . get ( object .size()-1).Oint( |
341 | tamabit, object . get (0).cx+rx, object . get (0).cy-ry, |
342 | 30, 30, tamabit.getWidth(), tamabit.getHeight(),r); |
345 | ms.playSound(jitama2s); |
349 | public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {} |
351 | public void surfaceCreated(SurfaceHolder arg0) {thread = new Thread( this );thread.start();} |
353 | public void surfaceDestroyed(SurfaceHolder arg0) {thread = null ;} |
ファイル名「Object.java」
14 | package and.roid.shooting2; |
16 | import android.graphics.Bitmap; |
17 | import android.graphics.Canvas; |
18 | import android.graphics.Color; |
19 | import android.graphics.Paint; |
20 | import android.graphics.Rect; |
21 | import android.graphics.drawable.BitmapDrawable; |
22 | import android.graphics.drawable.Drawable; |
24 | public abstract class Object { |
25 | public Mesod ms = new Mesod(); |
26 | public float disp_w,disp_h; |
40 | public int tamajoutai; |
48 | public Drawable[] boms = new Drawable[4]; |
49 | public boolean bomsflg= false ; |
50 | public int bomindex=0; |
53 | public Object( float dw, float dh){ |
66 | public abstract void ODraw(Canvas c); |
68 | public abstract void Oint(Bitmap imgb, float x, float y, float sx, float sy, int w, int h); |
69 | public abstract void Oint(Bitmap[] imgb, float x, float y, float sx, float sy, int w, int h, int tj); |
70 | public abstract void Oint(Bitmap imgb, float x, float y, float sx, float sy, int w, int h, int tj); |
71 | public abstract void OMove(); |
72 | public abstract void OMove( int x, int y); |
73 | public abstract Rect OgetTapRect(); |
77 | public boolean OsotoX( int ww){ return (cx-ww<0 || cx+ww>disp_w);} |
78 | public boolean OsotoY( int hh){ return (cy-hh<0 || cy+hh>disp_h);} |
79 | public boolean Ogetdead(){ return dead;} |
ファイル名「Jiki.java」
05 | package and.roid.shooting2; |
07 | import android.graphics.Bitmap; |
08 | import android.graphics.Canvas; |
09 | import android.graphics.Paint; |
10 | import android.graphics.Rect; |
11 | import android.graphics.drawable.BitmapDrawable; |
13 | public class Jiki extends Object{ |
16 | public Jiki( float dw, float dh){ |
20 | public void Oint(Bitmap imgb, float x, float y, float sx, float sy, int w, int h, int tj){ |
21 | img = new BitmapDrawable(imgb); |
22 | cx = ms.setSizeX(disp_w, x); |
23 | cy = ms.setSizeY(disp_h, y); |
31 | atarir = new Rect(( int )cx-30,( int )cy-30,( int )cx+30,( int )cy+30); |
34 | public void ODraw(Canvas c){ |
40 | img.setBounds(( int )(cx-imgw/2),( int )(cy-imgh/2), |
41 | ( int )(cx+imgw/2),( int )(cy+imgh/2)); |
43 | ms.OdrawRect(atarir,c); |
49 | public void OMove( int x, int y) { |
55 | atarir = new Rect(( int )cx-30,( int )cy-30,( int )cx+30,( int )cy+30); |
57 | if (OsotoX(imgw/2)== true ) cx = cxx; |
58 | if (OsotoY(imgh/2)== true ) cy = cyy; |
60 | public void OMove() {} |
69 | public Rect OgetTapRect(){ |
70 | Rect taprect = new Rect( |
71 | img.getBounds().left-50,img.getBounds().top-50, |
72 | img.getBounds().right+50,img.getBounds().bottom+50); |
76 | public void Oint(Bitmap imgb, float x, float y, float sx, float sy, int w, int h) {} |
78 | public void Oint(Bitmap[] imgb, float x, float y, float sx, float sy, |
79 | int w, int h, int tj) {} |
ファイル名「JiTama.java」
07 | package and.roid.shooting2; |
09 | import android.graphics.Bitmap; |
10 | import android.graphics.Canvas; |
11 | import android.graphics.Rect; |
12 | import android.graphics.drawable.BitmapDrawable; |
14 | public class JiTama extends Object{ |
18 | public int tamakakudo; |
21 | public JiTama( float dw, float dh){ |
24 | public void ODraw(Canvas c){ |
37 | img.setBounds(( int )(cx-imgw/2),( int )(cy-imgh/2), |
38 | ( int )(cx+imgw/2),( int )(cy+imgh/2)); |
39 | c.rotate(tamar, cx, cy); |
41 | ms.OdrawRect(atarir,c); |
53 | cx += ( float ) Math.cos(ms.toRadian(tamar-90)) * spx; |
54 | cy += ( float ) Math.sin(ms.toRadian(tamar-90)) * spy; |
56 | atarir = new Rect(( int )cx-10,( int )cy-10,( int )cx+10,( int )cy+10); |
60 | if (OsotoX(-imgw/2)== true ) dead = true ; |
61 | if (OsotoY(-imgh/2)== true ) dead = true ; |
64 | public void OMove( int x, int y) {} |
65 | public Rect OgetTapRect() { return null ;} |
68 | public void Oint(Bitmap imgb, float x, float y, float sx, float sy, int w, int h, int r) { |
69 | img = new BitmapDrawable(imgb); |
70 | cx = ms.setSizeX(disp_w, x); |
71 | cy = ms.setSizeY(disp_h, y); |
78 | atarir = new Rect(( int )cx-10,( int )cy-10,( int )cx+10,( int )cy+10); |
82 | public void Oint(Bitmap imgb, float x, float y, float sx, float sy, int w, int h) {} |
84 | public void Oint(Bitmap[] imgb, float x, float y, float sx, float sy, int w, int h, int tj) {} |
ファイル名「Tekiki.java」
01 | package and.roid.shooting2; |
03 | import android.graphics.Bitmap; |
04 | import android.graphics.Canvas; |
05 | import android.graphics.Rect; |
06 | import android.graphics.drawable.BitmapDrawable; |
08 | public class Tekiki extends Object{ |
11 | public Tekiki( float dw, float dh){ |
16 | public void ODraw(Canvas c) { |
22 | img.setBounds(( int )(cx-imgw/2),( int )(cy-imgh/2), |
23 | ( int )(cx+imgw/2),( int )(cy+imgh/2)); |
25 | ms.OdrawRect(atarir,c); |
32 | public void Oint(Bitmap imgb, float x, float y, float sx, float sy, int w, |
34 | img = new BitmapDrawable(imgb); |
35 | cx = ms.setSizeX(disp_w, x); |
36 | cy = ms.setSizeY(disp_h, y); |
43 | atarir = new Rect(( int )cx-30,( int )cy-30,( int )cx+30,( int )cy+30); |
48 | public void OMove() {} |
51 | public void OMove( int x, int y) {} |
54 | public Rect OgetTapRect() { return null ;} |
56 | public void Oint(Bitmap imgb, float x, float y, float sx, float sy, int w, |
59 | public void Oint(Bitmap[] imgb, float x, float y, float sx, float sy, |
60 | int w, int h, int tj) {} |
今回の追加ファイル ファイル名「Bom.java」
01 | package and.roid.shooting2; |
03 | import android.graphics.Bitmap; |
04 | import android.graphics.Canvas; |
05 | import android.graphics.Rect; |
06 | import android.graphics.drawable.BitmapDrawable; |
08 | public class Bom extends Object{ |
11 | public Bom( float dw, float dh){ |
15 | public void ODraw(Canvas c) { |
16 | boms[bomindex].setBounds(( int )(cx-40),( int )(cy-40), |
17 | ( int )(cx+40),( int )(cy+40)); |
18 | boms[bomindex].draw(c); |
20 | if (bomindex>3)dead= true ; |
24 | public void Oint(Bitmap[] imgb, float x, float y, float sx, float sy, |
25 | int w, int h, int tj) { |
27 | boms[i] = new BitmapDrawable(imgb[i]); |
29 | cx = ms.setSizeX(disp_w, x); |
30 | cy = ms.setSizeY(disp_h, y); |
38 | atarir = new Rect(( int )cx-30,( int )cy-30,( int )cx+30,( int )cy+30); |
42 | public void Oint(Bitmap imgb, float x, float y, float sx, float sy, int w, int h, int tj) {} |
43 | public void OMove() {} |
44 | public void OMove( int x, int y) {} |
45 | public Rect OgetTapRect() { return null ;} |
47 | public void Oint(Bitmap imgb, float x, float y, float sx, float sy, int w, int h) {} |
ファイル名「Mesod.java」
07 | package and.roid.shooting2; |
09 | import java.util.ArrayList; |
11 | import android.graphics.Canvas; |
12 | import android.graphics.Color; |
13 | import android.graphics.Paint; |
14 | import android.graphics.Rect; |
15 | import android.media.MediaPlayer; |
24 | static public final float XPERIA_W = 480f; |
25 | static public final float XPERIA_H = 854f; |
27 | static public final float ZERO = 0f; |
28 | private static final double PIE = 3.1415926; |
38 | public void OdrawRect(Rect rec,Canvas c){ |
40 | p.setColor(Color.RED); |
53 | public void Atarihantei(ArrayList<Object> object , int i){ |
54 | for ( int j=0;j< object .size()-1;j++){ |
55 | if (i!=j && object . get (i).obsyurui != object . get (j).obsyurui){ |
56 | if (RectRect( object . get (i).atarir, object . get (j).atarir)== true ){ |
57 | object . get (i).dead= true ; |
58 | object . get (i).bomsflg= true ; |
59 | object . get (j).dead= true ; |
66 | public boolean RectRect(Rect oa,Rect ob){ |
67 | return oa.left < ob.right && ob.left < oa.right && oa.top < ob.bottom && ob.top < oa.bottom; |
70 | public void playSound( MediaPlayer mp){ |
83 | public double toRadian( double deg){ return (deg * PIE / 180);} |
87 | public boolean RectTap( int x, int y,Rect gazou){ |
88 | return gazou.left < x && gazou.top < y && gazou.right > x && gazou.bottom > y; |
93 | public int setSizeX( float disp_w, float zahyou){ return ( int ) (zahyou * (disp_w / XPERIA_W));} |
94 | public int setSizeY( float disp_h, float zahyou){ return ( int ) (zahyou * (disp_h / XPERIA_H));} |

|