开始开发我们的小游戏
1. 增加窗口
import java.awt.*; publicclass BallGame extends Frame { void launchFrame(){ setSize(500, 300); setLocation(50, 50); setVisible(true); setTitle("---张三作品"); setBackground(Color.black); } publicstaticvoid main(String[] args){ System.out.println("我是张丹!"); new BallGame().launchFrame(); } } |
2. 加载图片
在项目下新建p_w_picpaths文件夹,将sun.jpg拷贝到p_w_picpaths下面 |
import java.awt.*; publicclass BallGame extends Frame { Image sun = Toolkit.getDefaultToolkit().getImage("p_w_picpaths/sun.jpg"); //这种写法并不好,但是是最简单的! publicvoid paint(Graphics g){ g.drawImage(sun, 100, 100, null); } void launchFrame(){ setSize(500, 300); setLocation(50, 50); setTitle("---张三作品"); setBackground(Color.black); setVisible(true); } publicstaticvoid main(String[] args){ System.out.println("我是张丹!"); new BallGame().launchFrame(); } } |
注意:运行时,第一次打开窗口看不到图片。需要将窗口最小化再打开即可看到! |
3. 学习画各种曲线、形状、字符串:
import java.awt.*; publicclass BallGame extends Frame { Image sun = Toolkit.getDefaultToolkit().getImage("p_w_picpaths/sun.jpg"); //这种写法并不好,但是是最简单的! publicvoid paint(Graphics g){ g.drawImage(sun, 100, 100, null); g.setColor(Color.blue); g.drawLine(100, 100, 200, 200); g.drawRect(50, 50, 100, 80); g.drawOval(50, 50, 100, 80); g.setColor(Color.yellow); g.drawString("游戏开始啦!!", 80, 80); } void launchFrame(){ setSize(500, 300); setLocation(50, 50); setTitle("---张三作品"); setBackground(Color.black); setVisible(true); } publicstaticvoid main(String[] args){ System.out.println("我是张丹!"); new BallGame().launchFrame(); } } |
4. 增加动画
import java.awt.*; publicclass BallGame extends Frame { Image sun = Toolkit.getDefaultToolkit().getImage("p_w_picpaths/sun.jpg"); //这种写法并不好,但是是最简单的! int x=100; int y=100; publicvoid paint(Graphics g){ System.out.println("窗口被画了一次!"); g.drawImage(sun, x, y, null); x = x+2; } void launchFrame(){ setSize(500, 300); setLocation(50, 50); setTitle("---张三作品"); setBackground(Color.black); setVisible(true); new PaintThread().start(); } publicstaticvoid main(String[] args){ System.out.println("我是张丹!"); new BallGame().launchFrame(); } class PaintThread extends Thread { publicvoid run(){ while(true){ repaint(); //重画窗口! try{ Thread.sleep(40); //40ms 1s=1000ms }catch (Exception e) { e.printStackTrace(); } } } } } |
5. 通过我们掌握的数学函数(抛物线、正弦曲线、椭圆等),控制游戏中物体的运动!
import java.awt.*; publicclass BallGame extends Frame { Image sun = Toolkit.getDefaultToolkit().getImage("p_w_picpaths/sun.jpg"); //这种写法并不好,但是是最简单的! double x=100; double y=100; double degree = 0; publicvoid paint(Graphics g){ System.out.println("窗口被画了一次!"); g.drawImage(sun, (int)x,(int)y, null); x =150+ 100*Math.cos(degree); y = 150+100*Math.sin(degree); degree = degree + 0.1; } void launchFrame(){ setSize(500, 300); setLocation(50, 50); setTitle("---张三作品"); setBackground(Color.black); setVisible(true); new PaintThread().start(); } publicstaticvoid main(String[] args){ System.out.println("我是张丹!"); new BallGame().launchFrame(); } class PaintThread extends Thread { publicvoid run(){ while(true){ repaint(); //重画窗口! try{ Thread.sleep(40); //40ms 1s=1000ms }catch (Exception e) { e.printStackTrace(); } } } } } |
6. 实现台球的运动!
import java.awt.*; publicclass BallGame extends Frame { Image sun = Toolkit.getDefaultToolkit().getImage("p_w_picpaths/sun.jpg"); //这种写法并不好,但是是最简单的! doublex=100; doubley=100; doubledegree = 3.14/3; publicvoid paint(Graphics g){ System.out.println("窗口被画了一次!"); g.drawImage(sun, (int)x,(int)y, null); x = x+ 10*Math.cos(degree); y = y+10*Math.sin(degree); if(y>300-30){ degree = - degree; } if(x>500-30){ degree = 3.14-degree; } if(x<0){ degree = 3.14-degree; } if(y<30){ degree = -degree; } } void launchFrame(){ setSize(500, 300); setLocation(50, 50); setTitle("---张三作品"); setBackground(Color.black); setVisible(true); new PaintThread().start(); } publicstaticvoid main(String[] args){ System.out.println("我是张丹!"); new BallGame().launchFrame(); } class PaintThread extends Thread { publicvoid run(){ while(true){ repaint(); //重画窗口! try{ Thread.sleep(40); //40ms 1s=1000ms }catch (Exception e) { e.printStackTrace(); } } } } } |
7. 通过键盘来控制物体的运动!