当前位置: 移动技术网 > IT编程>开发语言>Java > Java太阳系小游戏分析和源码详解

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

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

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

分析:

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一个行星对象,然后画的面板上即可。面向对象水太深,这只是初步小涉猎,仍需继续努力专研!!!

以上就是java太阳系小游戏分析和源码详解,希望对大家学习java语言有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网