commit 238b331ed1b1974c5070749be945fbb593823174 Author: liufeng <397688551@qq.com> Date: Fri May 3 20:56:46 2019 +0800 first commit diff --git a/Small-plane-game-master/.gitignore b/Small-plane-game-master/.gitignore new file mode 100644 index 0000000..441ca75 --- /dev/null +++ b/Small-plane-game-master/.gitignore @@ -0,0 +1,12 @@ +# Created by .ignore support plugin (hsz.mobi) +### Example user template template +### Example user template + +# IntelliJ project files +.idea +*.iml +out +gen +/bin +.settings +/classes \ No newline at end of file diff --git a/Small-plane-game-master/README.md b/Small-plane-game-master/README.md new file mode 100644 index 0000000..00706b4 --- /dev/null +++ b/Small-plane-game-master/README.md @@ -0,0 +1 @@ +# Small-plane-game diff --git a/Small-plane-game-master/images/airplane.png b/Small-plane-game-master/images/airplane.png new file mode 100644 index 0000000..9ec7d72 Binary files /dev/null and b/Small-plane-game-master/images/airplane.png differ diff --git a/Small-plane-game-master/images/background.png b/Small-plane-game-master/images/background.png new file mode 100644 index 0000000..62e8b61 Binary files /dev/null and b/Small-plane-game-master/images/background.png differ diff --git a/Small-plane-game-master/images/bee.png b/Small-plane-game-master/images/bee.png new file mode 100644 index 0000000..e746264 Binary files /dev/null and b/Small-plane-game-master/images/bee.png differ diff --git a/Small-plane-game-master/images/bullet.png b/Small-plane-game-master/images/bullet.png new file mode 100644 index 0000000..b6d1bec Binary files /dev/null and b/Small-plane-game-master/images/bullet.png differ diff --git a/Small-plane-game-master/images/gameover.png b/Small-plane-game-master/images/gameover.png new file mode 100644 index 0000000..305726a Binary files /dev/null and b/Small-plane-game-master/images/gameover.png differ diff --git a/Small-plane-game-master/images/pause.png b/Small-plane-game-master/images/pause.png new file mode 100644 index 0000000..bb8949b Binary files /dev/null and b/Small-plane-game-master/images/pause.png differ diff --git a/Small-plane-game-master/images/player0.png b/Small-plane-game-master/images/player0.png new file mode 100644 index 0000000..3c2c735 Binary files /dev/null and b/Small-plane-game-master/images/player0.png differ diff --git a/Small-plane-game-master/images/player1.png b/Small-plane-game-master/images/player1.png new file mode 100644 index 0000000..bbb856f Binary files /dev/null and b/Small-plane-game-master/images/player1.png differ diff --git a/Small-plane-game-master/images/start.png b/Small-plane-game-master/images/start.png new file mode 100644 index 0000000..28593df Binary files /dev/null and b/Small-plane-game-master/images/start.png differ diff --git a/Small-plane-game-master/src/META-INF/MANIFEST.MF b/Small-plane-game-master/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..bf6138f --- /dev/null +++ b/Small-plane-game-master/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: main.Game + diff --git a/Small-plane-game-master/src/main/Deamon.java b/Small-plane-game-master/src/main/Deamon.java new file mode 100644 index 0000000..2a69850 --- /dev/null +++ b/Small-plane-game-master/src/main/Deamon.java @@ -0,0 +1,65 @@ +package main; + +import main.Enum.FlyingObjectState; +import main.Enum.GameState; +import main.entry.Bullet; +import main.entry.FlyingObject; + +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +public class Deamon extends Thread { + private ExecutorService executorService = null; + private Game game = null; + private CyclicBarrier cyclicBarrier; + + public Deamon(Game game1) { + game = game1; + executorService = Executors.newCachedThreadPool(); + + } + + public void submit(FlyingObject temp) { + if (temp == null) return; + executorService.execute(temp); + } + + @Override + public void run() { + super.run(); + while (true) { + try { + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + if(game.getState()!= GameState.RUNNING) + continue; + if(game.getPlayer().getState()== FlyingObjectState.DEAD){ + game.getPlayer().setX(150); + game.getPlayer().setY(400); + game.getPlayer().setState(FlyingObjectState.LIVE); + this.submit(game.getPlayer()); + } + FlyingObject fo = game.addEnemy(); + if (fo != null) + executorService.submit(fo); + Bullet[] bs = game.shootAction(); + for (int i = 0; bs != null && i < bs.length; i++) { + executorService.submit(bs[i]); + } + game.checkBulletAction(); + game.outOfBoundsAction(); + game.checkGameOverAction(); + game.getMainFrame().repaint(); + if (game.getState() == GameState.GAME_OVER) { + game.saveHighestScore(); + game.clearAllFlyingObject(); + game.getPlayer().setState(FlyingObjectState.DEAD); + game.setScore(0); + game.getPlayer().setLife(3); + } + } + } +} diff --git a/Small-plane-game-master/src/main/Enum/FlyingObjectState.java b/Small-plane-game-master/src/main/Enum/FlyingObjectState.java new file mode 100644 index 0000000..4ab38e4 --- /dev/null +++ b/Small-plane-game-master/src/main/Enum/FlyingObjectState.java @@ -0,0 +1,5 @@ +package main.Enum; + +public enum FlyingObjectState { + DEAD,LIVE,STOP; +} diff --git a/Small-plane-game-master/src/main/Enum/GameState.java b/Small-plane-game-master/src/main/Enum/GameState.java new file mode 100644 index 0000000..a071f06 --- /dev/null +++ b/Small-plane-game-master/src/main/Enum/GameState.java @@ -0,0 +1,5 @@ +package main.Enum; + +public enum GameState { + START,RUNNING,PAUSE,GAME_OVER; +} diff --git a/Small-plane-game-master/src/main/Frame.java b/Small-plane-game-master/src/main/Frame.java new file mode 100644 index 0000000..6c79483 --- /dev/null +++ b/Small-plane-game-master/src/main/Frame.java @@ -0,0 +1,150 @@ +package main; + +import main.Enum.FlyingObjectState; +import main.Enum.GameState; +import main.entry.Bullet; +import main.entry.FlyingObject; +import main.entry.Player; + +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.image.BufferedImage; +import java.io.File; +import java.util.Set; + +public class Frame extends JPanel { + + static { + try { + background = ImageIO.read(new File("images/background.png")); + start = ImageIO.read(new File("images/start.png")); + airplane = ImageIO.read(new File("images/airplane.png")); + bee = ImageIO.read(new File("images/bee.png")); + bullet = ImageIO.read(new File("images/bullet.png")); + player0 = ImageIO.read(new File("images/player0.png")); + player1 = ImageIO.read(new File("images/player1.png")); + pause = ImageIO.read(new File("images/pause.png")); + gameover = ImageIO.read(new File("images/gameover.png")); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static final int WIDTH = 400; + public static final int HEIGHT = 654; + public static int ID; + + public static BufferedImage background; + public static BufferedImage start; + public static BufferedImage airplane; + public static BufferedImage bee; + public static BufferedImage bullet; + public static BufferedImage player0; + public static BufferedImage player1; + public static BufferedImage pause; + public static BufferedImage gameover; + + public Game game; + + public Frame(Game gamet){ + ID=1000; + game=gamet; + this.init(); + JFrame frame = new JFrame("飞机大战"); + frame.add(this); + frame.setSize(WIDTH, HEIGHT); + frame.setAlwaysOnTop(true); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setIconImage(new ImageIcon("images/icon.jpg").getImage()); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + } + + public void paint(Graphics g) { + Set flyings = game.getFlings(); + Set bullets = game.getBullets(); + Player player = game.getPlayer(); + g.drawImage(background, 0, 0, null); + if(player!=null) + g.drawImage(player.getImage(), player.getX(), player.getY(), null); + for(Bullet bl : bullets){ + g.drawImage(bl.getImage(), bl.getX() - bl.getWidth() / 2, bl.getY(), null); + } + for (FlyingObject fo:flyings) + g.drawImage(fo.getImage(), fo.getX(), fo.getY(), null); + paintScore(g); + paintState(g); + } + + public void paintScore(Graphics g) { + int x = 10,y = 25; + g.setColor(Color.RED); + g.setFont(new Font(null, Font.BOLD, 22)); + g.drawString("SCORE:" + game.getScore(), x, y); + y=y+20; + g.drawString("LIFE:" + game.getPlayer().getLife(), x, y); + y=y+25; + g.drawString("ID:" + ID, x, y); + y=y+25; + g.drawString("Highest score:" + game.getHighestScore(), x, y); + } + + public void paintState(Graphics g) { + switch (game.getState()) { + case START: + g.drawImage(start, 0, 0, null); + break; + case PAUSE: + g.drawImage(pause, 0, 0, null); + break; + case GAME_OVER: + g.drawImage(gameover, 0, 0, null); + break; + } + } + + public void init() { + MouseAdapter Ma = new MouseAdapter() { + Player player = game.getPlayer(); + Set flyings = game.getFlings(); + Set bullets = game.getBullets(); + public void mouseMoved(MouseEvent e) { + if (game.getState() == GameState.RUNNING) + player.moveTo(e.getX(), e.getY()); + } + public void mouseClicked(MouseEvent e) { + switch (game.getState()) { + case START: + game.setScore(0); + game.setState(GameState.RUNNING); + break; + case GAME_OVER: + game.setState(GameState.START); + repaint(); + break; + case RUNNING: + game.setState(GameState.PAUSE); + for(Bullet bl : bullets) + bl.setState(FlyingObjectState.STOP); + for (FlyingObject fo:flyings) + fo.setState(FlyingObjectState.STOP); + break; + case PAUSE: + game.setState(GameState.RUNNING); + for(Bullet bl : bullets) { + bl.setState(FlyingObjectState.LIVE); + } + for (FlyingObject fo:flyings) { + fo.setState(FlyingObjectState.LIVE); + } + break; + } + } + }; + this.addMouseListener(Ma); + this.addMouseMotionListener(Ma); + } +} diff --git a/Small-plane-game-master/src/main/Game.java b/Small-plane-game-master/src/main/Game.java new file mode 100644 index 0000000..3ce061c --- /dev/null +++ b/Small-plane-game-master/src/main/Game.java @@ -0,0 +1,197 @@ +package main; + +import main.Enum.FlyingObjectState; +import main.Enum.GameState; +import main.entry.*; + +import java.util.*; + + +public class Game { + + private static int flyIndex = 0, shootIndex = 0; + + private Frame mainFrame; + private Deamon deamon; + + volatile private GameState state; + private int highestScore; + private int score = 0; + + private HashSet flings; + private HashSet bullets; + private Player player = null; + + public Game() { + + } + + public static void main(String[] args) { + Game game = new Game(); + game.score = 0; + game.highestScore=0; + game.state=GameState.START; + game.player = new Player(); + game.flings = new HashSet<>(); + game.bullets = new HashSet<>(); + game.mainFrame = new Frame(game); + game.deamon = new Deamon(game); + game.deamon.start(); + } + + public Frame getMainFrame() { + return mainFrame; + } + + public void setState(GameState state) { + this.state = state; + } + + public GameState getState() { + return state; + } + + public void setScore(int score) { + this.score = score; + } + + public int getHighestScore() { + return highestScore; + } + + public void setHighestScore(int highestScore) { + this.highestScore = highestScore; + } + + public int getScore() { + return score; + } + + public Set getFlings() { + return flings; + } + + public Set getBullets() { + return bullets; + } + + public Player getPlayer() { + return player; + } + + public void setPlayer(Player player) { + this.player = player; + } + + public FlyingObject addEnemy() { + flyIndex++; + FlyingObject obj = null; + if (flyIndex % 40 == 0) { + obj = randomEnemy(); + flings.add(obj); + } + return obj; + } + + public Bullet[] shootAction() { + shootIndex++; + Bullet[] bs = null; + if (shootIndex % 30 == 0) { + bs = player.shoot(); + for (int i = 0; i < bs.length; i++) + bullets.add(bs[i]); + } + return bs; + } + + public void checkBulletAction() { + for (Bullet bl: bullets) { + checkBullet(bl); + } + } + + public void outOfBoundsAction() { + Iterator it= flings.iterator(); + while (it.hasNext()) { + FlyingObject fo =(FlyingObject)it.next(); + if (fo.outOfBounds()) { + fo.setState(FlyingObjectState.DEAD); + it.remove(); + } + } + it=bullets.iterator(); + while (it.hasNext()) { + Bullet bl =(Bullet)it.next(); + if (bl.outOfBounds()) { + bl.setState(FlyingObjectState.DEAD); + it.remove(); + } + } + } + + public void checkGameOverAction() { + Iterator it= flings.iterator(); + while(it.hasNext()){ + FlyingObject fo=(FlyingObject) it.next(); + if (player.hit(fo)) { + player.subLife(); + player.setDoubleFire(0); + it.remove(); + fo.setState(FlyingObjectState.DEAD); + } + } + if (player.getLife() <= 0) { + state = GameState.GAME_OVER; + } + } + + public void checkBullet(Bullet bullet) { + for (FlyingObject fo : flings) { + if (fo.shootBy(bullet)) { + fo.setState(FlyingObjectState.DEAD); + flings.remove(fo); + if (fo instanceof Airplane) { + Airplane airplane = (Airplane) fo; + score += airplane.getScore(); + } else { + Bee bee = (Bee) fo; + switch (bee.getType()) { + case 0: + player.addDoubleFire(); + break; + case 1: + player.addLife(); + break; + } + } + break; + } + } + } + + public void clearAllFlyingObject(){ + for(FlyingObject fo : flings) + fo.setState(FlyingObjectState.DEAD); + for(FlyingObject fo : bullets) + fo.setState(FlyingObjectState.DEAD); + flings.clear(); + bullets.clear(); + player.setState(FlyingObjectState.STOP); + player.setDoubleFire(0); + } + + public static FlyingObject randomEnemy() { + Random random = new Random(); + int type = random.nextInt(20); + if (type < 4) + return new Bee(); + else + return new Airplane(); + } + + public void saveHighestScore(){ + if(this.score>this.highestScore) + highestScore=score; + } + +} diff --git a/Small-plane-game-master/src/main/entry/Airplane.java b/Small-plane-game-master/src/main/entry/Airplane.java new file mode 100644 index 0000000..5d2caa9 --- /dev/null +++ b/Small-plane-game-master/src/main/entry/Airplane.java @@ -0,0 +1,37 @@ +package main.entry; + +import main.Enum.FlyingObjectState; +import main.Frame; + +import java.util.Random; + + +public class Airplane extends FlyingObject { + private int speed = 3; + + public Airplane(){ + this.image = Frame.airplane; + width = image.getWidth(); + height = image.getHeight(); + y = -height; + Random rand = new Random(); + x = rand.nextInt(Frame.WIDTH - width); + state= FlyingObjectState.LIVE; + } + + public int getScore() { + return 5; + } + + @Override + public boolean outOfBounds() { + return y>Frame.HEIGHT; + } + + @Override + public void step() { + y += speed; + } + +} + diff --git a/Small-plane-game-master/src/main/entry/Bee.java b/Small-plane-game-master/src/main/entry/Bee.java new file mode 100644 index 0000000..cef6ba5 --- /dev/null +++ b/Small-plane-game-master/src/main/entry/Bee.java @@ -0,0 +1,43 @@ +package main.entry; + +import main.Enum.FlyingObjectState; +import main.Frame; + +import java.util.Random; + +public class Bee extends FlyingObject { + private int xSpeed = 1; + private int ySpeed = 2; + private int awardType; + + public Bee(){ + this.image = Frame.bee; + width = image.getWidth(); + height = image.getHeight(); + y = -height; + Random rand = new Random(); + x = rand.nextInt(Frame.WIDTH - width); + awardType = rand.nextInt(2); + state= FlyingObjectState.LIVE; + } + + public int getType(){ + return awardType; + } + + @Override + public boolean outOfBounds() { + return y>Frame.HEIGHT; + } + + @Override + public void step() { + x += xSpeed; + y += ySpeed; + if(x > Frame.WIDTH-width) + xSpeed = -1; + if(x < 0) + xSpeed = 1; + } + +} \ No newline at end of file diff --git a/Small-plane-game-master/src/main/entry/Bullet.java b/Small-plane-game-master/src/main/entry/Bullet.java new file mode 100644 index 0000000..1c8b983 --- /dev/null +++ b/Small-plane-game-master/src/main/entry/Bullet.java @@ -0,0 +1,27 @@ +package main.entry; + +import main.Enum.FlyingObjectState; +import main.Frame; + +public class Bullet extends FlyingObject { + private int speed = 3; + + public Bullet(int x,int y){ + this.x = x; + this.y = y; + this.image = Frame.bullet; + state= FlyingObjectState.LIVE; + } + + @Override + public void step(){ + y-=speed; + } + + @Override + public boolean outOfBounds() { + return y<0; + } + + +} diff --git a/Small-plane-game-master/src/main/entry/FlyingObject.java b/Small-plane-game-master/src/main/entry/FlyingObject.java new file mode 100644 index 0000000..be60799 --- /dev/null +++ b/Small-plane-game-master/src/main/entry/FlyingObject.java @@ -0,0 +1,88 @@ +package main.entry; + +import main.Enum.FlyingObjectState; + +import java.awt.image.BufferedImage; + + +public abstract class FlyingObject implements Runnable{ + protected int x; + protected int y; + protected int width; + protected int height; + protected BufferedImage image; + protected FlyingObjectState state; + protected Thread thread=null; + + 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 getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public FlyingObjectState getState() { + return state; + } + + public void setState(FlyingObjectState state) { + this.state = state; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public BufferedImage getImage() { + return image; + } + + public void setImage(BufferedImage image) { + this.image = image; + } + + public abstract boolean outOfBounds(); + + public abstract void step(); + + public boolean shootBy(Bullet bullet){ + int x = bullet.x; + int y = bullet.y; + return this.x0){ + Bullet[] bullets = new Bullet[2]; + bullets[0] = new Bullet(x+xStep,y-yStep); + bullets[1] = new Bullet(x+3*xStep,y-yStep); + return bullets; + }else{ + Bullet[] bullets = new Bullet[1]; + bullets[0] = new Bullet(x+2*xStep,y-yStep); + return bullets; + } + } + + @Override + public void step() { + if(images.length>0){ + image = images[index++/10%images.length]; + } + } + + public boolean hit(FlyingObject other){ + + int x1 = other.x - this.width/2; + int x2 = other.x + this.width/2 + other.width; + int y1 = other.y - this.height/2; + int y2 = other.y + this.height/2 + other.height; + + int Playerx = this.x + this.width/2; + int Playery = this.y + this.height/2; + + return Playerx>x1 && Playerxy1 && Playery