package day01;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class World extends JPanel {
Sky sky = new Sky();
Hero hero = new Hero();
Bullet [] b=new Bullet[0];
FlyingObject enemies[] = new FlyingObject[0];
//随机生成敌人对象
public FlyingObject nextOne(){
int r = new Random().nextInt(21);
if(r<7){
return new BigAirplane();
}else if(r<14){
return new Airplane();
}else if(r<18){
return new Bee();
}else{
return new Bee1();
}
}
//敌人入场
int enterCount = 0;
public void enterAction(){
//敌人的初始化
enemies = Arrays.copyOf(enemies, enemies.length+1);
enemies[enemies.length-1]=nextOne();
}
//子弹入场
public void shootAction(){
Bullet b1[] = hero.shoot();
b = Arrays.copyOf(b, b1.length+b.length);
System.arraycopy(b1, 0, b, b.length-b1.length, b1.length);
}
public void stepAction(){
sky.step();
for(int i = 0; i<enemies.length; i++){
enemies[i].step();
}
for(int i = 0; i<b.length; i++){
b[i].step();
}
}
public void outOfBoundsActions(){
int index = 0;
FlyingObject[] fos = new FlyingObject[enemies.length];
for(int i=0; i<enemies.length; i++){
FlyingObject fo = enemies[i];
if(!fo.outOfBounds() && !fo.isRemove()){
fos[index] = fo;
index++;
}
}
enemies = Arrays.copyOf(fos, index);
index = 0;
Bullet[] bs = new Bullet[b.length];
for(int i = 0; i<b.length; i++){
Bullet bullet = b[i];
if(!bullet.outOfBounds() && !bullet.isRemove()){
bs[index] = bullet;
index++;
}
}
b = Arrays.copyOf(bs, index);
}
//子弹和敌人发生碰撞
int score = 0;
public void bulletHit(){
for(Bullet bullet : b){
for(FlyingObject enemy : enemies){
if(bullet.isLife() && enemy.isLife() && enemy.hit(bullet)){
bullet.goDead();//子弹死
enemy.goDead();//敌人死
//如果敌人是Enemies类型:加分
if(enemy instanceof Enemy){
score += ((Enemy) enemy).setScroe();
}
//如果敌人是Award类型:
//如果是小蜜蜂:0:调用addLife();
//如果是小蜜蜂:1:调用addDoubleFire();
if(enemy instanceof Award){
if(((Award) enemy).awardType() == 1){
hero.doubleFire += 40;
}else{
hero.life += 1;
}
}
}
}
}
}
//英雄机碰撞
public void heroHit(){
//敌人死
//英雄命-1
//火力值为0
for(FlyingObject obj : enemies){
if(hero.isLife() && obj.isLife() && obj.hit(hero)){
obj.goDead();
hero.subLIfe();
hero.clearDoubleFire();
}
}
}
//定义常量值
public static final int START = 0;
public static final int RUNNING = 1;
public static final int PAUSE = 2;
public static final int GAMEOVER = 3;
//定义变量保存图片
static BufferedImage start;
static BufferedImage pause;
static BufferedImage gameover;
int state = START;
static{
start = FlyingObject.getload("start.png");
pause = FlyingObject.getload("pause.png");
gameover = FlyingObject.getload("gameover.png");
}
public void stateSwitch(){
}
//游戏结束
public void gemeOver(){
if(hero.getLife() <= 0){
state = GAMEOVER;
}
}
public void action(){
MouseAdapter md = new MouseAdapter(){
public void mouseMoved(MouseEvent e){
if(state == RUNNING){
//获取下x,y点坐标
int x = e.getX();
int y = e.getY();
hero.moveTo(x, y);
}
}
public void mouseClicked(MouseEvent e){
switch(state){
case START:
state = RUNNING;
break;
case GAMEOVER:
score = 0;
hero = new Hero();
sky = new Sky();
enemies = new FlyingObject[0];
b = new Bullet[0];
state = START;
break;
}
}
//判断如果是running状态,把state改成暂停状态
public void mouseEntered(MouseEvent e){
if(state == PAUSE){
state = RUNNING;
}
}
public void mouseExited(MouseEvent e){
if(state == RUNNING){
state = PAUSE;
}
}
};
this.addMouseMotionListener(md);
this.addMouseListener(md);
//定时器
Timer timer = new Timer();
timer.schedule(new TimerTask(){
public void run() {
if(state == RUNNING){
//敌人的初始化
enterCount++;
if (score == 100){
if(enterCount%1 == 0){
enterAction();
}
}else{
if(enterCount%30 == 0){
enterAction();
}
}
//子弹的初始化
if(enterCount%20 == 0){
shootAction();
}
stepAction();
outOfBoundsActions();
bulletHit();
heroHit();
gemeOver();
}
repaint();//通知窗口Frame调用paint方法
}
}, 10, 10);
}
public void paint(Graphics g){
sky.paintObject(g);
hero.paintObject(g);
for(int i = 0; i<b.length; i++){
b[i].paintObject(g);
}
for(int i = 0; i<enemies.length; i++){
if(enemies[i] != null){
enemies[i].paintObject(g);
}
}
g.drawString("得分" + score, 10, 15);
g.drawString("生命" + hero.getLife(), 10, 35);
g.drawString("火力" + hero.doubleFire, 10, 55);
switch (state){
case START:
g.drawImage(start, 0, 0, null);
break;
case PAUSE:
g.drawImage(pause, 0, 0, null);
break;
case GAMEOVER:
g.drawImage(gameover, 0, 0, null);
g.drawString("本次得分: " + score, FlyingObject.WIDTH/2-7, FlyingObject.HEIGHT/2-10);
break;
}
}
public static void main(String args[]) {
//实现窗口
JFrame frame = new JFrame();
World world = new World();
frame.add(world);
frame.setTitle("飞机大战");
frame.setSize(FlyingObject.WIDTH, FlyingObject.HEIGHT);
//窗口在屏幕中央
frame.setLocationRelativeTo(null);
//关闭按钮
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
world.action();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
500行代码做出飞机大战
共121个文件
png:89个
class:13个
java:11个
需积分: 50 20 下载量 32 浏览量
2018-04-17
16:19:30
上传
评论 1
收藏 852KB RAR 举报
温馨提示
500行代码实现飞机大战,基本功能都包括。代码里表现出来的类与对象和数据结构清晰易懂,适合初学者
资源推荐
资源详情
资源评论
收起资源包目录
500行代码做出飞机大战 (121个子文件)
World.class 6KB
Hero.class 2KB
FlyingObject.class 2KB
Bee1.class 2KB
Bee.class 2KB
BigAirplane.class 2KB
World$1.class 1KB
Airplane.class 1KB
Sky.class 1KB
Bullet.class 1KB
World$2.class 935B
Award.class 206B
Enemy.class 122B
.classpath 1KB
World.java 6KB
FlyingObject.java 2KB
Hero.java 2KB
Bee1.java 944B
Bee.java 941B
BigAirplane.java 877B
Airplane.java 751B
Sky.java 697B
Bullet.java 625B
Award.java 112B
Enemy.java 72B
MANIFEST.MF 115B
start.png 42KB
start.png 42KB
start.png 42KB
background.png 26KB
background.png 26KB
background.png 26KB
hero4.png 20KB
hero4.png 20KB
hero4.png 20KB
gameover.png 20KB
gameover.png 20KB
gameover.png 20KB
hero3.png 18KB
hero3.png 18KB
hero3.png 18KB
7.png 17KB
hero2.png 17KB
hero2.png 17KB
hero2.png 17KB
9.png 16KB
10.png 16KB
8.png 16KB
hero1.png 16KB
hero1.png 16KB
hero1.png 16KB
pause.png 15KB
pause.png 15KB
pause.png 15KB
hero0.png 12KB
hero0.png 12KB
hero0.png 12KB
bigplane3.png 10KB
bigplane3.png 10KB
bigplane3.png 10KB
bigplane2.png 8KB
bigplane2.png 8KB
bigplane2.png 8KB
bigplane1.png 8KB
bigplane1.png 8KB
bigplane1.png 8KB
bigplane0.png 7KB
bigplane0.png 7KB
bigplane0.png 7KB
bee2.png 6KB
bee2.png 6KB
bee2.png 6KB
bee1.png 6KB
bee1.png 6KB
bee1.png 6KB
bee0.png 6KB
bee0.png 6KB
bee0.png 6KB
bee3.png 6KB
bee3.png 6KB
bee3.png 6KB
bee4.png 6KB
bee4.png 6KB
bee4.png 6KB
hero5.png 6KB
hero5.png 6KB
hero5.png 6KB
airplane3.png 4KB
airplane3.png 4KB
airplane3.png 4KB
airplane0.png 3KB
airplane0.png 3KB
airplane0.png 3KB
1.png 3KB
3.png 3KB
5.png 3KB
2.png 3KB
6.png 3KB
4.png 3KB
0.png 3KB
共 121 条
- 1
- 2
资源评论
innocence___
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功