⑴ 誰有java編寫的坦克大戰源代碼,發一份給我。
坦克大戰源代碼應該是個完整的項目吧。對於完整的帶項目配置文件的java源碼,按步驟操作即可:File->Import->General選擇ExistingProjectsintoWorkspace,選擇要導入的文件,點擊「finish",OK。
⑵ java版本坦克大戰源代碼
給你一個猜數字游戲代碼。辛辛苦苦打的,希望採納。謝謝。 package caishuzi.java; import javax.swing.JOptionPane; public class caishuzi { public static void main (String args[ ]) { JOptionPane.showMessageDialog(null,"給你一個1至100...
⑶ 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;
}
}
}
⑷ (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坦克大戰源代碼 怎麼導入
坦克大戰源代碼應該是個完整的項目吧。
對於完整的帶項目配置文件的java源碼,按步驟操作即可:
File -> Import -> General
選擇Existing Projects into Workspace,選擇要導入的文件,點擊「finish",OK。
⑹ java 編寫坦克大戰
需要看你的tank的draw方法里的內容代碼和碰撞檢測代碼。
一般二維碰撞檢測基本都是以單位格,也就是坦克的大小格作為單位,提前一格判斷兩個方格是否交叉,如果交叉則在當前,也就是交叉後退後一格這個位置讓它停止當前方向的移動。
⑺ JAVA坦克大戰,這段代碼為什麼子彈的坐標在變,卻不能repaint,但是按下任意鍵盤的建卻重繪了呢
Mypanel的 run方法里要調用repaint方法 否則你的repaint方法只會在keyPressed發生的時候才調用
修改一下兩個地方
(1)
// 鍵盤獲取事件的函數
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getKeyCode() == KeyEvent.VK_J) {
if (hero.shot.size() < 5) {
hero.shott();
}
}
if (arg0.getKeyCode() == KeyEvent.VK_W) {
hero.setSDC(hero.getSpeed(), 0, hero.getColor());
hero.moveUp();
} else if (arg0.getKeyCode() == KeyEvent.VK_S) {
hero.setSDC(hero.getSpeed(), 1, hero.getColor());
hero.moveDown();
} else if (arg0.getKeyCode() == KeyEvent.VK_A) {
hero.setSDC(hero.getSpeed(), 2, hero.getColor());
hero.moveLeft();
} else if (arg0.getKeyCode() == KeyEvent.VK_D) {
hero.setSDC(hero.getSpeed(), 3, hero.getColor());
hero.moveRight();
}
/**
* 這個repaint注釋掉
*/
//this.repaint();
}
(2)
// 線程
/**
* 一秒鍾60幀
*/
public void run() {
// TODO Auto-generated method stub
while(true){
this.repaint();
try {
Thread.sleep(1000 / 60);
} catch (InterruptedException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
}
}
完整代碼如下:
importjava.awt.*;
importjavax.swing.*;
importjava.util.*;
importjava.awt.event.*;
publicclassaaaextendsJFrame{
publicstaticvoidmain(String[]args){
aaaa1=newaaa();
Threadt1=newThread(a1.mp);
t1.start();
}
MyPanelmp=null;
publicaaa(){
mp=newMyPanel();
this.add(mp);
this.addKeyListener(mp);
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
,Runnable{
MyTankhero=null;
Vector<EmenyTank>emeny=newVector<EmenyTank>();
intemsize=5;
//鍵盤獲取事件的函數
publicvoidkeyPressed(KeyEventarg0){
//TODOAuto-generatedmethodstub
if(arg0.getKeyCode()==KeyEvent.VK_J){
if(hero.shot.size()<5){
hero.shott();
}
}
if(arg0.getKeyCode()==KeyEvent.VK_W){
hero.setSDC(hero.getSpeed(),0,hero.getColor());
hero.moveUp();
}elseif(arg0.getKeyCode()==KeyEvent.VK_S){
hero.setSDC(hero.getSpeed(),1,hero.getColor());
hero.moveDown();
}elseif(arg0.getKeyCode()==KeyEvent.VK_A){
hero.setSDC(hero.getSpeed(),2,hero.getColor());
hero.moveLeft();
}elseif(arg0.getKeyCode()==KeyEvent.VK_D){
hero.setSDC(hero.getSpeed(),3,hero.getColor());
hero.moveRight();
}
/**
*這個repaint注釋掉
*/
//this.repaint();
}
publicvoidkeyReleased(KeyEventarg0){
//TODOAuto-generatedmethodstub
}
publicvoidkeyTyped(KeyEventarg0){
//TODOAuto-generatedmethodstub
}
//完畢
publicMyPanel(){
hero=newMyTank(250,250);
hero.setSDC(5,2,2);
for(inti=0;i<emsize;++i){
EmenyTankem=newEmenyTank((i+1)*60,20);
em.setSDC(5,1,1);
emeny.add(em);
}
}
//線程
/**
*一秒鍾60幀
*/
publicvoidrun(){
//TODOAuto-generatedmethodstub
while(true){
this.repaint();
try{
Thread.sleep(1000/60);
}catch(InterruptedExceptione){
//TODO自動生成的catch塊
e.printStackTrace();
}
}
}
publicvoidpaint(Graphicsg){
super.paint(g);
//畫板,坦克得放在畫板後頭
g.fillRect(0,0,400,400);
//paint敵人坦克
for(inti=0;i<emeny.size();++i){
EmenyTankem=null;
em=emeny.get(i);
this.drawTank(em.getX(),em.getY(),g,em.getDirect(),
em.getColor());
}
//畫我自己的坦克
this.drawTank(hero.getX(),hero.getY(),g,hero.getDirect(),
hero.getColor());
//畫出我的子彈
for(inti=0;i<hero.shot.size();i++){
ShotmyShot=hero.shot.get(i);
if(myShot!=null&&myShot.live==true){
g.draw3DRect(myShot.x,myShot.y,2,2,false);
}
if(myShot.live==false){
hero.shot.remove(myShot);
}
}
}
publicvoiddrawTank(intx,inty,Graphicsg,intdirect,intcolor){
//判斷坦克的顏色(敵我)然後畫出坦克
switch(color){
case0:
g.setColor(Color.BLUE);
break;
case1:
g.setColor(Color.YELLOW);
break;
case2:
g.setColor(Color.GREEN);
break;
}
//判斷坦克的方向然後再畫出坦克
switch(direct){
case0:
g.fill3DRect(x,y,10,30,false);
g.fill3DRect(x+26,y,10,30,false);
g.fill3DRect(x+10,y+5,16,20,false);
g.drawLine(x+18,y+15,x+18,y);
break;
case1:
g.fill3DRect(x,y,10,30,false);
g.fill3DRect(x+26,y,10,30,false);
g.fill3DRect(x+10,y+5,16,20,false);
g.drawLine(x+18,y+15,x+18,y+30);
break;
case2:
g.fill3DRect(x+3,y-3,30,10,false);
g.fill3DRect(x+3,y+23,30,10,false);
g.fill3DRect(x+8,y+7,20,16,false);
g.drawLine(x+18,y+15,x+3,y+15);
break;
case3:
g.fill3DRect(x+3,y-3,30,10,false);
g.fill3DRect(x+3,y+23,30,10,false);
g.fill3DRect(x+8,y+7,20,16,false);
g.drawLine(x+18,y+15,x+33,y+15);
break;
}
}
}
{
publicEmenyTank(intx,inty){
//TODOAuto-generatedmethodstub
super(x,y);
}
publicvoidrun(){
}
}
classShotimplementsRunnable{
protectedintx;
protectedinty;
protectedintdirect;
protectedintspeed=4;
protectedbooleanlive=true;
publicvoidsetX(intx){
this.x=x;
this.y=y;
}
publicintgetX(){
returnx;
}
publicintgetY(){
returny;
}
publicvoidsetDirect(intdirect){
this.direct=direct;
}
publicintgetDirect(){
returndirect;
}
publicvoidsetSpeed(intspeed){
this.speed=speed;
}
publicintgetSpeed(){
returnspeed;
}
//子彈的上下左右以及走的速度
publicvoidrun(){
//TODOAuto-generatedmethodstub
while(true){
try{
Thread.sleep(100);
}catch(Exceptione){
}
switch(direct){
case0:
y-=speed;
break;
case1:
y+=speed;
break;
case2:
x-=speed;
break;
case3:
x+=speed;
break;
}
if(x>400||x<0||y>400||y<0){
this.live=false;
break;
}
}
}
}
classTank{
protectedintx;
protectedinty;
protectedintspeed=5;
protectedintdirect;
protectedintcolor;
booleanlive;
publicTank(intx,inty){
this.x=x;
this.y=y;
}
publicintgetX(){
returnx;
}
publicintgetY(){
returny;
}
publicvoidsetSDC(intspeed,intdirect,intcolor){
this.speed=speed;
this.direct=direct;
this.color=color;
}
publicintgetSpeed(){
returnspeed;
}
publicintgetDirect(){
returndirect;
}
publicintgetColor(){
returncolor;
}
}
classMyTankextendsTank{
publicMyTank(intx,inty){
//TODOAuto-generatedmethodstub
super(x,y);
}
Vector<Shot>shot=newVector<Shot>();
Shotshota=null;
publicvoidshott(){
switch(this.direct){
case0:
shota=newShot();
shota.x=x+18;
shota.y=y;
shota.direct=0;
shot.add(shota);
break;
case1:
shota=newShot();
shota.x=x+18;
shota.y=y+30;
shota.direct=1;
shot.add(shota);
break;
case2:
shota=newShot();
shota.x=x+3;
shota.y=y+15;
shota.direct=2;
shot.add(shota);
break;
case3:
shota=newShot();
shota.x=x+33;
shota.y=y+15;
shota.direct=3;
shot.add(shota);
break;
}
Threadt=newThread(shota);
t.start();
}
publicvoidmoveUp(){
if(y>0){
y-=speed;
}
}//我的坦克得在自己的類里定義怎麼移動
publicvoidmoveDown(){
if(y<367){
y+=speed;
}
}
publicvoidmoveLeft(){
if(x>0){
x-=speed;
}
}
publicvoidmoveRight(){
if(x<365){
x+=speed;
}
}
}
⑻ java 坦克大戰 一段代碼 關於圖像繪制 不懂 求解
這個是雙緩沖的繪制方法
if (offScreenImage == null)
{
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
這里是先在內存中創建一個 offScreenImage 的緩沖圖像
gOffScreen.drawImage(imgs, 0, 0, GAME_WIDTH, GAME_HEIGHT,null);
這里是將背景圖片繪制到剛才創建的這塊緩沖區上
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
這里是將緩沖區再繪制到實際屏幕上
採用雙緩沖可以保證畫面不會閃爍 因為圖片的刷新都是發生在內存區上的 實際屏幕上用戶是感覺不出來的 所以人眼感覺不到閃爍
⑼ 在java編寫坦克大戰游戲時,如何判斷兩輛坦克不能重疊運動,有什麼簡單的演算法
對於這個小游裡面的類的抽象很重要,對坦克及其它類我在這裡面就不定義了,其實J2SE的API裡面就有關於圖形重疊的演算法,就是這個intersects()方法,具體偽代碼如下:
public boolean collidesWithTanks(java.util.List<Tank> tanks) {
for(int i=0; i<tanks.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坦克大戰里的一個小問題,這是其中的一部分代碼
this.barrelDir = this.dir;
假設坦克向右行駛,這時——
this.barrelDir = this.dir = 「R」
它們的值都指向了R。
當坦克停止時,校驗
if(this.dir != Direction.STOP)
無法通過,所以不會重置 barrelDir 的值,也就是說它仍然等於R。
這時你發射子彈,barrelDir 值不為STOP,所以它就可以往右邊飛出去了。