package cn.tedu.shoot;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import cn.tedu.shoot.Music;
import cn.tedu.shoot.Enemy;
import cn.tedu.shoot.CarObject;
import cn.tedu.shoot.Herocar;
import cn.tedu.shoot.Herocar_Shield;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;
import java.awt.Font;
/** 主程序类 */
public class CarGame extends JPanel {
public static final int WIDTH = 1000; // 窗口宽
public static final int HEIGHT = 802; // 窗口高
public static BufferedImage background; // 背景图
public static BufferedImage start; // 启动图
public static BufferedImage pause; // 暂停图
public static BufferedImage gameover; // 游戏结束图
public static BufferedImage trucks1; // 货车0
public static BufferedImage trucks2; // 货车1
public static BufferedImage oiltanktruck1; // 油罐车
public static BufferedImage oiltanktruck2;
public static BufferedImage passenger1; // 客车
public static BufferedImage passenger2;
public static BufferedImage limousine1; // 轿车1
public static BufferedImage limousine2;
public static BufferedImage packs1;
public static BufferedImage packs2;// 大礼包 表示奖励
public static BufferedImage going; // 表示暴走
public static BufferedImage bullet0; // 表示射击
public static BufferedImage heroA0; // 跑车1
public static BufferedImage heroA1;
public static BufferedImage heroB0; // 跑车2
public static BufferedImage heroB1;
public static BufferedImage heroC0; // 跑车3
public static BufferedImage heroC1;
public static BufferedImage explode0; // 爆炸效果
public static BufferedImage explode1; // 爆炸效果
public static BufferedImage explode2; // 爆炸效果
public static BufferedImage explode3; // 爆炸效果
static { // 初始化静态图片
try {
background = ImageIO.read(CarGame.class.getResource("backgroundA.png"));
start = ImageIO.read(CarGame.class.getResource("back.png"));
pause = ImageIO.read(CarGame.class.getResource("pause3.jpg"));
gameover = ImageIO.read(CarGame.class.getResource("gameover1.jpg"));
trucks1 = ImageIO.read(CarGame.class.getResource("trucks0.png"));
// trucks2 = ImageIO.read(CarGame.class.getResource("trucks1.png"));
oiltanktruck1 = ImageIO.read(CarGame.class.getResource("oiltanktrucks0.png"));
// oiltanktruck2 =
// ImageIO.read(CarGame.class.getResource("oiltanktrucks1.png"));
passenger1 = ImageIO.read(CarGame.class.getResource("passenger0.png"));
// passenger2 =
// ImageIO.read(CarGame.class.getResource("passenger1.png"));
limousine1 = ImageIO.read(CarGame.class.getResource("limousine0.png"));
// limousine2 =
// ImageIO.read(CarGame.class.getResource("limousine1.png"));
packs1 = ImageIO.read(CarGame.class.getResource("packs111.png"));
packs2 = ImageIO.read(CarGame.class.getResource("packs222.png"));
going = ImageIO.read(CarGame.class.getResource("shield.png"));
bullet0 = ImageIO.read(CarGame.class.getResource("bullet11.png"));
heroA0 = ImageIO.read(CarGame.class.getResource("herocar1.png"));
heroA1 = ImageIO.read(CarGame.class.getResource("herocar2.png"));
heroB0 = ImageIO.read(CarGame.class.getResource("herocarB1.png"));
heroB1 = ImageIO.read(CarGame.class.getResource("herocarB2.png"));
heroC0 = ImageIO.read(CarGame.class.getResource("herocarC2.png"));
heroC1 = ImageIO.read(CarGame.class.getResource("herocarC3.png"));
explode0 = ImageIO.read(CarGame.class.getResource("explode0.png"));
explode1 = ImageIO.read(CarGame.class.getResource("explode1.png"));
explode2 = ImageIO.read(CarGame.class.getResource("explode2.png"));
explode3 = ImageIO.read(CarGame.class.getResource("explode3.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
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 state = START; // 当前状态(默认启动状态)
private Timer timer; // 添加定时器
// private Shield shield; // 添加能量罩
private Herocar hero = new Herocar(); // 跑车对象
private List<CarObject> Runings = new ArrayList<CarObject>(); // (货车+小蜜蜂+客车+油罐车+轿车)-集合
private List<Bullet> bullets = new ArrayList<Bullet>(); // 子弹集合
private Sky[] skys = { new Sky(0, 0), new Sky(0, -CarGame.HEIGHT) };
private CarObject[] explode = {}; // 敌机爆炸图片数组
/** 生成敌人(货车+小蜜蜂+客车+油罐车+轿车)对象 */
public CarObject nextOne() {
Random rand = new Random(); // 创建随机数对象
int type = rand.nextInt(100); // 0到100之间的随机数
if (type < 20) { // 0到20时,生成货车
return new Trucks();
} else if (type >= 20 && type < 40) {// 20到40时,生成油罐车
return new Oiltanktrucks();
} else if (type >= 40 && type < 60) { // 40到60时,生成客车
return new Passenger();
} else if (type >= 60 && type < 80) { // 60到80时,生成轿车
return new Limousine();
} else { // 80到100时,生成奖励
return new Packs();
}
}
/** 入场 */
int enterdIndex = 0;
public void enterAction() {
enterdIndex++;
if (enterdIndex % 100 == 0) {
CarObject obj = nextOne(); // 获取敌人对象
Runings.add(obj);
}
}
/** 控制移动 */
public void stepAction() {
hero.step(); // 调用跑车的移动————跑车入场
for (CarObject run : Runings) {
run.step();
}
for (Sky sky : skys) { // 控制图片滚动
sky.step();
}
for (Bullet bul : bullets) { // 控制子弹移动
bul.step();
}
}
// 发射子弹方法
public void shootAction() {
Bullet[] bs = hero.shoot(); // 获取子弹对象
Collection c = Arrays.asList(bs); // 集合转换成数组
bullets.addAll(c); // 子弹添加到集合上
}
/** 删除越界物体 */
public void outOfBoundsAction() {
Collection<CarObject> flyingLives = new ArrayList<CarObject>(); // 不越界敌人集合
for (CarObject f : flyingLives) {
if (!f.outOfBounds()) {
flyingLives.add(f);// 将不越界敌人添加到不越界敌人集合中
}
}
Runings.addAll(flyingLives);
Collection<Bullet> bulletLives = new ArrayList<Bullet>(); // 不越界子弹集合
for (Bullet b : bulletLives) {
if (!b.outOfBounds()) {
bulletLives.add(b);
}
}
bullets.addAll(bulletLives);// 添加不越界子弹集合
}
/** 移除撞到的物体 */
public void removeObject() {
for (CarObject carObject : Runings) {
if (carObject.isstate == 0) {
Runings.remove(carObject);
return;
}
}
}
/** 所有子弹与所有敌人碰撞 */
public void bangAction() throws Exception {
for (Bullet b : bullets) {
// Music.Musics(Music.loop, Music.HERO_BULLET); //调用射击子弹的音乐
bang(b);
}
}
/** 子弹与其他汽车的碰撞 */
int score = 0;
public void bang(Bullet b) throws Exception {
for (CarObject f : Runings) {
if (f.shootBy(b)) {
if (f instanceof Enemy) {// 若被撞对象是敌人
// Music.Musics(Music.loop, Music.HERO_BULLET);
// //调用撞击的音乐-----抛出异常
Enemy e = (Enemy) f; // 将被撞对象强转为敌人类型
score += e.getScore(); // 玩家得分
}
if (f instanceof Award) { // 若被撞对象是奖励
// Music.Musics(Music.loop, Music.GET_PACKS); //调用获得奖励的音乐
Award a = (Award) f; // 将被撞对象强转为奖励类型
int type = a.getType(); // 获取奖励类型
switch (type) { // 根据奖励类型不同来得到不同的奖励---暴走 射击 �
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
作为新手,有很大的参考价值,学了一段时间的Java之后,心血来潮写了一个赛车游戏,实现功能如下: 1.键盘监听,控制赛车的左右和上下移动,包括发射子弹。 2.赛车类型选择,可以选择其中的三种车型。 3.分数、生命数、奖励类型等等都已经实现。 4.爆炸效果的实现,撞击之后敌车自动消失的效果实现。
资源推荐
资源详情
资源评论

















收起资源包目录





































































































共 144 条
- 1
- 2
资源评论

- weixin_441214072018-12-18谢谢 非常有用

南丘xf
- 粉丝: 159
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- Android Course Work-移动应用开发资源
- python教案.pdf
- 网络技术及应用课件电子教案课件整套教学课件.pptx
- 本科毕业论文:LDPC码的编译码算法研究.pdf
- 网络营销教案完整版讲义.doc
- 史丰收速算法是以史丰收教授的名字命名的.pdf
- 数学教案-小数的连除、除加、除减混合运算和简便算法.docx
- 泸州市十郎区块链同城网人事管理系统.doc
- 项目管理理论的重大科技模式研究.doc
- 自动化生产实习心得体会.docx
- 银行软件测试面试题目.docx
- 学校网络规划投标书.doc
- 网络课程设计标准市公开课一等奖百校联赛优质课金奖名师赛课获奖课件.ppt
- 陕西省项目管理师报考条件.docx
- 使用正版软件自查报告.docx
- 武汉大学网络营销().pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
