坦克大战代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

private static final long serialVersionUID = 1L;
groundData o = new groundData();//地图数据,会在本线程中实时更新
int perStep = 1, sleepTime = 7;//线程的睡眠时间以及每次睡眠后坦克移动的像素
MyBullet3 enemyBullet[] = {sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,
sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,
int enemyNum = 0 , enemyMax = 6;//现有敌人数量,画面允许敌人的最大数量
boolean stuck = false ;//坦克是否无法前行
MyBullet3 bullet = new MyBullet3();//我方坦克炮弹
//敌方坦克炮弹
MyBullet3 sBullet = new MyBullet3();
g.fillOval(x + 35 , y + 15 , 10 , 10) ;
g.fillOval(x + 35 , y + 25 , 10 , 10) ;
g.fillOval(x + 35 , y + 35 , 10 , 10) ;
g.fillRect(x + 15 , y - 20 , 10 , 40) ; //炮口
g.fillOval(x + 35 , y + 5 , 10 , 10) ;
g.fillOval(x + 35 , y + 15 , 10 , 10) ;
g.fillOval(x + 35 , y + 25 , 10 , 10) ;
g.fillOval(x + 35 , y + 35 , 10 , 10) ;
g.fillOval(x - 5 , y + 25 , 10 , 10) ;
g.fillOval(x - 5 , y + 35 , 10 , 10) ;
g.drawRect(x + 35 , y - 5, 10 , 50 );
g.fillOval(x + 35 , y - 5 , 10 , 10) ;
}
public static void main(String[] args) {
Tank3 h = new Tank3("坦克大战(版本1.1)") ;
h.setVisible(true) ;
}
}
// 存储地图网格数据的类,以判断坦克碰撞,0为可行区域,大于0为不可行区域
g.fillOval(x + 25 , y+35 , 10 , 10) ;
g.fillOval(x + 35 , y+35 , 10 , 10) ;
g.fillRect(x + 20 , y + 15 , 40 , 10) ;
g.fillOval(x , y + 5 , 10 , 10) ;
//敌方坦克
EnemyTank sEnemy = new EnemyTank(2,5,5,o,-2);
EnemyTank enemy[] = {sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,
g.setColor(Color.black) ;
g.drawRect(x + 15, y + 10, 20 , 20 );
g.drawRect(x , y , 40 , 40 );
g.drawRect(x - 5, y - 5, 50 , 10 );
g.fillOval(x - 5 , y - 5 , 10 , 10) ;
sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,
sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet,sBullet};
g.fillOval(x + 5 , y + 30 , 10 , 10) ; //天线
g.fillOval(x + 25 , y + 30 , 10 , 10) ;
}// end of if ( op == 1 )
if(op == 1) {
g.fillRect(x , y , 40 , 40) ;
//new Thread(singlePlayer1).start();
MyBullet3 singlePlayer2 = new MyBullet3();
this.add( singlePlayer2 );
this.addKeyListener(singlePlayer2);
new Thread(singlePlayer2).start();
public void drawTank ( Graphics g , int op , int x ,int y,int i){
if( i == 1 ){
g.setColor(Color.yellow);//我方黄色,敌方红色
}else{
g.setColor(Color.red);
}
sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy};
//绘制坦克方法,i为坦克标号,我方为1,敌方大于1
g.fillOval(x - 5 , y + 35 , 10 , 10) ;
g.drawRect(x + 35 , y - 5, 10 , 50 );
g.fillOval(x + 35 , y - 5 , 10 , 10) ;
g.fillOval(x + 35 , y + 5 , 10 , 10) ;
g.fillRect(x + 15 , y + 20 , 10 , 40) ;
g.fillOval(x + 5 , y , 10 , 10) ;
g.fillOval(x + 25 , y , 10 , 10) ;
} //end of if ( op == 3 )
if(op == 3) {
sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,
sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,sEnemy,
this.setBackground(Color.WHITE) ;
//MyTank3 singlePlayer1 = new MyTank3();// 类Mytank3中包含所有游戏信息
//this.add( singlePlayer1 );
//this.addKeyListener(singlePlayer1);
int x = 105 , y = 355 ;//我方坦克出生点
int op = 0 ; // 0:up 1:right 2:down 3:left 方向指示
int tankStep = 0; //用于在tank移动中计数
int life = 100 , score = 0 , difficulty = 20 ; //生命,得分,难度
import java.awt.* ;
import javax.swing.* ;
import java.awt.event.*;
public class Tank3 extends JFrame {
g.fillOval(x - 5 , y - 5 , 10 , 10) ;
g.fillOval(x - 5 , y + 5 , 10 , 10) ;
g.fillOval(x - 5 , y + 15 , 10 , 10) ;
g.fillOval(x - 5 , y + 25 , 10 , 10) ;
g.fillOval(x + 5 , y - 5 , 10 , 10) ;
g.fillOval(x + 15 , y - 5 , 10 , 10) ;
g.fillOval(x + 25 , y - 5 , 10 , 10) ;
g.fillOval(x + 35 , y - 5 , 10 , 10) ;
class groundData {
volatile public int ground[][] = new int [18][18];
}
//主线程,我方坦克线程,paint线程 三合一 (这里应该更明确地对象化)
class MyTank3 extends JPanel implements KeyListener ,Runnable{
if(op == 0) {
g.fillRect(x , y , 40 , 40) ; //坦克体
g.setColor(Color.black);
g.drawRect(x + 10, y + 5, 20 , 20 );//轮廓
g.drawRect(x , y , 40 , 40 );
g.drawRect(x - 5, y - 5, 10 , 50 );//左右两侧履带
g.drawRect(x - 5 , y + 35, 50 , 10 );
g.fillOval(x - 5 , y+35 , 10 , 10) ;
g.fillOval(x + 5 , y+35 , 10 , 10) ;
g.fillOval(x + 15 , y+35 , 10 , 10) ;
g.fillOval(x , y + 25 , 10 , 10) ;
}// end of if (op==2)
if(op == 2) {
g.fillRect(x , y , 40 , 40) ;
g.setColor(Color.black) ;
g.drawRect(x + 10, y + 15, 20 , 20 );
private static final long serialVersionUID = 1L;
Tank3( String title ){ //重构
this.setTitle(title) ; //窗体名称
this.setSize(415 , 500) ; //窗体大小
this.setLocation(300 , 300) ; //窗体位置
g.drawRect(x , y , 40 , 40 );
g.drawRect(x - 5, y - 5, 10 , 50 );
g.fillOval(x - 5 , y - 5 , 10 , 10) ;
g.fillOval(x - 5 , y + 5 , 10 , 10) ;
g.fillOval(x - 5 , y + 15 , 10 , 10) ;
相关文档
最新文档