java课程设计--坦克大战游戏(带源码)
坦克大战程序代码
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坦克大战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。
Java语言坦克大战游戏设计附有代码和图片
JISHOU UNIVERSITY本科生毕业设计题目:基于java语言坦克大战游戏开发作者:学号:所属学院:信息科学与工程学院专业年级:指导教师:职称:完成时间:吉首大学教务处制目录基于java语言坦克大战游戏开发 (1)摘要 (1)Based on the development of Java language Tank Wars Game (2)第一章游戏简要说明 (3)第二章开发环境介绍 (4)2.1 开发环境 (4)2.2 java语言特点简介 (4)2.3 Eclipse开发工具的简介 (5)第三章系统总体设计 (6)3.1程序功能的总和 (6)3.2主体模块 (6)3.3子弹模块 (6)3.4坦克模块 (7)3.5 墙模块 (7)3.6血液模块 (7)3.7爆炸模块 (8)第四章游戏详细设计过程 (9)4.1创建游戏窗口 (9)4.2改变游戏背景颜色。
(11)4.3创建坦克 (11)4.4 让坦克动起来 (12)4.5改变窗口的大小 (13)4.6让坦克受控制 (14)4.7创建一个Tank类 (15)4.8让坦克向8个方向移动 (16)4.9修改4.8中所设计的不足 (18)4.10添加子弹类 (19)4.11根据己方坦克的位置,打出子弹 (21)4.12修改坦克停下来不能打出子弹的问题 (21)4.13让坦克能够打出多发子弹 (23)4.14解决炮弹不消亡和坦克出界的问题 (23)4.15增加敌方坦克 (24)4.16击毙出现的敌方坦克 (25)4.17加入爆炸效果 (26)4.18添加多辆敌方坦克 (27)4.19让敌方坦克智能化 (29)4.20建立游戏障碍物 (31)4.21阻止坦克互相穿越 (32)4.22为我方坦克添加大招和血条 (33)4.23添加血块 (34)4.24重新载入敌军或我军坦克 (35)4.25加入图片 (35)第五章游戏的可行性测试 (37)5.1游戏界面介绍 (37)5.2游戏运行测试 (40)第六章总结 (43)参考文献 (44)基于java语言坦克大战游戏开发摘要随着网络科技的不断发展,我们的生活出现了日新月异的变化,特别是在游戏这一块,出现了前所未有的繁荣,各种各样的游戏进入我们的生活,而当初的“俄罗斯方块”、“坦克大战”、“贪吃蛇”等这些经典中的经典的游戏曾被多少人所知,更是多少80后甚至90后常聊的话题。
JAVA课程设计 坦克大战
得分显示:在游戏 界面实时显示得分 情况,方便玩家了 解游戏进度
减少内存占用:优 化数据结构,减少 不必要的内存分配
提高渲染效率: 优化渲染算法, 减少渲染时间
优化网络通信:减 少网络延迟,提高 网络通信效率
优化AI算法:提高 AI算法的效率和准 确性,提高游戏体 验
界面布局:合 理规划界面布 局,提高用户
玩家需要控制坦克,消灭敌 人,保护自己的基地
游戏支持多人在线对战,玩 家可以与朋友一起挑战
玩家控制 坦克,在 战场上与 敌人战斗
坦克可以 发射炮弹, 摧毁敌人 或障碍物
玩家需要 躲避敌人 的攻击, 保护自己 的坦克
游戏有各 种关卡, 难度逐渐 增加
玩家可以 通过升级 坦克,提 高战斗力
游戏支持 多人在线 对战,增 加游戏乐 趣
生命值耗尽:当 玩家的生命值降 至0时,游戏结束
时间耗尽:当游 戏时间耗尽时, 游戏结束
胜利条件:当玩 家达到胜利条件 时,游戏结束
失败条件:当玩 家达到失败条件 时,游戏结束
得分规则:击毁敌 方坦克得分,被敌 方坦克击毁扣分
得分方式:根据击 毁敌方坦克的数量 和难度计算得分
得分限制:得分上 限为1000分,达到 上限后不再增加得 分
,a click to unlimited possibilities
汇报人:
01
02
03
04
05
06
游戏类型:坦克大战
游戏背景:二战时期
游戏目标:摧毁敌方坦克,保 护自己的基地
游戏玩法:玩家控制坦克,通 过射击和躲避来消灭敌方坦克
游戏包含多个关卡,每个关 卡有不同的难度和挑战
设计一款坦克大战游戏
游戏界面:包括 菜单、地图、角 色、道具等元素
JAVA课程设计坦克大战含代码
游戏结束判断
生命值耗尽:当坦克的生命值降至0时,游戏结束 游戏时间结束:当游戏时间达到预设值时,游戏结束 胜利条件达成:当一方达到预设的胜利条件时,游戏结束 玩家主动退出:当玩家主动选择退出游戏时,游戏结束
游戏得分计算
得分规则:击毁敌 方坦克、保护己方 坦克、完成特定任 务等
得分方式:根据击 毁敌方坦克的数量、 保护己方坦克的数 量、完成任务的难 度等计算得分
得分显示:在游戏 界面实时显示得分 情况,方便玩家了 解游戏进度
得分记录:游戏结 束后,记录玩家的 分情况,方便玩 家查看历史成绩
游戏优化与扩展
游戏性能优化
优化算法: 使用更高 效的算法, 如动态规 划、贪心 算法等
减少内存 占用:优 化数据结 构,减少 不必要的 内存占用
优化渲染: 使用更高 效的渲染 技术,如 OpenGL、 DirectX 等
优化网络 传输:使 用更高效 的网络传 输协议, 如TCP、 UDP等
优化AI: 使用更高 效的AI算 法,如神 经网络、 遗传算法 等
优化资源 管理:合 理分配系 统资源, 避免资源 浪费
游戏扩展功能实现
增加新的坦克类型:如重型坦克、轻 型坦克等
增加新的地图:如沙漠地图、雪地地 图等
增加新的游戏模式:如团队模式、生 存模式等
音效类型:背景音乐、战斗音效、 胜利音效等
音效处理:音量调节、音效混合、 音效剪辑等
添加标题
添加标题
添加标题
添加标题
音效来源:自行创作、购买版权、 网络下载等
音效与游戏场景的结合:根据游戏 场景选择合适的音效,增强游戏体 验
游戏实体设计
坦克类设计
坦克属性:生命值、攻击 力、防御力、速度等
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坦克大战,坦克大战Java代码这是一个Java坦克大战的源码,实现了大部分功能,是学习Java绘图技术的好例子主类:package com.qq.TankGame;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.Paint;import java.awt.Panel;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Vector;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JTextArea;/////**// * 功能:java绘图// * @author Administrator// *// */public class TankGame extends JFrame implements ActionListener{MyPanel mp = null;myStartPanel msp = null;EnemyTask em = null;JMenuBar jmb = null;JMenu jm = null;JMenuItem jmi1 = null;JMenuItem jmi2 = null;JMenuItem jmi3 = null;public static void main(String[] args) {TankGame tankGame = new TankGame();}public TankGame(){jmb = new JMenuBar();jm = new JMenu("游戏(G)");jm.setMnemonic('G');jmi1 = new JMenuItem("开始游戏(S)");jmi1.setMnemonic('S');jmi1.addActionListener(this);jmi1.setActionCommand("newGame");jmi2 = new JMenuItem("继续游戏(O)");jmi2.setMnemonic('O');jmi3 = new JMenuItem("退出游戏(X)");jmi3.setMnemonic('X');jmi3.addActionListener(this);jmi3.setActionCommand("exit");//启动mp线程//em = new EnemyTask();//mp = new MyPanel();msp = new myStartPanel();this.setJMenuBar(jmb);this.add(msp);//this.add(mp);jmb.add(jm);jm.add(jmi1);jm.add(jmi2);jm.add(jmi3);this.setSize(600,500);//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true);//this.addKeyListener(mp);Thread t1 = new Thread(msp);t1.start();// Thread t = new Thread(mp);// t.start();}@Overridepublic void actionPerformed(ActionEvent arg0) {if(arg0.getActionCommand().equals("newGame")){mp = new MyPanel();Thread t = new Thread(mp);t.start();this.remove(msp);this.remove(mp);this.add(mp);this.setVisible(true);this.addKeyListener(mp);//Recoder.getRecording();}else if(arg0.getActionCommand()== "exit"){//Recoder.KeepRecording();Recoder.KeepExit();//KeepExit();System.exit(0);}}}class myStartPanel extends JPanel implements Runnable{ int times = 0;public void paint(Graphics g ){super.paint(g);g.fillRect(0, 0, 400, 300);if(times%2 ==0){g.setColor(Color.BLUE);Font f = new Font("华文新魏", Font.BOLD, 30);g.setFont(f);g.drawString("stage:1", 150, 150);}}@Overridepublic void run() {while(true){try {Thread.sleep(300);} catch (InterruptedException e) {e.printStackTrace();}this.repaint();times ++;}}}class MyPanel extends JPanel implements KeyListener,Runnable{ int [] TanksX = new int[20];int [] TanksY = new int[20];int [] ShotsX = new int[100];int [] ShotsY = new int[100];// int [][]Tanks = new int[20][2];// int [][]Shots = new int[20][2];int [] ETdirects = new int[20];int [] HeroShotsX = new int[10];int [] HeroShotsY = new int[10];static Hero hero = null;EnemyTask et = null;shot s = null;boolean isPaintShot = true;int a = 0, b = 0 , c = 0;int [] directs = new int [10];static //敌人坦克集合Vector<EnemyTask> ets = new Vector<EnemyTask>();int enSize = 3; //敌人坦克初始数量int enSizes = 5; //敌人坦克画面总数量int hintEnemyTanks = 0;//击中敌人坦克数量public void showInfo(Graphics g ){//画提示信息//画出提出坦克this.drawTank(80, 330, g, 0, 1);this.drawTank(160, 330, g, 0, 0);g.setColor(Color.BLACK);g.drawString(Recoder.getEnNum() + "", 105, 350);g.drawString(Recoder.getMyLife() + "", 185, 350);//画出成绩g.setColor(Color.BLACK);Font f = new Font("宋体", Font.BOLD, 15);g.setFont(f);g.drawString("你的总成绩:", 400, 20);this.drawTank(410, 30, g, 0, 1);g.drawString((20 - Recoder.getEnNum()) + "", 435, 50);}public void paint(Graphics g){super.paint(g);g.fillRect(0, 0, 400, 300);this.showInfo(g);//画出提示信息//自己坦克if(hero.isLive == true){this.drawTank(hero.getX(), hero.getY(), g, hero.direct, hero.type); }else{if(Recoder.getMyLife() > 0){hero = new Hero(30, 270 ,0,0,10 , true);hero.isLive = true;}}//画出敌人坦克for(int i = 0; i <ets.size(); i++){EnemyTask et = ets.get(i);if(!et.isLive){ets.remove(i);if(Recoder.getEnNum() >0){this.drawTank(50, 0, g, 1, 1);ets.add(et);//et.isLive = true;Thread t = new Thread(et);t.start();}// if(Recoder.getEnNum() > 0){// et.isLive = true;// }}if(et.isLive){this.drawTank(et.getX(), et.getY(),g, et.direct, et.type);System.out.println("et.ss.size()"+et.ss.size());for(int j = 0; j < et.ss.size(); j++){//敌人坦克子弹s = et.ss.get(j);if(s.isLive){if(isPaintShot){g.fill3DRect(s.x, s.y, 5, 10, false);}}else{et.ss.remove(j);}}}// if(!et.isLive){// ets.remove(i);// }}//画出子弹for(int i = 0; i< hero.ss.size(); i++){if(hero.ss.get(i)!= null && hero.ss.get(i).isLive == true){ g.fill3DRect(hero.ss.get(i).x, hero.ss.get(i).y, 5, 10, false); }if(hero.ss.get(i).isLive == false){hero.ss.remove(hero.ss.get(i));}}}// public void hintTank2(shot s , Hero et){// switch (et.direct) {// case 0:// case 1:// if(s.x < et.x + 20 && s.x > et.x// && s.y < et.y + 30 && s.y > et.y){// s.isLive = false;// et.isLive = false;// }// //break;// case 2:// case 3:// if(s.x < et.x + 30 && s.x > et.x// && s.y < et.y + 20 && s.y > et.y){// s.isLive = false;// et.isLive = false;// }// //break;////// default://// break;// }//// }public boolean hintTank(shot s , Tank et){boolean b = false;switch (et.direct) {case 0:case 1:if(s.x < et.x + 20 && s.x > et.x&& s.y < et.y + 30 && s.y > et.y){s.isLive = false;et.isLive = false;b = true;}break;case 2:case 3:if(s.x < et.x + 30 && s.x > et.x&& s.y < et.y + 20 && s.y > et.y){s.isLive = false;et.isLive = false;b = true;}break;// default:// break;}return b;}public void drawTank (int x , int y , Graphics g , int direct , int type){ switch(type){//坦克类型case 0:g.setColor(Color.RED);break;case 1:g.setColor(Color.BLUE);break;}switch (direct) {case 0://向上g.fill3DRect(x, y, 5, 30, false );g.fill3DRect(x + 15, y, 5, 30, false );g.fill3DRect(x + 5, y + 5, 10, 20, false );g.fillOval(x + 5, y + 10, 10, 10);g.drawLine(x + 10, y,x + 10, y + 15);break;case 1://向下g.fill3DRect(x, y, 5, 30, false );g.fill3DRect(x + 15, y, 5, 30, false );g.fill3DRect(x + 5, y + 5, 10, 20, false );g.fillOval(x + 5, y + 10, 10, 10);g.drawLine(x + 10, y + 15,x + 10,y + 30 ); break;case 2://向左g.fill3DRect(x, y, 30, 5, false );g.fill3DRect(x , y + 15, 30, 5, false );g.fill3DRect(x + 5, y + 5, 20, 10, false );g.fillOval(x + 10, y + 5, 10, 10);g.drawLine(x , y + 10,x + 15,y + 10 );break;case 3://向右g.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 + 30 , y + 10); break;default:break;}}public MyPanel (){hero = new Hero(30, 270 ,0,0,10 , true);//tanks = new tank(100,100, 2 ,1);//初始化敌人坦克Recoder.setHero(hero);for(int i = 0; i < enSize; i++){//创建敌人坦克的对象Recoder.getRecording();EnemyTask et = new EnemyTask((i + 1)*50, 0, 1, 1, 5 ,true); et.setEts(ets);Recoder.setEtss(ets);//传ets到RecoderThread t = new Thread(et);t.start();shot s = new shot(et.x + 10 , et.y + 30, et.direct);et.ss.add(s);Thread t2 = new Thread(s);t2.start();ets.add(et);//加入到集合中//et.setColor(1);}Recoder.setEnSize(enSize);}@Overridepublic void keyPressed(KeyEvent arg0) {// TODO Auto-generated method stubif(arg0.getKeyCode() == KeyEvent.VK_S){//hero.setX(hero.getX() ++ 1);// int i= hero.getY();// i += hero.speed;// hero.setY(i);this.hero.MoveDown();hero.setD(1);}if(arg0.getKeyCode() == KeyEvent.VK_W){//hero.setX(hero.getX() ++ 1);// int i= hero.getY();// i -= hero.speed;// hero.setY(i);hero.MoveUp();hero.setD(0);}if(arg0.getKeyCode() == KeyEvent.VK_D){//hero.setX(hero.getX() ++ 1);// int i= hero.getX();// i += hero.speed;// hero.setX(i);hero.MoveRight();hero.setD(3);}if(arg0.getKeyCode() == KeyEvent.VK_A){//hero.setX(hero.getX() ++ 1);// int i= hero.getX();// i -= hero.speed;// hero.setX(i);hero.MoveLeft();hero.setD(2);}//判断是否按下Jif(arg0.getKeyCode() == KeyEvent.VK_J){if(hero.ss.size() <= 4){this.hero.shotEnemy();//System.out.println("j");}}if(arg0.getKeyCode() == KeyEvent.VK_SPACE){//空格暂停 if(hero.speed != 0){a = hero.speed ;hero.speed = 0;for(int i = 0; i < ets.size(); i ++){//System.out.println("aaaaaaaaaaaaaa");EnemyTask et = ets.get(i);//System.out.println("speed" + et.speed);b = et.speed;et.speed = 0;directs[i] = et.direct;//System.out.println(et.direct);//System.out.println(directs[i]);//et.direct = 0;et.isdirects = false;for(int j = 0 ; j < et.ss.size(); j ++){shot s = et.ss.get(j);c = s.speed;s.speed = 0;System.out.println("aa");}}//System.out.println("b1 =" + b);isPaintShot = false;}else{hero.speed = a;//System.out.println("a2 =" + a);for(int i = 0; i < ets.size(); i ++){//System.out.println("bbbbbbbbbbbbbbbbbbb");EnemyTask et = ets.get(i);et.isdirects = true;et.speed = b;et.direct = directs[i];for(int j = 0 ; j < et.ss.size(); j ++){shot s = et.ss.get(j);s.speed = c;isPaintShot = true;}}s.isShot = true;}}if(arg0.getKeyCode() == KeyEvent.VK_K){this.KeepExit();System.exit(0);}if(arg0.getKeyCode() == KeyEvent.VK_L){this.GetKeeper();}this.repaint();}@Overridepublic void keyReleased(KeyEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void keyTyped(KeyEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void run() {//每隔100mswhile(true){try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}this.hintEnemyTank();this.hintMyTank();// for(int i = 0; i < et.ss.size(); i ++){// shot myshot = et.ss.get(i);// if(et.isLive == true){//// if(hero.isLive = true){// this.hintTank2(myshot, hero);// }//// }// }this.repaint();}}private void hintMyTank() {for(int i = 0; i < ets.size(); i++){EnemyTask et = ets.get(i);for(int j = 0; j < et.ss.size(); j++ ){shot s = et.ss.get(j);if(s != null){if(hero.isLive){this.hintTank(s, hero);if(this.hintTank(s, hero)){//判断是否击中,返回true则hero数量减一Recoder.setMyLife(Recoder.getMyLife() - 1 );}}}}}}private void hintEnemyTank() {for(int i = 0; i < hero.ss.size(); i ++){shot myshot = hero.ss.get(i);if(myshot.isLive == true){for(int j = 0; j < ets.size(); j ++){EnemyTask et = ets.get(j);if(et.isLive = true){this.hintTank(myshot, et);if(this.hintTank(myshot, et)){hintEnemyTanks ++;Recoder.setEnNum(Recoder.getEnNum() - 1);}}}}}}private void KeepExit(){int [] heros = new int[]{hero.getX(),hero.getY(),hero.direct,};for(int i = 0 ; i < ets.size(); i ++){EnemyTask et = ets.get(i);TanksX[i] = et.getX();TanksY[i] = et.getY();ETdirects [i] = et.direct;for(int j = 1 ; j < et.ss.size(); j++){shot s = et.ss.get(j);ShotsX[j] = s.x;ShotsY[j] = s.y;}}for(int i = 0 ; i < hero.ss.size(); i++ ){shot s = hero.ss.get(i);HeroShotsX[i] = s.x ;HeroShotsY[i] = s.y;}BufferedWriter bw = null;try {bw = new BufferedWriter(new FileWriter("d:\\aa\\TanksX.txt")); for(int i = 0 ; i < enSize ; i ++){bw.write(TanksX[i] + "\r\n");bw.write(TanksY[i] + "\r\n");}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {bw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public void GetKeeper (){//继续游戏时还原数据BufferedReader br = null ;try {br = new BufferedReader(new FileReader("d:\\aa\\TanksX.txt")); for(int i = 0; i < enSize*2 ; i ++){if(i % 2 !=0){TanksY[i] = Integer.parseInt(br.readLine());System.out.println("TanksY" + TanksY[i]);}else{TanksX[i] = Integer.parseInt(br.readLine());System.out.println("TanksX" + TanksX[i]);}}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {br.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}//class tank extends Tank{//// public tank(int x, int y, int direct,int type) {// super(x, y, direct,type);// // TODO Auto-generated constructor stub// }////}下面是工具类:package com.qq.TankGame;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Vector;class Recoder{/****/private static Vector<EnemyTask> ets = new Vector<EnemyTask>(); public static Vector<EnemyTask> getEts() {return ets;}public static void setEtss(Vector<EnemyTask> ets) {Recoder.ets = ets;}private static int enSize ;public static int getEnSize() {return enSize;}public static void setEnSize(int enSize) {Recoder.enSize = enSize;}private static Hero hero ;public static Hero getHero() {return hero;}public static void setHero(Hero hero) { Recoder.hero = hero;}public static void KeepExit(){//System.out.println(hero.getX());int [] TanksX = new int[20];int [] TanksY = new int[20];int [] ShotsX = new int[100];int [] ShotsY = new int[100];int [] ETdirects = new int[20];int [] HeroShotsX = new int[10];int [] HeroShotsY = new int[10];int [] heros = new int[]{hero.getX(),hero.getY(),hero.direct};for(int i = 0 ; i < ets.size(); i ++){EnemyTask et = ets.get(i);TanksX[i] = et.getX();TanksY[i] = et.getY();ETdirects [i] = et.direct;for(int j = 1 ; j < et.ss.size(); j++){shot s = et.ss.get(j);ShotsX[j] = s.x;ShotsY[j] = s.y;}}for(int i = 0 ; i < hero.ss.size(); i++ ){ shot s = hero.ss.get(i);HeroShotsX[i] = s.x ;HeroShotsY[i] = s.y;}BufferedWriter bw = null;BufferedWriter bw1 = null ;try {bw = new BufferedWriter(new FileWriter("d:\\aa\\TanksX.txt"));for(int i = 0 ; i < enSize ; i ++){bw.write(TanksX[i] + "\r\n");bw.write(TanksY[i] + "\r\n");}bw1 = new BufferedWriter(new FileWriter("d:\\aa\\KeepRecoding.txt")); bw1.write((20 - getEnNum()) + "\r\n" );} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {bw1.close();bw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}private static int enNum = 5;public static int getEnNum() {return enNum;}public static void setEnNum(int enNum) {Recoder.enNum = enNum;}public static int getMyLife() {return myLife;}public static void setMyLife(int myLife) {Recoder.myLife = myLife;}private static int myLife = 3;private static BufferedReader br = null;public static void getRecording(){try {br = new BufferedReader(new FileReader("d:\\aa\\KeepRecoding.txt")); String n ;while((n = br.readLine()) != null ){String a = n;enNum = 20 - Integer.parseInt(n);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {br.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}private static BufferedWriter bw = null;public static void KeepRecording (){try {bw = new BufferedWriter(new FileWriter("d:\\aa\\KeepRecoding.txt")); bw.write((20 - getEnNum()) + "\r\n" );} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {bw.close();//先开后关//new FileWriter("d:\\aa\\KeepRecoding.txt").close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public class Tank {int x = 0 ;int y = 0;int direct = 0; //方向int type = 0 ;//类型int speed = 30;//速度int color;boolean isLive;public int getColor() {return color;}public void setColor(int color) {this.color = color;}public int getS() {return speed;}public void setS(int speed) {this.speed = speed;}public Tank(int x , int y , int direct , int type, int speed, boolean isLive){ this.x = x;this.y = y;this.direct = direct;this.type= type;this.speed = speed;this.isLive = isLive;}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 getD() {return direct;}public void setD(int direct) {this.direct = direct;}public int getT() {return type;}public void setT(int type) {this.type = type;}}class EnemyTask extends Tank implements Runnable{Vector<shot> ss = new Vector<shot>();Vector<EnemyTask> ets = new Vector<EnemyTask>();boolean isdirects = true;public EnemyTask(int x, int y, int direct, int type, int speed ,boolean isLive) {super(x, y, direct, type, speed, isLive);// TODO Auto-generated constructor stub}public boolean isTouchOhterTank (){boolean b = false;switch (this.direct) {case 0://此坦克向上for(int i = 0; i < ets.size(); i++){EnemyTask et = ets.get(i);if(et != this){if(et.direct == 0 || et.direct == 1){//上或下if((this.x>=et.x && this.x <= et.x + 20//第一个点x在内&& this.y>=et.y && this.y <= et.y + 30)//第一个点y在内||(this.x + 20>=et.x && this.x+ 20 <= et.x + 20//第二个点x在内&& this.y >=et.y && this.y <= et.y + 30)//第二个点y在内){b = true;}}if(et.direct == 2 || et.direct == 3){//左或右if((this.x>=et.x && this.x <= et.x + 30//第一个点x在内&& this.y>=et.y && this.y <= et.y + 20)//第一个点y在内||(this.x + 20>=et.x && this.x+ 20 <= et.x + 30//第二个点x在内&& this.y >=et.y && this.y <= et.y + 20)//第二个点y在内){b = true;}}}}break;case 1://向下for(int i = 0; i < ets.size(); i++){EnemyTask et = ets.get(i);if(et != this){if(et.direct == 0 || et.direct == 1){//上或下if((this.x>=et.x && this.x <= et.x + 20//第一个点x在内&& this.y + 30>=et.y && this.y + 30 <= et.y + 30)//第一个点y在内||(this.x + 20>=et.x && this.x+ 20 <= et.x + 20//第二个点x在内&& this.y + 30>=et.y && this.y + 30 <= et.y + 30)//第二个点y在内){b = true;}}if(et.direct == 2 || et.direct == 3){//左或右if((this.x>=et.x && this.x <= et.x + 30//第一个点x在内&& this.y + 30 >=et.y && this.y + 30 <= et.y + 20)//第一个点y在内||(this.x + 20 >=et.x && this.x+ 20 <= et.x + 30//第二个点x在内&& this.y + 30 >=et.y && this.y + 30 <= et.y + 20)//第二个点y在内){b = true;}}}}break;case 2:for(int i = 0; i < ets.size(); i++){EnemyTask et = ets.get(i);if(et != this){if(et.direct == 0 || et.direct == 1){//上或下if((this.x>=et.x && this.x <= et.x + 20//第一个点x在内&& this.y>=et.y && this.y <= et.y + 30)//第一个点y在内||(this.x >=et.x && this.x<= et.x + 20//第二个点x在内&& this.y + 20 >=et.y && this.y + 20 <= et.y + 30)//第二个点y在内){b = true;}}if(et.direct == 2 || et.direct == 3){//左或右if((this.x>=et.x && this.x <= et.x + 30//第一个点x在内&& this.y>=et.y && this.y <= et.y + 20)//第一个点y在内||(this.x >=et.x && this.x<= et.x + 30//第二个点x在内&& this.y + 20>=et.y && this.y + 20 <= et.y + 20)//第二个点y在内){b = true;}}}}break;case 3:for(int i = 0; i < ets.size(); i++){EnemyTask et = ets.get(i);if(et != this){if(et.direct == 0 || et.direct == 1){//上或下if((this.x + 30>=et.x && this.x + 30 <= et.x + 20//第一个点x在内&& this.y>=et.y && this.y <= et.y + 30)//第一个点y在内||(this.x + 30 >=et.x && this.x+ 30 <= et.x + 20//第二个点x在内&& this.y + 20 >=et.y && this.y + 20 <= et.y + 30)//第二个点y在内){b = true;}}if(et.direct == 2 || et.direct == 3){//左或右if((this.x + 30>=et.x && this.x + 30 <= et.x + 30//第一个点x在内&& this.y>=et.y && this.y <= et.y + 20)//第一个点y在内||(this.x + 30>=et.x && this.x+ 30 <= et.x + 30//第二个点x在内&& this.y + 20>=et.y && this.y + 20 <= et.y + 20)//第二个点y在内){b = true;}}}}break;default:break;}return b ;}@Overridepublic void run() {int a = (int) (Math.random() * 10 + 2);//连动步数int times = 0;while(true){try {Thread.sleep(300);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}//if(x > 0 && x < 400 - 30 & y > 0 && y < 300 - 30){ //if(!this.isTouchOhterTank()){switch(this.direct){case 0://System.out.println(isTouchOhterTank());for(int i = 0 ;i < a ; i++){if(y - this.speed > 0 && !this.isTouchOhterTank()){y -= this.speed;}try {Thread.sleep(50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}break;case 1:for(int i = 0 ;i < a ; i++){if(y + speed < 300 - 30 && !this.isTouchOhterTank()){ y += this.speed;}try {Thread.sleep(50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}break;case 2:for(int i = 0 ;i < a ; i++){if(x - speed > 0 && !this.isTouchOhterTank()){x -= this.speed;}try {Thread.sleep(50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}break;case 3:for(int i = 0 ;i < a ; i++){if(x + speed < 400 - 30 && !this.isTouchOhterTank()){ x += this.speed;}try {Thread.sleep(50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}break;}//}times ++;if(times % 2 == 0){if(isLive){if(ss.size() < 3){shot s = null;switch (direct) {case 0://向上s = new shot(x + 10 , y ,0);ss.add(s);break;case 1://向下s = new shot(x + 10 , y + 30 , 1);ss.add(s);break;case 2://向左s = new shot(x , y + 10 , 2);ss.add(s);break;case 3://右s = new shot(x + 30 , y + 10 , 3);ss.add(s);break;default:break;}Thread t = new Thread(s);t.start();}}}//}// //随机改变方向// public void changeDirect(){//// }if(isdirects){this.direct = (int) (Math.random() * 4); }if(this.isLive == false){break;}if(ss.size() < 3){}}}public void setEts(Vector<EnemyTask> ets) { this.ets = ets;}// @Override// public void run() {// while(true){// try {// Thread.sleep(50);// } catch (InterruptedException e) {// // TODO Auto-generated catch block// e.printStackTrace();// }//// y ++ ;// System.out.println(y);// }//// }}class Hero extends Tank{//public Hero(int x , int y, int direct, int type,int speed, boolean isLive){super(x, y, direct, type,speed , isLive);}Vector<shot> ss = new Vector<shot>();shot s = null;//开火public void shotEnemy(){//System.out.println("shotEnemy");if(Recoder.getMyLife() > 0){//生命没有了,不能发子弹 switch (this.direct) {case 0://向上s = new shot(x + 10 , y ,0);ss.add(s);break;case 3://右s = new shot(x + 30 , y + 10 , 3);ss.add(s);break;case 1://向下s = new shot(x + 10 , y + 30 , 1);ss.add(s);break;case 2://向左s = new shot(x , y + 10 , 2);ss.add(s);break;default:break;}Thread t = new Thread(s);t.start();}}//向上,下,左,右public void MoveUp(){if(y - this.speed > 0){y -= speed;}}public void MoveDown(){if(y + this.speed < 300 -30){y += speed;}}public void MoveLeft(){if(x - this.speed > 0){x -= speed;}}public void MoveRight(){if(x + this.speed < 400 -30){x += speed;}}}class shot implements Runnable{int x ,y;int direct;int speed = 20; //子弹速度boolean isShot = true;boolean isLive = true;//子弹是否活着 public shot(int x,int y, int direct){this.x = x;this.y = y;this.direct = direct;}@Overridepublic void run() {while(true){try {Thread.sleep(50);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}if(isShot){switch (direct) {case 0://向上y -= speed;break;case 1://下y += speed;break;case 2://右x -= speed;break;case 3://左x += speed;break;default:break;}}//System.out.println(x + " " + y);if(x < 0 || x > 400 || y < 0 || y > 300){ //System.out.println("this.islive = false");this.isLive = false;break;}}} }。
JAVA课程设计坦克大战游戏(含代码)
Java课程设计任务书简单电子游戏坦克大战的设计1、主要内容:设计一个简单的坦克大战电子游戏,可以允许多位玩家在局域网中同时对战2、系统要求:游戏具有图形游戏界面,能让用户看到游戏所给出的反馈。
有人机交互功能,使用户操作指定的游戏元素,并且坦克具有生命值,能攻击敌方,无法攻击友方,而且坦克的攻击具有视觉效果!3、具体要求(包括技术要求等):1.需要有图形游戏界面,让用户能看到游戏给出的反馈2.有人机交互功能,使用户能操作指定的游戏元素3.不同队伍的坦克,须显示不同外观,已分辨敌我4.坦克能够开火,攻击敌方,但不能攻击队友5需要有不可被子弹穿透的墙6.坦克有生命值,并非被击中一次就会爆炸7游戏结束后可重新开始学习并掌握以下技术:Java、JavaBean 等熟练使用以下开发工具:Myeclipse 等实现系统上述的功能。
4、进度安排:12月28日~ 12月29日:课程设计选题,查找参考资料12月30日~ 1月1日:完成系统设计和数据库设计1月2日~ 1月4日:完成程序代码的编写1月4日~ 1月5日:系统测试与完善1月5日~ 1月6日:完成课程设计报告,准备答辩分工:随着计算机的普及以及对电子游戏的认识,电子游戏已被越来越多的人选作放松、娱乐的休闲方式;电子游戏既不需要专门购买游戏器材,也不需要宽阔的游戏场地,只需要一台能独立完成任务的计算机即可,它可以是人们工作、学习时用的计算机,并不需要另行配置,这比起传统休闲游戏即省钱又省事。
局域网游戏更是可以将现实空间中零散的计算机,组织起来,使其在逻辑空间中集中起来,使游戏的组织、开展变得轻松。
如此轻松而又安逸的休闲方式,使得越来越多的人尝试,甚至已经将电子游戏作为主要休闲方式,既然有如此多的人使用,那其中必然存在巨大的商机,即使人们不愿意为一个小小的休闲游戏花费金钱,但在游戏中添加广告、游戏相关服务也包藏了巨大的财富;当然电子游戏的制作如今已是空前的火爆,大大小小的游戏公司横空出世,都想在电子游戏制作这个行业里占据一席之地,而一款成功的电子游戏需要精心的策划、分析、设计,只有这样才能获得游戏中包藏的财富,以及做出一款真正能够让玩家娱乐、放松的游戏,而不是骗取玩家钱财的工具。
坦克大战游戏编程实现
坦克大战游戏编程实现游戏编程实现坦克大战坦克大战是一款经典的电子游戏,在游戏中,玩家将控制一个坦克,通过操作坦克的移动和攻击,与其他玩家或电脑进行战斗。
本文将介绍如何使用编程语言实现坦克大战游戏。
设计思路在实现坦克大战游戏之前,首先需要明确游戏的基本要素和规则。
玩家将操作一个坦克,通过键盘或操纵杆控制坦克的移动和攻击。
游戏画面由地图、坦克、子弹等元素构成。
坦克可以向前、向后、向左、向右移动,可以发射子弹进行攻击。
玩家需要躲避敌方坦克的攻击,同时尽可能击败敌方坦克。
编程实现坦克的移动和攻击可以通过编程语言来实现。
以下是使用Java语言来编写坦克大战游戏的示例代码:```javapublic class TankGame extends JFrame {private Tank tank; // 玩家坦克private List<Tank> enemyTanks; // 敌方坦克列表private List<Bullet> bullets; // 子弹列表public TankGame() {// 初始化游戏窗口this.setSize(800, 600);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true);// 初始化玩家坦克tank = new Tank(400, 500);// 初始化敌方坦克列表enemyTanks = new ArrayList<>();enemyTanks.add(new Tank(100, 100));enemyTanks.add(new Tank(300, 200));// 初始化子弹列表bullets = new ArrayList<>();// 添加键盘监听器,控制坦克移动和攻击this.addKeyListener(new KeyListener() {@Overridepublic void keyPressed(KeyEvent e) {int keycode = e.getKeyCode();switch (keycode) {case KeyEvent.VK_UP:tank.moveUp();break;case KeyEvent.VK_DOWN: tank.moveDown();break;case KeyEvent.VK_LEFT: tank.moveLeft();break;case KeyEvent.VK_RIGHT: tank.moveRight();break;case KeyEvent.VK_SPACE: bullets.add(tank.fire());break;default:break;}// 重新绘制游戏画面repaint();}@Overridepublic void keyReleased(KeyEvent e) { }@Overridepublic void keyTyped(KeyEvent e) { }});}// 绘制游戏画面public void paint(Graphics g) {super.paint(g);// 绘制玩家坦克tank.draw(g);// 绘制敌方坦克for (Tank enemyTank : enemyTanks) { enemyTank.draw(g);}// 绘制子弹for (Bullet bullet : bullets) {bullet.draw(g);}}public static void main(String[] args) {new TankGame();}}```在上述示例代码中,我们创建了一个继承自JFrame的主窗口,并在窗口中添加了一个玩家坦克、敌方坦克和子弹的列表。
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的图形用户界面而开发的,可以放松心情的小游戏。
游戏从系统操作简便、界面友好、灵活、上手即会的基本要求出发,完成整款游戏的开发。
游戏更设置了暂停、计分、音乐、速度,用户信息保存等功能,可以根据自己的爱好,挑战更高难度,为用户带来愉悦的体验。
java课程设计--坦克大战游戏(带源码)
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();
坦克大战源代码
/**坦克大战*/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
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课程设计——坦克⼤战坦克⼤战——坦克类⼀. 团队课程设计博客链接⼆.个⼈负责模块和任务说明模块:坦克类(玩家坦克类+电脑坦克类),代码整合三.代码的提交记录截图四.负责模块和任务详细说明玩家坦克继承VisibleImage类,电脑坦克类继承⾃玩家坦克类,UML如下:1.玩家坦克类构造⽅法:设置坐标位置(坦克出⽣的位置),坦克的图⽚,坦克的类型(玩家还是电脑),⾯板(可将坦克放置于当前⾯板中),代码截图如下:移动⽅向:(上下左右)左移思路:若当前⽅向不是左移,则将图⽚更改为左移图⽚,并且更新⽅向为左。
若经过碰撞检测发现不会产⽣碰撞,则改变坐标,并且要判断坐标是否在边界。
其他⽅向同理,代码截图如下:碰撞检测:(墙与电脑坦克)思路:获取当前坐标周围⼀定范围内的矩形边框,同时获取当前⾯板内的所有墙体对象,将其存放于数组中。
将获得的矩形范围与所有墙体互相匹配,若符合则说明会产⽣碰撞。
注意草地例外。
坦克碰撞检测同理,注意要将⾃⼰除外,并且要查看当前坦克是否存活,代码截图如下:获得⼦弹:根据上下左右的⽅向判断坦克头的位置,即⼦弹发射的⽅向的预设,以保证之后⼦弹朝预设⽅向移动(⼦弹的移动在⼦弹类⾥)攻击⽅法:获得⼦弹,将⼦弹添加⾄⾯板(因为⼦弹有四个⽅向),启动攻击冷却线程。
触碰边界⽅法:根据屏幕与主窗体之间的⼤⼩关系,进⽽判断坦克是否移动到边界。
冷却线程:使⽤sleep()⽅法,经过休眠之后,要注意改变线程的状态,将其改为可攻击。
其余setter,getter⽅法则不进⾏详细描述。
2.电脑坦克类构造⽅法:继承⾃⽗类玩家坦克类,设置好初始⽅向,攻击冷却时间移动⽅向:在⽗类的基础上,改变了⽗类的⼿动产⽣新⽅向,⽽是随机产⽣新⽅向,并且在该⽅向的移动时间是设置好的,检测是否达到边界同理:碰撞检测,攻击⽅法的思路都与玩家坦克类相似,不同之处在于,玩家坦克类的⽅向是⼈为改动的,⽽电脑坦克类的⽅向是随机产⽣的,故要重写⽗类的这两个⽅法。
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();}}}。
坦克大战程序代码
坦克大战程序代码import java.awt.* ;import javax.swing.* ;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[] 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 + 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 + 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 + 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 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) ;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] + 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] , 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] + 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] + 15 , 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 ;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) { 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 ) { 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) {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 ) { 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) {Sfenshu = 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 ) { 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) { 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 ) { fenshu = fenshu + 100 ;xf[i] = (int)(Math.random() * 560 );yf[i] = (int)(Math.random() * 560 );}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{opf[i] = 2 ;}}else{if(Math.random() > 0.5){opf[i] = 3 ;}else{opf[i] = 4 ;}}}}//坦克的随机移动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{opf[i] = 4 ;}}}}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{opf[i] = 3 ;}else{opf[i] = 4 ;}}}}//重画if(shengming<=0){//弹出player1胜利对话框JOptionPane.showMessageDialog(null,"你结束了!!!","Game Over !", JOptionPane.ERROR_MESSAGE); //结束游戏System.exit(0) ;}this.repaint() ;}}}。
坦克大战源代码
import java.awt.Color;import java.awt.Frame;import java.awt.Graphics;import java.awt.Image;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.util.ArrayList;import java.util.List;public class TankClient extends Frame {public static final int GAME_WIDTH = 800;public static final int GAME_HEIGHT = 600;Tank myTank = new Tank(50, 50, true, Tank.Direction.STOP, this);List<Missile> missiles = new ArrayList<Missile>();List<Explode> explodes = new ArrayList<Explode>();List<Tank> tanks = new ArrayList<Tank>();Image offScreenImage = null;@Overridepublic void paint(Graphics g) {g.drawString("missiles count:" + missiles.size(), 10, 50);g.drawString("explodes count:" + explodes.size(), 10, 70);g.drawString("tanks count:" + tanks.size(), 10, 90);for(int i=0; i<missiles.size(); i++) {Missile m = missiles.get(i);m.hitTanks(tanks);m.draw(g);}for(int i=0; i<explodes.size(); i++) {Explode e = explodes.get(i);e.draw(g);}for(int i=0; i<tanks.size(); i++) {Tank t = tanks.get(i);t.draw(g);}myTank.draw(g);}@Overridepublic void update(Graphics g) {if(offScreenImage == null) {offScreenImage = this.createImage(800, 600);}Graphics gOffScreen = offScreenImage.getGraphics();Color c = gOffScreen.getColor();gOffScreen.setColor(Color.GREEN);gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);gOffScreen.setColor(c);paint(gOffScreen);g.drawImage(offScreenImage, 0, 0, null);}public void launchFrame() {//生产多少地方坦克for(int i=0; i<5; i++) {tanks.add(new Tank(50 + 40*(i+1), 50, false, Tank.Direction.D, this));}this.setLocation(400, 300);this.setSize(GAME_WIDTH, GAME_HEIGHT);this.setTitle("TankWar");this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});this.setResizable(false);this.setBackground(Color.GREEN);this.addKeyListener(new KeyMonitor());this.setVisible(true);new Thread(new PaintThread()).start();}public static void main(String[] args) {TankClient tc = new TankClient();unchFrame();}class PaintThread implements Runnable {public void run() {while(true) {repaint();try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}}}}class KeyMonitor extends KeyAdapter {@Overridepublic void keyReleased(KeyEvent e) {myTank.keyReleased(e);}@Overridepublic void keyPressed(KeyEvent e) {myTank.keyPressed(e);}}}import java.awt.Color;import java.awt.Graphics;import java.awt.Rectangle;import java.awt.event.KeyEvent;import java.util.Random;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;boolean good;int x, y;private static Random r = new Random();private boolean live = true;private int step = r.nextInt(12) + 3;TankClient tc;boolean bL, bU, bR, bD;enum Direction {L, LU, U, RU, R, RD, D, LD, STOP};Direction dir = Direction.STOP;Direction ptDir = Direction.D;public Tank(int x, int y, boolean good) {this.x = x;this.y = 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);switch(ptDir) {case L:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x, y + HEIGHT/2);break;case LU:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x, y);break;case U:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH/2, y);break;case RU:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH, y);break;case R:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH, y + HEIGHT/2);break;case RD:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH, y + HEIGHT);break;case D:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH/2, y + HEIGHT);break;case LD:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x, y + HEIGHT);break;}move();}private void move() {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(dir != Direction.STOP) {ptDir = dir;}if(x < 0) x = 0;if(y < 30) y = 30;if(x + WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - WIDTH;if(y+ HEIGHT> TankClient.GAME_HEIGHT) y= TankClient.GAME_HEIGHT- HEIGHT;if(!good) {if(step == 0) {step = r.nextInt(12) + 3;Direction[] dirs = Direction.values();dir = dirs[r.nextInt(dirs.length)];}step --;if(r.nextInt(40) > 38) this.fire();}}public void keyPressed(KeyEvent e) {int key = e.getKeyCode();switch (key) {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();}private 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 = false;break;case KeyEvent.VK_UP:bU = false;break;case KeyEvent.VK_RIGHT:bR = false;break;case KeyEvent.VK_DOWN:bD = false;break;}locateDirection();}private Missile fire() {int x = this.x + WIDTH/2 - Missile.WIDTH/2;int y = this.y + HEIGHT/2 - Missile.HEIGHT/2;Missile m = new Missile(x, y, this.good, this.ptDir, 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;}}import java.awt.Color;import java.awt.Graphics;import java.awt.Rectangle;import java.util.List;public class Missile {public static final int XSPEED = 10;public static final int YSPEED = 10;public static final int WIDTH = 10;public static final int HEIGHT = 10;TankClient tc;int x, y;Tank.Direction dir = Tank.Direction.R;boolean live = true;private boolean good;public Missile(int x, int y, boolean good, Tank.Direction dir) { this.x = x;this.y = y;this.good = good;this.dir = dir;}public Missile(int x, int y, boolean good, Tank.Direction dir, TankClient tc) {this(x, y, good, dir);this.tc = tc;}public void draw(Graphics g) {if(!live) {tc.missiles.remove(this);return;}Color c = g.getColor();g.setColor(Color.BLACK);g.fillOval(x, y, WIDTH, HEIGHT);g.setColor(c);move();}private void move() {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(x< 0 || y< 0 || x> TankClient.GAME_WIDTH|| y> TankClient.GAME_HEIGHT) {live = false;}}public Rectangle getRect() {return new Rectangle(x, y, WIDTH, HEIGHT);}public boolean hitTank(Tank t) {if(this.live && t.isLive() && this.good != t.good&&this.getRect().intersects(t.getRect())) {this.live = false;t.setLive(false);tc.explodes.add(new Explode(x, y, tc));return true;}return false;}public boolean hitTanks(List<Tank> tanks) { for(int i=0; i<tanks.size(); i++) {if(this.hitTank(tanks.get(i))) {return true;}}return false;}}import java.awt.Color;import java.awt.Graphics;public class Explode {int x, y;private int[] diameters = {4, 7, 12, 18, 26, 32, 49, 30, 14, 6};private boolean live = true;private TankClient tc;int step = 0;public Explode(int x, int y, TankClient tc) {this.x = x;this.y = y;this.tc = tc;}public void draw(Graphics g) {if(!live) {tc.explodes.remove(this);return;}Color c = g.getColor();g.setColor(Color.ORANGE);g.fillOval(x, y, diameters[step], diameters[step]);g.setColor(c);step ++;if(step == diameters.length) {live = false;}}}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Explode e=explodes.get(i); e.draw(g); }
for(int i=0;i<tanks.size();i++){ Tank t=tanks.get(i); t.collidesWithWall(w1); t.collidesWithWall(w2); t.collidesWithTanks(tanks); t.draw(g);
this.x = x; this.y = y; this.w = w; this.h = h; this.tc = tc; } public void draw(Graphics g){ g.fillRect(x,y,w,h); } public Rectangle getRect(){ return new Rectangle(x,y,w,h); } }
1. 实验设计目的:
(1)掌握类的定义和使用; (2)掌握对象的定义; (3)掌握线程的使用。
2. 实验设计内容:
设计一款坦克大战游戏
3. 技术要点:
游戏中的图形的碰撞校验
4. 实验条件:
(1)主要设备: 586或更高机型,256MB或更高的内存,40G或更大的硬盘。 (2)主要软件:
①操作系统可为Windows9X、WinMe、Win2000或更高版本等; ②开发环境为jdk或者jcreator。 (3)参考书目: ①尹继平,张帆.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){
}
int x, y;
private static Random r = new Random();
private boolean bL = false, bU = false, bR = false, bD = false;
//enum Direction {L, LU, U, RU, R, RD, D, LD, STOP};
类是生成坦克的,wall是生成游戏中的墙体的。TankClient是这个游戏的主函数的 类,这里调用其他类,运行游戏。
程序源码: //TankClient类:
import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.List; import java.util.ArrayList;
} } private class KeyMonitor extends KeyAdapter{
public void keyReleased(KeyEvent e) { myTank.KeyReleased(e);
}
@Override public void keyPressed(KeyEvent e) {
Color c=g.getColor(); g.setColor(Color.GREEN); g.drawRect(x,y-5, WIDTH,5); int w=WIDTH*life/100; g.fillRect(x, y-5, w, 5); g.setColor(c); } } public boolean isLive() { ret.draw(g); //enemyTank.draw(g); myTank.eat(b); w1.draw(g); w2.draw(g); b.draw(g);
} public void update(Graphics g) {
if(offScreenImage==null){ offScreenImage=this.createImage(GAME_WIDTH,GAME_HEIGHT);
public static void main(String[] args) { TankClient tc=new TankClient(); uchFrame();
} private class PaintThread implements Runnable{
public void run() { while(true){ repaint(); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } }
public void windowClosing(WindowEvent e) { System.exit(0); //super.windowClosing(e);
} });
this.addKeyListener(new KeyMonitor());
new Thread(new PaintThread()).start(); } Blood b=new Blood();
public class TankClient extends Frame{ public static final int GAME_WIDTH=800; public static final int GAME_HEIGHT=600;
Tank myTank=new Tank(700,500,true,Direction.STOP,this); //Tank enemyTank=new Tank(100,100,false,this);
myTank.keyPressed(e); } } }
//Wall类
import java.awt.*;
public class Wall {
int x, y, w, h; TankClient tc;
public Wall(int x, int y, int w, int h, TankClient tc) {
}
Graphics gOffScreen=offScreenImage.getGraphics(); Color c=gOffScreen.getColor(); gOffScreen.setColor(Color.GRAY); gOffScreen.fillRect(0, 0, GAME_WIDTH,GAME_HEIGHT); gOffScreen.setColor(c); paint(gOffScreen); g.drawImage(offScreenImage,0, 0,null); }
public int getLife() { return life;
}
public void setLife(int life) { this.life = life;
} private class BloodBar_G{
public void draw(Graphics g){ Color c=g.getColor();
public void setLive(boolean live) { this.live = live;
}
TankClient tc;
private boolean good;
private int oldX, oldY;
public boolean isGood() { return good;
public void paint(Graphics g) { g.drawString("子弹个数:"+missiles.size(),10,50); g.drawString("击中个数:"+explodes.size(),10,70); g.drawString("坦克数量:"+tanks.size(),10,90); g.drawString("生命值:"+myTank.getLife(),10,110); if(tanks.size()<=0){ for (int i=0;i<10;i++){ tanks.add(new Tank(100+40*i+1,100,false,Direction.D,this } }
5. 实验方法与步骤:
这个游戏的开发从1.0版本到2.8版本一共28个版本。从第一个版本的游戏 界面设计,到添加第一辆坦克,给坦克添加动作,再给坦克添加炮弹,添加游戏 中的墙体,生成敌军坦克,给游戏添加图片等。在最终版的程序中一共七个类如 图一所示
其中Blood类是控制坦克的生命值的,Direction用是enum是控制游戏中一切物体 运动方向。Explode是控制坦克被命中时爆炸的,Missile类是控制炮弹的。Tank
Wall w1 =new Wall(400,200,20,300,this),w2=new Wall(500,300,300,20, //Explode e=new Explode(70,80,this); List<Explode> explodes =new ArrayList<Explode>(); List<Missile> missiles =new ArrayList <Missile> (); List<Tank> tanks =new ArrayList <Tank> ();
for(int i=0;i<missiles.size();i++){ Missile m=missiles.get(i); m.hitTanks(tanks); m.hitTank(myTank); m.hitWall(w1); m.hitWall(w2); //m.hitTank(enemyTank); m.draw(g);