资源描述:
《飞机大战 java 源代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、packagecom;importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics;importjava.awt.Image;importjava.awt.Rectangle;importjava.awt.event.KeyEvent;importjava.io.IOException;importjavax.imageio.ImageIO;publicclassPlane{ImagefeijiImage=null;intx=300;inty=700;intlifeCount=5;publicPlane(
2、){try{feijiImage=ImageIO.read(Plane.class.getClassLoader().getResourceAsStream("images/feiji.png"));}catch(IOExceptione){e.printStackTrace();}}publicvoiddraw(Graphicsg){//画飞机图片g.drawImage(feijiImage,x,y,null);//飞机移动this.move();//血条if(lifeCount>0){g.setColor(Color.WHITE);g.fillRect(20,80,1
3、00,10);g.setColor(Color.red);g.fillRect(20,80,(100/5)*lifeCount,10);g.setColor(Color.blue);g.setFont(newFont("幼圆",Font.BOLD,30));g.drawString("Score:"+Play01.count,20,60);}}publicvoidmove(){if(isUP&&!isDown&&!isLeft&&!isRight){//上y=y-5;}elseif(!isUP&&isDown&&!isLeft&&!isRight){//下y=y+5;}e
4、lseif(!isUP&&!isDown&&isLeft&&!isRight){//左x=x-5;}elseif(!isUP&&!isDown&&!isLeft&&isRight){//右x=x+5;}elseif(isUP&&!isDown&&isLeft&&!isRight){//左上x=x-5;y=y-5;}elseif(!isUP&&isDown&&isLeft&&!isRight){//左下x=x-5;y=y+5;}elseif(isUP&&!isDown&&!isLeft&&isRight){//右上x=x+5;y=y-5;}elseif(!isUP&&isD
5、own&&!isLeft&&isRight){//右下x=x+5;y=y+5;}}booleanisUP=false;booleanisDown=false;booleanisLeft=false;booleanisRight=false;//摁下publicvoidkeyPressed(KeyEvente){intkeyCode=e.getKeyCode();if(keyCode==KeyEvent.VK_RIGHT
6、
7、keyCode==KeyEvent.VK_D){isRight=true;}elseif(keyCode==KeyEvent.VK_LEFT
8、
9、keyC
10、ode==KeyEvent.VK_A){isLeft=true;}elseif(keyCode==KeyEvent.VK_UP
11、
12、keyCode==KeyEvent.VK_W){isUP=true;}elseif(keyCode==KeyEvent.VK_DOWN
13、
14、keyCode==KeyEvent.VK_S){isDown=true;}}//放开publicvoidkeyReleased(KeyEvente){intkeyCode=e.getKeyCode();if(keyCode==KeyEvent.VK_RIGHT
15、
16、keyCode==KeyEvent.VK_D)
17、{isRight=false;}elseif(keyCode==KeyEvent.VK_LEFT
18、
19、keyCode==KeyEvent.VK_A){isLeft=false;}elseif(keyCode==KeyEvent.VK_UP
20、
21、keyCode==KeyEvent.VK_W){isUP=false;}elseif(keyCode==KeyEvent.VK_DOWN
22、
23、keyCode==KeyEvent.VK_S){isDown=false;}}publicRectanglegetRectangle(){returnn