• 82.游戏项目-椭圆轨迹的实现


     1 package test;
     2 
     3 import java.awt.Frame;
     4 import java.awt.event.WindowAdapter;
     5 import java.awt.event.WindowEvent;
     6 
     7 public class MyFrame extends Frame {
     8     
     9     public void launchFrame(){
    10         setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT); 
    11         setLocation(300,150);
    12         setVisible(true);
    13         
    14         new PaintThread().start();
    15         
    16         addWindowListener(new WindowAdapter(){
    17             public void windowClosing(WindowEvent e) {
    18                 System.exit(0);
    19             }    
    20         });
    21     }
    22     
    23     class PaintThread extends Thread{
    24         public void run(){
    25             while(true){
    26                 repaint();
    27                 try {
    28                     Thread.sleep(40);
    29                 } catch (InterruptedException e) {
    30                     e.printStackTrace();
    31                 }
    32             }
    33         }
    34     }
    35 
    36 }

    --------------继承 MyFrame的类 减少代码量

     1 package test;
     2 import java.awt.Color;
     3 import java.awt.Font;
     4 import java.awt.Frame;
     5 import java.awt.Graphics;
     6 import java.awt.Image;
     7 import java.awt.event.WindowAdapter;
     8 import java.awt.event.WindowEvent;
     9 /**
    10  * 椭圆轨迹的实现
    11  * @author Nicholas
    12  * 
    13  */
    14 public class GameFrame4 extends MyFrame {
    15     
    16     Image img = GameUtil.getImage("picture/test3.jpg");
    17     
    18     private double x = 100, y = 100;
    19     private double degree = 3.14/3;// [0,2pi];
    20     
    21     public void paint(Graphics g) {
    22         g.drawImage(img, (int)x, (int)y, null);
    23         x = 250 + 100* Math.cos(degree);
    24         y = 200 + 50* Math.sin(degree);
    25         degree += 0.1;
    26     }
    27     
    28     public static void main(String[] args) {
    29         GameFrame4 gf=new GameFrame4();
    30         gf.launchFrame();
    31     }    
    32 }

    补:

    经常用到的常量可以用一个类封装存储起来

     1 package test;
     2 /**
     3  * 游戏项目中用到的常量
     4  * @author Nicholas
     5  *
     6  */
     7 public class Constant {
     8     
     9     public static final int GAME_WIDTH=500;
    10     public static final int GAME_HEIGHT=500;
    11     
    12 }
  • 相关阅读:
    5个示例带你学习AngularJS
    快速入门:十分钟学会Python
    Memcache知识点梳理
    用Phaser实现Flappy Bird 游戏
    7 个顶级的 HTML5 Canvas 动画赏析
    避坑宝典:如何选择HTML5游戏引擎
    电商平台10大商业与盈利模式
    【英文版本】Android开源项目分类汇总
    Android精品开源整理
    Android开源项目汇总【转】
  • 原文地址:https://www.cnblogs.com/wydxry/p/8007012.html
Copyright © 2020-2023  润新知