ball game 啥都不懂的打字练习

新手兴趣篇

package com.bjxst;

import java.awt.*;
import javax.swing.*;

public class ball_game extends JFrame {

    Image ball = Toolkit.getDefaultToolkit().getImage("images/ball.png");
    Image desk = Toolkit.getDefaultToolkit().getImage("Images/desk.png");

    double x = 200;
    double y = 200;
    double degree = 3.14/3;   //弧度。60°
     //boolean right = true;//控制小球的水平方向

    //绘制窗口
    public void paint(Graphics g){
        System.out.println("窗口被画了一次!");
        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 > 431 || y < 40){
            degree = - degree;
        }
        //碰到左右边界,角度变为互补。
        if(x > 786 || x < 40){
            degree = 3.14 - degree;
        }

      /*if(right){
        x = x + 10;
    }else{
            x = x - 10;
        }
        if(x > 816){
            right = false;
        }
        if(x < 40){
            right = true;
        }*/

        }


    //创建窗口
    void launchFrame(){

        setSize( 856,501);
        setLocation(100,100);
        setVisible(true);

        //实现动画,每秒绘制窗口25次
        while(true){
            repaint();
            try {
                Thread.sleep(40);   //大约1s绘制1000ms/40=25次
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args){
        System.out.println("我的小游戏开始了!");

        ball_game game = new ball_game();
        game.launchFrame();
    }

}

总结:

图片也是瞎找的,
主要是兴趣,增加点信心。遇到了环境安装的问题,多问百度,多在csdn上找经验;
见到了常规的错误及修改的方法。后期有想法以及新知识再完善程序。
坚持下去,你一定可以的!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值