首页 > 编程知识 正文

包含java实现经典游戏坦克大战的词条

时间:2023-12-13 14:15:37 阅读:315277 作者:AFPT

本文目录一览:

java 坦克大战 相关思路 或者是代码 想学学思路

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(Y0)Y=0;

break;

case 2:

X+=10;

break;

case 3:

Y+=10;

break;

case 4:

X-=10;

if(X0)X=0;

break;

}

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编写坦克大战游戏时,如何判断两辆坦克不能重叠运动,有什么简单的算法

对于这个小游里面的类的抽象很重要,对坦克及其它类我在这里面就不定义了,其实J2SE的API里面就有关于图形重叠的算法,就是这个intersects()方法,具体伪代码如下:

public boolean collidesWithTanks(java.util.ListTank tanks) {

for(int i=0; itanks.size(); i++) {

Tank t = tanks.get(i);

if(this != t) {

if(this.live t.isLive() this.getRect().intersects(t.getRect())) {

this.stay();

t.stay();

return true;

}

}

}

return false;

}

您可以根据自己的实际需求来改写,在我的百度文库里面有关于“坦克大战”的所有代码,如果有需要我可以把代码发给你,可以通过百度HI联系我。

一款java游戏主角是四个人可以开装甲车

一款java游戏主角是四个人可以开装甲车是坦克大战。

《坦克大战》是由日本南梦宫Namco游戏公司开发的一款平面射击游戏,于1985年发售。游戏以坦克战斗及保卫基地为主题,属于策略型联机类。

java swing坦克大战,如何实现发子弹

创建子弹形状,初始位置为坦克前方,使用循环在坦克朝向上坐标递加或递减,并重新绘制子弹。就能模拟子弹的行进。判断击中,可以用形状是否相交的函数。

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));

setDefaultCloseOperation(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;inum;i++){

int x=(int)(Math.random()*getWidth())+1;

int y=(int)(Math.random()*getHeight())+1;

int dir=(int)(Math.random()*4);

Obj[] dst=new Obj[objs.length+1];

System.arraycopy(objs,0,dst,0,objs.length);

dst[dst.length-1]=new Obj(x,y,1,dir);

objs=dst;

}

}

public static void main(String[] args) {

new Tank();

}

}

class Obj{

int x,y;//坦克坐标

int type;

int dir;

public Obj(int x,int y,int type,int dir){

this.x=x;

this.y=y;

this.type=type;

this.dir=dir;

}

}

class mypane extends JPanel{

Obj[] objs;

public mypane(Obj[] objs){

this.objs=objs;

}

public void paint(Graphics g) {

super.paint(g);

for(int i=0;iobjs.length;i++){

Obj obj=objs[i];

drawtank(obj.x,obj.y, g, obj.type, obj.dir);

}

g.dispose();

}

public void drawtank(int x,int y,Graphics g, int type,int direct) {

/*type 为坦克类型,敌方,我方*/

switch(type) {

case 0://我方坦克,设置为红色

g.setColor(Color.red);

break;

case 1://敌方坦克,设置为蓝色

g.setColor(Color.blue);

break;

}

switch(direct) {

case 0://坦克方向朝上

g.drawRect(0+x, 0+y, 5, 30);

g.drawRect(5+x, 5+y, 10,20);

g.drawRect(15+x,0+y, 5,30);

g.drawLine(10+x, 15+y, 10+10+x, 15+y);

break;

case 1://坦克方向朝右

g.drawRect(0+x, 0+y, 30, 5);

g.drawRect(5+x, 5+y, 20, 10);

g.drawRect(0+x, 15+y, 30, 5);

g.drawLine(15+x, 10+y, 30+15+x, 10+10+y);

break;

case 2://方向向下

g.drawRect(0+x, 0+y, 5, 30);

g.drawRect(5+x, 5+y, 10,20);

g.drawRect(15+x,0+y, 5,30);

g.drawLine(10+x, 15+y, 10+10+x, 30+15+y);

break;

case 3://方向向左

g.drawRect(0+x, 0+y, 30, 5);

g.drawRect(5+x, 5+y, 20, 10);

g.drawRect(0+x, 15+y, 30, 5);

g.drawLine(15+x, 10+y, 15+x, 10+10+y);

break;

}

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。