首页 > 代码库 > 练习.小球撞墙反弹

练习.小球撞墙反弹

  1 package com.java7.myball.main;  2 import java.awt.*;  3   4 public class MyBall {  5     public static void main(String[] args) {  6         Frame w = new Frame();  7         w.setSize(300, 400);  8           9         MyPanel mp = new MyPanel(); 10         w.add(mp); 11          12         Thread t = new Thread(mp); 13         t.start(); 14          15         w.show(); 16     } 17 } 18  19 class MyPanel extends Panel implements Runnable { 20     int x1 = 30; 21     int y1 = 30; 22     int x2 = 280; 23     int y2 = 358; 24     int cr = 0; 25     int cg = 0; 26     int cb = 0; 27     int att1 = 0; // 0=右下;1=左下;2=左上;3=右上 28     int att2 = 0; 29     boolean b = true; 30     public void paint(Graphics g) { 31         g.setColor(new Color(cr, cg, cb)); 32         g.fillOval(x1, y1, 20, 20); 33         g.fillOval(x2, y2, 20, 20); 34     } 35     public void run() { 36         while(b) { 37             // 定义飞行姿态 38             if(att1 == 0) { 39                 x1++; 40                 y1++; 41             } 42             if(att1 == 1) { 43                 x1--; 44                 y1++; 45             } 46             if(att1 == 2) { 47                 x1--; 48                 y1--; 49             } 50             if(att1 == 3) { 51                 x1++; 52                 y1--; 53             } 54             if(att2 == 0) { 55                 x2++; 56                 y2++; 57             } 58             if(att2 == 1) { 59                 x2--; 60                 y2++; 61             } 62             if(att2 == 2) { 63                 x2--; 64                 y2--; 65             } 66             if(att2 == 3) { 67                 x2++; 68                 y2--; 69             } 70              71             // 改变飞行姿态 72             if(x1 > 280) { 73                 if(att1 == 0) { 74                     att1 = 1; 75                     cr = (int)(Math.random() * 255); 76                     cg = (int)(Math.random() * 255); 77                     cb = (int)(Math.random() * 255); 78                 } else { 79                     att1 = 2; 80                     cr = (int)(Math.random() * 255); 81                     cg = (int)(Math.random() * 255); 82                     cb = (int)(Math.random() * 255); 83                 } 84             } 85             if(y1 > 358) { 86                 if(att1 == 1) { 87                     att1 = 2; 88                     cr = (int)(Math.random() * 255); 89                     cg = (int)(Math.random() * 255); 90                     cb = (int)(Math.random() * 255); 91                 } else { 92                     att1 = 3; 93                     cr = (int)(Math.random() * 255); 94                     cg = (int)(Math.random() * 255); 95                     cb = (int)(Math.random() * 255); 96                 } 97             } 98             if(x1 < 0) { 99                 if(att1 == 2) {100                     att1 = 3;101                     cr = (int)(Math.random() * 255);102                     cg = (int)(Math.random() * 255);103                     cb = (int)(Math.random() * 255);104                 } else {105                     att1 = 0;106                     cr = (int)(Math.random() * 255);107                     cg = (int)(Math.random() * 255);108                     cb = (int)(Math.random() * 255);109                 }110             }111             if(y1 < 0) {112                 if(att1 == 3) {113                     att1 = 0;114                     cr = (int)(Math.random() * 255);115                     cg = (int)(Math.random() * 255);116                     cb = (int)(Math.random() * 255);117                 } else {118                     att1 = 1;119                     cr = (int)(Math.random() * 255);120                     cg = (int)(Math.random() * 255);121                     cb = (int)(Math.random() * 255);122                 }123             }            124             if(x2 > 280) {125                 if(att2 == 0) {126                     att2 = 1;127                     cr = (int)(Math.random() * 255);128                     cg = (int)(Math.random() * 255);129                     cb = (int)(Math.random() * 255);130                 } else {131                     att2 = 2;132                     cr = (int)(Math.random() * 255);133                     cg = (int)(Math.random() * 255);134                     cb = (int)(Math.random() * 255);135                 }136             }137             if(y2 > 358) {138                 if(att2 == 1) {139                     att2 = 2;140                     cr = (int)(Math.random() * 255);141                     cg = (int)(Math.random() * 255);142                     cb = (int)(Math.random() * 255);143                 } else {144                     att2 = 3;145                     cr = (int)(Math.random() * 255);146                     cg = (int)(Math.random() * 255);147                     cb = (int)(Math.random() * 255);148                 }149             }150             if(x2 < 0) {151                 if(att2 == 2) {152                     att2 = 3;153                     cr = (int)(Math.random() * 255);154                     cg = (int)(Math.random() * 255);155                     cb = (int)(Math.random() * 255);156                 } else {157                     att2 = 0;158                     cr = (int)(Math.random() * 255);159                     cg = (int)(Math.random() * 255);160                     cb = (int)(Math.random() * 255);161                 }162             }163             if(y2 < 0) {164                 if(att2 == 3) {165                     att2 = 0;166                     cr = (int)(Math.random() * 255);167                     cg = (int)(Math.random() * 255);168                     cb = (int)(Math.random() * 255);169                 } else {170                     att2 = 1;171                     cr = (int)(Math.random() * 255);172                     cg = (int)(Math.random() * 255);173                     cb = (int)(Math.random() * 255);174                 }175             }176             // 设置两球相撞后反弹,未完成177 //            if(x1 <= x2 - 30 && y1 <= y2 - 30) {178 //                if(att1 == 0) {179 //                    att1 = 1;180 //                    cr = (int)(Math.random() * 255);181 //                    cg = (int)(Math.random() * 255);182 //                    cb = (int)(Math.random() * 255);183 //                } else {184 //                    att1 = 2;185 //                    cr = (int)(Math.random() * 255);186 //                    cg = (int)(Math.random() * 255);187 //                    cb = (int)(Math.random() * 255);188 //                }189 //            }190 //            if(x2 >= x1 + 30 && y2 >= y1 + 30 ) {191 //                if(att1 == 0) {192 //                    att1 = 1;193 //                    cr = (int)(Math.random() * 255);194 //                    cg = (int)(Math.random() * 255);195 //                    cb = (int)(Math.random() * 255);196 //                } else {197 //                    att1 = 2;198 //                    cr = (int)(Math.random() * 255);199 //                    cg = (int)(Math.random() * 255);200 //                    cb = (int)(Math.random() * 255);201 //                }202 //            }203             try {204                 Thread.sleep(20);205             } catch(Exception e) {206                 207             }208             repaint();209         }210     }211 }

 

练习.小球撞墙反弹