1.JAVA 坦克大战
import java.awt.*;import javax.swing.*;public class Tank extends JFrame {mypane mp=null;Obj[] objs=new Obj[0];public Tank() {setTitle("坦克大战");setSize(800,600);pro();add(new mypane(objs));(JFrame.EXIT_ON_CLOSE);setLocationRelativeTo(null);//在这里添加键盘事件、鼠标事件、让坦克移动,修改objs数组对象让他们移动setVisible(true);}private void pro(){Obj[] tmp=new Obj[objs.length+1];System.arraycopy(objs,0,tmp,0,objs.length);tmp[tmp.length-1]=new Obj(1,1,0,1);objs=tmp;int num=(int)(Math.random()*5)+1;for(int i=0;i import java.awt.*;import javax.swing.*;public class Tank extends JFrame {mypane mp=null;Obj[] objs=new Obj[0];public Tank() {setTitle("坦克大战");setSize(800,600);pro();add(new mypane(objs));(JFrame.EXIT_ON_CLOSE);setLocationRelativeTo(null);//在这里添加键盘事件、鼠标事件、让坦克移动,修改objs数组对象让他们移动setVisible(true);}private void pro(){Obj[] tmp=new Obj[objs.length+1];System.arraycopy(objs,0,tmp,0,objs.length);tmp[tmp.length-1]=new Obj(1,1,0,1);objs=tmp;int num=(int)(Math.random()*5)+1;for(int i=0;i public class MainActivity extends Activity {/** Called when the activity is first created. */ items it; ViewGroup.LayoutParams pa = new ViewGroup.MarginLayoutParams(-2, -2); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); it = new items(MainActivity.this); AutomaticRoob ab = new AutomaticRoob(MainActivity.this); this.addContentView(it, pa); this.addContentView(ab, pa); Servicer s =new Servicer(ab); s.start(); AutomaticRoob ab2 = new AutomaticRoob(MainActivity.this); this.addContentView(ab2, pa); Servicer s2 =new Servicer(ab2); s2.start(); AutomaticRoob ab3 = new AutomaticRoob(MainActivity.this); this.addContentView(ab3, pa); new Thread(ab3).start();// Servicer s3 =new Servicer(ab3);// s3.start(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) {// TODO Auto-generated method stub Log.i("info",keyCode+""); switch(keyCode){ case 19:it.move(1); break; case 20:it.move(3); break; case 21:it.move(4); break; case 22:it.move(2); break; } return super.onKeyDown(keyCode, event); } }//----------------------------------------用户坦克------------------------------------- public class items extends View { public int Img=R.drawable.icon; public int X; public int Y; public int type; Handler hand; Bitmap bit; Context context; public items(Context context) { super(context); this.context=context; Init();// TODO Auto-generated constructor stub } public void setType(int type){ this.type = type; Init(); } public void move(int x){ switch(x){ case 1:this.Y-=10; break; case 2:this.X+=10; break; case 3:this.Y+=10; break; case 4:this.X-=10; break; } this.postInvalidate(); } private void Init() {// TODO Auto-generated method stub bit = BitmapFactory.decodeResource(context.getResources(), Img); this.postInvalidate(); } @Override protected void onDraw(Canvas canvas) {// TODO Auto-generated method stub super.onDraw(canvas); canvas.drawBitmap(bit, X, Y, null); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// TODO Auto-generated method stub super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }//---------------------------------机器人坦克------------------------------------------ public class AutomaticRoob extends View implements Runnable{ Context context; public int X=0; public int Y=0; public int Img=R.drawable.icon; Bitmap bit; Random r ; public AutomaticRoob(Context context) { super(context); r = new Random(); bit = BitmapFactory.decodeResource(context.getResources(), Img); } @Override protected void onDraw(Canvas canvas) {// TODO Auto-generated method stub super.onDraw(canvas); canvas.drawBitmap(bit, X, Y, null); } int temp; int Direction=3; boolean mark=true; public void run() { while(true){ temp =r.nextInt(2)+1; switch(temp){ case 1:switch(Direction){ case 1:Y-=10; if(Ybreak; case 2:X+=10; break; case 3:Y+=10; break; case 4:X-=10; if(Xbreak; } this.postInvalidate(); break; case 2:Direction=r.nextInt(4)+1; break; } try { new Thread().sleep(200); } catch (InterruptedException e) {// TODO Auto-generated catch block e.printStackTrace(); } } } } 这是android的。 思路差不多。 自己看看。 转载请注明出处众文网 » java坦克大战毕业论文2.JAVA 坦克大战
3.java 坦克大战 相关思路 或者是代码 想学学思路