本文目錄一覽:
- 1、java 坦克大戰 相關思路 或者是代碼 想學學思路
- 2、在java編寫坦克大戰遊戲時,如何判斷兩輛坦克不能重疊運動,有什麼簡單的演算法
- 3、一款java遊戲主角是四個人可以開裝甲車
- 4、java swing坦克大戰,如何實現發子彈
- 5、JAVA 坦克大戰
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;
}
}
}
原創文章,作者:NCHT5,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/129485.html