包含超级坦克大战java的词条

博主:adminadmin 2022-12-13 04:12:07 73

今天给各位分享超级坦克大战java的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

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做《坦克大战》需要积累那些java知识??

java AWT Swing 还有一些工具类 比如说Random(获取随机数的)一些简单数学计算在程序中的应用 另外还需要线程来控制画面刷新 也需要一点线程知识 另外就是一些游戏用到的基础知识了 比如说碰撞检测 双缓冲 一些paint方法的应用 如根据坦克方向将坦克画出来等等 都比较简单

用Java做坦克大战暂停我出了个大问题

你应该用wait方法来停止线程的执行

或者是你的循环里在暂停的时候没有做延时,所以死循环了

还有一种方法,你可以在暂停的时候记录子弹和坦克的信息保存起来,然后结束掉线程,这样做你还可以实现保存的功能

超级坦克大战java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、超级坦克大战java的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-13,除非注明,否则均为首码项目网原创文章,转载请注明出处。