本文目錄一覽:
- 1、用java做《坦克大戰》需要積累那些java知識??
- 2、在java編寫坦克大戰遊戲時,如何判斷兩輛坦克不能重疊運動,有什麼簡單的演算法
- 3、JAVA 坦克大戰
- 4、我用java做一個坦克大戰的小遊戲,怎麼實現遊戲暫停和繼續的功能,敵人的坦克還有子彈類是線程
用java做《坦克大戰》需要積累那些java知識??
java AWT Swing 還有一些工具類 比如說Random(獲取隨機數的)一些簡單數學計算在程序中的應用 另外還需要線程來控制畫面刷新 也需要一點線程知識 另外就是一些遊戲用到的基礎知識了 比如說碰撞檢測 雙緩衝 一些paint方法的應用 如根據坦克方向將坦克畫出來等等 都比較簡單
在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 坦克大戰
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做一個坦克大戰的小遊戲,怎麼實現遊戲暫停和繼續的功能,敵人的坦克還有子彈類是線程
線程設置sleep阻塞,或則使用Util.Timer類和TimerTask來實現暫停功能,繼續可以用interupt中斷阻塞,我是這樣想的,不知道正不正確,希望對你有所幫助。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/246737.html