制作的第一个java小游戏

package java1;
import java.awt.*;
public class java1 extends Frame {
    //球桌和桌球图片    
    Image ball = Toolkit.getDefaultToolkit().getImage("images/ball.png");
    Image desk = Toolkit.getDefaultToolkit().getImage("images/desk.jpg");
    //桌球的起始位置
    double x=100;
    double y=100;
    //
    double degree = 3.14/3;
    
    public void paint(Graphics g) {
        System.out.println("ok");
        g.drawImage(desk,0,0,null);
        g.drawImage(ball,(int)x,(int)y,null);
        
        x = x+10*Math.cos(degree);
        y = y+10*Math.sin(degree);
        
        if(y>410) {
            degree = - degree;
        }
        if(x>856-30-40||x<40) {
            degree = 3.14-degree;
        }
        if(y<20+20) {
            degree = -degree;
        }
    }
    
    void launchFrame(){
        
        setSize(856,480);
        setLocation(50,50);
        setTitle("我的作品");
        setVisible(true);
        
        while(true) {
            repaint();
            try {
                Thread.sleep(40);
                
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
        public static void main(String[] args) {
            System.out.println("ws");
            java1 bg = new java1();
            bg.launchFrame();
        }
    }
        

//小球ball

//desk

posted @ 2018-11-29 11:14  DWVictor  阅读(616)  评论(1编辑  收藏  举报