这是一款闯关的射击游戏,人物在不同的关 卡里面会触发不同的技能与对应的特效操作,有三个关卡与四个随机事件,每个关卡里面都会触发不同数量的怪物与能量血瓶,通过打败怪物与 到达特点时间点可通关
⭐本项目演示地址⭐
⭐资源图片源代码私信博主⭐
效果图以及关卡里面的演示
界面类
package game;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
class Main extends JFrame implements ActionListener { 鼠标点击行为
Image im1 = (new ImageIcon("src/images/游戏缩小图标.jpeg")).getImage();
ImageIcon icon = new ImageIcon("src/images/bg.jpeg"); // 游戏背景
JButton btn1 = new JButton("开始游戏"); // 开始界面的按钮
JButton btn2 = new JButton("选择关卡");
JButton btn3 = new JButton("关于游戏");
JButton btn4 = new JButton("退出游戏");
JPanel jpan = new JPanel();// 创建画板
JLabel jlbBackImg = new JLabel();// 创建标签
Main() {
this.setLayout(null);// 取消窗口布局管理器
this.setTitle("试炼之旅");
this.setIconImage(im1);// 设置与游戏窗口图标
this.setBounds(0, 0, 800, 800);
// this.setSize(800, 800);
this.setLocationRelativeTo(null); // 设置窗体居中显示
jpan.setBounds(0, 0, 800, 800); // 定义控件的x,y,w,h的大小
// jpan.setBounds(120, 120, 800, 800);
jpan.setLayout(null); // 取消面板的布局管理器,防止闪烁
btn1.setBounds(350, 350, 120, 40);
btn2.setBounds(350, 410, 120, 40);
btn3.setBounds(350, 470, 120, 40);
btn4.setBounds(350, 530, 120, 40);
btn1.addActionListener(this); // 窗口向按钮注册
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
jlbBackImg.setBounds(0, 0, 800, 800); // 800 800
jlbBackImg.setIcon(icon); // 设置图标
jpan.add(btn1); // 添加按钮
jpan.add(btn2);
jpan.add(btn3);
jpan.add(btn4);
jpan.add(jlbBackImg); // 向面板添加标签
this.add(jpan);
this.setResizable(false);// 使游戏窗口不能改变大小
this.setVisible(true);// 使窗口显示
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 单击窗口关闭按钮,结束程序
}
@Override // 按钮的动作事件
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) { // e.getSource 为获得事件源
this.dispose();// 点击按钮“开始游戏”关闭当前窗口
new Game();// 加载开始游戏窗口
}
if (e.getSource() == btn2) { // 这里记得写关于后面游戏关卡的操作
this.dispose();
new selectGame();
}
if (e.getSource() == btn3) {
int value = JOptionPane.showOptionDialog(this,
"关于游戏:第一关人物“WSAD”控制方向,“鼠标左键发射”,“按键Q消耗分数回血”" + "需要躲避周围射击出现的热旋。\n" + " "
+ "第二关人物1“WSAD”控制方向,“鼠标左键发射”,“按键Q消耗分数回血”,“按键K消耗能量释放技能”;“人物2箭头控制方向”,“右键发射攻击特定怪物”。\n"
+ " "
+ "第三关人物“WASD”控制方向,“鼠标左键发射”,“按键Q消耗分数回血”,“按键K消耗能量释放技能”,“达到1500分数后解锁右键射击,R键特殊功能会在第二个Boss解锁”\n"
+ " " + "“F1重新开始本关”" + "“F2”返回游戏主界面,“F3”退出游戏。游戏愉快!",
"试炼之旅", JOptionPane.YES_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null);
}
if (e.getSource() == btn4) {
System.exit(0);// 退出程序
}
}
public static void main(String[] args) {
new Main();
}
}
关卡的绘制类
@Override // 面板的各人物绘制
public void paint(Graphics gh) {
if (gamerun == 1) {
if (bg1y >= 800) {
bg1y = -800;
}
if (bg2y >= 800) {
bg2y = -800;
}
bg1y += 10;
bg2y += 10;
gh.drawImage(ic1, bg1x, bg1y, 800, 800, null);// 画的游戏背景
gh.drawImage(ic2, bg2x, bg2y, 800, 800, null);
man.move(gh);// 画角色
gametimeBoss1.paintSelf2(gh); // 画boss1
for (int i = 0; i < shocks.size(); i++) { // 画雷电
shocks.get(i).drowshock(gh);
}
for (int i = 0; i < timebossbullets.size(); i++) { // boss的射击
timebossbullets.get(i).drowshock2(gh);
}
for (int i = 0; i < skills.size(); i++) { // 画K技能特效
skills.get(i).drowshock2(gh);
}
for (int i = 0; i < gb.length; i++) {
gb[i].drawShell(gh); // 画出炮弹
gb[i].drawShells(gh);
if (gb[i].getRect().intersects(man.getRect())) {
Explode explode = new Explode(man.x, man.y);
explode.draw(gh);
explodes.add(explode);
manlife1 -= 10;
break;
}
if (gb[i].getRect1().intersects(man.getRect())) {
Explode explode = new Explode(man.x, man.y);
explode.draw(gh);
explodes.add(explode);
manlife1 -= 10;
break;
}
}
相关的素材包
随机Boss的子弹类
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
class gametimeboss { // 这里画的是随机关卡的东西
Image ima = (new ImageIcon("src/timebossshock/随机事件.png")).getImage();
protected int speed = 15;
protected double degree = Math.random() * Math.PI * 5; // 炸弹随机角度
protected int Shellx = 300, Shelly = 300;
public Rectangle getRect() {
return new Rectangle(Shellx, Shelly, 35, 35);
}
public void drawShell(Graphics gh) {
Shellx += speed * Math.cos(degree);
Shelly += speed * Math.sin(degree);
if (Shellx < 0 || Shellx > 760) {
degree = Math.PI - degree;
}
if (Shelly < 0 || Shelly > 760) {
degree = -degree;
}
gh.drawImage(ima, Shellx, Shelly, 40, 40, null);// 画出炮弹
}
/
Image ima1 = (new ImageIcon("src/timebossshock/随机事件1.png")).getImage();
protected int speed1 = 12;
protected double degree1 = Math.random() * 10; // 炸弹随机角度
protected int Shellx1 = 400, Shelly1 = 250;
public Rectangle getRect1() { // 获得矩形
return new Rectangle(Shellx, Shelly, 35, 35);
}
public void drawShells(Graphics gh) { // 画出炮弹使其碰撞窗口反弹
Shellx1 += speed1 * Math.cos(degree1);
Shelly1 += speed1 * Math.sin(degree1);
if (Shellx1 < 0 || Shellx1 > 760) {
degree1 = Math.PI - degree1;
}
if (Shelly1 < 0 || Shelly1 > 760) {
degree1 = -degree1;
}
gh.drawImage(ima1, Shellx1, Shelly1, 50, 50, null);// 画出炮弹
}
/
// 画levl3的boss1的随机技能
Image power2 = (new ImageIcon("src/bossimg/power2.png")).getImage();
protected int powerspeed = 10;
protected double powerdegree = Math.random() * 15; // 随机角度
protected int powerx = 400, powery = 250;
public Rectangle getRect2() { // 获得矩形
return new Rectangle(powerx, powery, 30, 30);
}
public void drawpower(Graphics gh) { // 画出炮弹使其碰撞窗口反弹
powerx += powerspeed * Math.cos(powerdegree);
powery += powerspeed * Math.sin(powerdegree);
if (powerx < 0 || powerx > 760) {
powerdegree = Math.PI - powerdegree;
}
if (powery < 0 || powery > 760) {
powerdegree = -powerdegree;
}
gh.drawImage(power2, powerx, powery, 50, 50, null);// 画出炮弹
}
}
作者:KJ.JK
本文仅用于交流学习,未经作者允许,禁止转载,更勿做其他用途,违者必究。
文章对你有所帮助的话,欢迎给个赞或者 star 呀,你的支持是对作者最大的鼓励,不足之处可以在评论区多多指正,交流学习呀