1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Random;
public class GamePanel extends JPanel {
GameLogic gameLogic = new GameLogic();
public GamePanel(GameFrame frame){ gameLogic.run(); frame.addKeyListener(new Listener().getKeyAdapter(gameLogic)); addMouseListener(new Listener().getMouseAdapter(gameLogic)); addMouseMotionListener(new Listener().getMouseAdapter(gameLogic)); }
@Override public void paint(Graphics g) { super.paint(g); g.drawImage(Data.getBackground(),0,0,null); for (int i = 0; i < gameLogic.enemies.size(); i++) { Enemy enemy = gameLogic.enemies.get(i); g.drawImage(enemy.img,enemy.x,enemy.y,null); } for (int i = 0; i < gameLogic.bullets.size(); i++) { Bullet bullet = gameLogic.bullets.get(i); g.drawImage(bullet.img,bullet.x,bullet.y,bullet.w,bullet.h,null);
} g.setColor(Color.white); g.setFont(new Font("\u6977\u4F53",Font.BOLD,30)); g.drawString("分数:"+gameLogic.score,10,30); g.drawImage(gameLogic.myPlane.img,gameLogic.myPlane.x,gameLogic.myPlane.y,null); for (int i = 0; i < gameLogic.myPlane.blood; i++) { g.drawImage(gameLogic.myPlane.img,380+i*35,5,30,30,null); } if(gameLogic.boss!=null){ g.drawImage(gameLogic.boss.img,gameLogic.boss.x,gameLogic.boss.y,null); }
if(gameLogic.boss!=null && gameLogic.score>=100){
for (int i = 0; i < gameLogic.fires.size(); i++) { Fire fire = gameLogic.fires.get(i); g.drawImage(fire.img,fire.x,fire.y,fire.w,fire.h,null);
} } if(gameLogic.gameover){
g.drawImage(Data.getOver(),0,0,Data.getBackground().getWidth(),Data.getBackground().getHeight(),null); g.drawString("点击屏幕任意位置重新开始",50,450); } repaint(); } }
|