作品展示
如有需要素材或者疑问可以关注点赞私信哦
登录页面
游戏页面
页面标签都可以进行点击操作
代码展示
1 定义一个main方法作为游戏入口
2 用JFrame绘制游戏面板
package Login;
import Login.IfCode;
import Login.User;
import ikun.second.beginGame.BeginGame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
public class LonginUI extends JFrame implements ActionListener, MouseListener
{
static ArrayList<User>allUsers=new ArrayList<>();//创建一个集合,存储用户名和密码
static{
allUsers.add(new User("虎儿真帅","123456"));
allUsers.add(new User("lyh123","654321"));
}
JButton jb=new JButton();
JLabel jlname=new JLabel("账号");
JLabel jlpassed=new JLabel("密码");
JLabel jlif=new JLabel("验证码");
JTextField jtname=new JTextField(30);
JPasswordField jppassed=new JPasswordField(30);
JTextField jtf =new JTextField();//验证码文本框
//JTextField tf =new JTextField();//随机生成的验证码文本框
JLabel tf=new JLabel();
JLabel explain=new JLabel("游戏介绍");
JLabel state=new JLabel("操作说明");
private JPanel contentPane;
JFrame jf=new JFrame();
public LonginUI()
{
setBg();
init();
//this.setVisible(true);
}
public void setBg()
{
((JPanel)jf.getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon
("C:\\Users\\16190\\Desktop\\log.png");
JLabel background = new JLabel(img);
jf.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
}
public void init(){
//jf.setLayout(null);
jf.setTitle("登录页面");
jf.setBounds(300,150,500,800);
jf.setLayout(null);
jf.setResizable(false);
jf.setBackground(Color.CYAN);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
//jf.setFont();
//1按扭 登录
jb.setIcon(new ImageIcon("C:\\Users\\16190\\Desktop\\log1.png"));
jb.setBounds(180,540,150,60);
jb.addMouseListener(this);
jb.setBorderPainted(false);//去除按钮的边框
jb.setContentAreaFilled(false);//设置按钮透明
jf.getContentPane().add(jb);
//标签 用户名
jlname.setBounds(100,380,30,30);
//jlname.setFont(new Font("微软雅黑",Font.BOLD,10));
jf.add(jlname);
jlname.setVisible(true);
//密码文字
jlpassed.setBounds(100,430,30,30);
jf.add(jlpassed);
jlpassed.setVisible(true);
//验证码
jlif.setBounds(90,480,50,30);
jf.add(jlif);
jlif.setVisible(true);
//可视化在所有组件加载之后
jf.setVisible(true);//可视化,交给系统渲染到屏幕上
//组件设置尺寸
// 文本框大小 用户文本框
jtname.setBounds(150,380,200,35);
jf.add(jtname);
jf.setVisible(true);
//密码文本框
jppassed.setBounds(150,430,200,35);
jf.add(jppassed);
//界面窗体中增加按钮
jf.setVisible(true);
//验证码文本框
jtf.setBounds(150,480,100,35);
jf.add(jtf);
//随机验证码文本框
//tf.setBounds(270,483,60,30);
//获取生成的验证码
String codestr= IfCode.randomcode(4);
//System.out.println(codestr);
tf.setText(codestr);//设置内容,在标签上添加验证码
tf.addMouseListener(this);//绑定鼠标事件
tf.setBounds(270,483,60,30);//位置和宽高
jf.getContentPane().add(tf);//将随机生成的验证码添加到界面
explain.setBounds(10,0,80,80);
explain.setFont(new Font("微软雅黑",Font.BOLD,15));
explain.addMouseListener(this);
jf.getContentPane().add(explain);
state.setBounds(400,0,80,80);
state.setFont(new Font("微软雅黑",Font.BOLD,15));
state.addMouseListener(this);
jf.getContentPane().add(state);
}
@Override
public void actionPerformed(ActionEvent e)//点击鼠标发生的事件
{
}
@Override
public void mouseClicked(MouseEvent e)
{
if(e.getSource()==jb){//就是获得你目前这个事件的事件源,说通俗点,比如有一个按钮事件,
// 你点击一个按钮,在处理事件中你用e.getSource(),就是获得这个按钮
System.out.println("点击登录按钮");
//获取俩个输入文本框的内容
String usernameInput= jtname.getText();
char[] passwordInput= jppassed.getPassword();
String str =new String(passwordInput);
String codeInput=jtf.getText();//获取用户输入的验证码
//创建一个user对象
User userInfo=new User(usernameInput,str);//把用户名和密码放入对象中
System.out.println("用户输入的用户名是"+usernameInput);
System.out.println("用户输入的密码是"+str);
if (contains(userInfo)&&codeInput.equalsIgnoreCase(tf.getText())){
System.out.println("输入正确");
jf.setVisible(false);//关闭当前登录界面
BeginGame frame =new BeginGame();
}else{
if(usernameInput.length()==0||passwordInput.length==0||codeInput.length()==0)
{
System.out.println("用户名,密码或验证码为空");
showJDialog("用户名,密码或验证码为空");
System.out.println("随机更换一个新的验证码");
String code=IfCode.randomcode(4);//获取一个新的验证码
tf.setText(code);
}else if( !codeInput.equalsIgnoreCase(tf.getText())){
showJDialog("您输入账户或密码或验证码有误");
String code=IfCode.randomcode(4);//获取一个新的验证码
tf.setText(code);
} else {
showJDialog("您输入账户或密码或验证码有误");
String code=IfCode.randomcode(4);//获取一个新的验证码
tf.setText(code);
}
}
if(e.getSource()== explain)
{
System.out.println("dianjij");
}
}
if (e.getSource()==tf){//点击屏幕验证码可以更换验证码
System.out.println("更换验证码");
String code=IfCode.randomcode(4);
tf.setText(code);
}
if (e.getSource()==explain){//利用网页进行对话框换行
show1JDialog("<html><body><p>" +"《飞机大战》这是一款经典飞行射击类游戏,<br/>" +
"精美绚丽的画面,整体环境主要还是围绕太空为主,<br/>" +
"高保真的音效,为玩家呈现一场不一样射击体验。<br/></p></body></html>");
}
if (e.getSource()==state){
show1JDialog(
"<html><body><p>" + "使用键盘方向键↑↓←→键或鼠标控制飞机移动,自动发射子弹<br/>当击中敌机时,增加1分," +
"被敌机打到时扣除一个生命值分数减10。<br/>吃到道具机子弹增加,最多加三颗,然后会增加生命值<br/>并且敌机的速度根据敌机大小不同速度不同," +
"撞上敌机时游戏结束。<br/></p></body></html>");
}
}
private void show1JDialog(String s) {
JDialog jDialog1=new JDialog();//创建一个弹窗对象
jDialog1.setSize(400,200); //设置弹窗大小
jDialog1.setAlwaysOnTop(true);//让弹窗顶置
jDialog1.setLocationRelativeTo(null);//让弹窗居中
jDialog1.setModal(true);//模式对话框,弹框不关闭无法操作下面的界面
//创建标签对象管理文字并添加到弹框中
JLabel sout=new JLabel(s);//创建新的标签,内容是输出的showJDialog
sout.setBounds(0,0,80,60);//设置标签大小
sout.setFont(new Font("微软雅黑",Font.BOLD,15));
jDialog1.getContentPane().add(sout);//setContentPane()/getContentPane()添加或设置组件, JFrame 有一个 Content Pane,
// 窗口能显示的所有组件都是添加在这个 Content Pane 中。JFrame 提供了两个方法:
// getContentPane 和 setContentPane 就是用于获取和设置其 Content Pane 的。
jDialog1.setVisible(true);//让弹框显示出来
}
private boolean contains(User userInput) {//userIput为用户输入的账号,密码
for (int i = 0; i < allUsers.size(); i++) {//size()返回类表中元素的个数
User rightUser=allUsers.get(i);//get()返回类表中指定的元素
//比较用户名和密码是否相同
if (userInput.getUsername().equals(rightUser.getUsername())&&
userInput.getPassword().equals(rightUser.getPassword()))
{
return true;//有相同的代表存在,返回true
}
}
// showJDialog("您输入账户或密码或验证码有误");
return false;//循环结束之后还没有找到就表示不存在
}
private void showJDialog(String content) {
JDialog jDialog=new JDialog();//创建一个弹窗对象
jDialog.setSize(200,150); //设置弹窗大小
jDialog.setAlwaysOnTop(true);//让弹窗顶置
jDialog.setLocationRelativeTo(null);//让弹窗居中
jDialog.setModal(true);//模式对话框,弹框不关闭无法操作下面的界面
//创建标签对象管理文字并添加到弹框中
JLabel sout=new JLabel(content);//创建新的标签,内容是输出的showJDialog
sout.setBounds(0,0,200,150);//设置标签大小
jDialog.getContentPane().add(sout);//setContentPane()/getContentPane()添加或设置组件, JFrame 有一个 Content Pane,
// 窗口能显示的所有组件都是添加在这个 Content Pane 中。JFrame 提供了两个方法:
// getContentPane 和 setContentPane 就是用于获取和设置其 Content Pane 的。
jDialog.setVisible(true);//让弹框显示出来
}
@Override
public void mousePressed(MouseEvent e)
{
if (e.getSource() == jb)
{
jb.setIcon(new ImageIcon("C:\\Users\\16190\\Desktop\\log1.png"));
}
}
@Override
public void mouseReleased(MouseEvent e)
{
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
3 展示验证码功能(验证码可以通过鼠标点击更换)
package Login;
import java.util.Random;
public class IfCode {
public IfCode(){
String code=randomcode(4);
//System.out.println(code);
}
public static String randomcode(int n){
String code ="";
for (int i = 0; i < n; i++) {
Random r=new Random();
int type=r.nextInt(3);
switch (type){
case 0:
char ch =(char)(r.nextInt(25)+65);
code+=ch;
break;
case 1:
char ch1=(char)(r.nextInt(25)+97);
code+=ch1;
break;
case 2:
code+=r.nextInt(9);
break;
}
}
return code;
}
}
利用JavaBean进行定义游戏的登录用户名和密码的设置
package Login;
public class User {
private String username;//用户名
private String password;//密码
//定义一个无参构造器
public User(){
}
public User(String username, String password)
{
this.username=username;
this.password=password;
}
public void setUsername(String username){
this.username=username;
}
public String getUsername(){
return username;
}
public void setPassword(String password){
this.password=password;
}
public String getPassword(){
return password;
}}
游戏开始界面
游戏初始界面
package ikun.second.beginGame;
import javax.swing.*;
/**
* 游戏的窗体绘制
* Java中的游戏窗体绘制类是JFrame
* 自定义窗体绘制的步骤
* 1写一个类,继承JFrame
* 2写一个构造方法,绘制窗口的属性
* 属性对应特点,方法对应行为
*/
public class BeginGame extends JFrame {
/**
* 构造方法;方法名与类名一致
*
*/
public BeginGame()
{
begin();
this.setVisible(true);//让界面显示出来
}
//explise中Alt+?开启代码提示
public void begin()
{
this.setSize(512, 768);//设置界面的宽高
this.setTitle("飞机大战"); //设置界面的标题
//this.setAlwaysOnTop(true); //设置界面置顶
this.setLocationRelativeTo(null);//设置界面居中
this.setResizable(false);//设置游戏界面不可以改变
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭模式
//this.setLayout(null); //取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件
GamePanel panel=new GamePanel(this);//创建面板对象,调用面板的构造方法,制作面板()中添加的this为frame,将其添加到窗口中
panel.action();//调用敌机的方法
this.add(panel);//将面板添加到窗体中
}
}
游戏页面制作
package ikun.second.beginGame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Java中的游戏面板; JPanel
* 自定义游戏面板
* 1写一个类继承JPanel
* 2写一个构造方法,初始化面板的属性
*/
public class GamePanel extends JPanel {
BufferedImage bg;//初始化背景
Hero hero=new Hero();
// Enemy en =new Enemy();
//创建敌机的集合的,存放所有的敌机
List<Enemy> ens=new ArrayList<Enemy>();
List<Fire> fs=new ArrayList<Fire>();
int score;
boolean gameover=false;
int power=1;
boolean isStart=false;
boolean isWin;
/**
* 使用线程开启一个游戏
* 线程的格式
* new Thread(){public void run(){....线程所有要完成的事情。。。。}}.start();
*/
public void action(){
new Thread(){
public void run(){
while(true){ //写一个死循环让敌机一直进入游戏
if (!gameover&&isStart==true&&isWin==false){
//调用敌机入场的方式
ensEnter();
//调用方法让敌机移动
ensMove();
//调用方法发射子弹
shoot();
//让子弹移动
fireMove();
//子弹射击敌机
shootEp();
//判断敌机是否撞到了英雄机
hit();
}else{
isStart=false;
}
//调用线程休眠,没执行一次,暂停一会
try {
Thread.sleep(9);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
repaint();
}
}
private void hit() {
//遍历敌机
for (int i = 0; i < ens.size(); i++) {
Enemy e=ens.get(i);
//如果敌机被英雄机撞上
if (e.hitBy(hero)){
//删除敌机
ens.remove(e);
//英雄机血量减少
hero.hp--;
power=1;//英雄机血量火力减少
score-=10;
if (hero.hp<=0){
gameover=true;
}
}
}
}
private void shootEp() {
//遍历所有的子弹
for (int i = 0; i < fs.size(); i++) {
//获取每一刻子弹
Fire f=fs.get(i);
//让没一颗子弹,判断是否集中敌机
bang(f);
}
}
/**
* 判断每一颗子弹是否击中敌机
*
* @param f
*/
private void bang(Fire f) {
//遍历所有敌机
for (int i = 0; i < ens.size(); i++) {
//获取每一个敌机
Enemy e=ens.get(i);
//判断敌机是否击中了子弹
if(e.shootBy(f)){
if (e.type!=15){
//让敌机的血量减少
e.hp--;
//如果子弹击中敌机,敌机消失
//当敌机血量减少到0时;
if (e.hp<=0){
if (e.type==5||e.type==6){
power++;
if (power>3){
hero.hp++;
power=3;
}
}
ens.remove(e);//敌机消失
score+=1; //增加分数
}
if (score>=10){
isWin=true;
}
}
fs.remove(f); //子弹消失
}
}
}
//子弹移动的方法
private void fireMove() {
for(int i=0;i<fs.size();i++){
Fire f=fs.get(i);
f.move();
//f.y=f.y-f.sp;
}
}
//发射子弹的方法
int findxe=0;
private void shoot() {
findxe++;
if(findxe>=5){
if (power==1){
Fire fire2=new Fire(hero.x+25,hero.y-15,1);
fs.add(fire2);
} else if (power==2) {
Fire fire1=new Fire(hero.x+50,hero.y,2);
fs.add(fire1);
//中间
Fire fire2=new Fire(hero.x+25,hero.y-15,1);
fs.add(fire2);
}else {
//左边
Fire fire=new Fire(hero.x,hero.y,0);
fs.add(fire);
Fire fire1=new Fire(hero.x+50,hero.y,2);
fs.add(fire1);
//中间
Fire fire2=new Fire(hero.x+25,hero.y-15,1);
fs.add(fire2);//增加子弹
}
findxe=0;//如果放在if语句外面,则没有机会执行if语句里面的内容
}
}
protected void ensMove() {
//遍历所有敌机
for(int i=0;i<ens.size();i++){
Enemy en=ens.get(i);
en.move();
}
}
//实现ensEnter()方法
//敌机出现的频率太高,执行20次 ensEnter()方法,才创建一个敌机
int index=0;
protected void ensEnter() {
index++;
if(index>=20){
Enemy e=new Enemy();
ens.add(e);
index=0;
}
}
}.start();
}
public GamePanel( BeginGame frame){//定义一个构造方法,添加窗体
setBackground(Color.PINK);//设置面板的背景
//将背景图导入
bg=App.getPicture("/picture/bg3.jpg");
//MouseAdapter是抽象的abstract,不能实例化
/**
* 使用鼠标监听器的固定格式
* 1 创建鼠标适配器
* 2 确定所需要的鼠标的事件
* 鼠标事件有
* 1 mouseMoved 监听鼠标移动事件
* 2 mouseCliked 监听鼠标点击事件
* 3 mousePressed 监听鼠标按下去事件
* 4 mouseEntered 监听鼠标移入游戏界面事件
* 5 mouseExited 监听鼠标移出游戏界面事件
*/
MouseAdapter adapter=new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e) {
if (gameover||isWin){
hero=new Hero();
gameover=false;
isWin=false;
score=0;
ens.clear();//清楚敌机
fs.clear();//清除子弹
power=1;//子弹等于1;
Random r=new Random();
int index=r.nextInt(3)+3;
bg=App.getPicture("/picture/bg"+index+".jpg");
repaint();
} else {
isStart=!isStart;
}
}
@Override
public void mouseMoved(MouseEvent e){
/**
* 当鼠标在游戏界面中移动时,会触动该方法,使得英雄机和鼠标一起移动
* 既是让鼠标的横纵坐标和英雄机的横纵坐标一致
*/
int mx=e.getX();//鼠标的横坐标
int my=e.getY();//鼠标的横坐标
if (!gameover&&isStart==true){
//让英雄机移动到鼠标的位置
hero.moveToMouse(mx,my);
}
//刷新页面,讲英雄级绘制到新的位置
repaint();
}
};
//将适配器加入到监听器中(固定模式)
addMouseListener(adapter);
addMouseMotionListener(adapter);
/**
* 使用鼠标监听器
* 1 创建鼠标适配器
* 2 确定需要监听的键盘事件
*/
KeyAdapter kd=new KeyAdapter() {
@Override
//按下键盘会触动该方法
public void keyPressed(KeyEvent e) {
int keycode=e.getKeyCode();
// System.out.println(keycode);
if(keycode==KeyEvent.VK_UP||keycode==KeyEvent.VK_W){
// hero.y=hero.y-10;
hero.moveUP();
}else if(keycode==KeyEvent.VK_DOWN||keycode==KeyEvent.VK_S){
//hero.y=hero.y+10;
hero.moveDOWN();
}else if(keycode==KeyEvent.VK_RIGHT||keycode==KeyEvent.VK_D){
//hero.x=hero.x+10;
hero.moveRIGHT();
} else if (keycode==KeyEvent.VK_LEFT||keycode==KeyEvent.VK_A) {
//hero.x=hero.x-10;
hero.moveLEFT();
}
//hero.moveToKey(150,500);
repaint();
}
};
frame. addKeyListener(kd);//frame 必须将键盘添加到窗体中
}
/**
*Graphics g 是画笔
* paint()是画图方法
*/
@Override
public void paint(Graphics g){
super.paint(g);
// g.drawImage(bg,0,0,null);
//如果不是原图,想要改变图片大小,可以用
g.drawImage(bg,0,0,512,768,null);//设置图片的大小,并将其画在画板上
// 如果图片与窗口一致 g.drawImage(bg,200,300,null);
g.setColor(Color.white);
g.setFont(new Font("楷体",Font.BOLD,18));
g.drawString("分数"+score,10,30);
//画敌机
g.drawImage(hero.img,hero.x,hero.y,hero.w,hero.h,null);
for (int i = 0; i < ens.size(); i++) {
Enemy en=ens.get(i);
g.drawImage(en.img,en.x,en.y,en.w,en.h,null);
}
//画子弹
for(int i=0;i<fs.size();i++){
Fire fire=fs.get(i);
g.drawImage(fire.img,fire.x,fire.y,fire.h,fire.w,null);
}
//画英雄级的血量
for (int i = 0; i < hero.hp; i++) {
g.drawImage(hero.img,360+i*35,5,30,30,null);
}
//画死亡判断
if (gameover){
g.setColor(Color.red);
g.setFont(new Font("楷体",Font.BOLD,35));
g.drawString("GAMEOVER,你个小菜鸡,",80,300);
g.drawString("点击屏幕重新开始游戏",80,350);
}
else if(isStart==false){
g.setColor(Color. WHITE);
g.setFont(new Font("微软雅黑",Font.ITALIC,40));
g.drawString("点击屏幕开始游戏", 90, 350);
}
if (isWin){
g.setColor(Color. pink);
g.setFont(new Font("微软雅黑",Font.ITALIC,60));
g.drawString("恭喜通关", 120, 300);
}
}
}
定义一个工具类,处理游戏图片
package ikun.second.beginGame;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
/**
* APP类是处理图片的工具类
* 工具类的使用; 工具类一般讲项目重复使用的代码抽离出来,定义成工具方法
* 工具类中的方法,一般用static修饰
*
* 家static的目的;
* static是公用的,被锁有对象公用
* static修饰的方法不依赖用对象,可以用类名直接调用
*/
public class App {
/**
*BufferedImage Java中用来表示图片的类
* /读取指定位置上的图片
* path图片的路径
* getResource()获取想要得到的资源
*/
public static BufferedImage getPicture(String path){
/**
*加载图片
* Java中的IO,传送数据的管道
* 表示输入输出流
* App.class找到App类的路径
*/
try {
BufferedImage picture= ImageIO.read(App.class.getResource(path));
//如果未找到图片,将图片返回
return picture;
} catch (IOException e) {
//catch如果找不到图片,就会捕获找不到图片的原因
e.printStackTrace();
}
return null;
}
}
定义一个敌机类
package ikun.second.beginGame;
import java.awt.image.BufferedImage;
import java.util.Random;
public class Enemy extends FlyObject {
int sp;
int hp;
int type;
public Enemy(){
//设置一个随机数,随机产生敌机的位置
Random r=new Random();
int index=r.nextInt(15)+1;//刚开始是[0,15)0~14, +1之后为[1,16)取值范围是1~15;
type=index;
//三元运算符判断
String path="/picture/ep"+(index<10?0:"")+index+".png";
img=App.getPicture(path);//获取敌机的图片
//定义敌机的属性,大小,宽高
w=img.getWidth();
h=img.getHeight();
x=r.nextInt(512-w);//飞机随机产生的位置减去飞机自身的宽度
y=-h;
sp=17-index;
hp=index+1;
}
public void move(){
if (type==12){
x-=5;y+=5;
} else if (type==13) {
x+=5;y+=5;
} else if (type==15) {
y+=10;
}else{
y+=sp;
}
}
}
定义英雄机类
package ikun.second.beginGame;
public class Hero extends FlyObject {
int hp;
public Hero(){
//导入英雄机的图片
img=App.getPicture("/picture/ep16.png");
//确定英雄机在游戏开始时显示的位置
x=150;
y=500;
//确定英雄机的大小
w=img.getWidth()/2;
h=img.getHeight()/2;
hp=3;
}
public void moveToMouse(int mx,int my){
x=mx-w/2;
y=my-h/2;
}
public void moveUP(){
y=y-10;
}
public void moveDOWN(){
y=y+10;
}
public void moveRIGHT(){
x=x+10;
}
public void moveLEFT(){
x=x-10;
}
}
定义英雄机和敌机共同属性的类
package ikun.second.beginGame;
import java.awt.image.BufferedImage;
public class FlyObject {
/**
* 定义飞机的属性,如位置,大小
* 飞机的的图片,把所有的飞机图片的导入声明,大小,宽高,等属性封装成一个父类,
* 其他类如需调用,让其子类继承父类中的变量
*/
BufferedImage img;
int x;
int y;
int w;
int h;
public void move() {
y=y-10;
}
//碰撞监测,敌机是否被子弹击中
public boolean shootBy(Fire f) {
boolean hit =x<=f.x+f.w&&x>=f.x-w&&y<=f.y+f.h&&y>=f.y-h;
return hit;
}
public boolean hitBy(Hero f) {
boolean hit =x<=f.x+f.w&&x>=f.x-w&&y<=f.y+f.h&&y>=f.y-h;
return hit;
}
}
定义子弹类
package ikun.second.beginGame;
public class Fire extends FlyObject{
/**
* 定义一个构造方法
* 初始化子弹
*/
int dir;
public Fire(int hx,int hy,int dir){
//子弹图片导入
img=App.getPicture("/picture/fire.png");
//确定子弹大小
w=img.getWidth()/4;
h=img.getHeight()/4;
//确定子弹的位置
x=hx;
y=hy;
this.dir=dir;
if (dir==0){
y-=10;
x-=3;
} else if (dir==1) {
y-=10;
}else if (dir==2){
x+=3;
y-=10;
}
}
}