[Java] 小球撞墙 →→→→→进入此内容的聊天室

来自 , 2020-11-08, 写在 Java, 查看 117 次.
URL http://www.code666.cn/view/226d1f15
  1. //MY
  2. import java.awt.*;
  3.  
  4. public class pp {
  5.         public static void main(String args[]) {
  6.                 Frame w = new Frame();
  7.                 w.setSize(300, 400);
  8.                 MyPanel mp = new MyPanel();
  9.                 w.add(mp);
  10.  
  11.                 Thread t = new Thread(mp);
  12.                 t.start();
  13.  
  14.                 w.show();
  15.         }
  16. }
  17.  
  18. class MyPanel extends Panel implements Runnable {
  19.         int x = 30;
  20.         int y = 30;
  21.         int att = 0;
  22.  
  23.         public void paint(Graphics g) {
  24.          
  25.          g.fillOval(x, y, 20, 30); }
  26.        
  27.  
  28.         public void run() {
  29.                 while (true) {
  30.                         //定义状态
  31.                         if(att==0)
  32.                         {
  33.                                 x++;
  34.                                 y++;
  35.                         }
  36.                         if(att==1)
  37.                         {
  38.                                 x--;
  39.                                 y++;
  40.                         }
  41.                         if(att==2)
  42.                         {
  43.                                 x--;
  44.                                 y--;
  45.                         }
  46.                         if(att==3)
  47.                         {
  48.                                 x++;
  49.                                 y--;
  50.                         }
  51.                         //改变状态
  52.                         if (x > 263) {
  53.                                 if (att == 0)
  54.                                         att = 1;
  55.                                 else
  56.                                         att = 2;
  57.                         }
  58.                                 if (y > 348) {
  59.                                         if (att == 1)
  60.                                                 att = 2;
  61.                                          else
  62.                                                 att = 3;
  63.                                 }
  64.                                         if (x < 0) {
  65.                                                 if (att == 2)
  66.                                                         att = 3;
  67.                                                 else
  68.                                                         att = 0;
  69.                                         }
  70.                                        
  71.                                         if (y < 0) {
  72.                                                 if (att == 3)
  73.                                                         att = 0;
  74.                                                 else
  75.                                                         att = 1;
  76.                                 }
  77.  
  78.                        
  79.                         try {
  80.                                 Thread.sleep(10);
  81.                         } catch (Exception e) {
  82.                         }
  83.                        
  84.                         repaint();
  85.                 }
  86.                 }
  87.         }
  88.  
  89.  
  90.  

回复 "小球撞墙"

这儿你可以回复上面这条便签

captcha