導航:首頁 > 編程語言 > java坦克代碼

java坦克代碼

發布時間:2022-08-01 04:48:18

『壹』 java坦克函數

System.out.println("坦克移動");
System.out.println("坦克停止");
System.out.println("坦克左轉");
System.out.println("坦克右轉");

『貳』 求坦克JAVA代碼

package com.tankgame;
import java.util.Vector;
//坦克類
class Tank
{
int x=0;
int y=0;
int color=0;
int speed=1;
int direct=0;
boolean isLive=true;
public Tank(int x,int y)
{
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}

『叄』 JAVA 坦克大戰

importjava.awt.*;
importjavax.swing.*;

publicclassTankextendsJFrame{
mypanemp=null;
Obj[]objs=newObj[0];

publicTank(){
setTitle("坦克大戰");
setSize(800,600);
pro();
add(newmypane(objs));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

//在這里添加鍵盤事件、滑鼠事件、讓坦克移動,修改objs數組對象讓他們移動


setVisible(true);
}

privatevoidpro(){

Obj[]tmp=newObj[objs.length+1];
System.array(objs,0,tmp,0,objs.length);
tmp[tmp.length-1]=newObj(1,1,0,1);
objs=tmp;

intnum=(int)(Math.random()*5)+1;
for(inti=0;i<num;i++){
intx=(int)(Math.random()*getWidth())+1;
inty=(int)(Math.random()*getHeight())+1;
intdir=(int)(Math.random()*4);

Obj[]dst=newObj[objs.length+1];
System.array(objs,0,dst,0,objs.length);
dst[dst.length-1]=newObj(x,y,1,dir);
objs=dst;
}
}

publicstaticvoidmain(String[]args){
newTank();
}
}

classObj{
intx,y;//坦克坐標
inttype;
intdir;
publicObj(intx,inty,inttype,intdir){
this.x=x;
this.y=y;
this.type=type;
this.dir=dir;
}
}

classmypaneextendsJPanel{
Obj[]objs;
publicmypane(Obj[]objs){
this.objs=objs;
}
publicvoidpaint(Graphicsg){
super.paint(g);
for(inti=0;i<objs.length;i++){
Objobj=objs[i];
drawtank(obj.x,obj.y,g,obj.type,obj.dir);
}
g.dispose();
}
publicvoiddrawtank(intx,inty,Graphicsg,inttype,intdirect){
/*type為坦克類型,敵方,我方*/
switch(type){
case0://我方坦克,設置為紅色
g.setColor(Color.red);
break;
case1://敵方坦克,設置為藍色
g.setColor(Color.blue);
break;
}
switch(direct){
case0://坦克方向朝上
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;
case1://坦克方向朝右
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;
case2://方向向下
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;
case3://方向向左
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的draw方法里的內容代碼和碰撞檢測代碼。

一般二維碰撞檢測基本都是以單位格,也就是坦克的大小格作為單位,提前一格判斷兩個方格是否交叉,如果交叉則在當前,也就是交叉後退後一格這個位置讓它停止當前方向的移動。

『伍』 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(Y<0)Y=0;
break;
case 2:
X+=10;
break;
case 3:
Y+=10;
break;
case 4:
X-=10;
if(X<0)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寫的坦克大戰雙人版的代碼

我有,不過在家裡

『柒』 java坦克大戰源代碼 怎麼導入

坦克大戰源代碼應該是個完整的項目吧。
對於完整的帶項目配置文件的java源碼,按步驟操作即可:
File -> Import -> General
選擇Existing Projects into Workspace,選擇要導入的文件,點擊「finish",OK。

『捌』 關於java坦克大戰里的一個小問題,這是其中的一部分代碼

this.barrelDir = this.dir;

假設坦克向右行駛,這時——
this.barrelDir = this.dir = 「R」

它們的值都指向了R。

當坦克停止時,校驗
if(this.dir != Direction.STOP)
無法通過,所以不會重置 barrelDir 的值,也就是說它仍然等於R。

這時你發射子彈,barrelDir 值不為STOP,所以它就可以往右邊飛出去了。

『玖』 java 「坦克大戰」代碼問題

int speed=1;
hero.setY(hero.getY()-speed);

『拾』 (100分)Java寫「坦克大戰」

package com.bjsxt.tank;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
* 這個類的作用是坦克游戲的主窗口
* @author mashibing
*
*/

public class TankClient extends Frame {
/**
* 整個坦克游戲的寬度
*/
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;

Tank myTank = new Tank(50, 50, true, Direction.STOP, this);

Wall w1 = new Wall(100, 200, 20, 150, this), w2 = new Wall(300, 100, 300, 20, this);

List<Explode> explodes = new ArrayList<Explode>();
List<Missile> missiles = new ArrayList<Missile>();
List<Tank> tanks = new ArrayList<Tank>();
Image offScreenImage = null;

Blood b = new Blood();

public void paint(Graphics g) {
/*
* 指明子彈-爆炸-坦克的數量
* 以及坦克的生命值
*/
g.drawString("missiles count:" + missiles.size(), 10, 50);
g.drawString("explodes count:" + explodes.size(), 10, 70);
g.drawString("tanks count:" + tanks.size(), 10, 90);
g.drawString("tanks life:" + myTank.getLife(), 10, 110);

if(tanks.size() <= 0) {
for(int i=0; i<Integer.parseInt(PropertyMgr.getProperty("reProceTankCount")); i++) {
tanks.add(new Tank(50 + 40*(i+1), 50, false, Direction.D, this));
}
}

for(int i=0; i<missiles.size(); i++) {
Missile m = missiles.get(i);
m.hitTanks(tanks);
m.hitTank(myTank);
m.hitWall(w1);
m.hitWall(w2);
m.draw(g);
//if(!m.isLive()) missiles.remove(m);
//else m.draw(g);
}

for(int i=0; i<explodes.size(); i++) {
Explode e = explodes.get(i);
e.draw(g);
}

for(int i=0; i<tanks.size(); i++) {
Tank t = tanks.get(i);
t.collidesWithWall(w1);
t.collidesWithWall(w2);
t.collidesWithTanks(tanks);
t.draw(g);
}

myTank.draw(g);
myTank.eat(b);
w1.draw(g);
w2.draw(g);
b.draw(g);
}

public void update(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.BLACK);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}

/**
* 本方法顯示坦克主窗口
*
*/
public void lauchFrame() {

int initTankCount = Integer.parseInt(PropertyMgr.getProperty("initTankCount"));
for(int i=0; i<initTankCount; i++) {
tanks.add(new Tank(50 + 40*(i+1), 50, false, Direction.D, this));
}

//this.setLocation(400, 300);
this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setTitle("TankWar");
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setResizable(false);
this.setBackground(Color.GREEN);

this.addKeyListener(new KeyMonitor());

setVisible(true);

new Thread(new PaintThread()).start();
}

public static void main(String[] args) {
TankClient tc = new TankClient();
tc.lauchFrame();
}

private class PaintThread implements Runnable {

public void run() {
while(true) {
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

private class KeyMonitor extends KeyAdapter {

public void keyReleased(KeyEvent e) {
myTank.keyReleased(e);
}

public void keyPressed(KeyEvent e) {
myTank.keyPressed(e);
}

}
}

閱讀全文

與java坦克代碼相關的資料

熱點內容
樹莓派都用python不用c 瀏覽:755
access文件夾樹的構造 瀏覽:660
安卓多指操作怎麼設置 瀏覽:656
linux樹形目錄 瀏覽:727
平方根的簡單演算法 瀏覽:898
千牛訂單頁面信息加密取消 瀏覽:558
單片機自製紅外遙控燈 瀏覽:719
伺服器最小配置怎麼弄 瀏覽:853
ibm伺服器硬體如何升級 瀏覽:923
全球程序員節點贊 瀏覽:986
php函數傳遞數組 瀏覽:631
人工峰群演算法的目標函數 瀏覽:468
如何刪加密文檔 瀏覽:105
塗鴉app一鍵執行如何刪除 瀏覽:756
安卓手機如何打開fr3文件 瀏覽:743
壓縮袋8絲和14絲是什麼意思 瀏覽:647
程序員大咖java 瀏覽:70
蘋果手機文檔安卓上怎麼打開 瀏覽:527
如何做淘寶代理伺服器 瀏覽:672
gz壓縮文件夾 瀏覽:179