飞机小游戏具体代码

阅读: 评论:0

飞机小游戏具体代码

飞机小游戏具体代码

飞机小游戏

实现类

  • Constant:用于存放不可变的参数
  • Explode:爆炸类
  • GameObject:所有实体的父类
  • GameUtil:加载图片的工具类
  • MyGameFrame:主体
  • Plane:飞机类
  • Shell:飞机类

图片



代码实现

不可更改的游戏数据:

package game;/*** 存放不可更改的游戏数据* * @author 浅墨**/
public class Constant {public static final int GAME_WIDTH = 500; // 游戏界面宽度public static final int GAME_HEIGHT = 500; // 游戏界面高度public static final int explode_x = 115; // 游戏结束后时间显示的位置xpublic static final int explode_y = 200; // 游戏结束后时间显示的位置y
}

读取图片:

package game;
/*** 读取图片*/
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.URL;import javax.imageio.ImageIO;public class GameUtil {//工具类私有化public GameUtil() {}//读取图片public static Image getImage(String path) {BufferedImage bi=null;try {URL u=ClassLoader().getResource(path);bi&#ad(u);} catch (IOException e) {e.printStackTrace();}return bi;}
}

游戏中所有的实体的父类:

package game;import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;/*** 游戏中所有物体的父类* @author 浅墨**/
public class GameObject {private Image img;	//图片private int speed;	//移动速度private double x;	//x坐标private double y;	//y坐标private int width;	//宽度private int height;	//高度//多次画出图片位置public void drawSelf(Graphics g) {g.drawImage(img, (int)x,(int)y, null);}/*** 返回物体所在的矩形,便于后续的碰撞检测* @return*/public Rectangle getRect() {return new Rectangle((int)x,(int)y,width,height);}public GameObject(Image img, int speed, double x, double y, int width, int height) {super();this.img = img;this.speed = speed;this.x = x;this.y = y;this.width = width;this.height = height;}public GameObject(Image img, double x, double y) {super();this.img = img;this.x = x;this.y = y;}public GameObject() {super();}public Image getImg() {return img;}public void setImg(Image img) {this.img = img;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}public double getX() {return x;}public void setX(double x) {this.x = x;}public double getY() {return y;}public void setY(double y) {this.y = y;}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}}

炮弹:

package game;import java.awt.Color;
import java.awt.Graphics;/*** 炮弹类* * @author 浅墨**/
public class Shell extends GameObject {double degree; // 移动的弧度public Shell() {setX(200);setY(200);setWidth(10);setHeight(10);setSpeed(2);degree = Math.random() * Math.PI * 2; // 生成[0,2π)之间的随机数}public void draw(Graphics g) {Color c = g.getColor();g.setColor(Color.YELLOW);// 填充一个点(炮弹)g.fillOval((int) getX(), (int) getY(), getWidth(), getHeight());// 改变炮弹的飞行角度setX(getX() + getSpeed() * s(degree));setY(getY() + getSpeed() * Math.sin(degree));// 炮弹碰到边界反弹(当碰到边界时,沿着碰撞的方向翻转,实现改变方向)if (getX() < 0 || getX() > Constant.GAME_WIDTH - getWidth()) {degree = Math.PI - degree;}if (getY() < 30 || getY() > Constant.GAME_HEIGHT - getHeight()) {degree = -degree;}g.setColor(c);}
}

飞机:

package game;import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;/*** 飞机类* * @author 浅墨**/
public class Plane extends GameObject {boolean left;boolean up;boolean right;boolean down;boolean live=true;/*** 重写画笔,持续刷新位置*/@Overridepublic void drawSelf(Graphics g) {if(live) {g.drawImage(getImg(), (int) getX(), (int) getY(), null);if(left&&getX()>0) {setX(getX()-getSpeed());}if(right&&getX()<480) {setX(getX()+getSpeed());}if(up&&getY()>30) {setY(getY()-getSpeed());}if(down&&getY()<470) {setY(getY()+getSpeed());}		}}public Plane(Image img, double x, double y) {this.setImg(img);this.setX(x);this.setY(y);setSpeed(3);	//给移动速度赋初值
//		this.Height(null));
//		this.Width(null));this.setHeight(10);this.setWidth(10);}//键盘按下某个键,则增加某个方向移动public void addDirection(KeyEvent e) {switch (e.getKeyCode()) {case KeyEvent.VK_LEFT:left = true;break;case KeyEvent.VK_UP:up = true;break;case KeyEvent.VK_RIGHT:right = true;break;case KeyEvent.VK_DOWN:down = true;break;default:break;}}//键盘按下某个键,则取消某个方向移动public void minusDirection(KeyEvent e) {switch (e.getKeyCode()) {case KeyEvent.VK_LEFT:left = false;break;case KeyEvent.VK_UP:up = false;break;case KeyEvent.VK_RIGHT:right = false;break;case KeyEvent.VK_DOWN:down = false;break;default:break;}}}

爆炸:

package game;import java.awt.Graphics;
import java.awt.Image;/*** 爆炸类* * @author 浅墨**/
public class Explode {private double x; // x地址private double y; // y地址static Image[] imgs = new Image[16];static {for (int i = 0; i < 16; i++) {imgs[i] = Image("explode/e" + (i + 1) + ".gif");imgs[i].getWidth(null);}}int count;// 计数public void draw(Graphics g) {if (count <= 15) {g.drawImage(imgs[count], (int) x, (int) y, null);count++;}}public Explode(double x, double y) {super();this.x = x;this.y = y;}}

主窗口:

package game;import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import urrent.TimeUnit;public class MyGameFrame extends Frame {/*** 加载图片*/Image fj = Image("image/fj.jpg");Image bj = Image("image/bj.jpg");/*** 制作飞机,爆炸效果,炮弹*/Plane plane = new Plane(fj, 250, 250);Explode boom;List<Shell> shellList = new ArrayList<>();	// 利用List存放多个炮弹/*** 计算游玩的时间*/Date start=new Date();	//游戏开始时间Date end;int time;	/*** 画出飞机,爆炸效果,炸弹*/public void paint(Graphics g) { // 自动被调用,g相当于画笔Color c&#Color();g.drawImage(bj, 0, 0, null);plane.drawSelf(g); // 持续刷新飞机位置// 画出所有炮弹for (int i = 0; i < shellList.size(); i++) {Shell b = (i);b.draw(g);// 将飞机和炮弹矩形化,并调用intersects方法判断矩形是否相交,来实现碰撞检测if ((i).getRect().Rect())) {plane.live = false;if (boom == null) {boom = new X(), Y());end=new Date();time=(int)(Time()-Time())/1000;}boom.draw(g);}/*** 显示游玩时间*/if(!plane.live) {g.d);Font f=new Font("宋体",Font.BOLD,50);g.setFont(f);g.drawString("生存时间:"+time+"秒", plode_x, plode_y);}g.setColor(c);}}/*** 定义键盘监听的内部类,实现飞机移动* @author 浅墨*/class KeyListenter extends KeyAdapter {@Overridepublic void keyPressed(KeyEvent e) {plane.addDirection(e);}@Overridepublic void keyReleased(KeyEvent e) {plane.minusDirection(e);}}/*** 双缓冲解决闪烁问题*/private Image offScreenImage = null;public void update(Graphics g) {if (offScreenImage == null) {offScreenImage = ateImage(Constant.GAME_HEIGHT, Constant.GAME_WIDTH);}Graphics gOff = Graphics();paint(gOff);g.drawImage(offScreenImage, 0, 0, null);}/*** 初始化窗口*/public void launch() {this.setTitle("飞机小游戏");this.setVisible(true);this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);this.setLocation(300, 300);this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {it(0);super.windowClosing(e);}});new Thread(() -> {while (true) {// 内部类调用外部类JFrame的方法,实际是componet的方法repaint(); // 持续重画窗口,重新编辑这个组件try {TimeUnit.MILLISECONDS.sleep(25);} catch (InterruptedException e) {e.printStackTrace();}}}).start();// 添加键盘监听addKeyListener(new KeyListenter());// 增加50个炮弹for (int i = 0; i < 50; i++) {Shell b = new Shell();shellList.add(b);}}public static void main(String[] args) {MyGameFrame f = new MyGameFrame();f.launch();}
}

本文发布于:2024-01-30 23:41:07,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170662927023677.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:小游戏   飞机   代码
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23