javaは飛行機の大戦のゲームを実現します。


javaは飛行機の大戦を実現して、みんなに参考にさせて、具体的な内容は次の通りです。
Javaで飛行機大戦のゲームを書いて練習して、ゲームの背景音楽とゲームの基本的な機能を再生することができます。
デザイン
1、相応の写真と背景音楽(.wavファイル)を準備します。
2、ソースコードを直接見る;
3、一部の機能が実装されていません。
ソース
package forGame
ゲームの画像クラスを読み込みます。

package forGame;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

//      
public class ImageUtil {

 //    
 public static int WIDTH_BACK;
 public static int HEIGHT_BACK;
 public static int WIDTH_PLANE;
 public static int HEIGHT_PLANE;

 //  
 public static BufferedImage START;
 public static ImageIcon BUTTON;
 public static BufferedImage PLANE_1;
 public static BufferedImage PLANE_2;
 public static BufferedImage Bullet_1;
 public static BufferedImage Bullet_2;
 public static BufferedImage XIAO_PLANE;
 public static BufferedImage BOMB_PLANE1;
 public static BufferedImage BOMB_PLANE2;

 static {
  try {
   START = ImageIO.read(new File("src\\image\\  2.png"));
   BUTTON = new ImageIcon("src\\image\\  .png");
   PLANE_1 = ImageIO.read(new File("src\\image\\  1.png"));
   PLANE_2 = ImageIO.read(new File("src\\image\\  2.png"));
   Bullet_1 = ImageIO.read(new File("src\\image\\  1.png"));
   Bullet_2 = ImageIO.read(new File("src\\image\\  2.png"));
   XIAO_PLANE = ImageIO.read(new File("src\\image\\   .png"));
   BOMB_PLANE1 = ImageIO.read(new File("src\\image\\    1.png"));
   BOMB_PLANE2 = ImageIO.read(new File("src\\image\\    2.png"));
  } catch (IOException e) {
   e.printStackTrace();
  }
  WIDTH_BACK = START.getWidth();
  HEIGHT_BACK = START.getHeight();
  WIDTH_PLANE = PLANE_1.getWidth();
  HEIGHT_PLANE = PLANE_1.getHeight();
 }
}
ゲームの背景音楽を再生

package forGame;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.io.File;

//      (.wav  )
public class PlayMusic {
 private Clip clip;
 //                
 public void start(File file)
 {
  try
  {
   //             
   clip = AudioSystem.getClip();
   //        
   AudioInputStream audioInput = AudioSystem.getAudioInputStream(file);
   //         
   clip.open(audioInput);
   //clip.start();//     
   //    
   clip.loop(Clip.LOOP_CONTINUOUSLY);
  } catch(Exception ex){
   ex.printStackTrace();
  }
  //          (swing   )
  /*
   while(true){
   }
  */
 }

 //      
 public void exit(){
  clip.close();//      ,        
 }

 //      
 public void stop(){
  clip.stop();//      ,        
 }
}
package planneGame
インターフェース

package planeGame;

import java.awt.*;

//    
public interface DrawMe {
 void drawMe(Graphics g);
}

package planeGame;

//    
public interface Grade {
 int getGrade();
}
ウィンドウの親

package planeGame;

import forGame.ImageUtil;
import forGame.PlayMusic;

import javax.swing.*;
import java.io.File;

//    
public class MyJFrameFather extends JFrame{
 protected int y1 = 0;
 protected int y2 = -830;
 protected PlayMusic playMusic = new PlayMusic();
 public MyJFrameFather(String name){
  super(name);
  setSize(ImageUtil.WIDTH_BACK, ImageUtil.HEIGHT_BACK);
  setLocationRelativeTo(null);
  setResizable(false);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  playMusic.start(new File("src\\music\\bgm.wav"));
 }
}
インターフェース

package planeGame;

import forGame.ImageUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//      
public class StartJFrame extends MyJFrameFather{
 public StartJFrame(){
  super("    ");
  ImageIcon imageIcon = ImageUtil.BUTTON;
  JButton jButton = new JButton(imageIcon);
  //        
  jButton.setBorder(null);
  jButton.setBounds(200,350,imageIcon.getIconWidth(),imageIcon.getIconHeight());
  jButton.setBackground(Color.lightGray);
  setLayout(null);
  add(jButton);
  setVisible(true);
  jButton.addActionListener(actionListener);
 }

 @Override
 public void paint(Graphics g) {
  g.drawImage(ImageUtil.START,0,0 ,null );
 }

 private ActionListener actionListener = new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
   playMusic.exit();
   new GameJFrame();
   dispose();
  }
 };
}
飛行機父類(抽象類)

package planeGame;

import java.awt.*;

//    
public abstract class Plane implements DrawMe{
 //    
 protected Point p = new Point();
 //      
 protected boolean isLive = true;
 //      
 protected int speed;
 public Plane(int x,int y){
  p.x = x;
  p.y = y;
 }
 //           
 public abstract void setP(int x);
 //   
 public abstract void drawMe(Graphics g);
 //  
 public abstract void move();
 //      
 protected Point getP(){
  return p;
 }

 //      
 public void playBullet(Bullet bullet){
  //     true
  bullet.setLive();
 }

 //      
 public void setLive(boolean aboolean){
  isLive = aboolean;
 }

 //      
 public boolean getIsLive(){
  return isLive;
 }
}
メインクラス

package planeGame;

import forGame.ImageUtil;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

//   
public class MainPlane extends Plane{
 //     1;  -1:  2:  -2: 
 private int direction = 1;
 public MainPlane(int x, int y) {
  super(x, y);
 }

 @Override
 public void setP(int x) {}

 private boolean aBoolean = true;//       
 @Override
 public void drawMe(Graphics g) {
  if(isLive){
   if(aBoolean) {
    g.drawImage(ImageUtil.PLANE_1, p.x, p.y, null);
    aBoolean = false;
   }
   else {
    g.drawImage(ImageUtil.PLANE_2, p.x, p.y, null);
    aBoolean = true;
   }
  }
  else{
   g.drawImage(ImageUtil.BOMB_PLANE1, p.x, p.y, null);//         
  }
 }

 @Override
 public void move() {
  if(direction == 1 && p.y > 30)
   p.move(p.x,p.y + speed);
  else if(direction == -1 && p.y < ImageUtil.HEIGHT_BACK - ImageUtil.HEIGHT_PLANE)
   p.move(p.x,p.y + speed);
  else if(direction == 2 && p.x < ImageUtil.WIDTH_BACK - ImageUtil.HEIGHT_PLANE)
   p.move(p.x + speed,p.y);
  if(direction == -2 && p.x > 0)
   p.move(p.x + speed,p.y);
 }

 //      
 private KeyListener keyListener = new KeyAdapter() {
  @Override
  public void keyPressed(KeyEvent e) {
   int keyCode = e.getKeyCode();
   //     1;  -1:  2:  -2: 
   // 
   if(keyCode == KeyEvent.VK_UP){
    direction = 1;
    speed = -20;
    move();
   }
   // 
   if(keyCode == KeyEvent.VK_DOWN){
    direction = -1;
    speed = 20;
    move();
   }
   // 
   if(keyCode == KeyEvent.VK_LEFT){
    direction = -2;
    speed = -32;
    move();
   }
   // 
   if(keyCode == KeyEvent.VK_RIGHT){
    direction = 2;
    speed = 32;
    move();
   }
  }
 };
 //       
 public KeyListener getKeyListener(){
  return keyListener;
 }
 //          
 public void isBomb(Plane[] planes){
  for(int i = 0;i < planes.length; i++) {
   if (planes[i].getIsLive()) {
    if(planes[i].getP().x > p.x && planes[i].getP().x < p.x + 128 && (p.y - planes[i].getP().y < 64)) {
     isLive = false;
     planes[i].setLive(false);
    }
   }
  }
 }
}
敵機類

package planeGame;

import forGame.ImageUtil;

import java.awt.*;

//  ,         
public class GamePlane extends Plane implements Grade{
 public GamePlane(int x, int y) {
  super(x, y);
 }

 @Override
 public void setP(int x) {
  p.x = x;
  p.y = 0;
 }

 @Override
 public void drawMe(Graphics g) {
  g.drawImage(ImageUtil.XIAO_PLANE,p.x,p.y,null);
  move();
 }

 @Override
 public void move() {
  if(p.y < 900)
   p.y = p.y +20;
  else
   isLive = false;
 }

 //   
 @Override
 public int getGrade() {
  return 0;
 }
}
弾丸類

package planeGame;

import forGame.ImageUtil;

import java.awt.*;

//   
public class Bullet implements DrawMe {
 private boolean isLive = false;//      
 private int x;//       
 private int y;//       
 private int color;//         
 public Bullet(int number,int x,int y){
  this.color = number;
  this.x =x;
  this.y =y;
 }

 //      
 public void setXY(int x,int y){
  this.x =x;
  this.y =y;
 }

 //      
 public void setLive(){
  isLive = true;
 }
 public boolean getLive(){
  return isLive;
 }
 //    
 @Override
 public void drawMe(Graphics g) {
  if(color == 1){
   g.drawImage(ImageUtil.Bullet_1, x, y,null);
  } else {
   g.drawImage(ImageUtil.Bullet_2, x, y, null);
  }
  move();
 }

 //    
 private void move(){
  if(color == 1){
   if(y > 30)
    y = y - 50;
   else
    isLive = false;
  }else{
   if(y < 900)
    y = y + 100;
   else
    isLive = false;
  }
 }

 //        
 public boolean isBom(Plane[] planes){
  boolean is = false;
  for(int i = 0;i < planes.length;i ++){
   if(planes[i].getIsLive()){
    if(x > planes[i].getP().x && x < planes[i].getP().x + 64){
     if(y - planes[i].getP().y <= 64) {
      isLive = false;
      planes[i].setLive(false);
      is = true;
     }
    }
   }
  }
  return is;
 }

 //         
 private void isBom(Plane plane){

 }

}
主機、敵機、銃弾類を創建する

package planeGame;

import java.util.Random;

//    、  
public class Production{
 Random random = new Random();
 private GamePlane[] gamePlanes = new GamePlane[16];
 private Bullet[] bullets = new Bullet[50];

 //   :596 x 854
 //   :128 x 128
 //   :9 x 21
 private MainPlane mainPlane = new MainPlane(random.nextInt(400),random.nextInt(160) + 400);
 public MainPlane getMainPlane() {
  return mainPlane;
 }

 //    

 public GamePlane[] getGamePlanes() {
  for(int i = 0;i < 16;i ++){
   gamePlanes[i] = new GamePlane(0,0);
   gamePlanes[i].setLive(false);
  }
  return gamePlanes;
 }

 //         true
 public void setGamePlanes(){
  for(int i = 0;i < 16;i ++){
   if(!gamePlanes[i].isLive){
    gamePlanes[i].setP(random.nextInt(12) * 45 + 32);
    gamePlanes[i].setLive(true);
    break;
   }
  }
 }
 //      boolean 
 public boolean getBoolean(){
  return random.nextBoolean();
 }

 //    
 public Bullet[] getBullets() {
  for(int i = 0;i < 50;i ++){
   if(i < 20)
    bullets[i] = new Bullet(1,0,0);
   else
    bullets[i] = new Bullet(2,0,0);
  }
  return bullets;
 }
}
ゲームインタフェース

package planeGame;

import forGame.ImageUtil;
import forGame.PlayMusic;

import java.awt.*;

//    ,     
public class GameJFrame extends MyJFrameFather{
 private boolean isRepaint = true;
 private PlayMusic playMusicB = new PlayMusic();
 private Production production = new Production();
 private GamePlane[] gamePlanes;
 private Bullet[] bullets;
 private MainPlane mainPlane = production.getMainPlane();
 private int grade = 0;
 public GameJFrame(){
  super("    ");
  setVisible(true);
  addKeyListener(mainPlane.getKeyListener());
  MyRunning myRunning = new MyRunning();
  myRunning.start();
  gamePlanes = production.getGamePlanes();
  bullets = production.getBullets();
 }

 @Override
 public void paint(Graphics g) {
  Image image = this.createImage(getWidth(),getHeight());
  Graphics gImage = image.getGraphics();
  gImage.setColor(gImage.getColor());
  gImage.fillRect(0,0,getWidth(),getHeight());
  super.paint(gImage);

  //596 x 854
  //      
  if(y2 == 0){
   y1 = 0;
   y2 = -830;
  }
  gImage.drawImage(ImageUtil.START,0 ,y1 ,null );
  gImage.drawImage(ImageUtil.START,0 ,y2 ,null );
  y1 = y1 + 10;
  y2 = y2 + 10;
  //      
  if(mainPlane.isLive){//     
   for(int i = 0;i < 20;i ++){
    //      false,           
    if(!bullets[i].getLive()){
     bullets[i].setXY(mainPlane.getP().x + 60,mainPlane.getP().y - 21);
     mainPlane.playBullet(bullets[i]);
     break;
    }
   }
   //       
   for(int i =0;i < 10;i ++){
    if(gamePlanes[i].isLive){
     gamePlanes[i].drawMe(gImage);
    }
   }
   //          
   if(production.getBoolean() && production.getBoolean())
    production.setGamePlanes();
   //         
   mainPlane.isBomb(gamePlanes);
   //     
   mainPlane.drawMe(gImage);
   //            ,         
   for(int i = 0;i < bullets.length;i ++){
    if(bullets[i].getLive()) {
     if (bullets[i].isBom(gamePlanes))
      grade = grade + 10;
     else
      bullets[i].drawMe(gImage);
    }
   }
  }else{
   isRepaint = false;
   mainPlane.drawMe(gImage);
   gImage.setFont(new Font("  ",Font.ITALIC ,50));
   gImage.drawString("GameOver",200,350);
  }
  gImage.drawString("  :" + grade,10,100);
  //          
  g.drawImage(image,0 ,0 , null);
 }
 
 //             
 private class MyRunning extends Thread{
  @Override
  public void run() {
   while (isRepaint){
    try {
     sleep(100);
     repaint();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }
 }
}
テストクラス

package planeGame;

//   
public class Demo {
 public static void main(String[] args) {
  new StartJFrame();//      
 }
}
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。