太阳系模型_飞机游戏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

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

Andrej Karpathy最新采访:认知核心模型10亿参数就够了,AI会打破教育不公的僵局

夕小瑶科技说 原创  作者 | 海野 AI圈子的红人,AI大神Andrej Karpathy,曾是OpenAI联合创始人之一,特斯拉AI总监。上一次的动态是官宣创办一家名为 Eureka Labs 的人工智能+教育公司 ,宣布将长期致力于AI原生教育。 近日,Andrej Karpathy接受了No Priors(投资博客)的采访,与硅谷知名投资人 Sara Guo 和 Elad G