您的位置:首页 > 编程语言 > Java开发

Java台球游戏2

2016-08-27 22:23 148 查看
效果:测试窗口物体沿着任意角度飞行。

/**

* 测试窗口物体沿着任意角度飞行。

* 最后,实现一个小的台球游戏。

* @author Administrator

*

*/

public class GameFrame02 extends Frame {

Image img = GameUtil.getImage(“images/sun.jpg”);

/**
* 加载窗口
*/
public void launchFrame(){
setSize(500, 500);
setLocation(100, 100);
setVisible(true);

new PaintThread().start();  //启动重画线程

addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}

});
}

private double x=100,y=100;
private double degree=3.14/3;    //[0,2pi]
private double speed = 10;
public void paint(Graphics g) {
g.drawImage(img, (int)x, (int)y, null);

if(speed>0){
speed -=0.05;
}else{
speed = 0;
}

x += speed*Math.cos(degree);
y += speed*Math.sin(degree);

if(y>500-30||y<30){
degree = -degree;
}

if(x<0||x>500-30){
degree = Math.PI-degree;
}

}
/**
* 定义一个重画窗口的线程类,是一个内部类
* @author Administrator
*/


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){
GameFrame02 gf=new GameFrame02();
gf.launchFrame();
}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java