JAVA_飞机大战简单案例
程序员文章站
2022-03-06 23:53:34
JAVA是面对对象的程序编程语言,那自然需要将飞机大战里的东西抽象化为对象,飞机大战不可避免将会出现可被玩家操控的飞机,敌机,子弹,小蜜蜂(这里暂且视为奖励机制),将四个对象分别设置该有的属性和方法,再进行编程,会非常好理解。代码部分:飞行物父类笔者这里建立四个类,分别指可被玩家操控的飞机,敌机,子弹,小蜜蜂,而这些对象理所当然的都有几处共同点,如飞行物图片,飞行物的宽和高,飞行物当前坐标...
该如何开始
JAVA是面对对象的程序编程语言,那自然需要将飞机大战里的东西抽象化为对象,飞机大战不可避免将会出现可被玩家操控的飞机,敌机,子弹,小蜜蜂(这里暂且视为奖励机制),将四个对象分别设置该有的属性和方法,再进行编程,会非常好理解。
素材
代码部分:
飞行物父类
笔者这里建立四个类,分别指可被玩家操控的飞机,敌机,子弹,小蜜蜂,而这些对象理所当然的都有几处共同点,如飞行物图片,飞行物的宽和高,飞行物当前坐标以及飞行物移动方法和出界方法,为了简化代码,这里建立抽象父类。
package cn.lijiajun.game;
import java.awt.image.BufferedImage;
import cn.lijiajun.inter.IScore;
//飞行物父类
public abstract class FlyObject {
public BufferedImage image;//当前飞行物图片
public int width;//当前飞行物宽度
public int heigth;//当前飞行物高度
public int x;//当前飞行物坐标位置
public int y;//当前飞行物坐标位置
/**
* 飞行物移动
*/
public abstract void move();
/**
* 飞行物是否出界
* true表示出界
*/
public abstract boolean isOut();
}
这里额外解释一下出界方法的用途,当图片从边框消失时,不在我们的视线时,我们应当将它去除,而不是让他继续做任何的操作。
玩家机
package cn.lijiajun.game;
//英雄机
import java.awt.image.BufferedImage;
import cn.lijiajun.main.GameUi;
//子类玩家机继承飞行物父类
public class hero extends FlyObject {
public int score;//当前英雄机的分数
public int life;//当前英雄机的声明
private int fire;//火力值
private BufferedImage[] images;//图片
private int index;//索引值,用于实现动态飞机
public hero()
{ //给属性初始化
//给英雄机定义哪张图片,GameUi是另一个负责外观的类,heroimg是以静态方式存入的
image=GameUi.heroimg;
//图片的宽度和高度
width=image.getWidth();
heigth=image.getHeight();
//坐标
x=180;
y=450;
score=0;//城国际
life=3;//生命
fire=0;//火力值
images=new BufferedImage[]{GameUi.heroimg,GameUi.heroimg2};
//这里的代码是为了图片切换,通关快速切换的方式动态的体现飞机
index=0;
}
public int getFire() {
return fire;
}
public void setFire(int fire) {
this.fire = fire;
}
public BufferedImage[] getImages() {
return images;
}
public void setImages(BufferedImage[] images) {
this.images = images;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
//因为是根据鼠标移动hero的位置,所以改为实现英雄机尾气效果
public void move() {
index++;
image=images[index/15%2];//切换图片
}
//判断是否出界
public boolean isOut(){
return false;
}
}
敌机
package cn.lijiajun.game;
import java.awt.image.BufferedImage;
import java.util.Random;
import cn.lijiajun.inter.IScore;
import cn.lijiajun.main.GameUi;
public class Plane extends FlyObject implements IScore {
private int ySpeed;//移动速度
Random r=new Random();//用于控制
public BufferedImage[] images;
public Plane()
{ //给属性初始化
image=GameUi.airplane;
images=new BufferedImage[]{GameUi.airplane,GameUi.airplaneboom};
width=image.getWidth();
heigth=image.getHeight();
x=r.nextInt(GameUi.width-width);
y=0;
ySpeed=1;
}
public void move() {
y+=ySpeed;
}
public int getySpeed() {
return ySpeed;
}
public void setySpeed(int ySpeed) {
this.ySpeed = ySpeed;
}
@Override
public boolean isOut() {
if(y>650)
{
return true;
}
else {
return false;
}
}
@Override
public void award() {
// TODO Auto-generated method stub
}
}
子弹
package cn.lijiajun.game;
import cn.lijiajun.main.GameUi;
public class Bullet extends FlyObject{
private int ySpeed;
public Bullet(hero h)
{ //给属性初始化
image=GameUi.bullet;
width=image.getWidth();
heigth=image.getHeight();
//子弹坐标是根据英雄机来的
x=h.x+h.width/2;
y=h.y;
ySpeed=1;
}
public int getySpeed() {
return ySpeed;
}
public void setySpeed(int ySpeed) {
this.ySpeed = ySpeed;
}
@Override
public void move() {
y-=ySpeed;
}
@Override
public boolean isOut() {
if(y==0)
{
return true;
}
else {
return false;
}
}
}
小蜜蜂
package cn.lijiajun.game;
import java.util.Random;
import cn.lijiajun.inter.IAward;
import cn.lijiajun.main.GameUi;
public class Bee extends FlyObject implements IAward{
private int ySpeed;
private int xSpeed;
private int awardType;//奖励类型(0代表加一条生命值,1代表加二十点火力值)
Random r=new Random();
public Bee()
{ //给属性初始化
image=GameUi.beeimg;
width=image.getWidth();
heigth=image.getHeight();
x=r.nextInt(GameUi.width-width);
y=0;
xSpeed=1;
ySpeed=1;
awardType=r.nextInt(2);
}
//移动方法
public void move() {
y+=ySpeed;
x+=xSpeed;
if(x>=GameUi.width-this.width) {
xSpeed=-1;
}
if(x<=0) {
xSpeed=1;
}
}
public int getySpeed() {
return ySpeed;
}
public void setySpeed(int ySpeed) {
this.ySpeed = ySpeed;
}
public int getxSpeed() {
return xSpeed;
}
public void setxSpeed(int xSpeed) {
this.xSpeed = xSpeed;
}
public int getAwardType() {
return awardType;
}
public void setAwardType(int awardType) {
this.awardType = awardType;
}
@Override
public boolean isOut() {
if(y>650)
{
return true;
}
else {
return false;
}
}
@Override
public void award() {
// TODO Auto-generated method stub
}
}
GamuUI
package cn.lijiajun.main;
import java.applet.Applet;
import java.applet.AudioClip;
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.io.File;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import cn.lijiajun.game.Bee;
import cn.lijiajun.game.Bullet;
import cn.lijiajun.game.Plane;
import cn.lijiajun.game.hero;
//主游戏窗体
@SuppressWarnings("deprecation")
public class GameUi extends JPanel{
//窗体的宽高
public static int width=400;
public static int height=625;
//图像
public static BufferedImage background;
public static BufferedImage heroimg;
public static BufferedImage heroimg2;
public static BufferedImage beeimg;
public static BufferedImage airplane;
public static BufferedImage start;
public static BufferedImage gameover;
public static BufferedImage pause;
public static BufferedImage bullet;
public static BufferedImage airplaneboom;
private int state=0;//0表示游戏前,1表示游戏中,2表示暂停,3表示游戏结束
//定义对象
hero hero;
Admin admin;
private List<Bee> bees;
private List<Plane> planes;
private List<Bullet> bullets;
static {
try {
background=ImageIO.read(new File("img/background.png"));
beeimg=ImageIO.read(new File("img/bee.png"));
heroimg=ImageIO.read(new File("img/hero1.png"));
heroimg2=ImageIO.read(new File("img/hero2.png"));
airplane=ImageIO.read(new File("img/airplane.png"));
start=ImageIO.read(new File("img/start.png"));
gameover=ImageIO.read(new File("img/gameover.png"));
pause=ImageIO.read(new File("img/pause.png"));
bullet=ImageIO.read(new File("img/bullet.png"));
airplaneboom=ImageIO.read(new File("img/airplane_boom.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
//构造器
public GameUi(Admin admin)
{
this.admin = admin;
playMusic("bg");
JFrame jFrame=new JFrame();
hero=new hero();
bees=new ArrayList<Bee>();
bullets=new ArrayList<Bullet>();
planes=new ArrayList<Plane>();
jFrame.setSize(GameUi.width, GameUi.height);
jFrame.setTitle("飞机大战");
jFrame.setLocationRelativeTo(null);//居中
jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);
jFrame.setResizable(false);//不可改变窗体大小
addListener();//调用监听器
addTimer();//调用定时器
jFrame.add(this);
jFrame.setVisible(true);
}
//画笔
public void paint(Graphics g) {
super.paint(g);
g.drawImage(background,0,0,null );//背景图
g.setFont(new Font("宋体", Font.BOLD, 20));
g.drawString("得分:", 20, 60);//画文字
g.drawString(Integer.toString(hero.score), 80, 60);//画文字
g.drawString("生命:", 20, 90);//画文字
g.drawString(Integer.toString(hero.life), 80, 90);
switch(state)
{
case 0:
g.drawImage(start,0,0,null );
break;
case 1:
g.drawImage(start,0,0,null );
break;
case 2:
g.drawImage(pause,0,0,null );
break;
case 3:
g.drawImage(gameover,0,0,null );
break;
}
g.drawImage(hero.image,hero.x,hero.y,null );
for(int i=0;i<planes.size();i++)
{
Plane plane=planes.get(i);
g.drawImage(plane.image,plane.x,plane.y,null);
}
for(int w=0;w<bees.size();w++)
{
Bee bee=bees.get(w);
g.drawImage(bee.image,bee.x,bee.y,null);
}
for(int w=0;w<bullets.size();w++)
{
Bullet bullet=bullets.get(w);
g.drawImage(bullet.image,bullet.x,bullet.y,null);
}
}
//监听
public void addListener() {
MouseAdapter mouseAdapter=new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
switch (state) {
case 0:
state=1;//游戏开始前点击状态变成游戏中
break;
case 1:
state=2;
break;
case 2:
state=1;
break;
case 3:
state=1;
break;
}
repaint();
}
//hero跟着鼠标走
public void mouseMoved(MouseEvent e)
{
if(state==1)
{
hero.x=e.getX()-hero.width/2;
hero.y=e.getY()-hero.heigth/2;
}
repaint();
}
//当鼠标移出暂停
public void mouseExited(MouseEvent e)
{
if(state==1)
{
state=2;
repaint();
}
}
};
this.addMouseListener(mouseAdapter);
this.addMouseMotionListener(mouseAdapter);
}
//定时器
public void addTimer() {
Timer t=new Timer();
TimerTask task=new TimerTask() {
@Override
public void run() {
//游戏中
if(state==1) {
hero.move();
createPlane();
createbee();
createBullet();
for(int i=0;i<planes.size();i++)
{
Plane plane=planes.get(i);
plane.move();
}
for(int i=0;i<bees.size();i++)
{
Bee bee=bees.get(i);
bee.move();
}
for(int i=0;i<bullets.size();i++)
{
Bullet bullet=bullets.get(i);
bullet.move();
}
boom();
boombee();
boomhero();
isoutplane();
isoutbee();
isoutbullet();
repaint();
}
}
};
t.schedule(task,10,10);
}
//打到敌机的方法
public static boolean flag=false;
private void boom() {
List<Bullet> removeBullets=new ArrayList<>();
List<Plane> removePlanes=new ArrayList<>();
for(int i=0;i<bullets.size();i++)
{
Bullet bullet=bullets.get(i);
for(int j=0;j<planes.size();j++) {
Plane plane=planes.get(j);
if(bullet.x+bullet.width>=plane.x&&bullet.x<=plane.x+plane.width)//打到了
{
if(bullet.y<=plane.y+plane.heigth&&bullet.y+bullet.heigth>=plane.y)
{
hero.score++;
plane.image=plane.images[1];
repaint();
try
{
Thread.currentThread().sleep(50);
}catch(InterruptedException e)
{
e.printStackTrace();
}
removeBullets.add(bullet);
removePlanes.add(plane);
break;
}
}
}
}
bullets.removeAll(removeBullets);
planes.removeAll(removePlanes);
}
//打到蜜蜂的方法
private void boombee() {
List<Bullet> removeBullets=new ArrayList<>();
List<Bee> removebees=new ArrayList<>();
for(int i=0;i<bullets.size();i++)
{
Bullet bullet=bullets.get(i);
for(int j=0;j<bees.size();j++) {
Bee bee=bees.get(j);
if(bullet.x+bullet.width>=bee.x&&bullet.x<=bee.x+bee.width)//打到了
{
if(bullet.y<=bee.y+bee.heigth&&bullet.y+bullet.heigth>=bee.y)
{
removeBullets.add(bullet);
removebees.add(bee);
if(bee.getAwardType()==0)
{
hero.life++;
}
else {
hero.setFire(20);
}
break;
}
}
}
}
bullets.removeAll(removeBullets);
bees.removeAll(removebees);
}
//hero被敌方碰到时结束游戏
private void boomhero() {
List<Bullet> removeBullets=new ArrayList<>();
List<Plane> removePlanes=new ArrayList<>();
for(int j=0;j<planes.size();j++) {
Plane plane=planes.get(j);
if(hero.x+hero.width>=plane.x+25&&hero.x+25<=plane.x+plane.width)//打到了
{
if(hero.y+25<=plane.y+plane.heigth&&hero.y+hero.heigth>=plane.y+25)
{
if(hero.life==1)
{
plane.image=plane.images[1];
repaint();
try
{
Thread.currentThread().sleep(50);
}catch(InterruptedException e)
{
e.printStackTrace();
}
try {
SaveData saveData = new SaveData();
saveData.setAdmin(this.admin);
saveData.setHero(this.hero);
saveData.addRanklist();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
//System.out.println(admin.getID());
state=3;
hero.life=3;
hero.score=0;
}
else {
hero.life--;
plane.image=plane.images[1];
repaint();
try
{
Thread.currentThread().sleep(50);
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
removePlanes.add(plane);
}
}
planes.removeAll(removePlanes);
}
}
//产生敌机的方法
int planeindex=0;//控制敌机少产生
private void createPlane() {
if(planeindex%100==0)
{
Plane plane=new Plane();
planes.add(plane);
}
planeindex++;
}
//产生蜜蜂的方法
int beeindex=1;//控制蜜蜂少产生
private void createbee() {
if(beeindex%1000==0)
{
Bee bee=new Bee();
bees.add(bee);
}
beeindex++;
}
//产生子弹的方法
int bulletindex=0;//控制子弹少产生
private void createBullet() {
if(bulletindex%100==0)
{
if(hero.getFire()==0)//单发
{
Bullet bullet=new Bullet(hero);
bullets.add(bullet);
}
else {//双发
Bullet b1=new Bullet(hero);
b1.x=hero.x+hero.width/4;//子弹位置
Bullet b2=new Bullet(hero);
b2.x=hero.x+hero.width/4*3;//子弹位置
bullets.add(b1);
bullets.add(b2);
hero.setFire(hero.getFire()-2);//火力值减少
}
}
bulletindex++;
}
//音频方法
public void playMusic(String s)
{
File file=new File("sound/" + s + ".wav");
try {
URL url =file.toURI().toURL();
AudioClip newAudioClip=Applet.newAudioClip(url);
newAudioClip.play();
} catch (Exception e) {
e.printStackTrace();
}
}
//处理敌机出界方法
public void isoutplane()
{
List<Plane> removePlanes=new ArrayList<>();
for(int j=0;j<planes.size();j++) {
Plane plane=planes.get(j);
if(plane.isOut())
{
removePlanes.add(plane);
break;
}
}
planes.removeAll(removePlanes);
}
//处理蜜蜂出界方法
public void isoutbee()
{
List<Bee> removeBees=new ArrayList<>();
for(int j=0;j<bees.size();j++) {
Bee bee=bees.get(j);
if(bee.isOut())
{
removeBees.add(bee);
break;
}
}
bees.removeAll(removeBees);
}
//处理子弹出界方法
public void isoutbullet(){
List<Bullet> removeBullets=new ArrayList<>();
for(int j=0;j<bullets.size();j++) {
Bullet bullet=bullets.get(j);
if(bullet.isOut())
{
removeBullets.add(bullet);
break;
}
}
bullets.removeAll(removeBullets);
}
//主方法
public static void main(String[] args) {
}
}
本文地址:https://blog.csdn.net/SatoKazuma/article/details/108571507
下一篇: 本地连接禁用/启用批处理脚本