Java游戏开发之坦克大战代码_第1页
Java游戏开发之坦克大战代码_第2页
Java游戏开发之坦克大战代码_第3页
Java游戏开发之坦克大战代码_第4页
Java游戏开发之坦克大战代码_第5页
已阅读5页,还剩24页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Java游戏开发之坦克大战代码标题:Java游戏开发之坦克大战代码关键词:Java坦克大战,坦克大战Java代码这是一个Java坦克大战的源码,实现了大部分功能,是学习Java绘图技术的好例子主类:package com.qq.TankGame;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.Paint;import java.awt.Panel;import java.awt.Toolkit;import java.awt.eve

2、nt.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import

3、java.util.Vector;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JTextArea;/*/ * 功能:java绘图/ * author Administrator/ */ */public class TankGame extends JFrame implements ActionListener MyPane

4、l mp = null;  myStartPanel msp = null; EnemyTask em = null; JMenuBar jmb = null; JMenu jm = null; JMenuItem jmi1 = null; JMenuItem jmi2 = null; JMenuItem jmi3 = null; public static void main(String args)   TankGame tankGame = new TankGame(); 

5、60;  public TankGame()  jmb = new JMenuBar();  jm = new JMenu("游戏(G)");  jm.setMnemonic('G');  jmi1 = new JMenuItem("开始游戏(S)");   jmi1.setMnemonic('S');  jmi1.addActionListener(this);  jmi1.setActionCommand("n

6、ewGame");  jmi2 = new JMenuItem("继续游戏(O)");   jmi2.setMnemonic('O');    jmi3 = new JMenuItem("退出游戏(X)");  jmi3.setMnemonic('X');  jmi3.addActionListener(this);  jmi3.setActionCommand("exit");  &

7、#160;   /启动mp线程  /em = new EnemyTask();  /mp = new MyPanel();  msp = new myStartPanel();    this.setJMenuBar(jmb);  this.add(msp);  /this.add(mp);  jmb.add(jm);  jm.add(jmi1);  jm.add(jmi2);  jm.add(jmi3);  this.setSize(600,5

8、00);  /this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  this.setVisible(true);  /this.addKeyListener(mp);  Thread t1 = new Thread(msp);  t1.start();/  Thread  t = new Thread(mp);/  t.start();  Override public void actionPerformed(ActionEven

9、t arg0)   if(arg0.getActionCommand().equals("newGame")   mp = new MyPanel();   Thread  t = new Thread(mp);   t.start();   this.remove(msp);   this.remove(mp);   this.add(mp);   this.setVisible(true);  

10、0;this.addKeyListener(mp);   /Recoder.getRecording();     else if(arg0.getActionCommand()= "exit")   /Recoder.KeepRecording();   Recoder.KeepExit();   /KeepExit();   System.exit(0);     class myStartPanel

11、extends JPanel implements Runnable int times = 0; public void paint(Graphics g )  super.paint(g);  g.fillRect(0, 0, 400, 300);  if(times%2 =0)    g.setColor(Color.BLUE);  Font f = new Font("华文新魏", Font.BOLD, 30);  g.setFont(f);  g.draw

12、String("stage:1", 150, 150);     Override public void run()   while(true)   try     Thread.sleep(300);        catch (InterruptedException e)     e.printStackTrace();      this.repaint()

13、;   times +;   class MyPanel extends JPanel implements KeyListener,Runnable int TanksX = new int20; int TanksY = new int20; int ShotsX = new int100; int ShotsY = new int100;/ int Tanks = new int202;/ int Shots = new int202; int ETdirects = new i

14、nt20; int HeroShotsX = new int10; int HeroShotsY = new int10;  static Hero hero = null;  EnemyTask et = null; shot s = null; boolean isPaintShot = true;   int a = 0, b = 0 , c = 0; int directs = new int 10;  static /敌人坦克集合 Ve

15、ctor<EnemyTask> ets = new Vector<EnemyTask>();  int enSize = 3; /敌人坦克初始数量 int enSizes = 5; /敌人坦克画面总数量  int hintEnemyTanks = 0;/击中敌人坦克数量 public void showInfo(Graphics g )/画提示信息  /画出提出坦克  this.drawTank(80, 330, g, 0, 1);  this.drawTank(160, 330,

16、 g, 0, 0);  g.setColor(Color.BLACK);  g.drawString(Recoder.getEnNum() + "", 105, 350);  g.drawString(Recoder.getMyLife() + "", 185, 350);    /画出成绩  g.setColor(Color.BLACK);  Font f = new Font("宋体", Font.BOLD, 15);  g.setFont(

17、f);  g.drawString("你的总成绩:", 400, 20);  this.drawTank(410, 30, g, 0, 1);  g.drawString(20 - Recoder.getEnNum() + "", 435, 50);    public void paint(Graphics g)  super.paint(g);  g.fillRect(0, 0, 400, 300);  this.showInfo(g);/画出提示信息

18、    /自己坦克   if(hero.isLive = true)   this.drawTank(hero.getX(), hero.getY(), g, hero.direct, hero.type);   else    if(Recoder.getMyLife() > 0)     hero = new Hero(30, 270 ,0,0,10 , true);     hero.isLive = true;&

19、#160;         /画出敌人坦克  for(int i = 0; i <ets.size(); i+)      EnemyTask et = ets.get(i);   if(!et.isLive)    ets.remove(i);    if(Recoder.getEnNum() >0)     this.drawTank(50, 0, g, 1, 1);   

20、;  ets.add(et);     /et.isLive = true;     Thread t = new Thread(et);     t.start();        /    if(Recoder.getEnNum() > 0)/     et.isLive = true;/          if(et.isLi

21、ve)       this.drawTank(et.getX(), et.getY(),     g, et.direct, et.type);   System.out.println("et.ss.size()"+et.ss.size();    for(int j = 0; j < et.ss.size(); j+)/敌人坦克子弹          s = et.ss.get(j);

22、     if(s.isLive)      if(isPaintShot)      g.fill3DRect(s.x, s.y, 5, 10, false);           else      et.ss.remove(j);            /   if(!et.isLive)/    et

23、s.remove(i);/       /画出子弹  for(int i = 0; i< hero.ss.size(); i+)   if(hero.ss.get(i)!= null && hero.ss.get(i).isLive = true)   g.fill3DRect(hero.ss.get(i).x, hero.ss.get(i).y, 5, 10, false);         if(hero.ss.get(

24、i).isLive = false)    hero.ss.remove(hero.ss.get(i);      / public void hintTank2(shot s , Hero et)/  switch (et.direct) /  case 0:/  case 1:/   if(s.x < et.x + 20 && s.x > et.x /    && s.y < et.y + 3

25、0 && s.y > et.y)/    s.isLive = false;/    et.isLive = false;/   /   /break;/  case 2:/  case 3:/   if(s.x < et.x + 30 && s.x > et.x /    && s.y < et.y + 20 && s.y > et.y)/ 

26、   s.isLive = false;/    et.isLive = false;/   /   /break;/  default:/   break;/   /  /  public boolean hintTank(shot s , Tank et)  boolean b = false;  switch (et.direct)   case 0:  case 1:  

27、0;if(s.x < et.x + 20 && s.x > et.x     && s.y < et.y + 30 && s.y > et.y)    s.isLive = false;    et.isLive = false;    b = true;      break;  case 2:  case 3:   if(s.x < et.x +

28、 30 && s.x > et.x     && s.y < et.y + 20 && s.y > et.y)    s.isLive = false;    et.isLive = false;    b = true;      break;/  default:/   break;     return b;  pu

29、blic void drawTank (int x , int y , Graphics g , int direct , int type)  switch(type)/坦克类型   case 0:   g.setColor(Color.RED);   break;      case 1:   g.setColor(Color.BLUE);   break;     switch (direct)  

30、case 0:/向上   g.fill3DRect(x, y, 5, 30, false );   g.fill3DRect(x + 15, y, 5, 30, false );   g.fill3DRect(x + 5, y + 5, 10, 20, false );   g.fillOval(x + 5, y + 10, 10, 10);   g.drawLine(x + 10, y,x + 10, y + 15);   break;  case 1:/向下&#

31、160;  g.fill3DRect(x, y, 5, 30, false );   g.fill3DRect(x + 15, y, 5, 30, false );   g.fill3DRect(x + 5, y + 5, 10, 20, false );   g.fillOval(x + 5, y + 10, 10, 10);   g.drawLine(x + 10, y + 15,x + 10,y + 30 );   break;  case 2:/向左  &#

32、160;g.fill3DRect(x, y, 30, 5, false );   g.fill3DRect(x , y + 15, 30, 5, false );   g.fill3DRect(x + 5, y + 5, 20, 10, false );   g.fillOval(x + 10, y + 5, 10, 10);   g.drawLine(x , y + 10,x + 15,y + 10 );   break;  case 3:/向右   g.fill

33、3DRect(x, y, 30, 5, false );   g.fill3DRect(x , y + 15, 30, 5, false );   g.fill3DRect(x + 5, y + 5, 20, 10, false );   g.fillOval(x + 10, y + 5, 10, 10);   g.drawLine(x + 15,y + 10, x + 30 , y + 10);   break;     default:   

34、break;          public MyPanel ()  hero = new Hero(30, 270 ,0,0,10 , true);  /tanks = new tank(100,100, 2 ,1);  /初始化敌人坦克  Recoder.setHero(hero);  for(int i = 0; i < enSize; i+)   /创建敌人坦克的对象   Recoder.getRecording

35、();   EnemyTask et = new EnemyTask(i + 1)*50, 0, 1, 1, 5 ,true);   et.setEts(ets);   Recoder.setEtss(ets);/传ets到Recoder         Thread t = new Thread(et);   t.start();      shot s = new shot(et.x + 10 , et.y +

36、30, et.direct);   et.ss.add(s);   Thread t2 = new Thread(s);   t2.start();      ets.add(et);/加入到集合中   /et.setColor(1);    Recoder.setEnSize(enSize);   Override public void keyPressed(KeyEvent arg0)   / TOD

37、O Auto-generated method stub  if(arg0.getKeyCode() = KeyEvent.VK_S)   /hero.setX(hero.getX() + 1);/   int i= hero.getY();/   i += hero.speed; /   hero.setY(i);   this.hero.MoveDown();   hero.setD(1);    if(arg0.getKeyCod

38、e() = KeyEvent.VK_W)   /hero.setX(hero.getX() + 1);/   int i= hero.getY();/   i -= hero.speed; /   hero.setY(i);   hero.MoveUp();   hero.setD(0);    if(arg0.getKeyCode() = KeyEvent.VK_D)   /hero.setX(hero.getX() + 1

39、);/   int i= hero.getX();/   i += hero.speed; /   hero.setX(i);   hero.MoveRight();   hero.setD(3);    if(arg0.getKeyCode() = KeyEvent.VK_A)   /hero.setX(hero.getX() + 1);/   int i= hero.getX();/   i -= he

40、ro.speed; /   hero.setX(i);   hero.MoveLeft();   hero.setD(2);    /判断是否按下J  if(arg0.getKeyCode() = KeyEvent.VK_J)   if(hero.ss.size() <= 4)    this.hero.shotEnemy();   /System.out.println("j");   &

41、#160;      if(arg0.getKeyCode() = KeyEvent.VK_SPACE)/空格暂停      if(hero.speed != 0)   a = hero.speed ;   hero.speed = 0;     for(int i = 0; i < ets.size(); i +)     /System.out.println("aaaaaaaaaaaaaa");

42、     EnemyTask et = ets.get(i);     /System.out.println("speed" + et.speed);     b = et.speed;      et.speed = 0;     directsi = et.direct;     /System.out.println(et.direct);     

43、/System.out.println(directsi);     /et.direct = 0;     et.isdirects = false;     for(int j = 0 ; j < et.ss.size(); j +)      shot  s = et.ss.get(j);      c = s.speed;      s.speed = 0;    

44、0; System.out.println("aa");                    /System.out.println("b1 =" + b);    isPaintShot = false;      else    hero.speed = a;    /System.out.println("a2 =&qu

45、ot; + a);    for(int i = 0; i < ets.size(); i +)     /System.out.println("bbbbbbbbbbbbbbbbbbb");     EnemyTask et = ets.get(i);     et.isdirects = true;     et.speed = b;      et.direct = directsi;

46、     for(int j = 0 ; j < et.ss.size(); j +)      shot  s = et.ss.get(j);      s.speed = c;      isPaintShot = true;              s.isShot = true;       if(arg0.getKeyCode()

47、 = KeyEvent.VK_K)   this.KeepExit();   System.exit(0);    if(arg0.getKeyCode() = KeyEvent.VK_L)   this.GetKeeper();    this.repaint();    Override public void keyReleased(KeyEvent arg0)   / TODO Auto-generated method stu

48、b    Override public void keyTyped(KeyEvent arg0)   / TODO Auto-generated method stub    Override public void run()   /每隔100ms  while(true)   try     Thread.sleep(100);    catch (InterruptedException e) &#

49、160;   / TODO Auto-generated catch block    e.printStackTrace();       this.hintEnemyTank();    this.hintMyTank();/    for(int i = 0; i < et.ss.size(); i +)/     shot myshot = et.ss.get(i);/     if(et.isLive = tr

50、ue)/      /       if(hero.isLive = true)/        this.hintTank2(myshot, hero);/       /      /     /       this.repaint();    private void hintMyTank(

51、)     for(int i = 0; i < ets.size(); i+)   EnemyTask et = ets.get(i);   for(int j = 0; j < et.ss.size(); j+ )    shot s = et.ss.get(j);    if(s != null)     if(hero.isLive)     this.hintTank(s, hero);  &#

52、160;  if(this.hintTank(s, hero)/判断是否击中,返回true则hero数量减一      Recoder.setMyLife(Recoder.getMyLife() - 1 );                       private void hintEnemyTank()     for(int i = 0; i < hero.ss.

53、size(); i +)   shot myshot = hero.ss.get(i);   if(myshot.isLive = true)    for(int j = 0; j < ets.size(); j +)     EnemyTask et = ets.get(j);     if(et.isLive = true)      this.hintTank(myshot, et);      if(

54、this.hintTank(myshot, et)       hintEnemyTanks +;       Recoder.setEnNum(Recoder.getEnNum() - 1);                               private void KeepExit()  &#

55、160;   int heros = new int   hero.getX(),   hero.getY(),   hero.direct,     ;  for(int i = 0 ; i < ets.size(); i +)   EnemyTask et = ets.get(i);   TanksXi = et.getX();   TanksYi = et.getY();   ETdi

56、rects i = et.direct;      for(int j = 1 ; j < et.ss.size(); j+)    shot s = et.ss.get(j);    ShotsXj = s.x;    ShotsYj = s.y;       for(int i = 0 ; i < hero.ss.size(); i+ )   shot s = hero.ss.get(i);   He

57、roShotsXi = s.x ;   HeroShotsYi = s.y;         BufferedWriter bw = null;   try    bw = new BufferedWriter(new FileWriter("d:aaTanksX.txt");   for(int i = 0 ; i < enSize ; i +)    bw.write(TanksXi + "rn&

58、quot;);    bw.write(TanksYi + "rn");        catch (Exception e)    / TODO Auto-generated catch block   e.printStackTrace();  finally    try     bw.close();    catch (IOException e)     / TOD

59、O Auto-generated catch block    e.printStackTrace();         public void GetKeeper ()/继续游戏时还原数据  BufferedReader br = null ;  try     br = new BufferedReader(new FileReader("d:aaTanksX.txt");   for(int i = 0; i <

60、enSize*2 ; i +)    if(i % 2 !=0)     TanksYi = Integer.parseInt(br.readLine();    System.out.println("TanksY" + TanksYi);    else     TanksXi = Integer.parseInt(br.readLine();     System.out.println("TanksX&quo

61、t; + TanksXi);             catch (Exception e)    / TODO Auto-generated catch block   e.printStackTrace();  finally    try     br.close();    catch (IOException e)     / TODO Auto-generated catch

62、 block    e.printStackTrace();        /class tank extends Tank/ public tank(int x, int y, int direct,int type) /  super(x, y, direct,type);/  / TODO Auto-generated constructor stub/ / /下面是工具类:package com.qq.TankGame;import java.io.Buff

63、eredReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Vector;class Recoder /*  *   */ private static Vector<EnemyTask> ets = new Vector<EnemyTask&

64、gt;(); public static Vector<EnemyTask> getEts()   return ets;  public static void setEtss(Vector<EnemyTask> ets)   Recoder.ets = ets;  private static int enSize ; public static int getEnSize()   return enSize;  public static void set

65、EnSize(int enSize)   Recoder.enSize = enSize;  private static Hero hero ;  public static Hero getHero()   return hero;  public static void setHero(Hero hero)   Recoder.hero = hero;  public static void KeepExit()  /System.out.println(hero.ge

66、tX();  int TanksX = new int20;  int TanksY = new int20;  int ShotsX = new int100;  int ShotsY = new int100;  int ETdirects = new int20;  int HeroShotsX = new int10;  int HeroShotsY = new int10;      int heros = new int    hero.getX(),&

67、#160;   hero.getY(),    hero.direct          for(int i = 0 ; i < ets.size(); i +)    EnemyTask et = ets.get(i);    TanksXi = et.getX();    TanksYi = et.getY();    ETdirects i = et.direct;    &

68、#160;   for(int j = 1 ; j < et.ss.size(); j+)     shot s = et.ss.get(j);     ShotsXj = s.x;     ShotsYj = s.y;          for(int i = 0 ; i < hero.ss.size(); i+ )    shot s = hero.ss.get(i);    He

69、roShotsXi = s.x ;    HeroShotsYi = s.y;          BufferedWriter bw = null;   BufferedWriter bw1 = null ;  try          bw = new BufferedWriter(new FileWriter("d:aaTanksX.txt");   for(int i =

70、 0 ; i < enSize ; i +)    bw.write(TanksXi + "rn");    bw.write(TanksYi + "rn");      bw1 = new BufferedWriter(new FileWriter("d:aaKeepRecoding.txt");   bw1.write(20 - getEnNum() + "rn" );    

71、 catch (Exception e)    / TODO Auto-generated catch block   e.printStackTrace();  finally    try     bw1.close();    bw.close();        catch (IOException e)     / TODO Auto-generated catch block    e

72、.printStackTrace();         private static int enNum = 5;  public static int getEnNum()   return enNum;  public static void setEnNum(int enNum)   Recoder.enNum = enNum;  public static int getMyLife()   return myLife;

73、0; public static void setMyLife(int myLife)   Recoder.myLife = myLife;  private static int myLife = 3;  private static BufferedReader br = null;  public static void getRecording()  try    br = new BufferedReader(new FileReader("d:aaKeepReco

74、ding.txt");   String n ;        while(n = br.readLine() != null )     String a = n;     enNum = 20 - Integer.parseInt(n);        catch (Exception e)    / TODO Auto-generated catch block   e.

75、printStackTrace();  finally    try     br.close();    catch (IOException e)     / TODO Auto-generated catch block    e.printStackTrace();           private static BufferedWriter bw = null; public static void KeepRecording ()  try    bw = new BufferedWriter(new File

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论