新手兴趣篇
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上找经验;
见到了常规的错误及修改的方法。后期有想法以及新知识再完善程序。
坚持下去,你一定可以的!