您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容介绍了“java实现弹幕小游戏代码分享”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
父类
import java.awt.*;public class GameObject { //游戏物体的父类 Image img; double x,y; int speed = 3; int width,height; public void drawSelf(Graphics g){ g.drawImage(img,(int)x,(int)y,null); } public GameObject(Image img, double x, double y, int X_speed,int Y_speed, int width, int height) { this.img = img; this.x = x; this.y = y; this.speed = speed; this.width = width; this.height = height; } public GameObject(Image img, double x, double y) { this.img = img; this.x = x; this.y = y; } public GameObject(){ } //返回物体所在的矩形,便于后续的碰撞检测 public Rectangle getRect(){ return new Rectangle((int)x,(int)y,width,height); }}
弹幕类
import com.sun.xml.internal.ws.model.wsdl.WSDLPortProperties;import java.awt.*;public class Shell extends GameObject { double degree; public Shell() { x = 200; y = 200; width = 10; height = 10; speed = 2; speed = 2; //弧度 degree = Math.random()*Math.PI*2; } public void draw(Graphics g){ Color c = g.getColor(); g.setColor(Color.YELLOW); g.fillOval((int)x,(int)y,width,height); //炮弹沿着任意角度去飞 x+=speed*Math.cos(degree); y+=speed*Math.sin(degree); if(x<0 || x> Constant.GAME_WIDTH-width){ degree = Math.PI - degree; } if(y<40 || y> Constant.GAME_HEIGHT-height){ degree = -degree; } g.setColor(c); }}
飞机类
import java.awt.*;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;public class Plane extends GameObject{ boolean left,right,up,down; boolean live = true; //飞机是否活着 boolean leftflag,rightflag,upflag,downflag;//这些布尔值判断当碰到上下左右的边框时的状态 //如果活着画出来 public void drawSelf(Graphics g){ if(live) { g.drawImage(img, (int) x, (int) y, null); //根据方向进行不同的移动 if (left) { x -= speed; } if (right) { x += speed; } if (up) { y -= speed; } if (down) { y += speed; } } } public Plane(Image img,double x,double y) { this.img = img; this.x = x; this.y = y; this.speed = 3; this.width = img.getWidth(null); this.height = img.getHeight(null); } //按下某个键,增加相应的方向 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; } } //按下某个键,取消相应的方向 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; } }}
爆炸类
import java.awt.*;//爆炸类public class Explode { double x,y; //爆炸的位置 static Image[] imgs = new Image[16]; static { for(int i=0;i<16;i++){ imgs[i] = PlayGameFrame.GameUtil.getImage("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){ this.x = x; this.y = y; }}
常量类
public class Constant { public static final int GAME_WIDTH = 500; public static final int GAME_HEIGHT = 500;}
主类
import jdk.internal.cmm.SystemResourcePressureImpl;import javax.imageio.ImageIO;import javax.swing.*;import java.awt.*;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.image.BufferedImage;import java.io.IOException;import java.net.URL;import java.awt.Rectangle;import java.util.Date;/** *飞机游戏的主窗口 */class PlayGameFrame extends Frame { Image planeImg= GameUtil.getImage("image/plane.png"); Image nightsky = GameUtil.getImage("image/nightsky.jpg"); //定义飞机的坐标 Plane plane = new Plane(planeImg,250,250); //定义炮弹 Shell[] shells = new Shell[50]; Explode boom; Date startTime = new Date(); Date endTime; int period;//游戏持续的时间 /** *画图 */ @Override public void paint(Graphics g) { //自动被调用,g这个变量相当于一个画笔 Color c = g.getColor(); super.paint(g);//防止黑屏 g.drawImage(nightsky,0,0,null); plane.drawSelf(g);//画飞机 if(plane.x <= Constant.GAME_WIDTH-plane.width && plane.x >= 30 && plane.y <= Constant.GAME_HEIGHT-30 && plane.y >= 0) plane.speed = 10; else plane.speed = -plane.speed; //画出所有的炮弹 for(int i=0;i<shells.length;i++) { shells[i].draw(g); //检测炮弹是否和飞机相撞 boolean peng = shells[i].getRect().intersects(plane.getRect()); if(peng){ plane.live = false; if(boom == null) { //如果不加这个if,飞机在碰到炮弹的时候会不停地生成爆炸类 boom = new Explode(plane.x, plane.y); endTime = new Date(); period = (int)((endTime.getTime() - startTime.getTime())/1000); } boom.draw(g); } //计时功能 if(!plane.live) { g.setColor(Color.red); Font f = new Font("宋体",Font.BOLD,50); g.setFont(f); g.drawString("时间:" + period + "秒", (int) plane.x, (int) plane.y); } } g.setColor(c); /*Color c=g.getColor(); //保存先颜色,等使用完之后要复原为最初的颜色 g.setColor(Color.BLUE); g.drawLine(100,100,300,300);//直线 g.drawRect(100 ,100 ,300 ,300);//矩形 g.drawOval(100,100,300,300);//椭圆 g.fillRect(100,100,40,40);//填充矩形 g.drawString("Ning",100 ,200);//写字符串 g.drawImage(ball,250,250,null); g.setColor(c); 随堂练习用,建立图片!! */ } //多线程,帮助我们反复的重画窗口 class PaintThread extends Thread{ @Override public void run() { while(true){ repaint(); //重画窗口 try { Thread.sleep(40);//重画次数 } catch (InterruptedException e) { e.printStackTrace(); } } } } //定义键盘监听的内部类 class KeyMonitor extends KeyAdapter{ @Override public void keyPressed(KeyEvent e) { plane.addDirection(e); } @Override public void keyReleased(KeyEvent e) { plane.minusDirection(e); } } /** *初始化窗口 */ public void launchFrame() { this.setTitle("拉 拉 人");/*给自己的窗口起个标题*/ this.setVisible(true);/*默认窗口可见*/ this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);/*给窗口设定大小*/ this.setLocation(300,300);/*定义窗口的位置*/ /** *关闭动作 */ this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0);/*表示正常结束*/ } }); //启动重画窗口的线程 new PaintThread().start(); //给窗口增加键盘的监听 addKeyListener(new KeyMonitor()); //初始化50个炮弹 for(int i=0;i<shells.length;i++){ shells[i] = new Shell(); } } public static class GameUtil { // 工具类最好将构造器私有化。 private GameUtil() { } public static Image getImage(String path) { BufferedImage bi = null; try { URL u = GameUtil.class.getClassLoader().getResource(path); bi = ImageIO.read(u); } catch (IOException e) { e.printStackTrace(); } return bi; } } public static void main(String[] args){ PlayGameFrame f = new PlayGameFrame(); f.launchFrame(); } //添加双缓冲技术 private Image offScreenImage = null; public void update(Graphics g) { if (offScreenImage == null) offScreenImage = this.createImage(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);//这是游戏窗口的宽度和高度 Graphics gOff = offScreenImage.getGraphics(); paint(gOff); g.drawImage(offScreenImage, 0, 0, null); }}
“java实现弹幕小游戏代码分享”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。