本文目錄一覽:
- 1、用java做《坦克大戰》需要積累那些java知識??
- 2、JAVA 坦克大戰
- 3、關於JAVA坦克大戰中坦克移動的問題,總有問題 不知道哪兒錯了,求高手解答
- 4、java坦克大戰源代碼 怎麼導入
- 5、java swing坦克大戰,如何實現發子彈
用java做《坦克大戰》需要積累那些java知識??
java AWT Swing 還有一些工具類 比如說Random(獲取隨機數的)一些簡單數學計算在程序中的應用 另外還需要線程來控制畫面刷新 也需要一點線程知識 另外就是一些遊戲用到的基礎知識了 比如說碰撞檢測 雙緩衝 一些paint方法的應用 如根據坦克方向將坦克畫出來等等 都比較簡單
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坦克大戰中坦克移動的問題,總有問題 不知道哪兒錯了,求高手解答
用多線程做的坦克大戰,這裡提供src目錄,運行請自己建立工程,並將src目錄下的所有包文件導入工程src目錄,main包運行;很多年前寫的希望能幫到你
java坦克大戰源代碼 怎麼導入
坦克大戰源代碼應該是個完整的項目吧。
對於完整的帶項目配置文件的java源碼,按步驟操作即可:
File – Import – General
選擇Existing Projects into Workspace,選擇要導入的文件,點擊“finish”,OK。
java swing坦克大戰,如何實現發子彈
創建子彈形狀,初始位置為坦克前方,使用循環在坦克朝向上坐標遞加或遞減,並重新繪製子彈。就能模擬子彈的行進。判斷擊中,可以用形狀是否相交的函數。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/300980.html