java坦克dazhan的简单介绍
今天给各位分享java坦克dazhan的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
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;
}
}
}
java,坦克大战,坦克碰到一起离开不了了,怎么改啊,
出现这个问题的原因是,程序在作出两者重叠为真的判断后,后续的动作没能改变这个问题,所以这是一个死循环,也就是碰撞之后,无论怎么动,tank重叠的判断一直为真。
从题目看你的tank是正方形的,但是你在判断是否碰撞时只用了其中的一个点这是不对的,应该用当前tank运动方向上最前面的那根线去判断是否碰撞。即
case 0: if (this.x=dt.xthis.x=dt.x+30this.y=dt.ythis.y=dt.y+30)return true;
if(this.x+30=dt.xthis.x+30=dt.x+30this.y=dt.ythis.y=dt.y+30)return true;
依次类推。。。。
关于JAVA坦克大战中坦克移动的问题,总有问题 不知道哪儿错了,求高手解答
用多线程做的坦克大战,这里提供src目录,运行请自己建立工程,并将src目录下的所有包文件导入工程src目录,main包运行;很多年前写的希望能帮到你
java坦克dazhan的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、java坦克dazhan的信息别忘了在本站进行查找喔。
发布于:2022-12-13,除非注明,否则均为
原创文章,转载请注明出处。