韩顺平java坦克大战1.0版本_源代码

合集下载

坦克大战程序代码

坦克大战程序代码

import java.awt.* ;import javax.swing.* ;import java.awt.event.*;import java.util.EventListener;class f extends JFrame {f(String title) {this.setTitle(title) ;this.setSize(608 , 630) ;this.setLocation(300 , 100) ;this.setBackground(Color.BLACK) ;MyTank mp = new MyTank() ;this.add(mp) ;this.addKeyListener(mp) ;new Thread(mp).start() ;}public static void main(String[] args) {f h = new f("坦克大战(版本1.0)") ;h.setVisible(true) ;}}//主战坦克class MyTank extends JPanel implements KeyListener , Runnable {int x = 280, y = 280 ;//坦克的初始位置int op = 1 ;//坦克的移动方向int color = 0 ;int tankspeed = 8 ;//坦克的速度int tankbullet = 8 ;//坦克的子弹速度int tankfbullet = 4 ;//敌军的子弹速度int shengming = 100 ;//生命int fenshu = 0 ;int nandu = 5 ; //设置游戏难度//子弹int dx = 295 , dy = 295 ;int dx1 = 295 , dy1 = -10 ;int dx2 = 600 , dy2 = 295 ;int dx3 = 295 , dy3 = 600 ;int dx4 = -10 , dy4 = 295 ;//敌军坦克int num = 10 ;//敌军坦克数量,不能修改int[] xf = new int[num] ;int[] yf = new int[num] ;int[] opf = new int[num] ;int[] dxf = new int[num] ;int[] dyf = new int[num] ;int[] dxf1 = new int[num] ;int[] dyf1 = new int[num] ;int[] dxf2 = new int[num] ;int[] dyf2 = new int[num] ;int[] dxf3 = new int[num] ;int[] dyf3 = new int[num] ;int[] dxf4 = new int[num] ;int[] dyf4 = new int[num] ;//构造函数,初始化敌军坦克的位置和状态MyTank() {for (int i = 0; i<num; i++) {xf[i] = (int) (Math.random() * 560) ; yf[i] = (int) (Math.random() * 560) ; dxf[i] = xf[i] + 15 ;dyf[i] = yf[i] + 15 ;}for (int i = 0; i<num; i++) {dxf1[i] = 295 ; dyf1[i] = -10 ;dxf2[i] = 600 ; dyf2[i] = 295 ;dxf3[i] = 295 ; dyf3[i] = 600 ;dxf4[i] = -10 ; dyf4[i] = 295 ;}}//主面版public void paint(Graphics g) { super.paint(g) ;this.setBackground(Color.YELLOW) ;g.setColor(Color.red) ;g.drawString("生命:" , 10 , 20 ) ;g.fillRect(50 , 10 , shengming * 5 , 10) ;g.drawRect(50 , 10 , 500 , 10) ;g.drawString("得分: "+ fenshu , 10 , 40) ;if(op == 1) {g.setColor(Color.red) ;g.fillRect(x , y , 40 , 40) ;switch (color % 6) {case 0: g.setColor(Color.blue) ; break; case 1: g.setColor(Color.yellow) ; break; case 2: g.setColor(Color.red) ; break;case 3: g.setColor(Color.orange) ; break; case 4: g.setColor(Color.green) ; break; case 5: g.setColor(Color.black) ; break; }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 + 35 , 10 , 10) ;g.fillOval(x + 35 , y - 5 , 10 , 10) ;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.setColor(Color.black) ;g.fillRect(x + 15 , y - 20 , 10 , 40) ;switch (color % 20) {case 0: g.setColor(Color.white) ; break; case 1: g.setColor(Color.white) ; break; case 2: g.setColor(Color.white) ; break; case 3: g.setColor(Color.white) ; break; case 4: g.setColor(Color.white) ; break; case 5: g.setColor(Color.white) ; break; case 6: g.setColor(Color.white) ; break;case 7: g.setColor(Color.white) ; break;case 8: g.setColor(Color.white) ; break;case 9: g.setColor(Color.white) ; break;case 10: g.setColor(Color.black) ; break;case 11: g.setColor(Color.black) ; break;case 12: g.setColor(Color.black) ; break;case 13: g.setColor(Color.black) ; break;case 14: g.setColor(Color.black) ; break;case 15: g.setColor(Color.black) ; break;case 16: g.setColor(Color.black) ; break;case 17: g.setColor(Color.black) ; break;case 18: g.setColor(Color.black) ; break;case 19: g.setColor(Color.black) ; break; }g.fillOval(x + 5 , y + 30 , 10 , 10) ;g.fillOval(x + 25 , y + 30 , 10 , 10) ;}if(op == 2) {g.setColor(Color.green) ;g.fillRect(x , y , 40 , 40) ;switch (color % 6) {case 0: g.setColor(Color.blue) ; break;case 1: g.setColor(Color.yellow) ; break;case 2: g.setColor(Color.red) ; break;case 3: g.setColor(Color.orange) ; break;case 4: g.setColor(Color.green) ; break;case 5: g.setColor(Color.black) ; break;}g.fillOval(x - 5 , y - 5 , 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) ;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 + 25 , y+35 , 10 , 10) ;g.fillOval(x + 35 , y+35 , 10 , 10) ;g.setColor(Color.black) ;g.fillRect(x + 20 , y + 15 , 40 , 10) ;switch (color % 20) {case 0: g.setColor(Color.white) ; break;case 1: g.setColor(Color.white) ; break;case 2: g.setColor(Color.white) ; break;case 3: g.setColor(Color.white) ; break;case 4: g.setColor(Color.white) ; break;case 5: g.setColor(Color.white) ; break;case 6: g.setColor(Color.white) ; break;case 7: g.setColor(Color.white) ; break;case 8: g.setColor(Color.white) ; break;case 9: g.setColor(Color.white) ; break;case 10: g.setColor(Color.black) ; break;case 11: g.setColor(Color.black) ; break;case 12: g.setColor(Color.black) ; break;case 13: g.setColor(Color.black) ; break;case 14: g.setColor(Color.black) ; break;case 15: g.setColor(Color.black) ; break;case 16: g.setColor(Color.black) ; break;case 17: g.setColor(Color.black) ; break;case 18: g.setColor(Color.black) ; break;case 19: g.setColor(Color.black) ; break; }g.fillOval(x , y + 5 , 10 , 10) ;g.fillOval(x , y + 25 , 10 , 10) ;}if(op == 3) {g.setColor(Color.blue) ;g.fillRect(x , y , 40 , 40) ;switch (color % 6) {case 0: g.setColor(Color.blue) ; break;case 1: g.setColor(Color.yellow) ; break;case 2: g.setColor(Color.red) ; break;case 3: g.setColor(Color.orange) ; break;case 4: g.setColor(Color.green) ; break;case 5: g.setColor(Color.black) ; break;}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 + 35 , 10 , 10) ;g.fillOval(x + 35 , y - 5 , 10 , 10) ;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.setColor(Color.black) ;g.fillRect(x + 15 , y + 20 , 10 , 40) ;switch (color % 20) {case 0: g.setColor(Color.white) ; break;case 1: g.setColor(Color.white) ; break;case 2: g.setColor(Color.white) ; break;case 3: g.setColor(Color.white) ; break;case 4: g.setColor(Color.white) ; break;case 5: g.setColor(Color.white) ; break;case 6: g.setColor(Color.white) ; break;case 7: g.setColor(Color.white) ; break;case 8: g.setColor(Color.white) ; break;case 9: g.setColor(Color.white) ; break;case 10: g.setColor(Color.black) ; break;case 11: g.setColor(Color.black) ; break;case 12: g.setColor(Color.black) ; break;case 13: g.setColor(Color.black) ; break;case 14: g.setColor(Color.black) ; break;case 15: g.setColor(Color.black) ; break;case 16: g.setColor(Color.black) ; break;case 17: g.setColor(Color.black) ; break;case 18: g.setColor(Color.black) ; break;case 19: g.setColor(Color.black) ; break; }g.fillOval(x + 5 , y , 10 , 10) ;g.fillOval(x + 25 , y , 10 , 10) ;}if(op == 4) {g.setColor(Color.yellow) ;g.fillRect(x , y , 40 , 40) ;switch (color % 6) {case 0: g.setColor(Color.blue) ; break;case 1: g.setColor(Color.yellow) ; break;case 2: g.setColor(Color.red) ; break;case 3: g.setColor(Color.orange) ; break;case 4: g.setColor(Color.green) ; break;case 5: g.setColor(Color.black) ; break;}g.fillOval(x - 5 , y - 5 , 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) ;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 + 25 , y+35 , 10 , 10) ;g.fillOval(x + 35 , y+35 , 10 , 10) ;g.setColor(Color.black) ;g.fillRect(x - 20 , y + 15 , 40 , 10) ;switch (color % 20) {case 0: g.setColor(Color.white) ; break;case 1: g.setColor(Color.white) ; break;case 2: g.setColor(Color.white) ; break;case 3: g.setColor(Color.white) ; break;case 4: g.setColor(Color.white) ; break;case 5: g.setColor(Color.white) ; break;case 6: g.setColor(Color.white) ; break;case 7: g.setColor(Color.white) ; break;case 8: g.setColor(Color.white) ; break;case 9: g.setColor(Color.white) ; break;case 10: g.setColor(Color.black) ; break;case 11: g.setColor(Color.black) ; break;case 12: g.setColor(Color.black) ; break;case 13: g.setColor(Color.black) ; break;case 14: g.setColor(Color.black) ; break;case 15: g.setColor(Color.black) ; break;case 16: g.setColor(Color.black) ; break;case 17: g.setColor(Color.black) ; break;case 18: g.setColor(Color.black) ; break;case 19: g.setColor(Color.black) ; break; }g.fillOval(x + 30 , y + 5 , 10 , 10) ;g.fillOval(x + 30 , y + 25 , 10 , 10) ;}g.setColor(Color.black) ;g.fillOval(dx , dy , 10 , 10) ;g.fillOval(dx1 , dy1 , 10 , 10) ;g.fillOval(dx2 , dy2 , 10 , 10) ;g.fillOval(dx3 , dy3 , 10 , 10) ;g.fillOval(dx4 , dy4 , 10 , 10) ;for (int i = 0; i<num; i++) {if(opf[i] == 1) {g.fillRect(xf[i] , yf[i] , 40 , 40) ;g.fillOval(xf[i] - 5 , yf[i] - 5 , 10 , 10) ; g.fillOval(xf[i] - 5 , yf[i] + 5 , 10 , 10) ; g.fillOval(xf[i] - 5 , yf[i] + 15 , 10 , 10) ;g.fillOval(xf[i] - 5 , yf[i] + 25 , 10 , 10) ;g.fillOval(xf[i] - 5 , yf[i] + 35 , 10 , 10) ;g.fillOval(xf[i] + 35 , yf[i] - 5 , 10 , 10) ;g.fillOval(xf[i] + 35 , yf[i] + 5 , 10 , 10) ;g.fillOval(xf[i] + 35 , yf[i] + 15 , 10 , 10) ;g.fillOval(xf[i] + 35 , yf[i] + 25 , 10 , 10) ;g.fillOval(xf[i] + 35 , yf[i] + 35 , 10 , 10) ;g.fillRect(xf[i] + 15 , yf[i] - 20 , 10 , 40) ;g.fillOval(xf[i] + 5 , yf[i] + 30 , 10 , 10) ;g.fillOval(xf[i] + 25 , yf[i] + 30 , 10 , 10) ; }if(opf[i] == 2) {g.fillRect(xf[i] , yf[i] , 40 , 40) ;g.fillOval(xf[i] - 5 , yf[i] - 5 , 10 , 10) ; g.fillOval(xf[i] + 5 , yf[i] - 5 , 10 , 10) ; g.fillOval(xf[i] + 15 , yf[i] - 5 , 10 , 10) ;g.fillOval(xf[i] + 25 , yf[i] - 5 , 10 , 10) ;g.fillOval(xf[i] + 35 , yf[i] - 5 , 10 , 10) ;g.fillOval(xf[i] - 5 , yf[i] + 35 , 10 , 10) ;g.fillOval(xf[i] + 5 , yf[i] + 35 , 10 , 10) ;g.fillOval(xf[i] + 15 , yf[i] + 35 , 10 , 10) ;g.fillOval(xf[i] + 35 , yf[i] + 35 , 10 , 10) ;g.fillRect(xf[i] + 20 , yf[i] + 15 , 40 , 10) ;g.fillOval(xf[i] , yf[i] + 5 , 10 , 10) ;g.fillOval(xf[i] , yf[i] + 25 , 10 , 10) ;}if(opf[i] == 3) {g.fillRect(xf[i] , yf[i] , 40 , 40) ;g.fillOval(xf[i] - 5 , yf[i] - 5 , 10 , 10) ; g.fillOval(xf[i] - 5 , yf[i] + 5 , 10 , 10) ; g.fillOval(xf[i] - 5 , yf[i] + 15 , 10 , 10) ;g.fillOval(xf[i] - 5 , yf[i] + 25 , 10 , 10) ;g.fillOval(xf[i] - 5 , yf[i] + 35 , 10 , 10) ;g.fillOval(xf[i] + 35 , yf[i] - 5 , 10 , 10) ;g.fillOval(xf[i] + 35 , yf[i] + 5 , 10 , 10) ;g.fillOval(xf[i] + 35 , yf[i] + 15 , 10 , 10) ;g.fillOval(xf[i] + 35 , yf[i] + 25 , 10 , 10) ;g.fillOval(xf[i] + 35 , yf[i] + 35 , 10 , 10) ;g.fillRect(xf[i] + 15 , yf[i] + 20 , 10 , 40) ;g.fillOval(xf[i] + 5 , yf[i] , 10 , 10) ;g.fillOval(xf[i] + 25 , yf[i] , 10 , 10) ;}if(opf[i] == 4) {g.fillRect(xf[i] , yf[i] , 40 , 40) ;g.fillOval(xf[i] - 5 , yf[i] - 5 , 10 , 10) ; g.fillOval(xf[i] + 5 , yf[i] - 5 , 10 , 10) ; g.fillOval(xf[i] + 15 , yf[i] - 5 , 10 , 10) ;g.fillOval(xf[i] + 25 , yf[i] - 5 , 10 , 10) ;g.fillOval(xf[i] + 35 , yf[i] - 5 , 10 , 10) ;g.fillOval(xf[i] - 5 , yf[i] + 35 , 10 , 10) ;g.fillOval(xf[i] + 5 , yf[i] + 35 , 10 , 10) ;g.fillOval(xf[i] + 25 , yf[i] + 35 , 10 , 10) ;g.fillOval(xf[i] + 35 , yf[i] + 35 , 10 , 10) ;g.fillRect(xf[i] - 20 , yf[i] + 15 , 40 , 10) ;g.fillOval(xf[i] + 30 , yf[i] + 5 , 10 , 10) ;g.fillOval(xf[i] + 30 , yf[i] + 25 , 10 , 10) ; }g.fillOval(dxf1[i] , dyf1[i] , 10 , 10 ) ;g.fillOval(dxf2[i] , dyf2[i] , 10 , 10 ) ;g.fillOval(dxf3[i] , dyf3[i] , 10 , 10 ) ;g.fillOval(dxf4[i] , dyf4[i] , 10 , 10 ) ;}}public void keyTyped(KeyEvent e) {}//键盘控制坦克的移动,发弹public void keyPressed(KeyEvent e) {color ++ ;if(e.getKeyCode() == KeyEvent.VK_UP) {op = 1 ;y = y - tankspeed ;dy = dy - tankspeed ;if(y <= 0) {y = y + tankspeed ;dy = dy + tankspeed ;}}if(e.getKeyCode() == KeyEvent.VK_RIGHT) {op = 2 ;x = x + tankspeed ;dx = dx + tankspeed ;if(x >= 560) {x = x - tankspeed ;dx = dx - tankspeed ;}}if(e.getKeyCode() == KeyEvent.VK_DOWN) {op = 3 ;y = y + tankspeed ;dy = dy + tankspeed ;if(y >= 560) {y = y - tankspeed ;dy = dy - tankspeed ;}}if(e.getKeyCode() == KeyEvent.VK_LEFT) { op = 4 ;x = x - tankspeed ;dx = dx - tankspeed ;if(x <= 0) {x = x + tankspeed ;dx = dx + tankspeed ;}}if(e.getKeyCode() == KeyEvent.VK_SPACE) { if(op == 1) {dx1 = dx ; dy1 = dy ;}if(op == 2) {dx2 = dx ; dy2 = dy ;}if(op == 3) {dx3 = dx ; dy3 = dy ;}if(op == 4) {dx4 = dx ; dy4 = dy ;}}this.repaint() ;}public void keyReleased(KeyEvent e) {}public void run() {for (int a = 0; a<60000; a++) {dy1 = dy1 - tankbullet ;dx2 = dx2 + tankbullet ;dy3 = dy3 + tankbullet ;dx4 = dx4 - tankbullet ;for (int i = 0; i<num; i++) {dyf1[i] = dyf1[i] - tankfbullet ;dxf2[i] = dxf2[i] + tankfbullet ;dyf3[i] = dyf3[i] + tankfbullet ;dxf4[i] = dxf4[i] - tankfbullet ;}//判断是否被击中for (int i = 0; i<num; i++) {if(dyf1[i]<y + 38 &&dyf1[i]>y +8 && dxf1[i]-x>-10 && dxf1[i]-x<40) { System.out.println ("被1击中") ;dxf1[i] = dxf[i] ; dyf1[i] = dyf[i] ;shengming = shengming - nandu ;}if(dxf2[i]>x+2 &&dxf2[i]<x+32 &&dyf2[i] - y >-10 && dyf2[i] - y <40 ) { System.out.println ("被2击中") ;dxf2[i] = dxf[i] ; dyf2[i] = dyf[i] ;shengming = shengming - nandu ;}if(dyf3[i]>y+2 && dyf3[i]< y+32 && dxf3[i]-x >-10&& dxf3[i]-x<40) { System.out.println ("被3击中") ;dxf3[i] = dxf[i] ; dyf3[i] = dyf[i] ;shengming = shengming - nandu ;}if(dxf4[i]>x+8 &&dxf4[i]<x+38 &&dyf4[i] - y >-10 && dyf4[i] - y <40 ) { System.out.println ("被4击中") ;dxf4[i] = dxf[i] ; dyf4[i] = dyf[i] ;shengming = shengming - nandu ;}}//判断是否击中敌军for (int i = 0; i<num; i++) {if(dy1<yf[i] + 38 &&dy1>yf[i] +8 && dx1-xf[i]>-10 && dx1-xf[i]<40) { System.out.println ("1击中") ;fenshu = fenshu + 100 ;xf[i] = (int)(Math.random() * 560 );yf[i] = (int)(Math.random() * 560 );}if(dx2>xf[i]+2 &&dx2<xf[i]+32 &&dy2 - yf[i] >-10 && dy2 - yf[i] <40 ) { System.out.println ("2击中") ;fenshu = fenshu + 100 ;xf[i] = (int)(Math.random() * 560 );yf[i] = (int)(Math.random() * 560 );}if(dy3>yf[i]+2 && dy3< yf[i]+32 && dx3-xf[i] >-10&& dx3-xf[i]<40) { System.out.println ("3击中") ;fenshu = fenshu + 100 ;xf[i] = (int)(Math.random() * 560 );yf[i] = (int)(Math.random() * 560 );}if(dx4>xf[i]+8 &&dx4<xf[i]+38 &&dy4 - yf[i] >-10 && dy4 - yf[i] <40 ) { System.out.println ("4击中") ;fenshu = fenshu + 100 ;xf[i] = (int)(Math.random() * 560 );yf[i] = (int)(Math.random() * 560 );}dxf[i] = xf[i] + 15 ;dyf[i] = yf[i] + 15 ;}//坦克的移动for (int i = 0; i<num; i++) {switch (opf[i]) {case 1:{yf[i]-- ;dyf[i] -- ;for (int s = 0; s<num; s++) {if(yf[i] <= 0) {yf[i] ++ ;dyf[i] ++ ;}}break;}case 2:{xf[i]++ ;dxf[i]++ ;for (int s = 0; s<num; s++) { if(xf[i] >= 560){xf[i] -- ;dxf[i] -- ;}}break;}case 3:{yf[i]++ ;dyf[i]++ ;for (int s = 0; s<num ; s++) { if(yf[i] >= 560){yf[i] -- ;dyf[i] -- ;}}break;}case 4:{xf[i]-- ;dxf[i]-- ;for (int s = 0; s<num; s++) { if(xf[i] <= 0){xf[i] ++ ;dxf[i] ++ ;}}break;}}}try{Thread.sleep(20) ;}catch(Exception e) {e.printStackTrace() ;}//坦克的开火if(a % 50 == 5) {if(Math.random()>0.5){for (int i = 0; i<2; i++) {if(opf[i] == 1) {dxf1[i] = dxf[i] ; dyf1[i] = dyf[i] ; }if(opf[i] == 2) {dxf2[i] = dxf[i] ; dyf2[i] = dyf[i] ; }if(opf[i] == 3) {dxf3[i] = dxf[i] ; dyf3[i] = dyf[i] ; }if(opf[i] == 4) {dxf4[i] = dxf[i] ; dyf4[i] = dyf[i] ; }}}}if(a % 50 == 15) {if(Math.random()>0.5) {for (int i = 2; i<4; i++) {if(opf[i] == 1) {dxf1[i] = dxf[i] ; dyf1[i] = dyf[i] ; }if(opf[i] == 2) {dxf2[i] = dxf[i] ; dyf2[i] = dyf[i] ; }if(opf[i] == 3) {dxf3[i] = dxf[i] ; dyf3[i] = dyf[i] ; }if(opf[i] == 4) {dxf4[i] = dxf[i] ; dyf4[i] = dyf[i] ; }}}}if(a % 50 == 25) {if(Math.random()>0.5){for (int i = 4; i<6; i++) {if(opf[i] == 1) {dxf1[i] = dxf[i] ; dyf1[i] = dyf[i] ; }if(opf[i] == 2) {dxf2[i] = dxf[i] ; dyf2[i] = dyf[i] ; }if(opf[i] == 3) {dxf3[i] = dxf[i] ; dyf3[i] = dyf[i] ; }if(opf[i] == 4) {dxf4[i] = dxf[i] ; dyf4[i] = dyf[i] ; }}}}if(a % 50 == 35) {if(Math.random()>0.5){for (int i = 6; i<8; i++) {if(opf[i] == 1) {dxf1[i] = dxf[i] ; dyf1[i] = dyf[i] ; }if(opf[i] == 2) {dxf2[i] = dxf[i] ; dyf2[i] = dyf[i] ; }if(opf[i] == 3) {dxf3[i] = dxf[i] ; dyf3[i] = dyf[i] ; }if(opf[i] == 4) {dxf4[i] = dxf[i] ; dyf4[i] = dyf[i] ; }}}}if(a % 50 == 45) {if(Math.random()>0.5) {for (int i = 8; i<10; i++) {if(opf[i] == 1) {dxf1[i] = dxf[i] ; dyf1[i] = dyf[i] ; }if(opf[i] == 2) {dxf2[i] = dxf[i] ; dyf2[i] = dyf[i] ; }if(opf[i] == 3) {dxf3[i] = dxf[i] ; dyf3[i] = dyf[i] ; }if(opf[i] == 4) {dxf4[i] = dxf[i] ; dyf4[i] = dyf[i] ; }}}}//坦克的随机移动if(a % 50 == 1 ) {for (int i = 0; i<2; i++) {if( Math.random() > 0.5 ) {if(Math.random() > 0.5){opf[i] = 1 ;}else{opf[i] = 2 ;}}else{if(Math.random() > 0.5){opf[i] = 3 ;}else{opf[i] = 4 ;}}}}if(a % 50 == 11 ) {//坦克的随机移动for (int i = 2; i<4; i++) {if( Math.random() > 0.5 ) {if(Math.random() > 0.5){opf[i] = 1 ;}else{}}else{if(Math.random() > 0.5){ opf[i] = 3 ;}else{opf[i] = 4 ;}}}}if(a % 50 == 21 ) {//坦克的随机移动for (int i = 4; i<6; i++) { if( Math.random() > 0.5 ) { if(Math.random() > 0.5){ opf[i] = 1 ;}else{opf[i] = 2 ;}}else{if(Math.random() > 0.5){ opf[i] = 3 ;}else{opf[i] = 4 ;}}}}if(a % 50 == 31 ) {//坦克的随机移动for (int i = 6; i<8; i++) { if( Math.random() > 0.5 ) { if(Math.random() > 0.5){ opf[i] = 1 ;}else{opf[i] = 2 ;}}else{if(Math.random() > 0.5){ opf[i] = 3 ;}else{}}}}if(a % 50 == 41 ) {//坦克的随机移动for (int i = 8; i<10; i++) {if( Math.random() > 0.5 ) {if(Math.random() > 0.5){opf[i] = 1 ;}else{opf[i] = 2 ;}}else{if(Math.random() > 0.5){opf[i] = 3 ;}else{opf[i] = 4 ;}}}}//重画if(shengming<=0){//弹出player1胜利对话框JOptionPane.showMessageDialog(null,"你结束了!!!","Game Over !", JOptionPane.ERROR_MESSAGE); //结束游戏System.exit(0) ;}this.repaint() ;}}}。

坦克大战java源代码审批稿

坦克大战java源代码审批稿

坦克大战j a v a源代码 YKK standardization office【 YKK5AB- YKK08- YKK2C- YKK18】有些图片路径会出错要注意package ;import 坦克类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){=x;=y;}public int getX() {return x;}public void setX(int x) {= x;}public int getY() {return y;}public void setY(int y) {= y;}public int getDirect() {return direct;}public void setDirect(int direct) {= direct;}public int getColor() {return color;}public void setColor(int color) {= color;}};import .*;import .*;import .*;public class MyTankGame4 extends JFrame implements ActionListener{ MyPanel mp=null;MyStartPanel msp=null;quals("New Game")){(msp);mp=new MyPanel();Thread mt=new Thread(mp);();(mp);(mp);(true);}}}etImage"/.jpg"));etImage"/"));image2=().getImage"/"));image3=().getImage"/"));}//控制坦克移动public void keyPressed(KeyEvent arg0) {// TODO Auto-generated method stubif()=={}else if()== {}else if()== {}else if()== {}else if()== {=0;=0;}//发射子弹(连发5枚子弹)if {if()=={(),(),());}}//控制坦克不能跑出边界if if if if//面板重绘();}public void keyReleased(KeyEvent arg0) {// TODO Auto-generated method stub}public void keyTyped(KeyEvent arg0) {// TODO Auto-generated method stub}public void run() {while(true){try {(50);} catch (InterruptedException e) {// TODO Auto-generated catch block();}//不停地调用敌人是否打中自己坦克函数for(int i=0;i<();i++){et=(i);for(int j=0;j< {Shot enshot=if==true&&==true){(enshot, hero);}}}//不停地调用是否打中敌人坦克函数for(int i=0;i< {Shot s=if(s!=null&&==true){for(int j=0;j<();j++){EnemyTank et=(j);if(et!=null&&==true){(s, et);}}}}//面板重绘();}}}。

韩顺平java坦克大战1.0版本_源代码

韩顺平java坦克大战1.0版本_源代码

/**画坦克1。

0*/import java.awt.Color;import java.awt.Graphics;import java.awt.event。

KeyEvent;import java.awt。

event。

KeyListener;import java。

awt。

event.MouseEvent;import java。

awt。

event。

MouseListener;import java。

awt。

event.MouseMotionListener;import java.awt。

event。

WindowEvent;import java.awt。

event。

WindowListener;import javax。

swing。

JFrame;import javax.swing。

JPanel;public class MyTankGame extends JFrame{MyPanel mp=null;public static void main(String[] args){MyTankGame mtk=new MyTankGame();}public MyTankGame(){mp=new MyPanel();this.add(mp);//把面板加入窗体//注册监听this。

addMouseListener(mp);this.addKeyListener(mp);this。

addMouseMotionListener(mp);this.addWindowListener(mp);this.setTitle(”坦克大战");//窗体标题this。

setSize(600,400);//大小,宽,高(像素)this。

setLocation(300,300);//显示位置。

左边距,上边距//禁止用户改变窗口大小this。

setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);//显示}}//我的面板class MyPanel extends JPanel implements WindowListener,MouseListener,MouseMotionListener,KeyListener{//定义一个我的坦克Hero hero=null;Diren diren=null;public MyPanel(){hero=new Hero(300,200);diren=new Diren(100,0);//diren。

坦克大战,及源代码

坦克大战,及源代码

采用双向循环链表作太极形式旋转的图案,图案的变化采用刷新屏幕的方法做到。

首先,建立一个二维字符数组,保存图形数据,然后通过更改该二维字符数组和清屏,再显示,做到图像的变化。

用它提示坦克大战游戏的操作方法。

再显示问题解决后,通过无回显读取函数getch();和输入流检查函数kbhit();读取输入的方向键或回车键进入下一页面。

用同样的方法,读取方向键和回车键,更改内部图案方法,使用户选择关:第一关:仅有4个坦克同时作战,总数为30个!第二关:仅有5个坦克同时作战,总数为40个!第三关:有6个坦克同时作战,总数为50个!第四关:有7个坦克同时作战,总数为60个!第五关:有8个坦克同时作战,总数为70个!选择关卡后,进入下一页面:进行相关提示。

最后进入游戏界面。

也可以通过按左方向键返回上一级重新选择!!!第一关到第三关:都是宽80,高42的。

最后两关:都是宽160,高42的。

右击标题栏选择属性,进入设置。

接着就是进入最重要的坦克大战游戏了:这时采用的就不是刷新屏幕能解决的问题了。

由于界面太大,不能刷新屏幕,仅能用光标移动函数来更改图像。

光标移动函数为gotoxy(int ,int );读者自己查看定义。

采用同样输入流读取方法。

接着是游戏规则,操作者用W.w.A.a.S.s.D.d移动坦克,空格是停下。

用方向键改变炮筒的方向,回车键是开火。

但当自己的炮弹还在运行时,不得开火!!!用链表保存每一辆坦克(包括主坦克)的数据。

电脑控制的坦克,被控制者的坦克炮弹击中,就会发生爆炸,并死亡。

当界面上的所有坦克小于特定值时,如果内部还有未出现的坦克,就会在上方的随机位置,产生新坦克。

如果操作者的坦克被击中,它的HP(我称为生命值)就会减一点。

刚开始游戏时的HP=5;但升级进入下一关时保留上一局的最后分数和HP。

不会重置。

但游戏中有补血包,可以补充生命值(HP)补血包每90秒出现一次,出现的时间长为60秒。

如果在这期间操作者的坦克运动到它的位置,补血包将给操作者的坦克补血。

JAVA课程设计坦克大战含代码

JAVA课程设计坦克大战含代码

游戏结束判断
生命值耗尽:当坦克的生命值降至0时,游戏结束 游戏时间结束:当游戏时间达到预设值时,游戏结束 胜利条件达成:当一方达到预设的胜利条件时,游戏结束 玩家主动退出:当玩家主动选择退出游戏时,游戏结束
游戏得分计算
得分规则:击毁敌 方坦克、保护己方 坦克、完成特定任 务等
得分方式:根据击 毁敌方坦克的数量、 保护己方坦克的数 量、完成任务的难 度等计算得分
得分显示:在游戏 界面实时显示得分 情况,方便玩家了 解游戏进度
得分记录:游戏结 束后,记录玩家的 分情况,方便玩 家查看历史成绩
游戏优化与扩展
游戏性能优化
优化算法: 使用更高 效的算法, 如动态规 划、贪心 算法等
减少内存 占用:优 化数据结 构,减少 不必要的 内存占用
优化渲染: 使用更高 效的渲染 技术,如 OpenGL、 DirectX 等
优化网络 传输:使 用更高效 的网络传 输协议, 如TCP、 UDP等
优化AI: 使用更高 效的AI算 法,如神 经网络、 遗传算法 等
优化资源 管理:合 理分配系 统资源, 避免资源 浪费
游戏扩展功能实现
增加新的坦克类型:如重型坦克、轻 型坦克等
增加新的地图:如沙漠地图、雪地地 图等
增加新的游戏模式:如团队模式、生 存模式等
音效类型:背景音乐、战斗音效、 胜利音效等
音效处理:音量调节、音效混合、 音效剪辑等
添加标题
添加标题
添加标题
添加标题
音效来源:自行创作、购买版权、 网络下载等
音效与游戏场景的结合:根据游戏 场景选择合适的音效,增强游戏体 验
游戏实体设计
坦克类设计
坦克属性:生命值、攻击 力、防御力、速度等

java坦克大战源代码

java坦克大战源代码

package com.cwj.tankGame;import javax.sound.sampled.*;import java.io*;import java.awt.*;import java.io.IOException;import java.util.*;import javax.swing,*;import javax.imageio.ImageIO;import javax.swing.*;class AePlayWave extends Thread {private String filename;public AePlayWave(String wavfile) {filename = wavfile;}public void run() {File soundFile = new File(filename);AudioInputStream audioInputStream = null;try {audioInputStream = AudioSystem.getAudioInputStream(soundFile);} catch (Exception e1) {e1.printStackTrace();return;}AudioFormat format = audioInputStream.getFormat(); SourceDataLine auline = null; info = new (SourceDataLine.class, format); try {auline = (SourceDataLine) AudioSystem.getLine(info);auline.open(format);} catch (Exception e) {e.printStackTrace();return;}auline.start();int nBytesRead = 0;//这是缓冲byte[] abData = new byte[512];try {while (nBytesRead != -1) {nBytesRead = audioInputStream.read(abData, 0, abData.length); if (nBytesRead >= 0)auline.write(abData, 0, nBytesRead);}} catch (IOException e) {e.printStackTrace();return;} finally {auline.drain();auline.close();}}}class Bomb{int x;int y;int lifeTime = 10;boolean isLive=true;public Bomb(int x,int y){this.x=x;this.y=y;}public void lifeDown(){if(lifeTime>0){lifeTime--;}else{this.isLive=false;}}}class EnemyTank extends Tank implements Runnable{int n=1;//敌方坦克转弯n次发射一颗子弹int MaxShot = 10;int runDistance = 10;//变一次方向前进的距离//定义一个向量可以访问MyPanel上所有地方坦克Vector<EnemyTank> ets =new Vector<EnemyTank>();Vector<Shot> ss = new Vector<Shot>();public void setEts(Vector<EnemyTank> v){this.ets=v;}public int abs(int x){if(x>0)return x;elsereturn -x;}public boolean isTouchOther(){switch(this.direction){case 0://上for(int i=0;i<ets.size();i++){EnemyTank t = ets.get(i);if(t!=this)//不是自己这辆坦克{if(abs(this.x-t.x)<=40&&this.y>t.y&&this.y-t.y<=40) {return true;}}}break;case 2://下for(int i=0;i<ets.size();i++){EnemyTank t = ets.get(i);if(t!=this)//不是自己这辆坦克{if(abs(this.x-t.x)<=40&&t.y>this.y&&t.y-this.y<=40) {return true;}}}break;case 1://左for(int i=0;i<ets.size();i++){EnemyTank t = ets.get(i);if(t!=this)//不是自己这辆坦克{if(abs(this.y-t.y)<=40&&this.x>t.x&&this.x-t.x<=40) {return true;}}}break;case 3://右for(int i=0;i<ets.size();i++){EnemyTank t = ets.get(i);if(t!=this)//不是自己这辆坦克{if(abs(this.y-t.y)<=40&&this.x<t.x&&t.x-this.x<=40) {return true;}}}break;}return false;}public EnemyTank(int x,int y){super(x,y);}int time=0;public void sleep(){try{Thread.sleep(50);}catch(Exception e){e.printStackTrace();}}public void run() {while(true){switch(direction){case 0:for(int i =0;i<this.runDistance;i++){if(y>20&&!this.isTouchOther())y-=speed;this.sleep();}break; case 1:for(int i =0;i<this.runDistance;i++){if(x>20&&!this.isTouchOther())x-=speed;this.sleep();}break; case 2:for(int i =0;i<this.runDistance;i++){if(y<580&&!this.isTouchOther())y+=speed;this.sleep();}break; case 3:for(int i =0;i<this.runDistance;i++){if(x<780&&!this.isTouchOther())x+=speed;this.sleep();}break; }this.direction=(int)(Math.random()*4);if(this.isLive==false){break;}else{time++;if(time%n==0)if(ss.size()<MaxShot){Shot s = new Shot(x,y);s.setDirection(direction);ss.add(s);Thread t = new Thread(s);t.start();}}}}}class MyTank extends Tank{int MAXSHOT=20;public int getMAXSHOT() {return MAXSHOT;}public void setMAXSHOT(int mAXSHOT) { MAXSHOT = mAXSHOT;}Shot shot=null;Vector<Shot> ss = new Vector<Shot>(); public MyTank(int x,int y){super(x,y);}public void fire(){if(this.isLive==true){shot = new Shot(x,y);ss.add(shot);Thread t = new Thread(shot);t.start();shot.setDirection(this.direction);}}public void moveUp(){if(y>20)y-=speed;}public void moveLeft(){if(x>20)x-=speed;}public void moveDown(){if(y<580)y+=speed;}public void moveRight(){if(x<780)x+=speed;}class Node {int x;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 getDirection() {return direction;}public void setDirection(int direction) { this.direction = direction;}int y;int direction;public Node(int x,int y){this.x=x;this.y=y;}}class Record {//记录每关有多少敌人private static int etNum=20;private static int sum = 0;private static FileWriter fw=null;private static BufferedWriter bw = null; private static FileReader fr = null; private static BufferedReader br = null; private static Vector<Node> nodes=null; public static Vector<Node> getNodes() { return nodes;public static void setNodes(Vector<Node> nodes) { Record.nodes = nodes;}public static Vector<EnemyTank> ets = null;public static Vector<EnemyTank> getEts() {return ets;}public static void setEts(Vector<EnemyTank> ets) { Record.ets = ets;}public static void restoreRecord(){try {nodes = new Vector<Node>();File file = new File("e:\\tankGame.txt");if(!file.isDirectory()){if(file.createNewFile())System.out.println("成功创建文件e:\\tankGame.txt"); }fr = new FileReader("e:\\tankGame.txt");br = new BufferedReader(fr);String strSum = br.readLine();if(strSum!=null)sum = Integer.parseInt(strSum);else return;String str="";while((str=br.readLine())!=null){String []xyd=str.split(" ");int x,y,direction;x = Integer.parseInt(xyd[0]);y = Integer.parseInt(xyd[1]);direction = Integer.parseInt(xyd[2]);Node node = new Node(x,y);node.setDirection(direction);nodes.add(node);}for(int i=0;i<nodes.size();i++){System.out.println(nodes.get(i).x+" "+nodes.get(i).y+" "+nodes.get(i).direction); }} catch (Exception e) {e.printStackTrace();}finally{try {if(br!=null)br.close();if(fr!=null)fr.close();} catch (IOException e) {e.printStackTrace();}}}public static void keepRecord(){try {fw = new FileWriter("e:\\tankGame.txt");bw = new BufferedWriter(fw);bw.write(sum+"\r\n");for(int i =0;i<ets.size();i++){EnemyTank et = ets.get(i);if(et.isLive==true){String record = et.x+" "+et.y+" "+et.getDirection()+"\r\n";bw.write(record);}}} catch (Exception e) {e.printStackTrace();}finally{try {if(bw!=null)bw.close();if(fw!=null)fw.close();} catch (IOException e) {e.printStackTrace();}}}public static int getSum() {return sum;}public static void setSum(int sum) { Record.sum = sum;}public static void addSum(){sum++;}public static int getEtNum() {return etNum;}public static void setEtNum(int etNum) { Record.etNum = etNum;}public static int getMyLife() {return myLife;}public static void setMyLife(int myLise) { Record.myLife = myLise;}//生命数private static int myLife=3;public static void reduceEtNum(){etNum--;}public static void reduceMyLife(){myLife--;}}class Shot implements Runnable{int x;int speed=5;int y;int direction;boolean isLive=true;public void run(){while(true){try{Thread.sleep(100);}catch(Exception e){e.printStackTrace();}switch(this.direction){case 0:y-=speed;break;case 1:x-=speed;break;case 2:y+=speed;break;case 3:x+=speed;break;}if(x<0||x>800||y<0||y>600){this.isLive=false;break;}}//while}public int getSpeed() {return speed;}public void setSpeed(int speed) { this.speed = speed;}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 getDirection() {return direction;}public void setDirection(int direction) { this.direction = direction;}public Shot(int x,int y){this.x=x;this.y=y;}}class Tank{boolean isLive=true;int x=0;int y=0;int color;public int getColor() {return color;}public void setColor(int color) {this.color = color;}int speed=3;int direction=0;//0上1左2下3右public int getSpeed() {return speed;}public void setSpeed(int speed) { this.speed = speed;}public int getDirection() {return direction;}public void setDirection(int direction) { this.direction = direction;}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 Tank(int x,int y){this.x=x;this.y=y;}}public class TankGame extends JFrame implements ActionListener{ MyPanel mp=null;MyStartPanel msp = null;JMenuBar jmb =null;JMenu jm1=null;JMenuItem jmi11 =null;JMenuItem jmi12 = null;JMenuItem jmi13 = null;JMenuItem jmi14 = null;public static void main(String[]args){TankGame t = new TankGame();}public TankGame(){/*mp=new MyPanel();this.add(mp);Thread t = new Thread(mp);t.start();this.addKeyListener(mp);*/jmb = new JMenuBar();jm1 = new JMenu("游戏(G)");jm1.setMnemonic('G');jmi11 = new JMenuItem("开始游戏(N)");jmi11.addActionListener(this);jmi11.setActionCommand("newgame");jmi11.setMnemonic('N');jmi12 = new JMenuItem("退出游戏(E)");jmi12.addActionListener(this);jmi12.setActionCommand("exitgame");jmi12.setMnemonic('E');jmi13 = new JMenuItem("退出并保存(O)");jmi13.addActionListener(this);jmi13.setActionCommand("exitandsave");jmi13.setMnemonic('O');jmi14 = new JMenuItem("继续游戏(S)");jmi14.addActionListener(this);jmi14.setActionCommand("continue");jmi14.setMnemonic('S');jm1.add(jmi11);jm1.add(jmi13);jm1.add(jmi14);jm1.add(jmi12);jmb.add(jm1);this.setJMenuBar(jmb);msp = new MyStartPanel();Thread t = new Thread(msp);t.start();this.add(msp);this.setSize(900, 700);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setTitle("坦克大战欢迎加qq1036624975交流学习编程爱好者学习交流群294787264");this.setVisible(true);}public void actionPerformed(ActionEvent e) {if(e.getActionCommand().equals("newgame")){if(mp!=null)this.remove(mp);mp = new MyPanel("newGame");Thread t = new Thread(mp);t.start();this.remove(msp);this.add(mp);this.addKeyListener(mp);this.setVisible(true);AePlayWave apw = new AePlayWave("e://tank.wav"); new Thread(apw).start();}else if(e.getActionCommand().equals("exitgame")){System.exit(0);}else if(e.getActionCommand().equals("exitandsave")) {Record.setEts(mp.et);Record.keepRecord();System.exit(0);}else if(e.getActionCommand().equals("continue")){Record.restoreRecord();if(mp!=null)this.remove(mp);mp = new MyPanel("oldGame");Thread t = new Thread(mp);t.start();this.remove(msp);this.add(mp);this.addKeyListener(mp);this.setVisible(true);}}}class MyStartPanel extends JPanel implements Runnable {int count=0;public void paint(Graphics g){super.paint(g);g.fillRect(0, 0, 800, 600);if(count%6==0)g.setColor(Color.YELLOW);else if(count%6==1)g.setColor(Color.BLUE);else if(count%6==2)g.setColor(Color.CYAN);else if(count%6==3)g.setColor(Color.GREEN);else if(count%6==4)g.setColor(Color.ORANGE);else if(count%6==5)g.setColor(Color.PINK);g.setFont(new Font("黑体",Font.PLAIN,100));g.drawString("第一关",250,300);g.setFont(new Font("黑体",Font.PLAIN,20));g.drawString("w上s下a左d右j发射子弹", 0, 20);if(count>20000)count=0;}public void run() {while(true){try {Thread.sleep(300);} catch (Exception e) {e.printStackTrace();}count++;this.repaint();}}}class MyPanel extends JPanel implements KeyListener,Runnable {Image bomb1=null;Image bomb2=null;Image bomb3=null;MyTank mt = null;//我的坦克Vector<EnemyTank>et=new Vector<EnemyTank>();Vector<Bomb>bombs = new Vector<Bomb>();Vector<Node>nodes =null;int enemyTankNum=20;public MyPanel(String ifNew){Record.restoreRecord();mt = new MyTank(300,570);mt.setSpeed(7);mt.setColor(0);//初始化敌人的坦克if(ifNew.equals("newGame")){for(int i =0;i<enemyTankNum;i++){EnemyTank tank = null;if(i<10){tank=new EnemyTank((i+1)*70,20);}else{tank = new EnemyTank(((i-9))*70,80);}tank.setDirection(2);Thread t = new Thread(tank);t.start();Shot s = new Shot(tank.x,tank.y);s.setDirection(tank.direction);new Thread(s).start();tank.ss.add(s);tank.setColor(1);et.add(tank);tank.setEts(et);}}else if(ifNew.equals("oldGame")){nodes=Record.getNodes();for(int i =0;i<nodes.size();i++){Node node = nodes.get(i);EnemyTank tank = null;tank = new EnemyTank(node.getX(),node.getY()); tank.setDirection(node.getDirection());Thread t = new Thread(tank);t.start();Shot s = new Shot(tank.x,tank.y);s.setDirection(tank.direction);new Thread(s).start();tank.ss.add(s);tank.setColor(1);et.add(tank);tank.setEts(et);}}//三张图片,组成一颗炸弹try {bomb1=ImageIO.read(new File("image/bomb_1.gif"));bomb2=ImageIO.read(new File("image/bomb_2.gif"));bomb3=ImageIO.read(new File("image/bomb_3.gif"));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}//以下方式加载图片第一次爆炸效果不明显/*bomb1=Toolkit.getDefaultToolkit().getImage("image/bomb_1.gif"); bomb2=Toolkit.getDefaultToolkit().getImage("image/bomb_2.gif"); bomb3=Toolkit.getDefaultToolkit().getImage("image/bomb_3.gif");*/ }public void showMessage(Graphics g){this.paintTank(825, 20, g, 0, 1);this.paintTank(825, 80, g, 0, 0);g.setColor(Color.BLACK);g.drawString("x"+Record.getEtNum(),850,20);g.drawString("x"+Record.getMyLife(), 850, 80);g.setFont(new Font("华文彩云",Font.BOLD,20));g.drawString("总成绩", 2, 620);g.drawString(""+Record.getSum(), 76, 620);}public void paint(Graphics g){super.paint(g);g.fillRect(0, 0, 800, 600);this.showMessage(g);if(mt.isLive==true)paintTank(mt.getX(),mt.getY(),g,mt.getDirection(),mt.getColor());//画敌人坦克for(int i=0;i<et.size();i++){EnemyTank t = et.get(i);if(t.isLive==true){paintTank(t.getX(),t.getY(),g,t.getDirection(),t.getColor());for(int j = 0;j<t.ss.size();j++)//画子弹{if(s.isLive==true){g.setColor(Color.RED);g.fillRect(s.getX(), s.getY(), 5,5);}else{t.ss.remove(s);}}}else{et.remove(t);}}//爆炸效果for(int i=0;i<bombs.size();i++){Bomb b = bombs.get(i);if(b.lifeTime>6){g.drawImage(bomb1,b.x,b.y,40,40,this);}else if(b.lifeTime>3){g.drawImage(bomb2,b.x,b.y,40,40,this);}else{g.drawImage(bomb3,b.x,b.y,40,40,this);}b.lifeDown();if(b.lifeTime==0){bombs.remove(b);}}for(int i =0;i<mt.ss.size();i++)//我的画子弹{Shot s = mt.ss.get(i);if(s!=null&&s.isLive==true){g.setColor(Color.YELLOW);g.fillRect(s.getX(), s.getY(),5, 5);}if(s.isLive==false) //子弹清空,才能打下一发子弹{}}}//被攻击public void hitMyTank(){for(int i=0;i<et.size();i++){EnemyTank e=et.get(i);for(int j=0;j<e.ss.size();j++){Shot enemyShot = e.ss.get(j); boolean res=false;res = this.isHitTank(enemyShot, mt); if(res==true)Record.reduceMyLife();}}}//攻击敌方坦克public void hitEnemyTank(){for(int i=0;i<mt.ss.size();i++){Shot shot = mt.ss.get(i);if(shot.isLive==true){for(int j =0;j<et.size();j++){EnemyTank etank = et.get(j);if(etank.isLive==true){boolean res=false;res=this.isHitTank(shot, etank);if(res==true){Record.reduceEtNum();Record.addSum();}}}}}}//判断子弹是否击中坦克public boolean isHitTank(Shot shot,Tank etank){boolean res=false;if(shot.x>etank.x-20&&shot.x<etank.x+20&&shot.y>etank.y-20&&shot.y<etank.y+20&&et ank.isLive==true)//子弹击中{shot.isLive=false;etank.isLive=false;Bomb bomb = new Bomb(etank.getX()-20,etank.getY()-20);bombs.add(bomb);res = true;}return res;}public void paintTank(int x,int y,Graphics g,int direction,int type){switch(type)//判断坦克类型{case 0:g.setColor(Color.yellow);//自己的break;case 1:g.setColor(Color.RED);//敌人的break;}switch(direction){case 0://上g.fillRect(x-20, y-20, 40, 40);g.fill3DRect(x-5, y-20, 10, 20,false);break;case 1://左g.fillRect(x-20, y-20, 40, 40);g.fill3DRect(x-20, y-5, 20, 10,false);break;case 2://下g.fillRect(x-20, y-20, 40, 40);g.fill3DRect(x-5, y, 10, 20,false);break;case 3://右g.fillRect(x-20, y-20, 40, 40);g.fill3DRect(x, y-5, 20, 10,false);break;}}@Overridepublic void keyTyped(KeyEvent e) {// TODO Auto-generated method stub}@Overridepublic void keyPressed(KeyEvent e) {// TODO Auto-generated method stubif(e.getKeyCode()==KeyEvent.VK_J)//发射子弹{if(this.mt.ss.size()<this.mt.getMAXSHOT()) {this.mt.fire();}}if(e.getKeyCode()==KeyEvent.VK_W){mt.setDirection(0);this.mt.moveUp();}else if(e.getKeyCode()==KeyEvent.VK_A) {mt.setDirection(1);this.mt.moveLeft();}else if(e.getKeyCode()==KeyEvent.VK_S) {mt.setDirection(2);this.mt.moveDown();}else if(e.getKeyCode()==KeyEvent.VK_D) {mt.setDirection(3);this.mt.moveRight();}this.repaint();}@Overridepublic void keyReleased(KeyEvent e) {// TODO Auto-generated method stub}@Overridepublic void run() {// TODO Auto-generated method stubwhile(true){try {Thread.sleep(50);//50毫秒重绘一次} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}this.hitEnemyTank();//判断己方坦克是否击中敌方坦克this.hitMyTank();//判断敌方坦克是否击中己方坦克this.repaint();}}}。

JAVA实现经典游戏坦克大战的示例代码

JAVA实现经典游戏坦克大战的示例代码

JAVA实现经典游戏坦克⼤战的⽰例代码⽬录前⾔主要设计功能截图代码实现总结前⾔⼩时候⼤家都玩过坦克⼤战吧,熟悉的旋律和丰富的关卡陪伴了我们⼀整个寒暑假,还记得传说中的经典坦克⼤战吗?那些怀旧的记忆,伴随着我们⼀起⾛过来的经典坦克⼤战,刚开始那战战兢兢,屡屡被敌⼈坦克击毁的情景历历在⽬。

现在好了,再也不⽤担⼼敌⼈坦克了,可以横冲直撞,横扫敌⼈坦克了。

快哉《坦克⼤战》游戏以坦克战⽃为主题,⽤java语⾔实现,采⽤了swing技术进⾏了界⾯化处理,设计思路⽤了⾯向对象思想。

主要需求可以⽣成不同的地图,消灭地图上的所有坦克为胜利;可以设置关卡地图,不断增加难度。

主要设计1、要有难度关卡:第⼀关,第⼆关,第三关,第四关,第五关;第⼀关地图最简单,第五关地图最难;2、坦克要有⾎条,打多次才会死3、地图要我有我⽅坦克四辆(相当于4条命)和多辆敌⽅坦克4、我⽅坦克的炮弹数量是固定的,设置为5005、地图右边显⽰基本信息6、地图上要在砖块,铁块,河流功能截图游戏开始页⾯代码实现窗⼝布局public class GameFrame extends JFrame {/*** serialVersionUID*/private static final long serialVersionUID = -1176914786963603304L;public GameFrame() {super();this.setSize(800, 700);this.setTitle("坦克⼤战");this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);this.setResizable(false);// 显⽰器屏幕⼤⼩Dimension screenSizeInfo = Toolkit.getDefaultToolkit().getScreenSize();int leftTopX = ((int) screenSizeInfo.getWidth() - this.getWidth()) / 2;int leftTopY = ((int) screenSizeInfo.getHeight() - this.getHeight()) / 2;// 设置显⽰的位置在屏幕中间this.setLocation(leftTopX, leftTopY);}}地图渲染核⼼算法@Servicepublic class PaintService {@Autowiredprivate GameContext context;private Brick rightBrick = new Brick(700, 50);private Iron rightIron = new Iron(700, 50);private Water rightWater = new Water(700, 50);/*** 画出东西(包括坦克、障碍物。

java简单坦克大战制作代码

java简单坦克大战制作代码

java简单坦克⼤战制作代码利⽤Java语⾔中的集合、Swing、线程等知识点编写⼀个坦克⼤战游戏。

(1)画出敌我坦克的原理:在坦克类⾥⾯有⼀个布尔类型变量good。

⽤于判断坦克的阵营,在创建坦克对象时在Tank类的构造⽅法中传⼊good的值。

在画坦克的时候判断good的值,区分敌我坦克的颜⾊;(2)坦克运动的原理:在坦克类⾥写⼊了监听键盘摁键的响应事件,对监听到的上下左右键进⾏记录,并合成坦克移动的⼋个⽅向的变量。

之后对应每个⽅向的不同对坦克坐标x,y的值做响应的更改实现我⽅坦克的移动。

⽽敌⽅坦克则⾃动移动,通过随机数对敌⽅坦克移动⽅向的随机,并且随机出每次移动的次数。

两个随机值相结合即实现了敌⽅坦克的移动。

(3)坦克发射⼦弹的原理:通过键盘监听,检测到发射⼦弹命令后将主类的⼦弹类集合中添加⼀个⼦弹类。

将炮筒的⽅向以及坦克的位置以及坦克的阵营传⼊给⼦弹类,在主类paint画⽅法中⼀直循环⼦弹类集合,如果集合内有⼦弹,就画出来。

这样就实现了发射⼦弹。

(4)坦克、⼦弹、墙的碰撞原理:在坦克类⼦弹类墙类中分别getRect⽅法获取⾃⾝的范围,然后在每次画坦克、⼦弹时都会进⾏相应的碰撞检测(在坦克类⾥有与墙和出⾃⼰外的坦克相撞的处理⽅法、在⼦弹类⾥有与墙和坦克相碰撞的处理⽅法。

),如果⾃⾝与不该碰撞的物体的范围相重合,则代表两物体相撞。

(5)坦克加⾎的原理:在⾎块类中有⾎块与我⽅坦克相碰撞的处理⽅法,如果⾎块范围与坦克范围重合则⾎块类死亡,并且坦克类的⾎量回复置满。

(6)坦克复活的原理:通过键盘监听,检测到我⽅坦克复活命令后,如果我⽅坦克处于死亡状态,则将我⽅坦克存货状态改为活着并且将我⽅坦克⾎量回置满⾎。

编程思想:坦克⼤战的编程思想在主类开启⼀个线程,没50毫秒循环⼀次画⽅法(绘制整个界⾯内的所有东西)。

画的东西有敌我坦克(颜⾊区分)、⼦弹、墙、⾎块、爆炸。

所以总共写出了⼏个类:Tank坦克类、Missile⼦弹类、Wall墙类、Blood ⾎块类、TankClient主类。

韩顺平坦克大战1.0_1

韩顺平坦克大战1.0_1
this.speed = speed;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
}
//敌人坦克
class EnemyTank extends Tank{
public EnemyTank(int x, int y) {
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
package Tank2;
//坦克类
class Tank{
//表示坦克的横坐标
int x = 0;
//坦克纵坐标
int y = 0;
/ห้องสมุดไป่ตู้坦克移动方向
int direct = 0; //0表示上, 1表示右, 2表示下, 3表示左
int color;//颜色
//坦克速度
int speed = 2;
public Tank(int x, int y){
}
//坦克向右移动
public void moveRight(){
x += speed;
}
//坦克向下移动
public void moveDown(){

JAVA课程设计坦克大战(含代码)

JAVA课程设计坦克大战(含代码)

Java程序课程设计任务书项目名称:坦克大战游戏的设计与开发英文名:Tank Battle1、主要内容:1、基于java的游戏开发与设计的分析;2、坦克大战游戏的架构搭建与设计实现;3、游戏可以通过用户可以注册新用户,修改自己的密码、个人资料。

2、具体要求1、系统的功能要求:(一)实现玩家试玩游戏功能,屏蔽用户注册和登陆功能(空功能)(二)玩家具备一个或者两个备选坦克战车(三)玩家可以选择一个到两个关卡进行作战(四)玩家可以选择两个敌对阵营中的一个加入,与其他AI战车坦克并肩作战(五)胜利判定条件为:时间制、全杀制(可选)(六)至少提供两个可以自由切换的视角(七)至少提供一种炮弹类型(八)假定项目:如果用户注册,登陆功能能够顺利做好,则提供一个玩家分数上传和排行榜功能。

2、学习并掌握以下技术:数据库的JDBC连接,JA V A编程语言,MySQL数据库的应用;JSP的开发与运用3、熟练使用以下开发工具:JCreator,Myeclipse ,MySQL ,PowerDesigner 等3、进度安排:12月28日~ 12 月29 日:查阅资料,完成文献综述,任务书;12月29 日~ 12月31日:进行系统的需求分析与设计;1月1日~ 1月3日:根据分析进行各个模块程序的设计与创建;1月4日~ 1 月6日:完成各个模块的程序编写;1月7 日~ 1 月8 日:完成全部程序的编写及系统测试;1月9日~ 1 月10日:完成课程设计报告的编写,答辩;摘要随着Java语言的不断发展和壮大,现在的Java已经广泛的应用于各个领域,包括医药,汽车工业,手机行业,游戏,等等地方。

Java游戏的开发为满足人们娱乐所需而设计。

本游戏为坦克大战游戏,是基于Java的图形用户界面而开发的,可以放松心情的小游戏。

游戏从系统操作简便、界面友好、灵活、上手即会的基本要求出发,完成整款游戏的开发。

游戏更设置了暂停、计分、音乐、速度,用户信息保存等功能,可以根据自己的爱好,挑战更高难度,为用户带来愉悦的体验。

坦克大战源码

坦克大战源码

坦克大战源码根据韩顺平老师视频所作/**坦克大战* 防重叠*/package ;import .*;import .*;import class MyTankGame5 extends JFrame implements ActionListener{quals("newgame")){quals("exit")){quals("saveExit")){quals("continue")){etImage"/"));image2=().getImage"/"));image3=().getImage"/"));etNodesAndEnemy();etSpeed();etSpeed(0);etSpeed();etSpeed(0);边的矩形(x, y, 5, 30,false);边的矩形(x, y, 5, 30,false);etSpeed();etSpeed(0);etSpeed(esp1);;import Node{int x;int y;int direct;public Node(int x,int y,int direct){=x;=y;=direct;}}//记录坦克信息的类class Recorder{//记录敌人和我的坦克数量private static int enlife=20;private static int mylife=10;private static int deadenemy=0;private static FileWriter fw=null;private static BufferedWriter bw=null;private static FileReader fr=null;private static BufferedReader br=null;private static Vector<EnemyTank> ets=new Vector<EnemyTank>();//从文件恢复记录static Vector<Node> nodes=new Vector<Node>();//完成读取public Vector<Node> getNodesAndEnemy(){try {//创建fr=new FileReader("d:\\chen\\");br=new BufferedReader(fr);String n="";//先读取第一行n=();deadenemy=(n);//用split分割从第二行开始取while((n=())!=null){//字符串str中从第一个字符起,//每遇到一个空格则切割为一个元素,//放入[]xyd数组中String []xyd=(" ");//这里的空格数与keepRecAndEnemyTank()中//写入(String record=+" "++" "+;)//!!!!!!!!!!!!!! //的空格数要对应,否则会报错!Node node1=new Node(xyd[0]),(xyd[1]),(xyd[2]));(node1);}} catch (IOException e) {// TODO Auto-generated catch block();}finally{//关闭文件//谁先开,谁后关!try {();();} catch (IOException e) {// TODO Auto-generated catch block();}}return nodes;}public static Vector<EnemyTank> getEts() {return ets;}public static void setEts(Vector<EnemyTank> ets) { = ets;}//保存击毁敌人的数目和敌人坐标,存盘退出public static void keepRecAndEnemyTank(){try {//创建fw=new FileWriter("d:\\chen\\");bw=new BufferedWriter(fw);(deadenemy+"\r\n");//保存当前敌人的数目和坐标for(int i=0;i<();i++){//取出第一个坦克EnemyTank et=(i);//保存活的if{String record=+" "++" "+;//写入(record+"\r\n");}}} catch (IOException e) {// TODO Auto-generated catch block();}finally{//关闭文件//谁先开,谁后关!try {();();} catch (IOException e) {// TODO Auto-generated catch block();}}}//保存击毁的敌人数目public static void keepRecording(){try {//创建fw=new FileWriter("d:\\chen\\");bw=new BufferedWriter(fw);(deadenemy+"\r\n");} catch (IOException e) {// TODO Auto-generated catch block();}finally{//关闭文件//谁先开,谁后关!try {();();} catch (IOException e) {// TODO Auto-generated catch block();}}}//读取出上一局击毁的敌人数目public static void getRecording(){try {//创建fr=new FileReader("d:\\chen\\");br=new BufferedReader(fr);String n=();deadenemy=(n);} catch (IOException e) {// TODO Auto-generated catch block();}finally{//关闭文件//谁先开,谁后关!try {();();} catch (IOException e) {// TODO Auto-generated catch block();}}}public static int getEnlife() {return enlife;}public static void setEnlife(int enlife) {= enlife;}public static int getMylife() {return mylife;}public static void setMylife(int mylife) {= mylife;}//减少数量public static void reduceMylife() {;}public static void reduceEnlife() {;}public static void deadenemy() {++;public static int getDeadenemy() {return deadenemy;}}class Tank{//坦克的横坐标int x=0;//纵坐标int y=0;//暂停速度int tempspeed;public int getTempspeed() {return tempspeed;}public void setTempspeed(int tempspeed) { = tempspeed;}//颜色int color;boolean isLive=true;public boolean isLive() {return isLive;}public void setLive(boolean isLive) {= isLive;}public int getColor() {return color;}public void setColor(int color) {= color;public int getX() {return x;}public void setX(int x) {= x;}public int getY() {return y;}public void setY(int y) {= y;}public Tank (int x,int y){=x;=y;}//坦克方向//0上,1右,2下,3左int direct=0;public int getDirect() {return direct;}public void setDirect(int direct) {= direct;}//坦克的速度int speed=3;public int getSpeed() {return speed;}public void setSpeed(int speed) {= speed;}}//敌方坦克class EnemyTank extends Tank implements Runnable{//设敌人可以复活10次static int newlife=10;//计数器static int counter=0;//让坦克随机产生步数//每次走多少步int steps=(int)()*50+10);int sleeptime=200; //睡眠时间//定义一个向量存放敌人子弹Vector<Shot> ss1=new Vector<Shot>();//在敌人创建和子弹死亡后在创建子弹public EnemyTank(int x,int y){super(x,y);}//定义一个向量,访问MyPanel的所有敌人坦克Vector<EnemyTank> ets=new Vector<EnemyTank>();//得到MyPanel的所有敌人坦克public void setEts(Vector<EnemyTank> vv){=vv;}//判断是否撞到了别的敌人坦克public boolean isTouchotherEnemy(){boolean b=false;//判断switch{case 0://向上//取出敌人所有坦克for(int i=0;i<();i++){//取出第一个坦克EnemyTank et=(i);//如果不是自己if(et!=this){//如果敌人方向是向上或者向下if==0||==2){if>=&&<=+20&&>=&&<=+30){return true;}if+20>=&&+20<=+20&&>=&&<=+30){return true;}}//如果敌人方向是向左或者向右if==1||==3){if>=&&<=+30&&>=&&<=+20){return true;}if+20>=&&+20<=+30&&>=&&<=+20){return true;}}}}break;case 1://向右//取出敌人所有坦克for(int i=0;i<();i++){//取出第一个坦克EnemyTank et=(i);//如果不是自己if(et!=this){//如果敌人方向是向上或者向下if==0||==2){if+30>=&&+30<=+20&&>=&&<=+30){return true;}if+30>=&&+30<=+20&&+20>=&&+20<=+30){return true;}}//如果敌人方向是向左或者向右if==1||==3){if>=&&<=+30&&>=&&<=+20){return true;}if+30>=&&+30<=+30&&+20>=&&+20<=+20){return true;}}}}break;case 2://向下//取出敌人所有坦克for(int i=0;i<();i++){//取出第一个坦克EnemyTank et=(i);//如果不是自己if(et!=this){//如果敌人方向是向上或者向下if==0||==2){if>=&&<=+20&&+30>=&&+30<=+30){return true;}if+20>=&&+20<=+20&&+30>=&&+30<=+30){return true;}}//如果敌人方向是向左或者向右if==1||==3){if>=&&<=+30&&+30>=&&+30<=+20){return true;}if+20>=&&+20<=+30&&+30>=&&+30<=+20){return true;}}}}break;case 3://向左//取出敌人所有坦克for(int i=0;i<();i++){//取出第一个坦克EnemyTank et=(i);//如果不是自己if(et!=this){//如果敌人方向是向上或者向下if==0||==2){if>=&&<=+20&&>=&&<=+30){return true;}if>=&&<=+20&&+20>=&&+20<=+30){return true;}}//如果敌人方向是向左或者向右if==1||==3){if>=&&<=+30&&>=&&<=+20){return true;}if>=&&<=+30&&+20>=&&+20<=+20){return true;}}}}break;}return b;}public void run() {// TODO Auto-generated method stubwhile(true){switch{case 0://继续让他再走几步for(int i=0;i<steps;i++){if(y>0&&!isTouchotherEnemy()){y-=speed;}// //撞到墙或者队友就转弯||isTouchotherEnemy()// else if(y<=0)// {// //让坦克变向// =(direct+1)%4;// }try {(sleeptime);} catch (InterruptedException e) {// TODO Auto-generated catch block();}}break;case 1://继续让他再走几步for(int i=0;i<steps;i++){if(x<&&!isTouchotherEnemy()){x+=speed;}// //撞到墙或者队友就转弯// else if(x>=// {// //让坦克变向// =(direct+1)%4;// }try {(sleeptime);} catch (InterruptedException e) {// TODO Auto-generated catch block();}}break;case 2://继续让他再走几步for(int i=0;i<steps;i++){if(y<&&!isTouchotherEnemy()){y+=speed;}//撞到墙或者队友就转弯// else if(y>=// {// //让坦克变向// =(direct+1)%4;// }try {(sleeptime);} catch (InterruptedException e) {// TODO Auto-generated catch block();}}break;case 3://继续让他再走几步for(int i=0;i<steps;i++){if(x>0&&!isTouchotherEnemy()){x-=speed;}// //撞到墙或者队友就转弯// else if(x<=0)// {// //让坦克变向// =(direct+1)%4;// }try {(sleeptime);} catch (InterruptedException e) {// TODO Auto-generated catch block();}}break;}//判断是否死亡,是否暂停if==false||=={//退出线程break;}//让坦克随机产生方向=(int)()*4);}}}//我的坦克class Hero extends Tank{//设我可以复活3次static int newlife=10;//计数器static int counter=0;Vector<Shot> ss=new Vector<Shot>();//子弹Shot s=null;public Hero(int x,int y){super(x,y);}//开火public void shotEnemy(){switch{case 0:s=new Shot(x+9,y-10,0);(s);break;case 1:s=new Shot(x+35,y+9,1);(s);break;case 2:s=new Shot(x+10,y+35,2);(s);break;case 3:s=new Shot(x-9,y+9,3);(s);break;}//创建线程对象Thread t1=new Thread(s);//启动();}//坦克向上移动public void moveUp(){if(y>0&&{y-=speed;}}//坦克向右移动public void moveRight(){if(x<&&{x+=speed;}}//坦克向下移动public void moveDown(){if(y<&&{y+=speed;}}//坦克向左移动public void moveLeft(){if(x>0&&{x-=speed;}}}//炸弹类class Bomb{//定义炸弹坐标int x,y;//炸弹生命int life=3;boolean isLive=true;public Bomb(int x,int y){=x;=y;}//减少生命值public void lifeDown(){if(life>0){life--;}else{=false;}}}//子弹类class Shot implements Runnable{//坐标int x;int y;int direct;int speed=5;public int getDirect() {return direct;}public void setDirect(int direct) {= direct;}public int getSpeed() {return speed;}public void setSpeed(int speed) {= speed;}//是否还活着boolean isLive=true;public Shot(int x,int y,int direct){=x;=y;=direct;}public void run(){while(true){try {(50);} catch (InterruptedException e) {// TODO Auto-generated catch block();}switch(direct){case 0://上方向y-=speed;break;case 1://向右x+=speed;break;case 2://向下y+=speed;break;case 3://向左x-=speed;break;}//"子弹坐标:"+"("+x+","+y+")");//子弹何时死亡?//判断该子弹是否碰到窗口边缘if(x<0||x>||y<0||y>{=false;break;}}}}//播放声音的类class AePlayWave extends Thread {private String filename;public AePlayWave(String wavfile){filename = wavfile;}public void run() {File soundFile = new File(filename);AudioInputStream audioInputStream = null;try {audioInputStream = (soundFile);} catch (Exception e1) {();return;}AudioFormat format = ();SourceDataLine auline = null;info = new , format);try {auline = (SourceDataLine) (info);(format);} catch (Exception e) {();return;}();int nBytesRead = 0;//这是缓冲byte[] abData = new byte[512];try {while (nBytesRead != -1){nBytesRead = (abData, 0, ;if (nBytesRead >= 0)(abData, 0, nBytesRead);}} catch (IOException e) {();return;} finally {();();}}}。

坦克大战源代码

坦克大战源代码

/**坦克大战*/package com.tankgame1;import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.util.*;public class TankGame1_0 extends JFrame implements KeyListener,ActionListener{ GamePanel gp;StartPanel sp;JMenuBar jmb=null;JMenu jm=null,jm1=null;JMenuItem jmi1,jmi2,jmi3,jmi4;TankGame1_0(){jmb=new JMenuBar();jm=new JMenu("文件");jm1=new JMenu("游戏设置");jmi1=new JMenuItem("新游戏");jmi1.setActionCommand("NEW");jmi1.addActionListener(this);jmi2=new JMenuItem("暂停");jmi2.setActionCommand("PAUSE");jmi2.addActionListener(this);jmi3=new JMenuItem("继续");jmi3.setActionCommand("CONTINUE");jmi3.addActionListener(this);jmi4=new JMenuItem("退出");jmi4.setActionCommand("QUIT");jmi4.addActionListener(this);jm.add(jmi1);jm.add(jmi2);jm.add(jmi3);jm.add(jmi4);jmb.add(jm);jmb.add(jm1);// gp=new GamePanel();// Thread t=new Thread(gp);// t.start();// //注册一下接收键盘的动作// this.addKeyListener(this);// //设置界面// this.add(gp);sp=new StartPanel();Thread t=new Thread(sp);t.start();this.add(sp);this.setJMenuBar(jmb);this.setBounds(180, 20, 305, 530);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }public static void main(String[] args) {TankGame1_0 tg=new TankGame1_0();}@Overridepublic void keyPressed(KeyEvent arg0) {if(!Record.pause){switch(arg0.getKeyCode()){case KeyEvent.VK_UP:if(!gp.mtk.isTouchTank()&&!gp.mtk.isTouchRiver())gp.mtk.moveUp();gp.mtk.setDirect(0);break;case KeyEvent.VK_DOWN:if(!gp.mtk.isTouchTank()&&!gp.mtk.isTouchRiver())gp.mtk.moveDown();gp.mtk.setDirect(2);break;case KeyEvent.VK_LEFT:if(!gp.mtk.isTouchTank()&&!gp.mtk.isTouchRiver())gp.mtk.moveLeft();gp.mtk.setDirect(3);break;case KeyEvent.VK_RIGHT:if(!gp.mtk.isTouchTank()&&!gp.mtk.isTouchRiver())gp.mtk.moveRight();gp.mtk.setDirect(1);break;}if(arg0.getKeyCode()=='A'||arg0.getKeyCode()=='a'){ gp.mtk.kitEnemy();}gp.repaint();}}@Overridepublic void keyReleased(KeyEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void keyTyped(KeyEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void actionPerformed(ActionEvent arg0) {if(arg0.getActionCommand().equals("NEW")){this.remove(sp);gp=new GamePanel();Thread t=new Thread(gp);t.start();//注册一下接收键盘的动作this.addKeyListener(this);//设置界面this.add(gp);this.setVisible(true);}if(arg0.getActionCommand().equals("PAUSE")){Record.pause=true;}if(arg0.getActionCommand().equals("CONTINUE")){ Record.pause=false;}if(arg0.getActionCommand().equals("QUIT")){System.exit(0);}}}class GamePanel extends JPanel implements Runnable{//定义的我的坦克MyTank mtk;River river=new River(50,300,200,30);Vector<SoilWall> sw=new V ector<SoilWall>();SoilWall s=null;Vector<SteelWall> stw=new Vector<SteelWall>();SteelWall st=null;Image im1=null;Image im2=null;Image im3=null;Image im4=null;Image im5=null;Vector<Bomb> bb=new Vector<Bomb>();//定义敌人的坦克int drtssize=4;Vector<EnemyTank> drts;EnemyTank dt=null;GamePanel(){//初始化爆炸的图片im1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif"));im2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif"));im3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif"));im4=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/img2.jpg"));im5=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/img1.jpg"));//土墙的初始化for(int i=5;i<24;i++,i++){s=new SoilWall(i*10,120);sw.add(s);}//钢墙的初始化for(int i=5;i<24;i++,i++){st=new SteelWall(i*10,200);stw.add(st);}//我的坦克初始化mtk=new MyTank(135,350,0,0);mtk.setStw(stw);mtk.setSw(sw);//敌人的坦克初始化drts=new Vector<EnemyTank>();for(int i=0;i<this.drtssize;i++){EnemyTank dt=new EnemyTank((i+1)*50,10,2,1);EnemyTank dt1=new EnemyTank((i+1)*50,60,2,1);Thread t=new Thread(dt);Thread t1=new Thread(dt1);t.start();t1.start();dt.setEts(drts);dt1.setEts(drts);dt.setR(river);dt1.setR(river);dt.setSw(sw);dt.setStw(stw);mtk.setEts(drts);mtk.setR(river);drts.add(dt);drts.add(dt1);}}public void paint(Graphics g){super.paint(g);//游戏的操作背景色g.setColor(Color.BLACK);//g.fillRect(0, 0, 300, 400);g.drawImage(im4, 0, 0, 300, 400,this);//让tank接收到panel上的对象mtk.setEts(drts);if(river!=null)mtk.setR(river);//画出河流if(river!=null)g.drawImage(im5, river.x, river.y, river.x1, river.y1, this); //画出墙来g.setColor(Color.yellow);for(int i=0;i<sw.size();i++){s=sw.get(i);if(s.isLive)g.fill3DRect(s.x, s.y, 15, 20, true);elsesw.remove(s);}g.setColor(Color.WHITE);for(int i=0;i<stw.size();i++){st=stw.get(i);if(st.isLive)g.fill3DRect(st.x, st.y, 15, 20, true);elsestw.remove(st);}//画出炸弹for(int i=0;i<bb.size();i++){Bomb bomb=bb.get(i);if(bomb.isLive==true){bomb.times--;if(bomb.times>14)g.drawImage(im1, bomb.x, bomb.y, 30, 30,this);else if(bomb.times>7)g.drawImage(im2, bomb.x, bomb.y, 30, 30,this);else if(bomb.times>0)g.drawImage(im3, bomb.x, bomb.y, 30, 30,this);}if(bomb.times==0){bomb.isLive=false;bb.remove(bomb);}}//画我的坦克if(Record.MyLife>0)drawTank(mtk.getX(),mtk.getY(),mtk.getDirect(),0,g);if(mtk.isLive==false){Record.MyLife--;mtk=new MyTank(135,350,0,0);mtk.setSw(sw);mtk.setStw(stw);}//画我的坦克的子弹g.setColor(Color.white);for(int i=0;i<mtk.bs.size();i++){Bullet b=mtk.bs.get(i);if(b!=null&&b.isLive==true)g.drawOval(b.getX(), b.getY(), 2, 2);if(b.isLive==false)mtk.bs.remove(b);}//画出敌人的坦克for(int i=0;i<drts.size();i++){EnemyTank dt=drts.get(i);if(dt.isLive==true && dt!=null)drawTank(dt.getX(),dt.getY(),dt.getDirect(),dt.getType(),g);if(dt.isLive==false){drts.remove(dt);Record.EnemyLife--;if(Record.EnemyLife>7){dt=new EnemyTank(120,10,2,1);dt.setSw(sw);dt.setStw(stw);Thread t=new Thread(dt);t.start();drts.add(dt);}}}//画出敌人的坦克的子弹g.setColor(Color.white);for(int i=0;i<drts.size();i++){EnemyTank dt=drts.get(i);if(dt.isLive==true)for(int j=0;j<dt.bs.size();j++){Bullet b=dt.bs.get(j);if(b.isLive)g.drawOval(b.getX(), b.getY(), 2, 2);if(b.isLive==false)dt.bs.remove(b);}}//显示游戏结束画面if(Record.EnemyLife<=0||Record.MyLife<=0){g.setColor(Color.YELLOW);g.setFont(new Font("宋体",Font.BOLD,20));g.drawString("GAME OVER!", 100, 100);Record.pause=true;}//显示游戏中的各种提示信息showInfo(g);}//显示信息的方法public void showInfo(Graphics g){Color c=null;c=g.getColor();this.drawTank(50, 420, 0, 0, g);this.drawTank(150, 420, 0, 1, g);g.setColor(Color.BLACK);g.setFont(new Font("黑体",Font.BOLD,20));g.drawString(Record.MyLife+"", 80, 435);g.drawString(Record.EnemyLife+"", 185, 435);g.setColor(c);}//用于绘制坦克public void drawTank(int x,int y,int direct,int type,Graphics g){ switch(type){case 0:g.setColor(Color.YELLOW);break;case 1:g.setColor(Color.green);break;}switch(direct){case 0:g.fill3DRect(x, y, 5, 30, false);g.draw3DRect(x+5, y+5, 10, 20, true);g.fill3DRect(x+15, y, 5, 30, false);g.fillOval(x+7, y+8, 6, 13);g.drawRect(x+10, y-4, 1, 17);break;case 2:g.fill3DRect(x, y, 5, 30, false);g.draw3DRect(x+5, y+5, 10, 20, true);g.fill3DRect(x+15, y, 5, 30, false);g.fillOval(x+7, y+8, 6, 13);g.drawRect(x+10, y+13, 1, 17);break;case 1:g.fill3DRect(x, y, 30, 5, false);g.draw3DRect(x+5, y+5, 20, 10, true);g.fill3DRect(x, y+15, 30, 5, false);g.fillOval(x+9, y+7, 13, 6);g.drawRect(x+15, y+9, 17, 1);break;case 3:g.fill3DRect(x, y, 30, 5, false);g.draw3DRect(x+5, y+5, 20, 10, true);g.fill3DRect(x, y+15, 30, 5, false);g.fillOval(x+9, y+7, 13, 6);g.drawRect(x-2, y+9, 17, 1);break;}}public void kill(Tank t,Bullet b){switch(t.direct){case 0:case 2:if(b.x>t.x&&b.y>t.y&&b.x<t.x+20&&b.y<t.y+30) {t.isLive=false;b.isLive=false;this.bb.add(new Bomb(t.getX(),t.getY()));}break;case 1:case 3:if(b.x>t.x&&b.y>t.y&&b.x<t.x+30&&b.y<t.y+20) {t.isLive=false;b.isLive=false;this.bb.add(new Bomb(t.getX(),t.getY()));}break;}}public void killWall(Wall w,Bullet b){if(b.x>=w.x&&b.y>=w.y&&b.x<=w.x+15&&b.y<=w.y+20) {w.life--;b.isLive=false;if(w.life<=0)w.isLive=false;}}@Overridepublic void run() {while(true){this.repaint();try {Thread.sleep(10);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}if(mtk!=null)for(int i=0;i<mtk.bs.size();i++){Bullet b=mtk.bs.get(i);if(b.isLive){for(int j=0;j<drts.size();j++){Tank t=drts.get(j);if(t.isLive){this.kill(t, b);}}}}for(int i=0;i<drts.size();i++){Tank et=drts.get(i);if(et.isLive){for(int j=0;j<et.bs.size();j++){Bullet b=et.bs.get(j);if(b.isLive&&b!=null){this.kill(mtk, b);}}}}for(int i=0;i<drts.size();i++){Tank et=drts.get(i);if(et.isLive){for(int j=0;j<et.bs.size();j++){Bullet b=et.bs.get(j);for(int k=0;k<sw.size();k++){SoilWall t=sw.get(k);if(t.isLive){this.killWall(t, b);}}for(int k=0;k<stw.size();k++){SteelWall t=stw.get(k);if(t.isLive){this.killWall(t, b);}}}}}for(int i=0;i<mtk.bs.size();i++){Bullet b=mtk.bs.get(i);if(b.isLive){for(int j=0;j<sw.size();j++){SoilWall t=sw.get(j);if(t.isLive){this.killWall(t, b);}}for(int j=0;j<stw.size();j++){SteelWall t=stw.get(j);if(t.isLive){this.killWall(t, b);}}}}}}}//游戏开始界面class StartPanel extends JPanel implements Runnable{ boolean f=true;public void paint(Graphics g){super.paint(g);g.setColor(Color.RED);g.setFont(new Font("宋体",Font.BOLD,30));if(f){g.drawString("请选择菜单", 60, 150);f=false;}elsef=true;}@Overridepublic void run() {while(true){try {Thread.sleep(200);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}this.repaint();}}}//坦克类class Tank{int x;int y;int direct;int type;int speed;boolean isLive=true;Vector<SoilWall> sw=new V ector<SoilWall>();public Vector<SoilWall> getSw() {return sw;}public void setSw(Vector<SoilWall> sw) {this.sw = sw;}public SoilWall getS() {return s;}public void setS(SoilWall s) {this.s = s;}public Vector<SteelWall> getStw() {return stw;}public void setStw(Vector<SteelWall> stw) {this.stw = stw;}public SteelWall getSt() {return st;}public void setSt(SteelWall st) {this.st = st;}SoilWall s=null;Vector<SteelWall> stw=new Vector<SteelWall>(); SteelWall st=null;Vector<EnemyTank> ets=new Vector<EnemyTank>(); River r=null;public boolean isLive() {return isLive;}public void setLive(boolean isLive) {this.isLive = isLive;}public River getR() {return r;}public void setR(River r) {this.r = r;}public Vector<Bullet> getBs() {return bs;}public void setBs(Vector<Bullet> bs) {this.bs = bs;}public Bullet getB() {return b;}public void setB(Bullet b) {this.b = b;}public Vector<EnemyTank> getEts() {return ets;}public void setEts(Vector<EnemyTank> ets) { this.ets = ets;}Vector<Bullet> bs=new V ector<Bullet>(); Bullet b;public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}public int getType() {return type;}public void setType(int type) {this.type = type;}public int getDirect() {return direct;}public void setDirect(int direct) {this.direct = direct;}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;}Tank(int x,int y,int direct,int type){this.direct=direct;this.x=x;this.y=y;this.type=type;}public void moveUp(){if(!Record.pause){if(y>=0&&!this.isTouchRiver()&&!this.isTouchWall()) y=y-speed;if(isTouchRiver())y=y+speed;}}public void moveDown(){if(!Record.pause){if(y<=370&&!this.isTouchRiver()&&!this.isTouchWall()) y=y+speed;if(isTouchRiver())y=y-speed;}}public void moveLeft(){if(!Record.pause){if(x>=0&&!this.isTouchRiver()&&!this.isTouchWall()) x=x-speed;if(isTouchRiver())x=x+speed;}}public void moveRight(){if(!Record.pause){if(x<=270&&!this.isTouchRiver()&&!this.isTouchWall()) x=x+speed;if(isTouchRiver())x=x-speed;}}public void kitEnemy(){switch(this.direct){case 0:b=new Bullet(this.x+9,this.y-6,0);break;case 1:b=new Bullet(this.x+31,this.y+8,1);break;case 2:b=new Bullet(this.x+9,this.y+31,2);break;case 3:b=new Bullet(this.x-4,this.y+8,3);break;}if(bs.size()<5){bs.add(b);Thread t=new Thread(b);t.start();}}public boolean isTouchWall(){boolean f=false;switch(this.direct){case 0:case 2:for(int i=0;i<sw.size();i++){s=sw.get(i);if(this.x>=s.x&&this.y>=s.y&&this.x<=s.x+15&&this.y<=s.y+20)f=true;if(this.x+20>=s.x&&this.y>=s.y&&this.x+20<=s.x+15&&this.y<=s.y+20) f=true;if(this.x>=s.x&&this.y+30>=s.y&&this.x<=s.x+15&&this.y+30<=s.y+20) f=true;if(this.x+20>=s.x&&this.y+30>=s.y&&this.x+20<=s.x+15&&this.y+30<=s.y+20)f=true;if(this.x+5>=s.x&&this.y>=s.y&&this.x+5<=s.x+15&&this.y<=s.y+20) f=true;if(this.x+5>=s.x&&this.y+20>=s.y&&this.x+5<=s.x+15&&this.y+20<=s.y+20)f=true;}if(this.direct==0&&f)this.y=this.y+this.speed;if(this.direct==2&&f)this.y=this.y-this.speed;for(int i=0;i<stw.size();i++){st=stw.get(i);if(this.x>=st.x&&this.y>=st.y&&this.x<=st.x+15&&this.y<=st.y+20)f=true;if(this.x+20>=st.x&&this.y>=st.y&&this.x+20<=st.x+15&&this.y<=st.y+20) f=true;if(this.x>=st.x&&this.y+30>=st.y&&this.x<=st.x+15&&this.y+30<=st.y+20) f=true;if(this.x+20>=st.x&&this.y+30>=st.y&&this.x+20<=st.x+15&&this.y+30<=st.y+20)f=true;if(this.x+5>=st.x&&this.y>=st.y&&this.x+5<=st.x+15&&this.y<=st.y+20) f=true;if(this.x+5>=st.x&&this.y+20>=st.y&&this.x+5<=st.x+15&&this.y+20<=st.y+20)f=true;}if(this.direct==0&&f)this.y=this.y+this.speed;if(this.direct==2&&f)this.y=this.y-this.speed;break;case 1:case 3:for(int i=0;i<sw.size();i++){s=sw.get(i);if(this.x>=s.x&&this.y>=s.y&&this.x<=s.x+15&&this.y<=s.y+20)f=true;if(this.x+30>=s.x&&this.y>=s.y&&this.x+30<=s.x+15&&this.y<=s.y+20) f=true;if(this.x>=s.x&&this.y+20>=s.y&&this.x<=s.x+15&&this.y+20<=s.y+20) f=true;if(this.x+30>=s.x&&this.y+20>=s.y&&this.x+30<=s.x+15&&this.y+20<=s.y+20)f=true;if(this.x>=s.x&&this.y+10>=s.y&&this.x<=s.x+15&&this.y+10<=s.y+20) f=true;if(this.x+30>=s.x&&this.y+10>=s.y&&this.x+30<=s.x+15&&this.y+10<=s.y+20)f=true;}if(this.direct==1&&f)this.x=this.x-this.speed;if(this.direct==3&&f)this.x=this.x+this.speed;for(int i=0;i<stw.size();i++){st=stw.get(i);if(this.x>=st.x&&this.y>=st.y&&this.x<=st.x+15&&this.y<=st.y+20)f=true;if(this.x+30>=st.x&&this.y>=st.y&&this.x+30<=st.x+15&&this.y<=st.y+20) f=true;if(this.x>=st.x&&this.y+20>=st.y&&this.x<=st.x+15&&this.y+20<=st.y+20) f=true;if(this.x+30>=st.x&&this.y+20>=st.y&&this.x+30<=st.x+15&&this.y+20<=st.y+20)f=true;if(this.x>=st.x&&this.y+10>=st.y&&this.x<=st.x+15&&this.y+10<=st.y+20) f=true;if(this.x+30>=st.x&&this.y+10>=st.y&&this.x+30<=st.x+15&&this.y+10<=st.y+20)f=true;}if(this.direct==1&&f)this.x=this.x-this.speed;if(this.direct==3&&f)this.x=this.x+this.speed;break;}return f;}public boolean isTouchTank(){boolean f=false;for(int i=0;i<ets.size();i++){EnemyTank et=ets.get(i);if(et!=this){switch(this.direct){case 0:if(et.direct==0||et.direct==2){if(this.x>=et.x&&this.y>=et.y&&this.x<=et.x+20&&this.y<=et.y+30)f=true;if(this.x+20>=et.x&&this.y>=et.y&&this.x+20<=et.x+20&&this.y<=et.y+30)f=true;}if(et.direct==1||et.direct==3){if(this.x>=et.x&&this.y>=et.y&&this.x<=et.x+30&&this.y<=et.y+20)f=true;if(this.x+20>=et.x&&this.y>=et.y&&this.x+20<=et.x+30&&this.y<=et.y+20)f=true;}break;case 1:if(et.direct==0||et.direct==2){if(this.x+30>=et.x&&this.y>=et.y&&this.x+30<=et.x+20&&this.y<=et.y+30)f=true;if(this.x+30>=et.x&&this.y+20>=et.y&&this.x+30<=et.x+20&&this.y+20<=et.y+30)f=true;}if(et.direct==1||et.direct==3){if(this.x+30>=et.x&&this.y>=et.y&&this.x+30<=et.x+30&&this.y<=et.y+20)f=true;if(this.x+30>=et.x&&this.y+20>=et.y&&this.x+30<=et.x+30&&this.y+20<=et.y+20)f=true;}break;case 2:if(et.direct==0||et.direct==2){if(this.x>=et.x&&this.y+30>=et.y&&this.x<=et.x+20&&this.y<=et.y+30){f=true;}if(this.x+20>=et.x&&this.y+30>=et.y&&this.x+20<=et.x+20&&this.y+30<=et.y+30){f=true;}}if(et.direct==1||et.direct==3){if(this.x>=et.x&&this.y+30>=et.y&&this.x<=et.x+30&&this.y<=et.y+20){f=true;}if(this.x+20>=et.x&&this.y+30>=et.y&&this.x+20<=et.x+30&&this.y+30<=et.y+20){f=true;}}break;case 3:if(et.direct==0||et.direct==2){if(this.x>=et.x&&this.y>=et.y&&this.x<=et.x+20&&this.y<=et.y+30)f=true;if(this.x>=et.x&&this.y+20>=et.y&&this.x<=et.x+20&&this.y+20<=et.y+30)f=true;}if(et.direct==1||et.direct==3){if(this.x>=et.x&&this.y>=et.y&&this.x<=et.x+30&&this.y<=et.y+20)f=true;if(this.x>=et.x&&this.y+20>=et.y&&this.x<=et.x+30&&this.y+20<=et.y+20)f=true;}break;}}}return f;}public boolean isTouchRiver(){boolean f=false;if(this.r!=null)switch(this.direct){case 0:case 2:if(this.x>=r.x&&this.y>=r.y&&this.x<=r.x+r.x1&&this.y<=r.y+r.y1)f=true;if(this.x+20>=r.x&&this.y>=r.y&&this.x+20<=r.x+r.x1&&this.y<=r.y+r.y1) f=true;if(this.x>=r.x&&this.y+30>=r.y&&this.x<=r.x+r.x1&&this.y+30<=r.y+r.y1) f=true;if(this.x+20>=r.x&&this.y+30>=r.y&&this.x+20<=r.x+r.x1&&this.y+30<=r.y+r.y1)f=true;if(this.direct==0&&f)this.y=this.y+this.speed;if(this.direct==2&&f)this.y=this.y-this.speed;break;case 1:case 3:if(this.x>=r.x&&this.y>=r.y&&this.x<=r.x+r.x1&&this.y<=r.y+r.y1)f=true;if(this.x+30>=r.x&&this.y>=r.y&&this.x+30<=r.x+r.x1&&this.y<=r.y+r.y1) f=true;if(this.x>=r.x&&this.y+20>=r.y&&this.x<=r.x+r.x1&&this.y+20<=r.y+r.y1) f=true;if(this.x+30>=r.x&&this.y+20>=r.y&&this.x+30<=r.x+r.x1&&this.y+20<=r.y+r.y1) f=true;if(this.direct==1&&f)this.x=this.x-this.speed;if(this.direct==3&&f)this.x=this.x+this.speed;break;}return f;}}//我的坦克类class MyTank extends Tank{MyTank(int x,int y,int direct,int type){super(x,y,direct,type);this.setDirect(0);this.setType(0);this.setSpeed(5);}}//敌人的坦克类class EnemyTank extends Tank implements Runnable{EnemyTank(int x,int y,int direct,int type){super(x,y,direct,type);this.setDirect(2);this.setType(1);this.setSpeed(5);}@Overridepublic void run() {while(true){while(Record.pause){try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}this.direct=(int)(Math.random()*4);this.kitEnemy();switch(this.direct){case 0:if(!this.isTouchTank()&&!this.isTouchRiver()&&!this.isTouchWall())for(int i=0;i<=15;i++){this.moveUp();try {Thread.sleep(50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}break;case 1:if(!this.isTouchTank()&&!this.isTouchRiver()&&!this.isTouchWall())for(int i=0;i<=15;i++){if(!this.isTouchTank()&&!this.isTouchRiver()&&!this.isTouchWall())this.moveRight();try {Thread.sleep(50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}break;case 2:if(!this.isTouchTank()&&!this.isTouchRiver()&&!this.isTouchWall())for(int i=0;i<=15;i++){if(!this.isTouchTank()&&!this.isTouchRiver()&&!this.isTouchWall())this.moveDown();try {Thread.sleep(50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}break;case 3:if(!this.isTouchTank()&&!this.isTouchRiver()&&!this.isTouchWall())for(int i=0;i<=15;i++){if(!this.isTouchTank()&&!this.isTouchRiver()&&!this.isTouchWall())this.moveLeft();try {Thread.sleep(50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}break;}}}}class Bullet implements Runnable{int x;boolean isLive=true;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;}int y;int direct;int speed=10;public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}public Bullet(int x,int y,int direct){this.x=x;this.y=y;this.direct=direct;}@Overridepublic void run() {while(true){while(Record.pause){try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}switch(this.direct){case 0:y=y-speed;try {Thread.sleep(50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}break;case 1:x=x+speed;try {Thread.sleep(50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}break;case 2:y=y+speed;try {Thread.sleep(50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}break;case 3:x=x-speed;try {Thread.sleep(50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}break;}if(x<-5||y<-5||x>300||y>400){this.isLive=false;break;}}}}//爆炸类class Bomb{int x;int y;int times=24;boolean isLive=true;Bomb(int x,int y){this.x=x;this.y=y;}}class Record{static int MyLife=3;static int EnemyLife=20;static boolean pause=false;}class River{int x,x1;int y,y1;River(int x,int y,int x1,int y1){this.x=x;this.y=y;this.x1=x1;this.y1=y1;}}class Wall{int x;int y;boolean isLive=true;int life=1;Wall(int x,int y){this.x=x;this.y=y;}}class SoilWall extends Wall{SoilWall(int x,int y){super(x, y);life=1;}}class SteelWall extends Wall{SteelWall(int x,int y){super(x,y);life=3;}}。

坦克大战Java代码Tank

坦克大战Java代码Tank

import java.awt.*;import java.awt.event.*;import java.util.*;//定义坦克类public class Tank {public static final int XSPEED = 5; //坦克水平速度public static final int YSPEED = 5; //坦克垂直速度public static final int WIDTH = 30; //坦克宽度public static final int HEIGHT = 30; //坦克高度private boolean live = true; //初始化生命为trueprivate BloodBar bb = new BloodBar();//声明血块变量private int life = 100; //初始化生命值为100TankClient tc;//生命坦克客户端变量private boolean good;//设置区分敌我两方标志private int x, y; //声明两个整型私有变量x,y坐标private int oldX, oldY;//记录上一步的坐标private static Random r = new Random(); //创建一个随机数r对象//声明方向变量private boolean bL=false, bU=false, bR=false, bD = false;enum Direction {L, LU, U, RU, R, RD, D, LD, STOP};/*坦克的八个方向和一个stop 状态*/private Direction dir = Direction.STOP;private Direction ptDir = Direction.D;private int step = r.nextInt(12) + 3;//整数step在[3,14]间随机取值//构造方法public Tank(int x, int y, boolean good) {this.x = x;this.y = y;this.oldX = x;this.oldY = y;this.good = good;}//重写构造方法public Tank(int x, int y, boolean good, Direction dir, TankClient tc) { this(x, y, good);this.dir = dir;this.tc = tc;}//画出坦克public void draw(Graphics g) {if(!live) {if(!good) {tc.tanks.remove(this);}return;}Color c = g.getColor();//获取颜色if(good) g.setColor(Color.RED);//我方坦克颜色设置为红色else g.setColor(Color.BLUE);g.fillOval(x, y, WIDTH, HEIGHT);g.setColor(c);//画出子弹if(good) bb.draw(g);switch(ptDir) {case L:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT/2);break;case LU:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y);break;case U:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y);break;case RU:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y);break;case R:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT/2);break;case RD:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT);break;case D:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y + Tank.HEIGHT);break;case LD:g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT);break;}move();}//移动方法void move() {//记录上一次坐标this.oldX = x;this.oldY = y;switch(dir) {case L:x -= XSPEED;break;case LU:x -= XSPEED;y -= YSPEED;break;case U:y -= YSPEED;break;case RU:x += XSPEED;y -= YSPEED;break;case R:x += XSPEED;break;case RD:x += XSPEED;y += YSPEED;break;case D:y += YSPEED;break;case LD:x -= XSPEED;y += YSPEED;break;case STOP:break;}if(this.dir != Direction.STOP) {this.ptDir = this.dir;}//控制坦克不出界if(x < 0) x = 0;if(y < 30) y = 30;if(x + Tank.WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - Tank.WIDTH;if(y + Tank.HEIGHT > TankClient.GAME_HEIGHT) y = TankClient.GAME_HEIGHT - Tank.HEIGHT;if(!good) {Direction[] dirs = Direction.values();if(step == 0) {step = r.nextInt(12) + 3;int rn = r.nextInt(dirs.length);//改变方向dir = dirs[rn];}step --;if(r.nextInt(40) > 38) this.fire();}}private void stay() {x = oldX;y = oldY;}//键按下的消息处理public void keyPressed(KeyEvent e) {int key = e.getKeyCode();switch(key) {case KeyEvent.VK_Q :if(!this.live) {this.live = true;this.life = 100;}break;case KeyEvent.VK_LEFT :bL = true;break;case KeyEvent.VK_UP :bU = true;break;case KeyEvent.VK_RIGHT :bR = true;break;case KeyEvent.VK_DOWN :bD = true;break;}locateDirection();}//设定方向void locateDirection() {if(bL && !bU && !bR && !bD) dir = Direction.L;else if(bL && bU && !bR && !bD) dir = Direction.LU;else if(!bL && bU && !bR && !bD) dir = Direction.U;else if(!bL && bU && bR && !bD) dir = Direction.RU;else if(!bL && !bU && bR && !bD) dir = Direction.R;else if(!bL && !bU && bR && bD) dir = Direction.RD;else if(!bL && !bU && !bR && bD) dir = Direction.D;else if(bL && !bU && !bR && bD) dir = Direction.LD;else if(!bL && !bU && !bR && !bD) dir = Direction.STOP;}//键抬起的消息处理public void keyReleased(KeyEvent e) {int key = e.getKeyCode();switch(key) {case KeyEvent.VK_CONTROL:fire();break;case KeyEvent.VK_LEFT :bL =true;break;case KeyEvent.VK_UP :bU = true;break;case KeyEvent.VK_RIGHT :bR = true;break;case KeyEvent.VK_DOWN :bD = true;break;case KeyEvent.VK_A ://按下A开火superFire();break;}locateDirection();}//开火发射方法public Missile fire() {if(!live) return null;int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2;int y = this.y + Tank.HEIGHT/2 - Missile.HEIGHT/2;Missile m = new Missile(x, y, good, ptDir, this.tc);tc.missiles.add(m);return m;}public Missile fire(Direction dir) {if(!live) return null;int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2;int y = this.y + Tank.HEIGHT/2 - Missile.HEIGHT/2;Missile m = new Missile(x, y, good, dir, this.tc);tc.missiles.add(m);return m;}//看坦克是否与子弹碰撞public Rectangle getRect() {return new Rectangle(x, y, WIDTH, HEIGHT);}//判断坦克是否还活着public boolean isLive() {return live;}//设定坦克的生死状态public void setLive(boolean live) {this.live = live;}//判断坦克状态是否良好public boolean isGood() {return good;}//坦克与墙相撞public boolean collidesWithWall(Wall w) {if(this.live && this.getRect().intersects(w.getRect())) {this.stay();return true;}return false;}//坦克与坦克相撞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;}//发射超级子弹private void superFire() {Direction[] dirs = Direction.values();for(int i=0; i<8; i++) {fire(dirs[i]);}}//返回坦克的生命值public int getLife() {return life;}//设置坦克的生命值public void setLife(int life) {this.life = life;}//创建生命值private class BloodBar {//在坦克上方画出生命条public void draw(Graphics g) {Color c = g.getColor();g.setColor(Color.RED);g.drawRect(x, y-10, WIDTH, 10);int w = WIDTH * life/100 ;g.fillRect(x, y-10, w, 10);g.setColor(c);}}//判断坦克是否吃到东西public boolean eat(Blood b) {if(this.live && b.isLive() && this.getRect().intersects(b.getRect())) { this.life = 100;b.setLive(false);return true;}return false;}}。

java 坦克大战游戏教程完整代码

java 坦克大战游戏教程完整代码

项目:用java做的坦克大战效果图:进入《坦克大战》游戏主界面点击开始菜单,即可进入游戏:使用W/A/S/D控制坦克移动,使用J发射子弹消灭所有坦克游戏获得胜利,被击中后,游戏将会结束:完整代码见下文,需要准备一个进入坦克大战的音乐声音,放在TankVs.java代码文件的同路径的shengyin文件夹下。

(没有也可以)代码运行方法:1,在以及配置好java JDK的情况下,将下面的完整的代码复制到并粘贴到记事本内,并将后缀.txt改为.java,文件命名为TankVs.java。

2,运行cmd,使用javac TankVs.java命令编译该java文件,编译成功后,使用java TankVs 命令即可运行游戏。

//以下为完整的坦克游戏代码import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.util.*;import java.io.*;import javax.sound.sampled.*;//导入声音类包public class TankVs extends JFrame implements ActionListener,Runnable{OverPanel op=null;WinPanel wp=null;FirstPanel fmp=null;MyPanel mp=null;JMenuBar jmb=null;JMenu jm=null;JMenuItem jmi=null;public static void main(String[] args){TankVs tk1=new TankVs();}public TankVs(){fmp=new FirstPanel();jmb=new JMenuBar();jm=new JMenu("开始");jmi=new JMenuItem("开始游戏");jm.add(jmi);jmb.add(jm);this.setJMenuBar(jmb);this.add(fmp);jmi.addActionListener(this);//添加单击事件监听this.setSize(1000,800);this.setLocation(200,100);this.setTitle("坦克大战");//文件的路径自己改一下this.setIconImage(new ImageIcon("D:\\JAVA\\高级\\坦克大战\\image\\tank.jpg").getImage());this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);this.setVisible(true);Thread t=new Thread(fmp);t.start();}public void actionPerformed(ActionEvent e){if(fmp!=null){this.remove(fmp);}if(op!=null){this.remove(op);}if(wp!=null){this.remove(wp);}mp=new MyPanel();mp.repaint();this.addKeyListener(mp);//添加键盘事件监听this.add(mp);Thread t=new Thread(mp);t.start();this.setVisible(true);Thread t1=new Thread(this);t1.start();shengyin sy=new shengyin(".\\shengyin\\tank.wav");sy.start();}public void run(){while(true){try{Thread.sleep(50);}catch(Exception e){}if(this.mp.dtk.size()==0){try{Thread.sleep(1000);}catch(Exception e){}this.wp=new WinPanel();this.add(wp);this.remove(mp);this.setVisible(true);}if(this.mp.mt.shengming==false){try{Thread.sleep(1000);}catch(Exception e){}this.op=new OverPanel();this.add(op);this.remove(mp);this.setVisible(true);}}}}class FirstPanel extends JPanel implements Runnable{int times=0;public void paint(Graphics g){super.paint(g);g.fillRect(0,0,800,600);if(times%2==0){g.setColor(Color.yellow);Font myFont=new Font("华文行楷",Font.BOLD,80);g.setFont(myFont);g.drawString("坦克大战",230,300);}}public void run(){while(true){try{Thread.sleep(500);}catch(Exception e){}times++;this.repaint();}}}class OverPanel extends JPanel{public void paint(Graphics g){super.paint(g);g.fillRect(0,0,800,600);g.setColor(Color.yellow);g.setFont(new Font("Cambria",Font.BOLD,80));g.drawString("GAME OVER",200,300);}}class WinPanel extends JPanel{public void paint(Graphics g){super.paint(g);g.fillRect(0,0,800,600);g.setColor(Color.yellow);g.setFont(new Font("Cambria",Font.BOLD,80));g.drawString("YOU WIN",200,300);}}class MyPanel extends JPanel implements KeyListener,Runnable{WoTank mt=null;Vector<DiTank> dtk=new Vector<DiTank>();//集合类+泛型,Vector适用与多线程,int tks=12;public MyPanel(){mt=new WoTank(260,500);mt.setSudu(8);for(int i=0;i<tks;i++){DiTank dt=new DiTank((750/(tks-1))*i,20);dt.setSudu(2);dtk.add(dt);Thread t=new Thread(dt);//启动敌人坦克线程t.start();}}public void paint(Graphics g){super.paint(g);g.fillRect(0,0,800,600);if(mt.shengming==true){this.drawTank(mt.getX(),mt.getY(),g,mt.getFangxiang(),0);//绘制我方坦克}for(int i=0;i<dtk.size();i++){DiTank dt=dtk.get(i);this.drawTank(dt.getX(),dt.getY(),g,dt.getFangxiang(),1);if(dt.zd.shengming==true){g.setColor(Color.white);g.fill3DRect(dt.zd.x,dt.zd.y,3,3,false);//绘制敌方坦克}}for(int i=0;i<mt.aa.size();i++)//绘制子弹{Zidan zd=mt.aa.get(i);if(zd.shengming==true && mt.shengming==true){g.setColor(Color.red);g.fill3DRect(zd.x,zd.y,3,3,false);}if(zd.shengming==false){mt.aa.remove(zd);}}}public void drawTank(int x,int y,Graphics g,int fangxiang,int leixing){switch(leixing){case 0://我的坦克g.setColor(Color.yellow);break;case 1://敌人的坦克g.setColor(Color.green);break;}switch(fangxiang){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+15,x+10,y-3);break;case 1://左方向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+15,y+10,x-3,y+10);break;case 2://下方向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+33);break;case 3://右方向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+15,y+10,x+33,y+10);break;}}public void keyTyped(KeyEvent e){}public void keyReleased(KeyEvent e){}public void keyPressed(KeyEvent e){if(mt.shengming==true){if(e.getKeyCode()==KeyEvent.VK_W){this.mt.setFangxiang(0);this.mt.xiangshang();}else if(e.getKeyCode()==KeyEvent.VK_A)this.mt.setFangxiang(1);this.mt.xiangzuo();}else if(e.getKeyCode()==KeyEvent.VK_S){this.mt.setFangxiang(2);this.mt.xiangxia();}else if(e.getKeyCode()==KeyEvent.VK_D){this.mt.setFangxiang(3);this.mt.xiangyou();}if(e.getKeyCode()==KeyEvent.VK_J){if(mt.aa.size()<8)//允许我的坦克同时存在8颗子弹{this.mt.fszd();Thread t=new Thread(this);//启动画板线程t.start();}}}this.repaint();}public void run(){while(mt.zd.shengming==true ||dtk.size()>0){try{Thread.sleep(50);}catch(Exception e){}for(int i=0;i<mt.aa.size();i++){Zidan zd=mt.aa.get(i);for(int j=0;j<dtk.size();j++){DiTank dt=dtk.get(j);this.jzdf(zd,dt);}for(int i=0;i<dtk.size();i++){DiTank dt=dtk.get(i);this.jzwf(dt.zd,mt);}this.repaint();//System.out.println("线程1");}}public void jzdf(Zidan zd,DiTank dt)//击中敌方{switch(dt.fangxiang){case 0:case 2:if(zd.x>dt.getX() && zd.x<dt.getX()+20 && zd.y>dt.getY() && zd.y<dt.getY()+30){zd.shengming=false;dt.shengming=false;mt.aa.remove(zd);dtk.remove(dt);}break;case 1:case 3:if(zd.x>dt.getX() && zd.x<dt.getX()+30 && zd.y>dt.getY() && zd.y<dt.getY()+20){zd.shengming=false;dt.shengming=false;mt.aa.remove(zd);dtk.remove(dt);}break;}}public void jzwf(Zidan zd,WoTank mt)//击中我方{if(mt.shengming==true){switch(mt.fangxiang){case 0:case 2:if(zd.x>mt.getX() && zd.x<mt.getX()+20 && zd.y>mt.getY() && zd.y<mt.getY()+30){zd.shengming=false;mt.shengming=false;}break;case 1:case 3:if(zd.x>mt.getX() && zd.x<mt.getX()+30 && zd.y>mt.getY() && zd.y<mt.getY()+20){zd.shengming=false;mt.shengming=false;}break;}}}}class Tank{private int x=0,y=0;int fangxiang=0;int sudu=1;boolean shengming=true;public int getSudu(){return sudu;}public void setSudu(int sudu){this.sudu=sudu;}public int getFangxiang(){return fangxiang;}public void setFangxiang(int fangxiang){this.fangxiang=fangxiang;}public int getX(){return x;}public int getY(){return y;}public void setX(int x){this.x=x;}public void setY(int y){this.y=y;}public Tank(int x,int y){this.x=x;this.y=y;}}class WoTank extends Tank{Zidan zd=new Zidan();Vector<Zidan> aa=new Vector<Zidan>();public WoTank(int x,int y){super(x,y);}public void xiangshang(){if(getY()-sudu>0){this.setY(this.getY()-sudu);}}public void xiangzuo(){if(getX()-sudu>0){this.setX(this.getX()-sudu);}}public void xiangxia(){if(getY()+sudu<520){this.setY(this.getY()+sudu);}}public void xiangyou(){if(getX()+sudu<750){this.setX(this.getX()+sudu);}}public void fszd(){switch(this.fangxiang){case 0:zd=new Zidan(super.getX()+10,super.getY(),super.getFangxiang(),true);break;case 1:zd=new Zidan(super.getX(),super.getY()+10,super.getFangxiang(),true);break;case 2:zd=new Zidan(super.getX()+10,super.getY()+30,super.getFangxiang(),true);break;case 3:zd=new Zidan(super.getX()+30,super.getY()+10,super.getFangxiang(),true);break;}aa.add(zd);Thread t=new Thread(zd);//启动子弹线程t.start();}}class Zidan implements Runnable{int x;int y;int fangxiang;int sudu=8;public boolean shengming=false;public Zidan(){}public Zidan(int x,int y,int fangxiang,boolean shengming){this.x=x;this.y=y;this.fangxiang=fangxiang;this.shengming=shengming;}public void run(){while(true){try{Thread.sleep(50);//凡是线程下的死循环,都要有个睡眠}catch(Exception e){}switch(fangxiang){case 0:y-=sudu;break;case 1:x-=sudu;break;case 2:y+=sudu;break;case 3:x+=sudu;break;}if(x<0 ||y<0 ||x>800 ||y>600){this.shengming=false;break;}//System.out.println("线程2");}}}class DiTank extends Tank implements Runnable{Zidan zd=new Zidan();public DiTank(int x,int y){super(x,y);}public void run(){while(this.shengming=true){try{Thread.sleep(50);}catch(Exception e){}this.fangxiang=(int)(Math.random()*4);switch(this.fangxiang){case 0:for(int i=0;i<30;i++){if(getY()-sudu<0) {break;}this.setY(this.getY()-sudu);try{Thread.sleep(50);}catch(Exception e){}}break;case 1:for(int i=0;i<30;i++)if(getX()-sudu<0) {break;}this.setX(this.getX()-sudu);try{Thread.sleep(50);}catch(Exception e){}}break;case 2:for(int i=0;i<30;i++){if(getY()+sudu>520) {break;}this.setY(this.getY()+sudu);try{Thread.sleep(50);}catch(Exception e){}}break;case 3:for(int i=0;i<30;i++){if(getX()+sudu>750) {break;}this.setX(this.getX()+sudu);try{Thread.sleep(50);}catch(Exception e){}}break;}if(this.zd.shengming==false){switch(fangxiang){case 0:zd=new Zidan(super.getX()+10,super.getY(),super.getFangxiang(),true);break;zd=new Zidan(super.getX(),super.getY()+10,super.getFangxiang(),true);break;case 2:zd=newZidan(super.getX()+10,super.getY()+30,super.getFangxiang(),true);break;case 3:zd=newZidan(super.getX()+30,super.getY()+10,super.getFangxiang(),true);break;}Thread t=new Thread(zd);t.start();}}}}class shengyin extends Thread//播放声音的类{private String wjm;public shengyin(String ypwj){wjm=ypwj;}public void run(){File wjl=new File(wjm);AudioInputStream ypsrl=null;//AudioInputStream类:音频输入流try{ypsrl=AudioSystem.getAudioInputStream(wjl);}catch(Exception e){}AudioFormat format=ypsrl.getFormat();SourceDataLine aqsj=null; info=new (SourceDataLine.class,format);try{aqsj=(SourceDataLine)AudioSystem.getLine(info);aqsj.open(format);}catch(Exception e){}aqsj.start();int zjtj=0;byte[] hczj=new byte[1024];try{while(zjtj!=1){zjtj=ypsrl.read(hczj,0,hczj.length);if(zjtj>=0){aqsj.write(hczj,0,zjtj);}}}catch(Exception e){}finally{aqsj.drain();aqsj.close();}}}。

JAVA课程设计坦克大战(含代码)

JAVA课程设计坦克大战(含代码)

JAVA课程设计坦克⼤战(含代码)Java程序课程设计任务书项⽬名称:坦克⼤战游戏的设计与开发英⽂名:Tank Battle1、主要内容:1、基于java的游戏开发与设计的分析;2、坦克⼤战游戏的架构搭建与设计实现;3、游戏可以通过⽤户可以注册新⽤户,修改⾃⼰的密码、个⼈资料。

2、具体要求1、系统的功能要求:(⼀)实现玩家试玩游戏功能,屏蔽⽤户注册和登陆功能(空功能)(⼆)玩家具备⼀个或者两个备选坦克战车(三)玩家可以选择⼀个到两个关卡进⾏作战(四)玩家可以选择两个敌对阵营中的⼀个加⼊,与其他AI战车坦克并肩作战(五)胜利判定条件为:时间制、全杀制(可选)(六)⾄少提供两个可以⾃由切换的视⾓(七)⾄少提供⼀种炮弹类型(⼋)假定项⽬:如果⽤户注册,登陆功能能够顺利做好,则提供⼀个玩家分数上传和排⾏榜功能。

2、学习并掌握以下技术:数据库的JDBC连接,JA V A编程语⾔,MySQL数据库的应⽤;JSP的开发与运⽤3、熟练使⽤以下开发⼯具:JCreator,Myeclipse ,MySQL ,PowerDesigner 等3、进度安排:12⽉28⽇~ 12 ⽉29 ⽇:查阅资料,完成⽂献综述,任务书;12⽉29 ⽇~ 12⽉31⽇:进⾏系统的需求分析与设计;1⽉1⽇~ 1⽉3⽇:根据分析进⾏各个模块程序的设计与创建;1⽉4⽇~ 1 ⽉6⽇:完成各个模块的程序编写;1⽉7 ⽇~ 1 ⽉8 ⽇:完成全部程序的编写及系统测试;1⽉9⽇~ 1 ⽉10⽇:完成课程设计报告的编写,答辩;摘要随着Java语⾔的不断发展和壮⼤,现在的Java已经⼴泛的应⽤于各个领域,包括医药,汽车⼯业,⼿机⾏业,游戏,等等地⽅。

Java游戏的开发为满⾜⼈们娱乐所需⽽设计。

本游戏为坦克⼤战游戏,是基于Java的图形⽤户界⾯⽽开发的,可以放松⼼情的⼩游戏。

游戏从系统操作简便、界⾯友好、灵活、上⼿即会的基本要求出发,完成整款游戏的开发。

韩顺平坦克大战1.0_2

韩顺平坦克大战1.0_2

package Tank2;import java.awt.Color;import java.awt.Graphics;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.Vector;import javax.swing.JFrame;import javax.swing.JPanel;/** 1.画坦克* 2.我的坦克可以动起来* 3.画出敌人的3辆坦克* 1.0*/public class MyTankGame2 extends JFrame{MyPanel mp = null;public static void main(String[] args) {// TODO 自动生成的方法存根MyTankGame2 mt = new MyTankGame2();}//构造函数public MyTankGame2(){mp = new MyPanel();this.add(mp);this.addKeyListener(mp);//注册监听this.setSize(400, 300);this.setLocation(100, 100);// this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭java虚拟机this.setVisible(true);}}//我的面板class MyPanel extends JPanel implements KeyListener{//定义一个我的坦克Hero hero = null;//定义敌人坦克组Vector<EnemyTank> ets=new Vector<EnemyTank>();int enSize =3;//构造函数public MyPanel(){//初始化我的坦克hero = new Hero(200, 150);//初始化敌人坦克for(int i=0;i<enSize; i++){//创建一辆敌人坦克对象EnemyTank et=new EnemyTank((i+1)*50,0);et.setColor(0);et.setDirect(2);//加入ets.add(et);}}//覆盖JPanel的paint方法public void paint(Graphics g){super.paint(g);g.fillRect(0, 0, 600, 400);//填充矩形//画出自己的坦克this.drawTank(hero.getX(), hero.getY(), g, this.hero.direct, 1);//调用函数//画出敌人坦克for(int i=0; i<ets.size(); i++){this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).getDirect(), 0);// this.drawTank(ets.get(i).getX()+50, ets.get(i).getY(), g, ets.get(i).getDirect(), 0);// this.drawTank(ets.get(i).getX()+100, ets.get(i).getY(), g, ets.get(i).getDirect(), 0);}}//画出坦克函数画笔方向坦克类型public void drawTank(int x, int y, Graphics g, int direct, int type){//判断是什么类型的坦克switch (type){case 0:g.setColor(Color.cyan);break;case 1:g.setColor(Color.yellow);break; }//判断方向switch (direct){//向上case 0://1.画出左边的矩形g.fill3DRect(x, y, 5, 30, false);//2.画出右边的矩形g.fill3DRect(x+15, y, 5, 30, false);//3.画出中间矩形g.fill3DRect(x+5, y+5, 10, 20, false);//画园g.fillOval(x+4, y+10, 10, 10);//画直线g.drawLine(x+9, y+15, x+9, y);break;case 1: //向右//1.画出上边的矩形g.fill3DRect(x, y, 30, 5, false);//2.画出下边的矩形g.fill3DRect(x, y+15, 30, 5, false);//3.画出中间矩形g.fill3DRect(x+5, y+5, 20, 10, false);//画园g.fillOval(x+10, y+4, 10, 10);//画直线g.drawLine(x+15, y+9, x+30, y+9);break;case 2: //向下//1.画出左边的矩形g.fill3DRect(x, y, 5, 30, false);//2.画出右边的矩形g.fill3DRect(x+15, y, 5, 30, false);//3.画出中间矩形g.fill3DRect(x+5, y+5, 10, 20, false);//画园g.fillOval(x+4, y+10, 10, 10);//画直线g.drawLine(x+9, y+15, x+9, y+30);break;case 3: //向左//1.画出上边的矩形g.fill3DRect(x, y, 30, 5, false);//2.画出下边的矩形g.fill3DRect(x, y+15, 30, 5, false);//3.画出中间矩形g.fill3DRect(x+5, y+5, 20, 10, false);//画园g.fillOval(x+10, y+4, 10, 10);//画直线g.drawLine(x+15, y+9, x, y+9);break;}}@Overridepublic void keyTyped(KeyEvent e) {// TODO 自动生成的方法存根}//键按下处理a表示向左, s表示向下, w表示向上, d表示向右public void keyPressed(KeyEvent e) {if(e.getKeyCode()==KeyEvent.VK_W){//设置我的坦克的方向this.hero.setDirect(0);//向上this.hero.moveUp();}else if(e.getKeyCode()==KeyEvent.VK_D){this.hero.setDirect(1);//向右this.hero.moveRight();}else if(e.getKeyCode()==KeyEvent.VK_S){this.hero.setDirect(2);//向下this.hero.moveDown();}else if(e.getKeyCode()==KeyEvent.VK_A){this.hero.setDirect(3);//向左this.hero.moveLift();}this.repaint();//重绘Panel}@Overridepublic void keyReleased(KeyEvent e) { // TODO 自动生成的方法存根}}。

java课程设计--坦克大战游戏(带源码)

java课程设计--坦克大战游戏(带源码)
g.setColor(Color.RED);
g.drawRect(x,y-5, WIDTH,5);
int w=WIDTH*life/100;
g.fillRect(x, y-5, w, 5);
g.setColor(c);
}
}
private class BloodBar_B{
public void draw(Graphics g){
}
Graphics gOffScreen=offScreenImage.getGraphics();
Color c=gOffScreen.getColor();
gOffScreen.setColor(Color.GRAY);
gOffScreen.fillRect(0, 0,GAME_WIDTH,GAME_HEIGHT);
g.drawString("击中个数:"+explodes.size(),10,70);
g.drawString("坦克数量:"+tanks.size(),10,90);
g.drawString("生命值:"+myTank.getLife(),10,110);
if(tanks.size()<=0){
public static final int HEIGHT = 30;
private boolean live = true;
private BloodBar_G bb_G=new BloodBar_G();
private BloodBar_B bb_B=new BloodBar_B();
private int life =100;
Color c=g.getColor();
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/**画坦克1.0*/import java.awt.Color;import java.awt.Graphics;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import javax.swing.JFrame;import javax.swing.JPanel;public class MyTankGame extends JFrame{MyPanel mp=null;public static void main(String[] args){MyTankGame mtk=new MyTankGame();}public MyTankGame(){mp=new MyPanel();this.add(mp);//把面板加入窗体//注册监听this.addMouseListener(mp);this.addKeyListener(mp);this.addMouseMotionListener(mp);this.addWindowListener(mp);this.setTitle("坦克大战");//窗体标题this.setSize(600,400);//大小,宽,高(像素)this.setLocation(300,300);//显示位置。

左边距,上边距//禁止用户改变窗口大小this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);//显示}}//我的面板class MyPanel extends JPanel implementsWindowListener,MouseListener,MouseMotionListener,KeyListener {//定义一个我的坦克Hero hero=null;Diren diren=null;public MyPanel(){hero=new Hero(300,200);diren=new Diren(100,0);//diren.start();}public void paint(Graphics g){super.paint(g);//画坦克(到时候封装到一个函数)//画左边矩形//g.setColor(Color.black);g.fillRect(0, 0, 600, 400);this.drawTank(hero.getX(),hero.getY(), g,hero.dstct,1);this.drawTank(diren.getX(),diren.getY(), g,1,0);this.drawTank(diren.getX()+50,diren.getY(), g,1,0);this.drawTank(diren.getX()+100,diren.getY(), g,1,0);}//画坦克的函数public void drawTank(int x,int y,Graphics g,int direct,int type) {switch (type){//0我的坦克case 0:g.setColor(Color.blue);break;//1敌人坦克case 1:g.setColor(Color.GREEN);break;}// 判断方向switch (direct){//0向上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,true);//中g.setColor(Color.RED);g.drawLine(x+10, y+15, x+10,y);//线g.setColor(Color.YELLOW);g.fillOval(x+5, y+10, 10, 10);//圆break;case 1://下g.fill3DRect(x, y, 5, 30,false);g.fill3DRect(x+15, y, 5, 30,false);g.fill3DRect(x+5, y+5, 10, 20,true);g.setColor(Color.RED);g.drawLine(x+10, y+15, x+10,y+30);g.setColor(Color.YELLOW);g.fillOval(x+5, y+10, 10, 10);break;case 2://左g.fill3DRect(x, y, 30, 5,false);g.fill3DRect(x, y+15, 30, 5,false);g.fill3DRect(x+5, y+5, 20, 10,true);g.setColor(Color.RED);g.drawLine(x+15, y+10, x,y+10);g.setColor(Color.YELLOW);g.fillOval(x+10, y+5, 10, 10);break;case 3://右g.fill3DRect(x, y, 30, 5,false);g.fill3DRect(x, y+15, 30, 5,false);g.fill3DRect(x+5, y+5, 20, 10,true);g.setColor(Color.RED);g.drawLine(x+15, y+10, x+30,y+10);g.setColor(Color.YELLOW);g.fillOval(x+10, y+5, 10, 10);break;}}public void keyPressed(KeyEvent e){// 键按下监听System.out.println("按下:"+e.getKeyChar());if(e.getKeyCode()==KeyEvent.VK_DOWN){hero.setY(hero.getY()+hero.getSpeed());hero.dstct=1;}else if(e.getKeyCode()==KeyEvent.VK_UP){hero.setY(hero.getY()-hero.getSpeed());hero.dstct=0;}else if(e.getKeyCode()==KeyEvent.VK_LEFT){hero.setX(hero.getX()-hero.getSpeed());hero.dstct=2;}else if(e.getKeyCode()==KeyEvent.VK_RIGHT){hero.setX(hero.getX()+hero.getSpeed());hero.dstct=3;}this.repaint();}public void keyReleased(KeyEvent e) {// 键松开监听}public void keyTyped(KeyEvent e) {// 键的一个值被输出}public void mouseClicked(MouseEvent e) {//点击System.out.println("鼠标被点击了x="+e.getX()+"y="+e.getY()); }public void mouseEntered(MouseEvent e) {// TODO Auto-generated method stub//进入System.out.println("鼠标进入x="+e.getX()+"y="+e.getY()); }public void mouseExited(MouseEvent e) {// TODO Auto-generated method stub//离开System.out.println("鼠标离开x="+e.getX()+"y="+e.getY()); }public void mousePressed(MouseEvent e) {// TODO Auto-generated method stub//压下System.out.println("鼠标被按下x="+e.getX()+"y="+e.getY()); }public void mouseReleased(MouseEvent e) {// TODO Auto-generated method stubSystem.out.println("鼠标被松开x="+e.getX()+"y="+e.getY());//松开}public void mouseDragged(MouseEvent e) {// 拖拽System.out.println("鼠标被拖拽x="+e.getX()+"y="+e.getY()); }public void mouseMoved(MouseEvent e) {// 移动System.out.println("鼠标被移动x="+e.getX()+"y="+e.getY()); }public void windowActivated(WindowEvent e) {// 激活TODO Auto-generated method stub}public void windowClosed(WindowEvent e) {// 窗口关闭}public void windowClosing(WindowEvent e) {//正在关闭TODO Auto-generated method stub}public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub}public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub}public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub}public void windowOpened(WindowEvent e) { // 打开TODO Auto-generated method stub}}//坦克父类class Tank{//坦克横坐标int x=0;//坦克纵坐标int y=0;int speed=5;//速度static int dstct=0;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 static int getDstct() {return dstct;}public static void setDstct(int dstct) {Tank.dstct = dstct;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}}//我的坦克class Hero extends Tank{public Hero(int x ,int y ){super(x, y);}// public int tankup// {// y=y-getSpeed();// }// public int tankdown// {// y=y+speed;// }// public int tankleft// {// x=x-speed;// }// public int tankright// {// x=x+speed;// }//}//敌人坦克class Diren extends Tank implements Runnable {public Diren(int x ,int y ){super(x, y);}public void run(){while(true){ //休眠一秒//1000秒=1秒.进入堵塞。

相关文档
最新文档