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.image.BufferedImage;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
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;
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;
private int score = 0; // 得分
private Timer timer; // 定时器
private int intervel = 1000/100; // 时间间隔(毫秒)
public static BufferedImage background;
public static BufferedImage start;
public static BufferedImage pause;
public static BufferedImage gameover;
public static BufferedImage bullet;
public static BufferedImage airplane;
public static BufferedImage[] airplaneEmber=new BufferedImage[4];
public static BufferedImage bee;
public static BufferedImage[] beeEmber=new BufferedImage[4];;
public static BufferedImage hero0;
public static BufferedImage hero1;
public static BufferedImage[] heroEmber=new BufferedImage[4];;
public static BufferedImage bigPlane;
public static BufferedImage[] bigPlaneEmber=new BufferedImage[4];;
private FlyingObject[] flyings = {}; // 敌机数组
private Bullet[] bullets = {}; // 子弹数组
private Hero hero = new Hero(); // 英雄机
private Ember[] embers = {}; // 灰烬
static {// 静态代码块
try {
background = ImageIO.read(ShootGame.class
.getResource("background.png"));
bigPlane = ImageIO
.read(ShootGame.class.getResource("bigplane.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"));
start = ImageIO
.read(ShootGame.class.getResource("start.png"));
for(int i=0; i<4; i++){
beeEmber[i] = ImageIO.read(
ShootGame.class.getResource("bee_ember"+i+".png"));
airplaneEmber[i] = ImageIO.read(
ShootGame.class.getResource("airplane_ember"+i+".png"));
bigPlaneEmber[i] = ImageIO.read(
ShootGame.class.getResource("bigplane_ember"+i+".png"));
heroEmber[i] = ImageIO.read(
ShootGame.class.getResource("hero_ember"+i+".png"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void paint(Graphics g) {
g.drawImage(background, 0, 0, null); // 画背景图
paintEmber(g);
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 paintEmber(Graphics g) {
for (int i = 0; i < embers.length; i++) {
Ember e = embers[i];
g.drawImage(e.getImage(), e.getX(), e.getY(), null);
}
}
/** 画子弹 */
public void paintBullets(Graphics g) {
for (int i = 0; i < bullets.length; i++) {
Bullet b = bullets[i];
if(! b.isBomb())
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);
}
}
Object a = new Object();
/** 画分数 */
public void paintScore(Graphics g) {
int x = 10;
int y = 25;
Font font = new Font(Font.SANS_SERIF,Font.BOLD, 14);
g.setColor(new Color(0x3A3B3B));
g.setFont(font); // 设置字体
g.drawString("SCORE:" + score, x, y); // 画分数
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("Shoot Game");
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 != GAME_OVER) {
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(); // 检查游戏结束
emberAction();
}
repaint(); // 重绘,调用paint()方法
}
}, intervel, intervel);
}
private void emberAction() {
Ember[] live = new Ember[embers.length];
int index = 0;
for (int i = 0; i < embers.length; i++) {
Ember ember = embers[i];
if(! ember.burnDown()){
live[index++]=ember;
}
}
embers = Arrays.copyOf(live, index);
}
int flyEnteredIndex = 0; // 飞行物入场计数
/** 飞行物入场 */
public void enterAction() {
flyEnteredIndex++;
if (flyEnteredIndex % 40 == 0) { // 300毫秒--10*30
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();
}
int shootIndex = 0; // 射击计数
/** 射击 */
public void shootAction() {
shootIndex++;
if (shootIndex % 30 == 0) { // 100毫秒发一颗
Bullet[] bs = hero.shoot(); // 英雄打出子弹
bullets = Arrays.copyOf(bullets, bullets.length + bs.length
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 42 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/6b2ea2d510824682ac2af676104b9caf_abackcab.jpg!1)
毕业小助手
- 粉丝: 2413
- 资源: 5558
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)