太阳系模型_飞机游戏JAVA084-097

2024-09-03 14:48

本文主要是介绍太阳系模型_飞机游戏JAVA084-097,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

来源:http://www.bjsxt.com/
1、S01E084-087太阳系模型实操

package com.test.util;/*** 游戏中关于窗口大小的常量*/
public class Constant {public static final int GAME_WIDTH = 500;public static final int GAME_HEIGHT = 500;
}
//
package com.test.util;import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;import javax.imageio.ImageIO;/*** 游戏开发中常用的工具类(比如:加载图片等方法)*/
public class GameUtil {private GameUtil() {}   //工具类通常会将构造方法私有,直接调用static方法public static Image getImage(String path) {URL url = GameUtil.class.getClassLoader().getResource(path);BufferedImage img = null;try {img = ImageIO.read(url);} catch (IOException e) {e.printStackTrace();}return img;}
}

package com.test.util;import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;/*** 游戏窗口类*/
public class MyFrame extends Frame {    //GUI编程:AWT,SWING等//参考http://www.cnblogs.com/S-E-P/archive/2010/01/27/2045076.html@Overridepublic void update(Graphics g) {//覆盖update方法,截取默认的调用过程//创建图形缓冲区Image imageBuffer = createImage(this.getWidth(),this.getHeight());Graphics graImage = imageBuffer.getGraphics();//获取图形缓冲区的图形上下文paint(graImage);//用paint方法中编写的绘图过程对图形缓冲区绘图graImage.dispose();//释放图形上下文资源g.drawImage(imageBuffer,0,0,this);//将图形缓冲区绘制到屏幕上}/*** 加载窗口*/public void launchFrame() {setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);//窗口大小setLocation(100,100);//窗口位置setVisible(true);//默认不可见new PaintThread().start();//启动重画线程addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {//点击右上角"X"退出System.exit(0);}});}/*** 定义一个重画窗口的线程类*/class PaintThread extends Thread {@Overridepublic void run() {while(true) {repaint();try {Thread.sleep(40);} catch (InterruptedException e) {e.printStackTrace();}}}}
}

package com.test.plane;import java.awt.Color;
import java.awt.Graphics;import com.test.util.Constant;public class Bullet extends GameObject{public Bullet(){degree = Math.random() * Math.PI * 2;x = Constant.GAME_WIDTH/2;y = Constant.GAME_HEIGHT/2;width = 10;height =10;}public void draw(Graphics g){Color c = g.getColor();g.setColor(Color.YELLOW);g.fillOval((int)x, (int)y, (int)width, (int)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 < 30 || y > Constant.GAME_HEIGHT - height){degree = -degree;}g.setColor(c);}
}
/
package com.test.plane;import java.awt.Graphics;
import java.awt.Image;import com.test.util.GameUtil;/*** 爆炸类*/
public class Explode {double x,y;int count;static Image[] imags = new Image[16];static{for (int i = 0; i < 16; i++) {imags[i] = GameUtil.getImage("images/explode/e" + (i + 1) + ".gif");imags[i].getWidth(null);//懒加载,要加上此代码才会马上加载图片}}public Explode(double x,double y){this.x = x;this.y = y;}public void draw(Graphics g){if(count < 16){g.drawImage(imags[count], (int)x, (int)y, null);count++;}}
}
///
package com.test.plane;import java.awt.Image;
import java.awt.Rectangle;public class GameObject {Image img;double x,y;int speed = 3;double width,height;double degree;public Rectangle getRect(){//碰撞检测中用到的矩形框return new Rectangle((int)x, (int)y, (int)width, (int)height);}
}
///
package com.test.plane;import java.awt.Graphics;
import java.awt.event.KeyEvent;import com.test.util.GameUtil;public class Plane extends GameObject{private boolean left,right,up,down; private boolean live = true;public Plane(){}public Plane(String imgpath, double x, double y) {this.img = GameUtil.getImage(imgpath);this.width = img.getWidth(null);this.height = img.getHeight(null);this.x = x;this.y = y;}public boolean isLive() {return live;}public void setLive(boolean live) {this.live = live;}/*** 画图和移动* @param g*/public void draw(Graphics g){if(live){g.drawImage(img, (int)x, (int)y, null);move();}}/*** 移动图片*/public void move(){if(left){x -= speed;}if(right){x += speed;}if(up){y -= speed;}if(down){y += speed;}}/*** 根据按键设置相应方向的布尔值为ture* @param e*/public void setDirectionTrue(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;}}/*** 根据按键设置相应方向的布尔值为false* @param e*/public void setDirectionFalse(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 com.test.plane;import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Date;import com.test.util.GameUtil;
import com.test.util.MyFrame;public class PlaneGameFrame extends MyFrame{Image bg = GameUtil.getImage("images/bg.jpg");Plane p = new Plane("images/plane.png", 50, 50);ArrayList bulletList = new ArrayList();//暂不加泛型Date startTime,endTime;Explode bao;public void paint(Graphics g){g.drawImage(bg, 0, 0, null);p.draw(g);for (int i = 0; i < bulletList.size(); i++) {Bullet b = (Bullet) bulletList.get(i);b.draw(g);//检测跟飞机的碰撞boolean peng = b.getRect().intersects(p.getRect());if (peng){p.setLive(false);//飞机死掉if(bao == null){endTime = new Date();//游戏结束bao = new Explode(p.x,p.y);//最好用容器来处理,以后再优化}bao.draw(g);break;//碰撞一下}}if(!p.isLive()){int period = (int)((endTime.getTime() - startTime.getTime())/1000);//获取毫秒时间/1000printInfo(g,"时间:" + period + "秒",20,120,150,Color.WHITE);switch (period/10) {case 0://不足10秒case 1:printInfo(g,"菜鸟!",50,100,100,Color.WHITE);              break;case 2:printInfo(g,"小鸟!",50,100,100,Color.WHITE);      case 3:printInfo(g,"大鸟!",50,100,100,Color.YELLOW); case 4:printInfo(g,"鸟王",50,100,100,Color.YELLOW);  default:printInfo(g,"鸟人!",50,100,100,Color.WHITE);  break;}}}/*** 飞机死掉,在窗口上打印信息* @param g* @param str* @param size* @param x* @param y* @param color*/public void printInfo(Graphics g,String str,int size,int x,int y,Color color){Font font = new Font("宋体", Font.BOLD, size);g.setFont(font);Color c = g.getColor();g.setColor(color);      g.drawString(str, x, y);g.setColor(c);}//定义为内部类,方便使用外部类的普通属性class KeyMonitor extends KeyAdapter{@Overridepublic void keyPressed(KeyEvent e) {//System.out.println("按下:" + e.getKeyCode());p.setDirectionTrue(e);}@Overridepublic void keyReleased(KeyEvent e) {//System.out.println("释放:" + e.getKeyCode());p.setDirectionFalse(e);}}@Overridepublic void launchFrame() {super.launchFrame();addKeyListener(new KeyMonitor());//增加键盘的监听//生成一堆字弹for (int i = 0; i < 25; i++) {Bullet b = new Bullet();bulletList.add(b);}startTime = new Date();//游戏开始}public static void main(String[] args) {new PlaneGameFrame().launchFrame();}
}

2、S01E088-097飞机游戏实操

package com.test.util;/*** 游戏中关于窗口大小的常量*/
public class Constant {public static final int GAME_WIDTH = 800;public static final int GAME_HEIGHT = 600;
}

package com.test.util;import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;import javax.imageio.ImageIO;/*** 游戏开发中常用的工具类(比如:加载图片等方法)*/
public class GameUtil {private GameUtil() {}   //工具类通常会将构造方法私有,直接调用static方法public static Image getImage(String path) {URL url = GameUtil.class.getClassLoader().getResource(path);BufferedImage img = null;try {img = ImageIO.read(url);} catch (IOException e) {e.printStackTrace();}return img;}
}
//
package com.test.util;import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;/*** 游戏窗口类*/
public class MyFrame extends Frame {    //GUI编程:AWT,SWING等//参考http://www.cnblogs.com/S-E-P/archive/2010/01/27/2045076.html@Overridepublic void update(Graphics g) {//覆盖update方法,截取默认的调用过程//创建图形缓冲区Image imageBuffer = createImage(this.getWidth(),this.getHeight());Graphics graImage = imageBuffer.getGraphics();//获取图形缓冲区的图形上下文paint(graImage);//用paint方法中编写的绘图过程对图形缓冲区绘图graImage.dispose();//释放图形上下文资源g.drawImage(imageBuffer,0,0,this);//将图形缓冲区绘制到屏幕上}/*** 加载窗口*/public void launchFrame() {setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);//窗口大小setLocation(100,100);//窗口位置setVisible(true);//默认不可见new PaintThread().start();//启动重画线程addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {//点击右上角"X"退出System.exit(0);}});}/*** 定义一个重画窗口的线程类*/class PaintThread extends Thread {@Overridepublic void run() {while(true) {repaint();try {Thread.sleep(40);} catch (InterruptedException e) {e.printStackTrace();}}}}
}

package com.test.solar;import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;import com.test.util.GameUtil;public class Planet extends Star{//除了图片、坐标。行星还沿着某个椭圆运行:长轴、短轴、速度(角度的增量)、角度。绕着某个Stars飞。double longAxis;//椭圆的长轴double shortAxis;//椭圆的短轴double speed;//飞行的速度double degree;Star center;boolean isSatellite;@Overridepublic void draw(Graphics g) {super.draw(g);//调用父类方法,优化代码结构move();if(!isSatellite){drawTrace(g);}}public void move(){//沿着椭圆轨迹飞行x = (center.x + center.width/2) + longAxis * Math.cos(degree) - this.width/2;y = (center.y + center.height/2) + shortAxis * Math.sin(degree) - this.height/2;degree += speed;}public void drawTrace(Graphics g){double ovalX,ovalY,ovalWidth,ovalHeight;ovalX = (center.x + center.width/2) - longAxis;ovalY = (center.y + center.height/2) -shortAxis;ovalWidth = longAxis * 2;ovalHeight = shortAxis * 2;Color c = g.getColor();g.setColor(Color.blue);g.drawOval((int)ovalX, (int)ovalY, (int)ovalWidth, (int)ovalHeight);g.setColor(c);}//一开始都放在同一水平线上,通过Star可计算x,ypublic Planet(Star center, String imgpath, double longAxis,double shortAxis, double speed) {super(GameUtil.getImage(imgpath));this.center = center;this.x = (center.x + center.width/2) + longAxis - this.width/2;//计算出xthis.y = (center.y + center.height/2) - this.height/2;this.longAxis = longAxis;this.shortAxis = shortAxis;this.speed = speed;}//一开始都放在同一水平线上,通过Star可计算x,ypublic Planet(Star center, String imgpath, double longAxis,double shortAxis, double speed,boolean isSatellite) {this(center, imgpath, longAxis, shortAxis, speed);this.isSatellite = isSatellite;}public Planet(Image img, double x, double y) {super(img, x, y);}public Planet(String imgpath, double x, double y) {super(imgpath, x, y);}
}
///
package com.test.solar;import java.awt.Graphics;
import java.awt.Image;import com.test.util.GameUtil;public class Star {Image img;double x,y;int width,height;public void draw(Graphics g){g.drawImage(img, (int)x, (int)y, null);}public Star(){} public Star(Image img){this.img = img;this.width = img.getWidth(null);this.height = img.getHeight(null);}public Star(Image img,double x,double y){//传图片this(img);//构造器调用,优化代码结构this.x = x;this.y = y;}public Star(String imgpath,double x,double y){//传路径this(GameUtil.getImage(imgpath),x,y);//构造器调用,优化代码结构}
}
/
package com.test.solar;import java.awt.Graphics;
import java.awt.Image;import com.test.util.Constant;
import com.test.util.GameUtil;
import com.test.util.MyFrame;/*** 太阳系的主窗口,以后再研究窗口闪烁问题*/
public class SolarFrame extends MyFrame{Image bg = GameUtil.getImage("images/bg.jpg");Star sun = new Star("images/sun.jpg", Constant.GAME_WIDTH/2, Constant.GAME_HEIGHT/2);Planet earth = new Planet(sun, "images/earth.jpg", 150, 100, 0.1);Planet moon = new Planet(earth, "images/moon.jpg", 30, 20, 0.2,true);Planet mars = new Planet(sun, "images/mars.jpg", 200, 130, 0.2);public void paint(Graphics g){g.drawImage(bg, 0, 0, null);sun.draw(g);earth.draw(g);moon.draw(g);mars.draw(g);}public static void main(String[] args){new SolarFrame().launchFrame();}
}

这篇关于太阳系模型_飞机游戏JAVA084-097的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1133265

相关文章

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

Spring Cloud Hystrix原理与注意事项小结

《SpringCloudHystrix原理与注意事项小结》本文介绍了Hystrix的基本概念、工作原理以及其在实际开发中的应用方式,通过对Hystrix的深入学习,开发者可以在分布式系统中实现精细... 目录一、Spring Cloud Hystrix概述和设计目标(一)Spring Cloud Hystr

Spring Boot整合消息队列RabbitMQ的实现示例

《SpringBoot整合消息队列RabbitMQ的实现示例》本文主要介绍了SpringBoot整合消息队列RabbitMQ的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录RabbitMQ 简介与安装1. RabbitMQ 简介2. RabbitMQ 安装Spring

springMVC返回Http响应的实现

《springMVC返回Http响应的实现》本文主要介绍了在SpringBoot中使用@Controller、@ResponseBody和@RestController注解进行HTTP响应返回的方法,... 目录一、返回页面二、@Controller和@ResponseBody与RestController

JAVA集成本地部署的DeepSeek的图文教程

《JAVA集成本地部署的DeepSeek的图文教程》本文主要介绍了JAVA集成本地部署的DeepSeek的图文教程,包含配置环境变量及下载DeepSeek-R1模型并启动,具有一定的参考价值,感兴趣的... 目录一、下载部署DeepSeek1.下载ollama2.下载DeepSeek-R1模型并启动 二、J

springboot rocketmq配置生产者和消息者的步骤

《springbootrocketmq配置生产者和消息者的步骤》本文介绍了如何在SpringBoot中集成RocketMQ,包括添加依赖、配置application.yml、创建生产者和消费者,并展... 目录1. 添加依赖2. 配置application.yml3. 创建生产者4. 创建消费者5. 使用在

Spring Retry 实现乐观锁重试实践记录

《SpringRetry实现乐观锁重试实践记录》本文介绍了在秒杀商品SKU表中使用乐观锁和MybatisPlus配置乐观锁的方法,并分析了测试环境和生产环境的隔离级别对乐观锁的影响,通过简单验证,... 目录一、场景分析 二、简单验证 2.1、可重复读 2.2、读已提交 三、最佳实践 3.1、配置重试模板

Spring中@Lazy注解的使用技巧与实例解析

《Spring中@Lazy注解的使用技巧与实例解析》@Lazy注解在Spring框架中用于延迟Bean的初始化,优化应用启动性能,它不仅适用于@Bean和@Component,还可以用于注入点,通过将... 目录一、@Lazy注解的作用(一)延迟Bean的初始化(二)与@Autowired结合使用二、实例解

SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)

《SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)》本文介绍了如何在SpringBoot项目中使用Jasypt对application.yml文件中的敏感信息(如数... 目录SpringBoot使用Jasypt对YML文件配置内容进行加密(例:数据库密码加密)前言一、J

Java中有什么工具可以进行代码反编译详解

《Java中有什么工具可以进行代码反编译详解》:本文主要介绍Java中有什么工具可以进行代码反编译的相关资,料,包括JD-GUI、CFR、Procyon、Fernflower、Javap、Byte... 目录1.JD-GUI2.CFR3.Procyon Decompiler4.Fernflower5.Jav