package com.tarena.shoot;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
//import java.awt.event.MouseListener;
//import java.awt.event.MouseMotionListener;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.image.BufferedImage;
import java.lang.reflect.Array;
import java.util.Random;
import javax.imageio.ImageIO; //图片的输入和输出
import javax.swing.JFrame;
import javax.swing.JPanel;//带main的类
import java.util.Arrays;
import java.awt.color.*;
public class ShootGame extends JPanel implements Runnable {
public static final int WIDTH = 400;
public static final int HIGHT = 654;
public static BufferedImage background;
public static BufferedImage start;
public static BufferedImage pause;
public static BufferedImage gameover;
public static BufferedImage airplane;
public static BufferedImage bee;
public static BufferedImage bullet;
public static BufferedImage hero_0;
public static BufferedImage hero_1;
public static BufferedImage boss;
public static BufferedImage win;
public static final int START = 0;
public static final int RUNNING = 1;
public static final int PAUSE = 2;
public static final int GAME_OVER = 3;
public static final int WIN = 4;
private int state = START; //当前状态
public static int score = 0;
public static boolean cc = true;
// public static boolean ph;
private Hero hero = new Hero();
private Boss bos = new Boss();
private FlyingObject[] flyings = {};
private Bullet[] bullets = {};
private ZiDan[] zds = {};
int bg_x, bg_y;
ShootGame() {
// ph = true;
bg_x = 0;
bg_y = -553;// 553
}
static {// 加载静态资源
try {
background = ImageIO.read(ShootGame.class.getResource("background.png"));
start = ImageIO.read(ShootGame.class.getResource("start.png"));
pause = ImageIO.read(ShootGame.class.getResource("pause.png"));
gameover = ImageIO.read(ShootGame.class.getResource("gameover.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"));
hero_0 = ImageIO.read(ShootGame.class.getResource("hero0.png"));
hero_1 = ImageIO.read(ShootGame.class.getResource("hero1.png"));
boss = ImageIO.read(ShootGame.class.getResource("boss.png"));
win = ImageIO.read(ShootGame.class.getResource("win.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
public FlyingObject nextOne() { // 生成小蜜蜂或者敌机
if (score>=100) {
cc = false;
}
Random rand = new Random();
int type = rand.nextInt(20);
if (type == 0) {
return new Bee();
} else {
return new Airplane();
}
}
int flyEnteredIndex = 0;
// 敌人入场
public void enterAction() {
flyEnteredIndex++;
if (flyEnteredIndex % 40 == 0) { // 控制敌人出现的频率
FlyingObject obj = nextOne();// 从nextOne方法里面获得一个类型
flyings = Arrays.copyOf(flyings, flyings.length + 1);// 扩容flyings数组
flyings[flyings.length - 1] = obj;
}
}
public void stepAction(){ //十毫秒 走一次(调用一次)
hero.step();
if (!cc) {
bos.step();
}
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(); //调用哪一个重写方法,看指向哪一个子类;
}
for (int i = 0; i < zds.length; i++) {
ZiDan zd = zds[i];
zd.step();
}
}
int shootindex = 0;
public void shootAction(){
shootindex++;
if (shootindex%10 == 0) {
Bullet[] bs = hero.shoot();
bullets = Arrays.copyOf(bullets, bullets.length+bs.length);
System.arraycopy(bs, 0, bullets, bullets.length-bs.length, bs.length);
//以上两行代码相当于代码的接龙,把后一数组接到原数组之后
//称为数组的追加
// if(bs.length>1){
// bullets[bullets.length-2] = bs[0];
// bullets[bullets.length-1] = bs[1];
//
// }else {
// bullets[bullets.length-1] = bs[0];
// }
}
}
public void outOfBoundsAction() { //删除越界的敌机和子弹
FlyingObject[] flyinglives = new FlyingObject[flyings.length];
int index = 0;
for (int i = 0; i < flyings.length; i++) {
FlyingObject f = flyings[i];
if (! f.outOfBounds()) {
flyinglives[index] = f;
index ++;
}
}
flyings = Arrays.copyOf(flyinglives, index);//以上为数组的 缩容
index = 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;
index++;
}
}
bullets = Arrays.copyOf(bulletlives, index);
index = 0; //用相同的方法删除子弹的越界
ZiDan[] zdlives = new ZiDan[zds.length];
for (int i = 0; i < zds.length; i++) {
ZiDan zd = zds[i];
if (! zd.outOfBounds()) {
zdlives[index] = zd;
index++;
}
}
zds = Arrays.copyOf(zdlives, index);
}
// public void accidentAction(){
// for (int i = 0; i < bullets.length; i++) {
// for (int j = 0; j < flyings.length; j++) {
// FlyingObject f = flyings[j];
// Bullet b = bullets[i];
// if (f.accident(b.x, b.y)) {
// flyings[j].y = 1000;
// bullets[i].y = -1000;
// }
// }
// }
// }
public void bangAction(){ //获取所有的子弹
for (int i = 0; i < bullets.length; i++) {
Bullet b = bullets[i];
bang(b);
}
}
public void bang(Bullet bullet){ //提取一个子弹
int index = -1;//用于获取被撞敌人的下标
int index2 = -1;
for (int i = 0; i < flyings.length; i++) {
FlyingObject f = flyings[i];
if (bos.shootBy(bullet) == 2) {
bullet.y = -1000;
}
if (bos.shootBy(bullet) == 0) {
cc = true;
}
if (f.shootBy(bullet) == 1) { //如果撞上了
index = i;
bullet.y = -1000;
break;
}
}
for (int i = 0; i < zds.length; i++) {
ZiDan zd = zds[i];
if (zd.shootBy(bullet)==1) {
index2 = i;
zd.y = -1000;
bullet.y = -1000;
}
}
if (index != -1) {
FlyingObject one = flyings[index];
if (one instanceof Enemy) {
Enemy e = (Enemy) one;
score += e.getScore();
}
if (one instanceof Award) {
Award a = (Award) one;
int type = a.getType();
switch (type) {
case 0:
hero.adddoubleFire();
break;
case 1:
hero.addlife();
break;
}
}
FlyingObject t = flyings[index];
flyings[index] = flyings[flyings.length-1];
flyings[flyings.length-1] = t;
flyings = Arrays.copyOf(flyings, flyings.length-1);
}
if (index2 != -1) {
ZiDan one = zds[index2];
score += one.getScore();
}
}
public void checkGameOverAction(){
if (isGameOver()) {
state = GAME_OVER;
}
}
public boolean isGameOver(){ /*is开头的方法表示判断*/
for (int i = 0; i < flyings.length; i++) {
FlyingObject f = flyings[i];
if (hero.hit(f)) {
hero.reducelife();
hero.cleandoubleFire();
FlyingObject t = flyings[i];
flyings[i] = flyings[flyings.length-1];
flyings[flyings.length-1] = t;
flyings = Arrays.copyOf(flyings, flyings.length-1);
}
}
for (int i = 0; i < zds.length; i++) {
ZiDan f = zds[i];
if (hero.hit(f)) {
hero.reducelife();
hero.cleandoubleFire();
f.y = -1000;
}
}
return hero.getLive()<=0;
}
int zidanindex = 0;
public void zidanAction() {
// TODO Auto-generated method stub
zidanindex++;
if (zidanindex%30 == 0) {
ZiDan zd = bos.shoot();
zds = Arrays.copyOf(zds, zds.length+1);
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Myshoot.zip (64个子文件)
Myshoot
bgm
bg.ogg 1.04MB
fishbg2.mp3 1.38MB
hundredpoints.mp3 113KB
bgm_fire.ogg 8KB
coinsnone.ogg 24KB
firechange.ogg 10KB
highpoints.ogg 34KB
coinanimate.ogg 29KB
bgm_net.ogg 9KB
.project 383B
src
com
tarena
shoot
Enemy.java 83B
bee.png 6KB
airplane.png 3KB
ZiDan.java 1KB
bullet.png 408B
fishbg2.mp3 1.38MB
Airplane.java 872B
Bullet.java 630B
Hero.java 2KB
ShootGame.java 13KB
fishbg2.wav 1.38MB
Boss.java 1KB
FlyingObject.java 770B
start.png 45KB
Bee.java 1KB
background.png 593KB
Music.java 682B
Award.java 209B
hero1.png 16KB
background1.png 26KB
win.png 25KB
hero0.png 12KB
pause.png 15KB
gameover.png 20KB
boss.png 54KB
.settings
org.eclipse.jdt.core.prefs 598B
.classpath 301B
bin
com
tarena
shoot
FlyingObject.class 937B
bee.png 6KB
ShootGame.class 11KB
Airplane.class 1KB
airplane.png 3KB
bullet.png 408B
fishbg2.mp3 1.38MB
fishbg2.wav 1.38MB
Boss.class 2KB
ShootGame$2.class 976B
Bullet.class 965B
start.png 45KB
background.png 593KB
Music.class 1KB
ShootGame$1.class 2KB
Bee.class 1KB
Award.class 215B
hero1.png 16KB
background1.png 26KB
ZiDan.class 2KB
win.png 25KB
Hero.class 3KB
hero0.png 12KB
pause.png 15KB
gameover.png 20KB
boss.png 54KB
Enemy.class 133B
共 64 条
- 1
资源评论
- 谜6362023-02-10用的idea,连运行都运行不了 #运行出错
怪力乌龟
- 粉丝: 155
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功