Repository: wblearn/Small-plane-game
Branch: master
Commit: 2065e23767c4
Files: 12
Total size: 34.2 KB
Directory structure:
gitextract_6vaf1i7i/
├── .classpath
├── .project
├── .settings/
│ └── org.eclipse.jdt.core.prefs
├── README.md
└── src/
└── com/
└── tarena/
└── fly/
├── Airplane.java
├── Award.java
├── Bee.java
├── Bullet.java
├── Enemy.java
├── FlyingObject.java
├── Hero.java
└── ShootGame.java
================================================
FILE CONTENTS
================================================
================================================
FILE: .classpath
================================================
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="output" path="bin"/>
</classpath>
================================================
FILE: .project
================================================
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Shoot</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
================================================
FILE: .settings/org.eclipse.jdt.core.prefs
================================================
#Mon Jul 21 11:48:12 CST 2014
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
================================================
FILE: README.md
================================================

## 写在前面
技术源于分享,所以今天抽空把自己之前用java做过的小游戏整理贴出来给大家参考学习。java确实不适合写桌面应用,这里只是**通过这个游戏让大家理解oop面向对象编程的过程**,纯属娱乐。代码写的很简单,也很容易理解,并且注释写的很清楚了,还有问题,自己私下去补课学习。
## 完整代码
>敌飞机
```
import java.util.Random;
敌飞机: 是飞行物,也是敌人
public class Airplane extends FlyingObject implements Enemy {
private int speed = 3; //移动步骤
/** 初始化数据 */
public Airplane(){
this.image = ShootGame.airplane;
width = image.getWidth();
height = image.getHeight();
y = -height;
Random rand = new Random();
x = rand.nextInt(ShootGame.WIDTH - width);
}
/** 获取分数 */
@Override
public int getScore() {
return 5;
}
/** //越界处理 */
@Override
public boolean outOfBounds() {
return y>ShootGame.HEIGHT;
}
/** 移动 */
@Override
public void step() {
y += speed;
}
}
```
>分数奖励
```
/**
* 奖励
*/
public interface Award {
int DOUBLE_FIRE = 0; //双倍火力
int LIFE = 1; //1条命
/** 获得奖励类型(上面的0或1) */
int getType();
}
```
>蜜蜂
```
import java.util.Random;
/** 蜜蜂 */
public class Bee extends FlyingObject implements Award{
private int xSpeed = 1; //x坐标移动速度
private int ySpeed = 2; //y坐标移动速度
private int awardType; //奖励类型
/** 初始化数据 */
public Bee(){
this.image = ShootGame.bee;
width = image.getWidth();
height = image.getHeight();
y = -height;
Random rand = new Random();
x = rand.nextInt(ShootGame.WIDTH - width);
awardType = rand.nextInt(2); //初始化时给奖励
}
/** 获得奖励类型 */
public int getType(){
return awardType;
}
/** 越界处理 */
@Override
public boolean outOfBounds() {
return y>ShootGame.HEIGHT;
}
/** 移动,可斜着飞 */
@Override
public void step() {
x += xSpeed;
y += ySpeed;
if(x > ShootGame.WIDTH-width){
xSpeed = -1;
}
if(x < 0){
xSpeed = 1;
}
}
}
```
>子弹类:是飞行物体
```
/**
* 子弹类:是飞行物
*/
public class Bullet extends FlyingObject {
private int speed = 3; //移动的速度
/** 初始化数据 */
public Bullet(int x,int y){
this.x = x;
this.y = y;
this.image = ShootGame.bullet;
}
/** 移动 */
@Override
public void step(){
y-=speed;
}
/** 越界处理 */
@Override
public boolean outOfBounds() {
return y<-height;
}
}
```
>敌人的分数
```
/**
* 敌人,可以有分数
*/
public interface Enemy {
/** 敌人的分数 */
int getScore();
}
```
>飞行物(敌机,蜜蜂,子弹,英雄机)
```
import java.awt.image.BufferedImage;
/**
* 飞行物(敌机,蜜蜂,子弹,英雄机)
*/
public abstract class FlyingObject {
protected int x; //x坐标
protected int y; //y坐标
protected int width; //宽
protected int height; //高
protected BufferedImage image; //图片
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 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;
}
/**
* 检查是否出界
* @return true 出界与否
*/
public abstract boolean outOfBounds();
/**
* 飞行物移动一步
*/
public abstract void step();
/**
* 检查当前飞行物体是否被子弹(x,y)击(shoot)中
* @param Bullet 子弹对象
* @return true表示被击中了
*/
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;
}
}
```
>英雄机
```
import java.awt.image.BufferedImage;
/**
* 英雄机:是飞行物
*/
public class Hero extends FlyingObject{
private BufferedImage[] images = {}; //英雄机图片
private int index = 0; //英雄机图片切换索引
private int doubleFire; //双倍火力
private int life; //命
/** 初始化数据 */
public Hero(){
life = 3; //初始3条命
doubleFire = 0; //初始火力为0
images = new BufferedImage[]{ShootGame.hero0, ShootGame.hero1}; //英雄机图片数组
image = ShootGame.hero0; //初始为hero0图片
width = image.getWidth();
height = image.getHeight();
x = 150;
y = 400;
}
/** 获取双倍火力 */
public int isDoubleFire() {
return doubleFire;
}
/** 设置双倍火力 */
public void setDoubleFire(int doubleFire) {
this.doubleFire = doubleFire;
}
/** 增加火力 */
public void addDoubleFire(){
doubleFire = 40;
}
/** 增命 */
public void addLife(){ //增命
life++;
}
/** 减命 */
public void subtractLife(){ //减命
life--;
}
/** 获取命 */
public int getLife(){
return life;
}
/** 当前物体移动了一下,相对距离,x,y鼠标位置 */
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; //4半
int yStep = 20; //步
if(doubleFire>0){ //双倍火力
Bullet[] bullets = new Bullet[2];
bullets[0] = new Bullet(x+xStep,y-yStep); //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]; //切换图片hero0,hero1
}
}
/** 碰撞算法 */
public boolean hit(FlyingObject other){
int x1 = other.x - this.width/2; //x坐标最小距离
int x2 = other.x + this.width/2 + other.width; //x坐标最大距离
int y1 = other.y - this.height/2; //y坐标最小距离
int y2 = other.y + this.height/2 + other.height; //y坐标最大距离
int herox = this.x + this.width/2; //英雄机x坐标中心点距离
int heroy = this.y + this.height/2; //英雄机y坐标中心点距离
return herox>x1 && herox<x2 && heroy>y1 && heroy<y2; //区间范围内为撞上了
}
}
```
>游戏启动主类
```
import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ShootGame extends JPanel {
public static final int WIDTH = 400; // 面板宽
public static final int HEIGHT = 654; // 面板高
/** 游戏的当前状态: START RUNNING PAUSE GAME_OVER */
private int state;
private static final int START = 0;
private static final int RUNNING = 1;
private static final int PAUSE = 2;
private static final int GAME_OVER = 3;
private int score = 0; // 得分
private Timer timer; // 定时器
private int intervel = 1000 / 100; // 时间间隔(毫秒)
public static BufferedImage background;
public static BufferedImage start;
public static BufferedImage airplane;
public static BufferedImage bee;
public static BufferedImage bullet;
public static BufferedImage hero0;
public static BufferedImage hero1;
public static BufferedImage pause;
public static BufferedImage gameover;
private FlyingObject[] flyings = {}; // 敌机数组
private Bullet[] bullets = {}; // 子弹数组
private Hero hero = new Hero(); // 英雄机
static { // 静态代码块,初始化图片资源
try {
background = ImageIO.read(ShootGame.class
.getResource("background.png"));
start = ImageIO.read(ShootGame.class.getResource("start.png"));
airplane = ImageIO
.read(ShootGame.class.getResource("airplane.png"));
bee = ImageIO.read(ShootGame.class.getResource("bee.png"));
bullet = ImageIO.read(ShootGame.class.getResource("bullet.png"));
hero0 = ImageIO.read(ShootGame.class.getResource("hero0.png"));
hero1 = ImageIO.read(ShootGame.class.getResource("hero1.png"));
pause = ImageIO.read(ShootGame.class.getResource("pause.png"));
gameover = ImageIO
.read(ShootGame.class.getResource("gameover.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
/** 画 */
@Override
public void paint(Graphics g) {
g.drawImage(background, 0, 0, null); // 画背景图
paintHero(g); // 画英雄机
paintBullets(g); // 画子弹
paintFlyingObjects(g); // 画飞行物
paintScore(g); // 画分数
paintState(g); // 画游戏状态
}
/** 画英雄机 */
public void paintHero(Graphics g) {
g.drawImage(hero.getImage(), hero.getX(), hero.getY(), null);
}
/** 画子弹 */
public void paintBullets(Graphics g) {
for (int i = 0; i < bullets.length; i++) {
Bullet b = bullets[i];
g.drawImage(b.getImage(), b.getX() - b.getWidth() / 2, b.getY(),
null);
}
}
/** 画飞行物 */
public void paintFlyingObjects(Graphics g) {
for (int i = 0; i < flyings.length; i++) {
FlyingObject f = flyings[i];
g.drawImage(f.getImage(), f.getX(), f.getY(), null);
}
}
/** 画分数 */
public void paintScore(Graphics g) {
int x = 10; // x坐标
int y = 25; // y坐标
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 22); // 字体
g.setColor(new Color(0xFF0000));
g.setFont(font); // 设置字体
g.drawString("SCORE:" + score, x, y); // 画分数
y=y+20; // y坐标增20
g.drawString("LIFE:" + hero.getLife(), x, y); // 画命
}
/** 画游戏状态 */
public void paintState(Graphics g) {
switch (state) {
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 static void main(String[] args) {
JFrame frame = new JFrame("Fly");
ShootGame game = new ShootGame(); // 面板对象
frame.add(game); // 将面板添加到JFrame中
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); // 尽快调用paint
game.action(); // 启动执行
}
/** 启动执行代码 */
public void action() {
// 鼠标监听事件
MouseAdapter l = new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) { // 鼠标移动
if (state == RUNNING) { // 运行状态下移动英雄机--随鼠标位置
int x = e.getX();
int y = e.getY();
hero.moveTo(x, y);
}
}
@Override
public void mouseEntered(MouseEvent e) { // 鼠标进入
if (state == PAUSE) { // 暂停状态下运行
state = RUNNING;
}
}
@Override
public void mouseExited(MouseEvent e) { // 鼠标退出
if (state == RUNNING) { // 游戏未结束,则设置其为暂停
state = PAUSE;
}
}
@Override
public void mouseClicked(MouseEvent e) { // 鼠标点击
switch (state) {
case START:
state = RUNNING; // 启动状态下运行
break;
case GAME_OVER: // 游戏结束,清理现场
flyings = new FlyingObject[0]; // 清空飞行物
bullets = new Bullet[0]; // 清空子弹
hero = new Hero(); // 重新创建英雄机
score = 0; // 清空成绩
state = START; // 状态设置为启动
break;
}
}
};
this.addMouseListener(l); // 处理鼠标点击操作
this.addMouseMotionListener(l); // 处理鼠标滑动操作
timer = new Timer(); // 主流程控制
timer.schedule(new TimerTask() {
@Override
public void run() {
if (state == RUNNING) { // 运行状态
enterAction(); // 飞行物入场
stepAction(); // 走一步
shootAction(); // 英雄机射击
bangAction(); // 子弹打飞行物
outOfBoundsAction(); // 删除越界飞行物及子弹
checkGameOverAction(); // 检查游戏结束
}
repaint(); // 重绘,调用paint()方法
}
}, intervel, intervel);
}
int flyEnteredIndex = 0; // 飞行物入场计数
/** 飞行物入场 */
public void enterAction() {
flyEnteredIndex++;
if (flyEnteredIndex % 40 == 0) { // 400毫秒生成一个飞行物--10*40
FlyingObject obj = nextOne(); // 随机生成一个飞行物
flyings = Arrays.copyOf(flyings, flyings.length + 1);
flyings[flyings.length - 1] = obj;
}
}
/** 走一步 */
public void stepAction() {
for (int i = 0; i < flyings.length; i++) { // 飞行物走一步
FlyingObject f = flyings[i];
f.step();
}
for (int i = 0; i < bullets.length; i++) { // 子弹走一步
Bullet b = bullets[i];
b.step();
}
hero.step(); // 英雄机走一步
}
/** 飞行物走一步 */
public void flyingStepAction() {
for (int i = 0; i < flyings.length; i++) {
FlyingObject f = flyings[i];
f.step();
}
}
int shootIndex = 0; // 射击计数
/** 射击 */
public void shootAction() {
shootIndex++;
if (shootIndex % 30 == 0) { // 300毫秒发一颗
Bullet[] bs = hero.shoot(); // 英雄打出子弹
bullets = Arrays.copyOf(bullets, bullets.length + bs.length); // 扩容
System.arraycopy(bs, 0, bullets, bullets.length - bs.length,
bs.length); // 追加数组
}
}
/** 子弹与飞行物碰撞检测 */
public void bangAction() {
for (int i = 0; i < bullets.length; i++) { // 遍历所有子弹
Bullet b = bullets[i];
bang(b); // 子弹和飞行物之间的碰撞检查
}
}
/** 删除越界飞行物及子弹 */
public void outOfBoundsAction() {
int index = 0; // 索引
FlyingObject[] flyingLives = new FlyingObject[flyings.length]; // 活着的飞行物
for (int i = 0; i < flyings.length; i++) {
FlyingObject f = flyings[i];
if (!f.outOfBounds()) {
flyingLives[index++] = f; // 不越界的留着
}
}
flyings = Arrays.copyOf(flyingLives, index); // 将不越界的飞行物都留着
index = 0; // 索引重置为0
Bullet[] bulletLives = new Bullet[bullets.length];
for (int i = 0; i < bullets.length; i++) {
Bullet b = bullets[i];
if (!b.outOfBounds()) {
bulletLives[index++] = b;
}
}
bullets = Arrays.copyOf(bulletLives, index); // 将不越界的子弹留着
}
/** 检查游戏结束 */
public void checkGameOverAction() {
if (isGameOver()==true) {
state = GAME_OVER; // 改变状态
}
}
/** 检查游戏是否结束 */
public boolean isGameOver() {
for (int i = 0; i < flyings.length; i++) {
int index = -1;
FlyingObject obj = flyings[i];
if (hero.hit(obj)) { // 检查英雄机与飞行物是否碰撞
hero.subtractLife(); // 减命
hero.setDoubleFire(0); // 双倍火力解除
index = i; // 记录碰上的飞行物索引
}
if (index != -1) {
FlyingObject t = flyings[index];
flyings[index] = flyings[flyings.length - 1];
flyings[flyings.length - 1] = t; // 碰上的与最后一个飞行物交换
flyings = Arrays.copyOf(flyings, flyings.length - 1); // 删除碰上的飞行物
}
}
return hero.getLife() <= 0;
}
/** 子弹和飞行物之间的碰撞检查 */
public void bang(Bullet bullet) {
int index = -1; // 击中的飞行物索引
for (int i = 0; i < flyings.length; i++) {
FlyingObject obj = flyings[i];
if (obj.shootBy(bullet)) { // 判断是否击中
index = i; // 记录被击中的飞行物的索引
break;
}
}
if (index != -1) { // 有击中的飞行物
FlyingObject one = flyings[index]; // 记录被击中的飞行物
FlyingObject temp = flyings[index]; // 被击中的飞行物与最后一个飞行物交换
flyings[index] = flyings[flyings.length - 1];
flyings[flyings.length - 1] = temp;
flyings = Arrays.copyOf(flyings, flyings.length - 1); // 删除最后一个飞行物(即被击中的)
// 检查one的类型(敌人加分,奖励获取)
if (one instanceof Enemy) { // 检查类型,是敌人,则加分
Enemy e = (Enemy) one; // 强制类型转换
score += e.getScore(); // 加分
} else { // 若为奖励,设置奖励
Award a = (Award) one;
int type = a.getType(); // 获取奖励类型
switch (type) {
case Award.DOUBLE_FIRE:
hero.addDoubleFire(); // 设置双倍火力
break;
case Award.LIFE:
hero.addLife(); // 设置加命
break;
}
}
}
}
/**
* 随机生成飞行物
*
* @return 飞行物对象
*/
public static FlyingObject nextOne() {
Random random = new Random();
int type = random.nextInt(20); // [0,20)
if (type < 4) {
return new Bee();
} else {
return new Airplane();
}
}
}
```
#写在最后
以上就是这个游戏我整理的完整代码,因为图片差不多9张,所以图片没上传,需要图片的友友请简信我,最后,我做了一张思维导图贴出来让大家更好的理解OOP面向对象编程的过程。

>ps:码字很累,友友们点个赞或者关注,谢谢,么么哒~~
>资源已上传(包括图片),下载地址请<a href="http://download.csdn.net/detail/wudalang_gd/9694422">戳这里</a>
------2016/12/25更新---------------
上面的小飞机游戏下载地址失效了,最近开通了[我的github博客](https://wblearn.github.io/),欢迎大家测试,同时方便以后托管一些项目及资源。下载地址:[Java打飞机小游戏(附完整源码)](https://github.com/wblearn/Small-plane-game)
================================================
FILE: src/com/tarena/fly/Airplane.java
================================================
package com.tarena.fly;
import java.util.Random;
/**
* зɻ: ǷҲǵ
*/
public class Airplane extends FlyingObject implements Enemy {
private int speed = 3; //ƶ
/** ʼ */
public Airplane(){
this.image = ShootGame.airplane;
width = image.getWidth();
height = image.getHeight();
y = -height;
Random rand = new Random();
x = rand.nextInt(ShootGame.WIDTH - width);
}
/** ȡ */
@Override
public int getScore() {
return 5;
}
/** //Խ紦 */
@Override
public boolean outOfBounds() {
return y>ShootGame.HEIGHT;
}
/** ƶ */
@Override
public void step() {
y += speed;
}
}
================================================
FILE: src/com/tarena/fly/Award.java
================================================
package com.tarena.fly;
/**
*
*/
public interface Award {
int DOUBLE_FIRE = 0; //˫
int LIFE = 1; //1
/** ý(01) */
int getType();
}
================================================
FILE: src/com/tarena/fly/Bee.java
================================================
package com.tarena.fly;
import java.util.Random;
/** ۷ */
public class Bee extends FlyingObject implements Award{
private int xSpeed = 1; //xƶٶ
private int ySpeed = 2; //yƶٶ
private int awardType; //
/** ʼ */
public Bee(){
this.image = ShootGame.bee;
width = image.getWidth();
height = image.getHeight();
y = -height;
Random rand = new Random();
x = rand.nextInt(ShootGame.WIDTH - width);
awardType = rand.nextInt(2); //ʼʱ
}
/** ý */
public int getType(){
return awardType;
}
/** Խ紦 */
@Override
public boolean outOfBounds() {
return y>ShootGame.HEIGHT;
}
/** ƶбŷ */
@Override
public void step() {
x += xSpeed;
y += ySpeed;
if(x > ShootGame.WIDTH-width){
xSpeed = -1;
}
if(x < 0){
xSpeed = 1;
}
}
}
================================================
FILE: src/com/tarena/fly/Bullet.java
================================================
package com.tarena.fly;
/**
* ӵ:Ƿ
*/
public class Bullet extends FlyingObject {
private int speed = 3; //ƶٶ
/** ʼ */
public Bullet(int x,int y){
this.x = x;
this.y = y;
this.image = ShootGame.bullet;
}
/** ƶ */
@Override
public void step(){
y-=speed;
}
/** Խ紦 */
@Override
public boolean outOfBounds() {
return y<-height;
}
}
================================================
FILE: src/com/tarena/fly/Enemy.java
================================================
package com.tarena.fly;
/**
* ˣз
*/
public interface Enemy {
/** ˵ķ */
int getScore();
}
================================================
FILE: src/com/tarena/fly/FlyingObject.java
================================================
package com.tarena.fly;
import java.awt.image.BufferedImage;
/**
* (л۷䣬ӵӢۻ)
*/
public abstract class FlyingObject {
protected int x; //x
protected int y; //y
protected int width; //
protected int height; //
protected BufferedImage image; //ͼƬ
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 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;
}
/**
* Ƿ
* @return true
*/
public abstract boolean outOfBounds();
/**
* ƶһ
*/
public abstract void step();
/**
* 鵱ǰǷӵ(x,y)(shoot)
* @param Bullet ӵ
* @return trueʾ
*/
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;
}
}
================================================
FILE: src/com/tarena/fly/Hero.java
================================================
package com.tarena.fly;
import java.awt.image.BufferedImage;
/**
* Ӣۻ:Ƿ
*/
public class Hero extends FlyingObject{
private BufferedImage[] images = {}; //ӢۻͼƬ
private int index = 0; //ӢۻͼƬл
private int doubleFire; //˫
private int life; //
/** ʼ */
public Hero(){
life = 3; //ʼ3
doubleFire = 0; //ʼΪ0
images = new BufferedImage[]{ShootGame.hero0, ShootGame.hero1}; //ӢۻͼƬ
image = ShootGame.hero0; //ʼΪhero0ͼƬ
width = image.getWidth();
height = image.getHeight();
x = 150;
y = 400;
}
/** ȡ˫ */
public int isDoubleFire() {
return doubleFire;
}
/** ˫ */
public void setDoubleFire(int doubleFire) {
this.doubleFire = doubleFire;
}
/** ӻ */
public void addDoubleFire(){
doubleFire = 40;
}
/** */
public void addLife(){ //
life++;
}
/** */
public void subtractLife(){ //
life--;
}
/** ȡ */
public int getLife(){
return life;
}
/** ǰƶһ£Ծ룬x,yλ */
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; //4
int yStep = 20; //
if(doubleFire>0){ //˫
Bullet[] bullets = new Bullet[2];
bullets[0] = new Bullet(x+xStep,y-yStep); //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]; //лͼƬhero0hero1
}
}
/** ײ㷨 */
public boolean hit(FlyingObject other){
int x1 = other.x - this.width/2; //xС
int x2 = other.x + this.width/2 + other.width; //x
int y1 = other.y - this.height/2; //yС
int y2 = other.y + this.height/2 + other.height; //y
int herox = this.x + this.width/2; //Ӣۻxĵ
int heroy = this.y + this.height/2; //Ӣۻyĵ
return herox>x1 && herox<x2 && heroy>y1 && heroy<y2; //䷶ΧΪײ
}
}
================================================
FILE: src/com/tarena/fly/ShootGame.java
================================================
package com.tarena.fly;
import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ShootGame extends JPanel {
public static final int WIDTH = 400; //
public static final int HEIGHT = 654; //
/** Ϸĵǰ״̬: START RUNNING PAUSE GAME_OVER */
private int state;
private static final int START = 0;
private static final int RUNNING = 1;
private static final int PAUSE = 2;
private static final int GAME_OVER = 3;
private int score = 0; // ÷
private Timer timer; // ʱ
private int intervel = 1000 / 100; // ʱ()
public static BufferedImage background;
public static BufferedImage start;
public static BufferedImage airplane;
public static BufferedImage bee;
public static BufferedImage bullet;
public static BufferedImage hero0;
public static BufferedImage hero1;
public static BufferedImage pause;
public static BufferedImage gameover;
private FlyingObject[] flyings = {}; // л
private Bullet[] bullets = {}; // ӵ
private Hero hero = new Hero(); // Ӣۻ
static { // ̬飬ʼͼƬԴ
try {
background = ImageIO.read(ShootGame.class
.getResource("background.png"));
start = ImageIO.read(ShootGame.class.getResource("start.png"));
airplane = ImageIO
.read(ShootGame.class.getResource("airplane.png"));
bee = ImageIO.read(ShootGame.class.getResource("bee.png"));
bullet = ImageIO.read(ShootGame.class.getResource("bullet.png"));
hero0 = ImageIO.read(ShootGame.class.getResource("hero0.png"));
hero1 = ImageIO.read(ShootGame.class.getResource("hero1.png"));
pause = ImageIO.read(ShootGame.class.getResource("pause.png"));
gameover = ImageIO
.read(ShootGame.class.getResource("gameover.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
/** */
@Override
public void paint(Graphics g) {
g.drawImage(background, 0, 0, null); // ͼ
paintHero(g); // Ӣۻ
paintBullets(g); // ӵ
paintFlyingObjects(g); //
paintScore(g); //
paintState(g); // Ϸ״̬
}
/** Ӣۻ */
public void paintHero(Graphics g) {
g.drawImage(hero.getImage(), hero.getX(), hero.getY(), null);
}
/** ӵ */
public void paintBullets(Graphics g) {
for (int i = 0; i < bullets.length; i++) {
Bullet b = bullets[i];
g.drawImage(b.getImage(), b.getX() - b.getWidth() / 2, b.getY(),
null);
}
}
/** */
public void paintFlyingObjects(Graphics g) {
for (int i = 0; i < flyings.length; i++) {
FlyingObject f = flyings[i];
g.drawImage(f.getImage(), f.getX(), f.getY(), null);
}
}
/** */
public void paintScore(Graphics g) {
int x = 10; // x
int y = 25; // y
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 22); //
g.setColor(new Color(0xFF0000));
g.setFont(font); //
g.drawString("SCORE:" + score, x, y); //
y=y+20; // y20
g.drawString("LIFE:" + hero.getLife(), x, y); //
}
/** Ϸ״̬ */
public void paintState(Graphics g) {
switch (state) {
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 static void main(String[] args) {
JFrame frame = new JFrame("Fly");
ShootGame game = new ShootGame(); //
frame.add(game); // ӵJFrame
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); // paint
game.action(); // ִ
}
/** ִд */
public void action() {
// ¼
MouseAdapter l = new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) { // ƶ
if (state == RUNNING) { // ״̬ƶӢۻ--λ
int x = e.getX();
int y = e.getY();
hero.moveTo(x, y);
}
}
@Override
public void mouseEntered(MouseEvent e) { //
if (state == PAUSE) { // ͣ״̬
state = RUNNING;
}
}
@Override
public void mouseExited(MouseEvent e) { // ˳
if (state == RUNNING) { // ϷδΪͣ
state = PAUSE;
}
}
@Override
public void mouseClicked(MouseEvent e) { //
switch (state) {
case START:
state = RUNNING; // ״̬
break;
case GAME_OVER: // Ϸֳ
flyings = new FlyingObject[0]; // շ
bullets = new Bullet[0]; // ӵ
hero = new Hero(); // ´Ӣۻ
score = 0; // ճɼ
state = START; // ״̬Ϊ
break;
}
}
};
this.addMouseListener(l); //
this.addMouseMotionListener(l); // 껬
timer = new Timer(); // ̿
timer.schedule(new TimerTask() {
@Override
public void run() {
if (state == RUNNING) { // ״̬
enterAction(); // 볡
stepAction(); // һ
shootAction(); // Ӣۻ
bangAction(); // ӵ
outOfBoundsAction(); // ɾԽPӵ
checkGameOverAction(); // Ϸ
}
repaint(); // ػ棬paint()
}
}, intervel, intervel);
}
int flyEnteredIndex = 0; // 볡
/** 볡 */
public void enterAction() {
flyEnteredIndex++;
if (flyEnteredIndex % 40 == 0) { // 400һ--10*40
FlyingObject obj = nextOne(); // һ
flyings = Arrays.copyOf(flyings, flyings.length + 1);
flyings[flyings.length - 1] = obj;
}
}
/** һ */
public void stepAction() {
for (int i = 0; i < flyings.length; i++) { // һ
FlyingObject f = flyings[i];
f.step();
}
for (int i = 0; i < bullets.length; i++) { // ӵһ
Bullet b = bullets[i];
b.step();
}
hero.step(); // Ӣۻһ
}
/** һ */
public void flyingStepAction() {
for (int i = 0; i < flyings.length; i++) {
FlyingObject f = flyings[i];
f.step();
}
}
int shootIndex = 0; //
/** */
public void shootAction() {
shootIndex++;
if (shootIndex % 30 == 0) { // 300뷢һ
Bullet[] bs = hero.shoot(); // Ӣ۴ӵ
bullets = Arrays.copyOf(bullets, bullets.length + bs.length); //
System.arraycopy(bs, 0, bullets, bullets.length - bs.length,
bs.length); //
}
}
/** ӵײ */
public void bangAction() {
for (int i = 0; i < bullets.length; i++) { // ӵ
Bullet b = bullets[i];
bang(b); // ӵͷ֮ײ
}
}
/** ɾԽPӵ */
public void outOfBoundsAction() {
int index = 0; //
FlyingObject[] flyingLives = new FlyingObject[flyings.length]; // ŵķ
for (int i = 0; i < flyings.length; i++) {
FlyingObject f = flyings[i];
if (!f.outOfBounds()) {
flyingLives[index++] = f; // Խ
}
}
flyings = Arrays.copyOf(flyingLives, index); // Խķﶼ
index = 0; // Ϊ0
Bullet[] bulletLives = new Bullet[bullets.length];
for (int i = 0; i < bullets.length; i++) {
Bullet b = bullets[i];
if (!b.outOfBounds()) {
bulletLives[index++] = b;
}
}
bullets = Arrays.copyOf(bulletLives, index); // Խӵ
}
/** Ϸ */
public void checkGameOverAction() {
if (isGameOver()==true) {
state = GAME_OVER; // ı״̬
}
}
/** ϷǷ */
public boolean isGameOver() {
for (int i = 0; i < flyings.length; i++) {
int index = -1;
FlyingObject obj = flyings[i];
if (hero.hit(obj)) { // ӢۻǷײ
hero.subtractLife(); //
hero.setDoubleFire(0); // ˫
index = i; // ¼ϵķ
}
if (index != -1) {
FlyingObject t = flyings[index];
flyings[index] = flyings[flyings.length - 1];
flyings[flyings.length - 1] = t; // ϵһサ
flyings = Arrays.copyOf(flyings, flyings.length - 1); // ɾϵķ
}
}
return hero.getLife() <= 0;
}
/** ӵͷ֮ײ */
public void bang(Bullet bullet) {
int index = -1; // еķ
for (int i = 0; i < flyings.length; i++) {
FlyingObject obj = flyings[i];
if (obj.shootBy(bullet)) { // жǷ
index = i; // ¼еķ
break;
}
}
if (index != -1) { // леķ
FlyingObject one = flyings[index]; // ¼еķ
FlyingObject temp = flyings[index]; // еķһサ
flyings[index] = flyings[flyings.length - 1];
flyings[flyings.length - 1] = temp;
flyings = Arrays.copyOf(flyings, flyings.length - 1); // ɾһ(е)
// one(˼ӷ֣ȡ)
if (one instanceof Enemy) { // ͣǵˣӷ
Enemy e = (Enemy) one; // ǿת
score += e.getScore(); // ӷ
} else { // Ϊý
Award a = (Award) one;
int type = a.getType(); // ȡ
switch (type) {
case Award.DOUBLE_FIRE:
hero.addDoubleFire(); // ˫
break;
case Award.LIFE:
hero.addLife(); // ü
break;
}
}
}
}
/**
* ɷ
*
* @return
*/
public static FlyingObject nextOne() {
Random random = new Random();
int type = random.nextInt(20); // [0,20)
if (type < 4) {
return new Bee();
} else {
return new Airplane();
}
}
}
gitextract_6vaf1i7i/
├── .classpath
├── .project
├── .settings/
│ └── org.eclipse.jdt.core.prefs
├── README.md
└── src/
└── com/
└── tarena/
└── fly/
├── Airplane.java
├── Award.java
├── Bee.java
├── Bullet.java
├── Enemy.java
├── FlyingObject.java
├── Hero.java
└── ShootGame.java
SYMBOL INDEX (64 symbols across 8 files)
FILE: src/com/tarena/fly/Airplane.java
class Airplane (line 8) | public class Airplane extends FlyingObject implements Enemy {
method Airplane (line 12) | public Airplane(){
method getScore (line 22) | @Override
method outOfBounds (line 28) | @Override
method step (line 34) | @Override
FILE: src/com/tarena/fly/Award.java
type Award (line 5) | public interface Award {
method getType (line 9) | int getType();
FILE: src/com/tarena/fly/Bee.java
class Bee (line 6) | public class Bee extends FlyingObject implements Award{
method Bee (line 12) | public Bee(){
method getType (line 23) | public int getType(){
method outOfBounds (line 28) | @Override
method step (line 34) | @Override
FILE: src/com/tarena/fly/Bullet.java
class Bullet (line 6) | public class Bullet extends FlyingObject {
method Bullet (line 10) | public Bullet(int x,int y){
method step (line 17) | @Override
method outOfBounds (line 23) | @Override
FILE: src/com/tarena/fly/Enemy.java
type Enemy (line 6) | public interface Enemy {
method getScore (line 8) | int getScore();
FILE: src/com/tarena/fly/FlyingObject.java
class FlyingObject (line 8) | public abstract class FlyingObject {
method getX (line 15) | public int getX() {
method setX (line 19) | public void setX(int x) {
method getY (line 23) | public int getY() {
method setY (line 27) | public void setY(int y) {
method getWidth (line 31) | public int getWidth() {
method setWidth (line 35) | public void setWidth(int width) {
method getHeight (line 39) | public int getHeight() {
method setHeight (line 43) | public void setHeight(int height) {
method getImage (line 47) | public BufferedImage getImage() {
method setImage (line 51) | public void setImage(BufferedImage image) {
method outOfBounds (line 59) | public abstract boolean outOfBounds();
method step (line 64) | public abstract void step();
method shootBy (line 71) | public boolean shootBy(Bullet bullet){
FILE: src/com/tarena/fly/Hero.java
class Hero (line 7) | public class Hero extends FlyingObject{
method Hero (line 16) | public Hero(){
method isDoubleFire (line 28) | public int isDoubleFire() {
method setDoubleFire (line 33) | public void setDoubleFire(int doubleFire) {
method addDoubleFire (line 38) | public void addDoubleFire(){
method addLife (line 43) | public void addLife(){ //
method subtractLife (line 48) | public void subtractLife(){ //
method getLife (line 53) | public int getLife(){
method moveTo (line 58) | public void moveTo(int x,int y){
method outOfBounds (line 64) | @Override
method shoot (line 70) | public Bullet[] shoot(){
method step (line 86) | @Override
method hit (line 94) | public boolean hit(FlyingObject other){
FILE: src/com/tarena/fly/ShootGame.java
class ShootGame (line 19) | public class ShootGame extends JPanel {
method paint (line 67) | @Override
method paintHero (line 78) | public void paintHero(Graphics g) {
method paintBullets (line 83) | public void paintBullets(Graphics g) {
method paintFlyingObjects (line 92) | public void paintFlyingObjects(Graphics g) {
method paintScore (line 100) | public void paintScore(Graphics g) {
method paintState (line 112) | public void paintState(Graphics g) {
method main (line 126) | public static void main(String[] args) {
method action (line 141) | public void action() {
method enterAction (line 207) | public void enterAction() {
method stepAction (line 217) | public void stepAction() {
method flyingStepAction (line 231) | public void flyingStepAction() {
method shootAction (line 241) | public void shootAction() {
method bangAction (line 252) | public void bangAction() {
method outOfBoundsAction (line 260) | public void outOfBoundsAction() {
method checkGameOverAction (line 283) | public void checkGameOverAction() {
method isGameOver (line 290) | public boolean isGameOver() {
method bang (line 313) | public void bang(Bullet bullet) {
method nextOne (line 355) | public static FlyingObject nextOne() {
Condensed preview — 12 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (49K chars).
[
{
"path": ".classpath",
"chars": 295,
"preview": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<classpath>\n\t<classpathentry kind=\"src\" path=\"src\"/>\n\t<classpathentry kind=\"con\" "
},
{
"path": ".project",
"chars": 364,
"preview": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<projectDescription>\n\t<name>Shoot</name>\n\t<comment></comment>\n\t<projects>\n\t</proj"
},
{
"path": ".settings/org.eclipse.jdt.core.prefs",
"chars": 617,
"preview": "#Mon Jul 21 11:48:12 CST 2014\neclipse.preferences.version=1\norg.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enab"
},
{
"path": "README.md",
"chars": 19769,
"preview": "\n\n## 写在前面\n\n技术源于分享,所以今天抽空把自己之前用java做过的小游戏整理贴"
},
{
"path": "src/com/tarena/fly/Airplane.java",
"chars": 616,
"preview": "package com.tarena.fly;\n\nimport java.util.Random;\n\n/**\n * зɻ: ǷҲǵ\n */\npublic class Airplane extends FlyingObject implem"
},
{
"path": "src/com/tarena/fly/Award.java",
"chars": 141,
"preview": "package com.tarena.fly;\n/**\n * \n */\npublic interface Award {\n\tint DOUBLE_FIRE = 0; //˫\n\tint LIFE = 1; //1\n\t/** ý(01) "
},
{
"path": "src/com/tarena/fly/Bee.java",
"chars": 776,
"preview": "package com.tarena.fly;\n\nimport java.util.Random;\n\n/** ۷ */\npublic class Bee extends FlyingObject implements Award{\n\tpri"
},
{
"path": "src/com/tarena/fly/Bullet.java",
"chars": 360,
"preview": "package com.tarena.fly;\n\n/**\n * ӵ:Ƿ\n */\npublic class Bullet extends FlyingObject {\n\tprivate int speed = 3; //ƶٶ\n\t\n\t/** "
},
{
"path": "src/com/tarena/fly/Enemy.java",
"chars": 95,
"preview": "package com.tarena.fly;\n\n/**\n * ˣз\n */\npublic interface Enemy {\n\t/** ˵ķ */\n\tint getScore();\n}\n"
},
{
"path": "src/com/tarena/fly/FlyingObject.java",
"chars": 1132,
"preview": "package com.tarena.fly;\n\nimport java.awt.image.BufferedImage;\n\n/**\n * (л۷䣬ӵӢۻ)\n */\npublic abstract class FlyingObject {\n"
},
{
"path": "src/com/tarena/fly/Hero.java",
"chars": 2140,
"preview": "package com.tarena.fly;\nimport java.awt.image.BufferedImage;\n\n/**\n * Ӣۻ:Ƿ\n */\npublic class Hero extends FlyingObject{\n\t\n"
},
{
"path": "src/com/tarena/fly/ShootGame.java",
"chars": 8669,
"preview": "package com.tarena.fly;\n\nimport java.awt.Font;\nimport java.awt.Color;\nimport java.awt.Graphics;\nimport java.awt.event.Mo"
}
]
About this extraction
This page contains the full source code of the wblearn/Small-plane-game GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 12 files (34.2 KB), approximately 14.0k tokens, and a symbol index with 64 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.