package com.tarena.shout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.tarena.shout.ShootGame.KeyMonitor;
import com.tarena.shout.ShootGame.PaintTread;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Arrays;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
public class ShootGame extends Frame{
public static final int WIDTH=590;
public static final int HEIGHT=764;
public static int yPos=-646;
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 hero0;
public static BufferedImage hero1;
public static BufferedImage bigBee;
public static final int START=0;//启动
public static final int RUNNING=1;//运行
public static final int PAUSE=2;//暂停
public static final int GAMEOVER=3;
private int state=START;
public static PlaySound pbg = new PlaySound("bgmusic.mp3", true);
private Hero hero=new Hero();
private FlyingObject[] flyings={};
private Bullet[] bullets={};
static{
try {
background=ImageIO.read(ShootGame.class.getResource("background5.bmp"));
start=ImageIO.read(ShootGame.class.getResource("startbg1.jpg"));
pause=ImageIO.read(ShootGame.class.getResource("game_pause_pressed.png"));
gameover=ImageIO.read(ShootGame.class.getResource("loser.png"));
airplane=ImageIO.read(ShootGame.class.getResource("enemy17.png"));
bee=ImageIO.read(ShootGame.class.getResource("enemy7.png"));
bullet=ImageIO.read(ShootGame.class.getResource("m5.png"));
hero0=ImageIO.read(ShootGame.class.getResource("plane11.png"));
hero1=ImageIO.read(ShootGame.class.getResource("plane23.png"));
bigBee=ImageIO.read(ShootGame.class.getResource("enemy21.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
//画图
public void paint(Graphics g){
g.drawImage(background,0,0,null);
g.drawImage(background, 0, yPos, null);
g.drawImage(background, 0, yPos-1411, null);
paintHero(g);
paintFlyingObjects(g);
paintBullets(g);
paintScroreAndLife(g);
patintState(g);
}
//分 命
public void paintScroreAndLife(Graphics g){
g.setColor(new Color(0xFF0000));//设置颜色
g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,30));
g.drawString("SCORE:"+score,10,50);//画分
g.drawString("LIFE:"+hero.getLife(),10,75);
g.drawString("DoubleFire:"+hero.getDoubleFire(),10,100);
}
//自己战机
public void paintHero(Graphics g){
g.drawImage(hero.image,hero.x,hero.y,null);
}
static class BgThread extends Thread{
@Override
public void run() {
while(true){
if(yPos==764){
yPos=-646;
}else{
yPos+=2;
}
try {
Thread.sleep(40);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//敌机
public void paintFlyingObjects(Graphics g){
for(int i=0;i<flyings.length;i++){
FlyingObject f=flyings[i];
g.drawImage(f.image, f.x, f.y, null);
}
}
//子弹
public void paintBullets(Graphics g){
for(int i=0;i<bullets.length;i++){
Bullet b=bullets[i];
g.drawImage(b.image,b.x,b.y,null);
}
}
//状态
public void patintState(Graphics g){
switch(state){
case START:
g.drawImage(start, 0, 0, null);
break;
case PAUSE:
g.drawImage(pause, 270, 300, null);
break;
case GAMEOVER:
g.drawImage(gameover, 0, 300, null);
}
}
//生成敌人
public FlyingObject nextOne(){
Random rand=new Random();//建立对象
int type=rand.nextInt(20);//生成0-20之间的随机数
if(type<4){
return new BigBee();
}else if (type==5) {
return new Bee();
}else{
return new Airplane();
}
}
int flyEnteredIndex=0;
public void enterAction(){//10毫秒走一次
//创建敌人对象
//将敌人对象添加到FlyingObject中
flyEnteredIndex++;
if(flyEnteredIndex%40==0){
FlyingObject obj=nextOne();//获取敌人对象
flyings=Arrays.copyOf(flyings, flyings.length+1);
flyings[flyings.length-1]=obj;
}
}
//子弹入场
int shootIndex=0;
public void shootAxtion(){
//获取子弹对象
//添加到bullets数组中
shootIndex++;
if (shootIndex%30==0) {
Bullet[] bs=hero.shoot();
bullets=Arrays.copyOf(bullets, bullets.length+bs.length);
System.arraycopy(bs, 0, bullets, bullets.length-bs.length, bs.length);//bs追加
}
}
//飞行物走一步
public void stepAction(){
hero.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();
}
}
//删除越界的敌人和子弹
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;
index++;
}
}
flyings=Arrays.copyOf(flyinglives, index);//将不越界的值赋给flyings
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);
}
//子弹与敌人相撞
//所有子弹和所有敌人撞
int j=0;
public void bangAction(){
for(int i=0;i<bullets.length;i++){
bang(bullets[i],i);
}
}
//一个子弹与所有的敌人撞
int score=0;
public void bang(Bullet b,int bu){
int index=-1;
for(int i=0;i<flyings.length;i++){
FlyingObject f=flyings[i];
if (f.shootBy(b)) {//撞上了
index=i;
break;//其余的敌人不比较
}
}
if(index!=-1){
FlyingObject one=flyings[index];
if(one instanceof Airplane){//是敌人
Enemy e=(Enemy)one;
score+=e.getScore();
FlyingObject t=flyings[index];
flyings[index]=flyings[flyings.length-1];
flyings[flyings.length-1]=t;
flyings=Arrays.copyOf(flyings, flyings.length-1);//去掉被子弹撞上的敌人
}
if(one instanceof BigBee){
j++;
if(j==5){
Enemy e=(Enemy)one;
score+=e.getScore();
j=0;
FlyingObject t=flyings[index];
flyings[index]=flyings[flyings.length-1];
flyings[flyings.length-1]=t;
flyings=Arrays.copyOf(flyings, flyings.length-1);//去掉被子弹撞上的敌人
}
}
if(one instanceof Award){//奖励
Award a=(Award)one;
int type=a.getType();
switch(type){
case Award.DOUBLE_FIRE://奖励火力值
hero.addDoubleFire();
break;
case Award.LIFE://奖励生命
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);//去掉被子弹装上的敌人
}
Bullet tBullet=bullets[bu];
bullets[bu]=bullets[bullets.length-1];
bullets[bullets.length-1]=tBullet;
bullets=Arrays.copyOf(bullets, bullets.length-1);//去掉子弹
}
}
//敌人和英雄机相撞
public void checkGameOverAction(){
if(isGameOver()){
state=GAMEOVER;
new PlaySound("baozou.mp3").start();
}
}
//判断游戏是否结束
public boolean isGameOver(){
for(int i=0;i<flyings.length;i++){
FlyingObject f=flyings[i];
if(hero.hit(f)){
hero.subtractLife();
hero.clealDoubleFire();
FlyingObject t=flyings[i];//将被撞敌人与最后的一个元素交换
flyings[i]=flyings[flyings.length-1];
flyings[flyings.length-1]=t;
flyings=Arrays.copyOf(flyings, flyings.length-1);//去掉被子弹撞上的敌人
}
}
return hero.getLife()<=0;//命数小于等于0结束
}
//启动程序的执行
public void action(){
MouseAdapter l=new MouseAdapter() {
//重写鼠标移动事件
public void mouseMoved(MouseEvent e){
if(state==RUNNING){
int x=e.getX();//获取鼠标的x坐标
int y=e.getY();
hero.moveTo(x, y);
}
}
//鼠标点击事件
public void mouseClicked(MouseEvent e){
switch(state){
case START:
state=RUNNING;
break;
case GAMEOVER:
state=START;
//清理现场
score=0;
hero=new Hero();
flyings=new FlyingObject[0];
bullets=new Bullet[0];
break;
}
}
//鼠标移除
public void mouseExited(MouseEvent e){
if(state==RUNNING){
state=PAUSE;
}
}
//鼠标移入
public void mouseEntered(MouseEvent e){
if(state==PAUSE){
state=RUNNING;
}
}
};
this.addMouseListener(l);//处理鼠标操作事件
this.addMouseMotionListener(l);//鼠标的滑动
Timer timer=new Timer();//定时器
int intervel=10;//定时间隔
timer.schedule(new TimerTask() {
public void run(){
if(state==RUNNING){
enterAction();//敌人入场
stepAction();//敌人移动
shootAxtion();//子弹入场
bangAction();//子弹与敌人相撞
outOfBoundsAction();//删除越界的敌人和子弹
checkGameOverAction();//敌机和英雄机相撞
}
repaint();//重画
}
},intervel,intervel);//第一个要求timetask类型,第二个开始到第一次触发的间隔,第三个是第二次到第三次的 触发
}class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
hero.addDirection(e);
}
}
public void launchFrame() {//加载窗体
this.setTitle("雷霆战机");
setSize(WIDTH,HEIGHT);//设置大小
setLocation(400,0);//设置位置
setVisible(true);//设置窗体可见
setAlwaysOnTop(true);
setLocationRelativeTo(null);//窗体居中
setResizable(false);//大小不可以改变
new PaintTread().start();//启动重画线程
this.action();
addKeyListener(new KeyMonitor());
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);//关闭窗口
}
});
}
//使图片动起来,定义一个重画窗口的线程类
class PaintTread extends Thread{
public void run() {
while(true){
repaint();
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}//使CPU歇会
}
}
}
private Image offScreenImage = null;//利用双缓冲技术消除屏幕闪烁
public void update(Graphics g){
if(offScreenImage == null){
offScreenImage = this.createImage(WIDTH,HEIGHT);
}
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
public static void main(String[] args) {
/* JFrame frame=new JFrame("雷霆战机");
ShootGame game=new ShootGame();
frame.add(game);//代表把面板添加到窗口
frame.setSize(WIDTH,HEIGHT);
frame.setAlwaysOnTop(true);//设置窗体一直居上
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置默认关闭操作,窗体关闭程序结束
frame.setLocationRelativeTo(null);//窗体居中
frame.setVisible(true);//设置窗体可见*/
ShootGame sg=new ShootGame();
sg.launchFrame();
pbg.start();
new BgThread().start();
}
}
package com.tarena.shout;
import java.awt.image.BufferedImage;
public abstract class FlyingObject {
protected BufferedImage image;
protected int width;
protected int heighet;
protected int x;
protected int y;
public abstract void step();
public abstract boolean outOfBounds();
public boolean shootBy(Bullet bullet){
int x1=this.x;
int x2=this.x+this.width;
int y1=this.y;
int y2=this.y+this.heighet;
int x=bullet.x;
int y=bullet.y;
return x>x1&&x<x2&&y>y1&&y<y2;
}
}
package com.tarena.shout;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
public class Hero extends FlyingObject{
private int life;
private int doubleFire;
private int speed=8;
private BufferedImage[] images;
private int index;
public Hero(){
image=ShootGame.hero0;
width=image.getWidth();//鑾峰彇鍥剧墖鐨勫
heighet=image.getHeight();
x=150;
y=400;
life=3;
doubleFire=0;
images=new BufferedImage[]{ShootGame.hero0,ShootGame.hero1};
index=0;
}
@Override
public void step() {
image=images[index++/10%images.length];
}
public Bullet[] shoot(){
int xStep=this.width/4;
int yStep=20;
if (doubleFire>=100) {
Bullet[] bs=new Bullet[3];
bs[0]=new Bullet(this.x+1*xStep, this.y-yStep);
bs[1]=new Bullet(this.x+2*xStep, this.y-yStep);
bs[2]=new Bullet(this.x+3*xStep, this.y-yStep);
doubleFire-=2;
return bs;
} else if (doubleFire<100&&doubleFire>0) {
Bullet[] bs=new Bullet[2];
bs[0]=new Bullet(this.x+1*xStep, this.y-yStep);
bs[1]=new Bullet(this.x+3*xStep, this.y-yStep);
doubleFire-=2;
return bs;
} else{
Bullet[] bs=new Bullet[1];
bs[0]=new Bullet(this.x+2*xStep, this.y-yStep);
return bs;
}
}
//鑻遍泟绾ф満闅忛紶鏍囩Щ鍔�
public void moveTo(int x,int y){
this.x=x-this.width/2;
this.y=y-this.heighet/2;
}
@Override
public boolean outOfBounds() {
return false;
}
//澧炲懡
public void addLife(){
life+=2;
}
public int getLife(){
return life;
}
public void addDoubleFire(){
doubleFire+=20;
}
public int getDoubleFire(){
return doubleFire;
}
//other锛� 鏁屼汉
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.heighet/2;
int y2=other.y+this.heighet/2+other.heighet;
int x=this.x+this.width/2;
int y=this.y+this.heighet;
return x>x1&&x<x2&&y>y1&&y<y2;
}
//鍑忓懡
public void subtractLife(){
life--;
}
public void clealDoubleFire(){
doubleFire=0;
}
public void addDirection(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_LEFT:
this.x-=speed;
break;
case KeyEvent.VK_RIGHT:
this.x+=speed;
break;
case KeyEvent.VK_UP:
this.y-=speed;
break;
case KeyEvent.VK_DOWN:
this.y+=speed;
break;
}
}
}
package com.tarena.shout;
public class Bullet extends FlyingObject{
private int speed=3;
public Bullet(int x,int y){
image=ShootGame.bullet;
width=image.getWidth();
heighet=image.getHeight();
this.x=x;
this.y=y;
}
@Override
public void step() {
y-=speed;
}
@Override
public boolean outOfBounds() {
// TODO Auto-generated method stub
return this.y<=-this.heighet;
}
}
package com.tarena.shout;
public interface Enemy {
public int getScore();
}
package com.tarena.shout;
public interface Award {
int DOUBLE_FIRE=0;
int LIFE=1;
public int getType();
}
package com.tarena.shout;
import java.util.Random;
public class Airplane extends FlyingObject implements Enemy {
private int speed=2;
public Airplane(){
image=ShootGame.airplane;
width=image.getWidth();
heighet=image.getHeight();
Random rand=new Random();
x=rand.nextInt(ShootGame.WIDTH-this.width);
y=-this.heighet;
}
public int getScore(){
return 5;
}
public void step() {
y+=speed;
}
@Override
public boolean outOfBounds() {
return this.y>=ShootGame.HEIGHT;
}
}
package com.tarena.shout;
import java.util.Random;
public class Bee extends FlyingObject implements Award{
private int xSpeed=1;
private int ySpeed=2;
private int awardType;
public Bee(){
image=ShootGame.bee;
width=image.getWidth();
heighet=image.getHeight();
Random rand=new Random();
x=rand.nextInt(ShootGame.WIDTH-this.width);
y=-this.heighet;
awardType=rand.nextInt(2);
}
public int getType() {
return awardType;
}
public void step() {
y+=ySpeed;
x+=xSpeed;
if(x>=ShootGame.WIDTH-this.width){
xSpeed=-1;
}
if(x<=0){
xSpeed=1;
}
}
@Override
public boolean outOfBounds() {
// TODO Auto-generated method stub
return this.y>=ShootGame.HEIGHT;
}
}
package com.tarena.shout;
import java.util.Random;
public class BigBee extends FlyingObject implements Enemy{
private int speed=1;
public BigBee(){
image=ShootGame.bigBee;
width=image.getWidth();
heighet=image.getHeight();
Random rand=new Random();
x=rand.nextInt(ShootGame.WIDTH-this.width);
y=-this.heighet;
}
@Override
public void step() {
y+=speed;
}
@Override
public boolean outOfBounds() {
return this.y>=ShootGame.HEIGHT;
}
@Override
public int getScore() {
// TODO Auto-generated method stub
return 40;
}
}
package com.tarena.shout;
import java.io.InputStream;
import javazoom.jl.player.advanced.AdvancedPlayer;
public class PlaySound extends Thread{
private String mp3Url;
private boolean isLoop;
public PlaySound(String mp3Url, boolean isLoop) {
super();
this.mp3Url = mp3Url;
this.isLoop = isLoop;
}
public PlaySound(String mp3Url) {
super();
this.mp3Url = mp3Url;
}
public void run() {
do{
InputStream mp3 = PlaySound.class.getClassLoader().getResourceAsStream("music/"+mp3Url);
try {
AdvancedPlayer adv = new AdvancedPlayer(mp3);
adv.play();
} catch (Exception e) {
e.printStackTrace();
}
}while(isLoop);
}
public static void main(String[] args) {
new PlaySound("bgmusic.mp3", true).start();
}
}
相关源码以及答辩屁屁踢已经上传至本人资源中,希望大家都能上天,和太阳肩并肩~~