first commit

master
liufeng 6 years ago
commit 238b331ed1

@ -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

@ -0,0 +1 @@
# Small-plane-game

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 408 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: main.Game

@ -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);
}
}
}
}

@ -0,0 +1,5 @@
package main.Enum;
public enum FlyingObjectState {
DEAD,LIVE,STOP;
}

@ -0,0 +1,5 @@
package main.Enum;
public enum GameState {
START,RUNNING,PAUSE,GAME_OVER;
}

@ -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<FlyingObject> flyings = game.getFlings();
Set<Bullet> 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<FlyingObject> flyings = game.getFlings();
Set<Bullet> 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);
}
}

@ -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<FlyingObject> flings;
private HashSet<Bullet> 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<FlyingObject> getFlings() {
return flings;
}
public Set<Bullet> 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;
}
}

@ -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;
}
}

@ -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;
}
}

@ -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;
}
}

@ -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.x<x && x<this.x+width && this.y<y && y<this.y+height;
}
@Override
public void run() {
while(state!= FlyingObjectState.DEAD) {
try {
if(state!=FlyingObjectState.STOP)
step();
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

@ -0,0 +1,13 @@
package main.entry;
public class Gamer {
private int id;
private String name;
private int score;
public Gamer(int id, String name, int score) {
this.id = id;
this.name = name;
this.score = score;
}
}

@ -0,0 +1,106 @@
package main.entry;
import main.Frame;
import main.Enum.FlyingObjectState;
import java.awt.image.BufferedImage;
public class Player extends FlyingObject {
private BufferedImage[] images = {};
private int index = 0;
private int doubleFire;
private int life;
public Player(){
life = 3;
doubleFire = 0;
images = new BufferedImage[]{Frame.player0, Frame.player1};
image = Frame.player0;
width = image.getWidth();
height = image.getHeight();
x = 150;
y = 400;
state= FlyingObjectState.LIVE;
}
public void setLife(int life) {
this.life = life;
}
public void setDoubleFire(int doubleFire) {
this.doubleFire = doubleFire;
}
public void addDoubleFire(){
doubleFire = 40;
}
public void addLife(){
life++;
}
public void subLife(){
life--;
}
public int getLife(){
return life;
}
public void moveTo(int x,int y){
this.x = x - width/2;
this.y = y - height/2;
}
@Override
public boolean outOfBounds() {
return false;
}
public Bullet[] shoot(){
int xStep = width/4;
int yStep = 20;
if(doubleFire>0){
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 && Playerx<x2 && Playery>y1 && Playery<y2;
}
}
Loading…
Cancel
Save