首页 技术 正文
技术 2022年11月14日
0 收藏 317 点赞 3,425 浏览 4368 个字

Java太阳系小游戏分析和源代码

-20150809

近期看了面向对象的一些知识。然后跟着老师的解说做了一个太阳系各行星绕太阳转的小游戏,来练习巩固一下近期学的知识:

用到知识点:类的继承、方法的重载与重写、多态、封装等

分析:

1.须要载入图片、绘图

2.建一个面板。主页面

3.行星类

效果图:

先看一下源代码结构图:

如今逐步分析各个类的功能:

1)工具类—–util包中

–Constant类   封装了游戏中用到的常量

–GameUtil类  封装了游戏的图片载入功能

–MyFrame类  封装了游戏面板的构造。用于各面板的父类

——之所以这样做。目的是为了封装数据。便于程序的扩充

Constant.java

package util;public class Constant {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;}

GameUtil.java

package util;import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;import javax.imageio.ImageIO;/**
* 工具类(载入图片)
* @author long
*
*/
public class GameUtil {private GameUtil(){ } //工具类通常将构造方法私有public static Image getImage(String path){
URL u = GameUtil.class.getClassLoader().getResource(path);
BufferedImage img = null;
try {
img = ImageIO.read(u);
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
}

MyFrame.java

package util;import javax.swing.JFrame;
import javax.swing.JPanel;/**
* 游戏面板的父类
* @author long
*
*/
public class MyFrame extends JPanel{/**
* 载入Frame的方法
*/
public void launchFrame(){
JFrame frame = new JFrame("MyGame");
frame.add(this);
frame.setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
frame.setAlwaysOnTop(true); // 设置其总在最上
frame.setLocationRelativeTo(null); // 设置窗口初始位置
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);new PaintThread().start();
}/**
* 定义一个重画窗口的线程类。是一个内部类
* @author dell
*
*/
class PaintThread extends Thread {public void run(){
while(true){
repaint();
try {
Thread.sleep(40); //1s = 1000ms
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}}public static void main(String[] args) {
new MyFrame().launchFrame();
}}

2)基本的事件处理类—solar包中

–Planet类   行星类继承至Star类

–SolarFrame类  游戏主面板类继承至MyFrame类

–Star类  星球类,各个星球的父类

–Test类  測试类。不须要说明

Planet.java

package solar;import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;import util.GameUtil;/**
* 行星类,继承至Star类
* @author long
*
*/
public class Planet extends Star{
//除了图片、坐标,行星沿着椭圆执行:长轴、短轴、移动速度、旋转角度。绕着某个star执行
double longAxis; //椭圆长轴
double shortAxis; //椭圆短轴
double speed; //飞行速度
double degree; //旋转角度
Star center; //环绕行星public void draw(Graphics g){
//g.drawImage(img, (int)x, (int)y, null);
super.draw(g);
drawTrace(g);
move();
}public void drawTrace(Graphics g){
double traceX,traceY,traceWidth,traceHeight;
traceX = (center.x+center.w/2)-longAxis;
traceY = (center.y+center.h/2)-shortAxis;
traceWidth = 2*longAxis;
traceHeight = 2*shortAxis;Color c = g.getColor();
g.setColor(Color.blue);
g.drawOval((int)traceX, (int)traceY, (int)traceWidth, (int)traceHeight);
g.setColor(c);
}public void move(){
//沿着椭圆轨迹飞行
x = center.x + longAxis * Math.cos(degree);
y = center.y + shortAxis * Math.sin(degree);
degree += speed;
}public Planet(Image img,double x,double y){
super(img,x,y);
}
public Planet(String imgpath,double x,double y){
super(imgpath,x,y);
}
public Planet( Star center,Image img,double longAxis,
double shortAxis,double speed) {
super();
this.x = (center.x+center.w/2) + longAxis;
this.y = (center.y+center.h/2) + shortAxis;
this.img = img;
this.longAxis = longAxis;
this.shortAxis = shortAxis;
this.speed = speed;
this.center = center;
}
public Planet( Star center,String imgPath,double longAxis,
double shortAxis,double speed) {
this(center,GameUtil.getImage(imgPath),longAxis,shortAxis,speed);
}}

SolarFrame.java

package solar;import java.awt.Graphics;
import java.awt.Image;import util.Constant;
import util.GameUtil;
import util.MyFrame;public class SolarFrame extends MyFrame{int width = Constant.GAME_WIDTH/2;
int height = Constant.GAME_HEIGHT/2;Image bg=GameUtil.getImage("images/bg.png");Star sun = new Star("images/sun.jpg",width,height);
Planet earth = new Planet(sun,"images/earth.png",100,60,0.1);
Planet mars = new Planet(sun,"images/mars.png",180,100,0.15);@Override
public void paint(Graphics g) {
g.drawImage(bg, 0, 0, null);
sun.draw(g);
earth.draw(g);
mars.draw(g);
}public static void main(String[] args) {
new SolarFrame().launchFrame();
}}

Star.java

package solar;import java.awt.Graphics;
import java.awt.Image;import util.GameUtil;public class Star {
public Image img;
public double x,y;
int w,h;public void draw(Graphics g){
g.drawImage(img, (int)x, (int)y, null);
}public Star(){
}
public Star(Image img){
this.img = img;
this.w = img.getWidth(null);
this.h = 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);
}}

———————————————————————————————–

总结:该小游戏对代码的封装处理的比較好,便于程序的扩充。体现了面向对象的强大,不同的功能封装在不同的类与方法中。把类的公共的部分封装在父类中,提高代码的重用性。前期各个类写的过程中会有各种小问题与细节,但处理完这些后,后期想扩充行星的个数就比較简单了,new一个行星对象,然后画的面板上就可以。面向对象水太深,这仅仅是初步小涉猎,仍需继续努力专研。!!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,022
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,773
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,851