java弹幕小游戏1.0版本

java 弹幕小游戏的最初版本,供大家参考,具体内容如下

最近在学习javaSE,根据b站视频老师的讲解,也参考了他的代码,做了一个弹幕小游戏,也增添了一些自己的代码进去,因为只是最简单的游戏体,以后会慢慢做完整,所以如果有错误,或者代码不够整洁的话,可以帮我改正,谢谢啦。

父类

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);
 }
}

因为是初学,还有很多地方都不会,代码也写得不到位,我会慢慢学习并改正。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。