Adol  
日历
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
统计
  • 随笔 - 2
  • 文章 - 6
  • 评论 - 5
  • 引用 - 0

导航

常用链接

留言簿(1)

随笔档案

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 
  1package game7;
  2
  3/*
  4    每按一下空格,就出现一个子弹,完成 O(∩_∩)O
  5*/

  6
  7import java.awt.*;
  8import java.awt.event.*;
  9import javax.swing.*;
 10
 11class TestPanel extends JPanel implements Runnable
 12{
 13    private int x,y; //那个圆的坐标
 14    
 15    private final int ovalWidth=50//圆的直径
 16    
 17    TestPanel()
 18    {
 19        this.setSize(400400);
 20        x=200;
 21        y=50;
 22    }

 23    
 24    public void leftMove() //圆左移
 25    {
 26        x-=50;
 27        if(x<=-50) x=this.getWidth();
 28        updater();
 29    }

 30    
 31    public void rightMove() //圆右移
 32    {
 33        x+=50;
 34        if(x>=this.getWidth()) x=-50;
 35        updater();
 36    }

 37    
 38    public void upMove() //圆上移
 39    {
 40        y-=50;
 41        if(y<=-50) y=this.getHeight();
 42        updater();
 43    }

 44    
 45    public void downMove() //圆下移
 46    {
 47        y+=50;
 48        if(y>=this.getHeight()) y=-50;
 49        updater();
 50    }

 51    
 52    public void paint(Graphics g)
 53    {
 54        super.paint(g);  //加上,可以清空原来的,而且没闪烁,而且不会出问题
 55        g.fillOval(x, y, ovalWidth, ovalWidth); //画圆
 56    }

 57    
 58    public void updater() //实现消除闪烁
 59    {
 60        Image image;      //创建一张和原来大小一样的图像
 61        image=this.createImage(this.getWidth(),this.getHeight()); 
 62        Graphics gp=image.getGraphics(); //获得此创建图像的 画笔
 63        
 64        paint(gp);  //调用paint  对此图像作画            
 65        
 66        Graphics g=getGraphics();
 67        g.drawImage(image, 00this);  //将此图像画到(this)画布上
 68        
 69    }

 70    
 71    public void run()
 72    {
 73        int xx,yy;
 74        xx=(int)(Math.random()*10000)%300+50;
 75        yy=(int)(Math.random()*10000)%300+50;
 76        Bullet bl=new Bullet(xx,yy); //创建子弹
 77        bl.setLocation(); //设置子弹初始方位
 78        this.add(bl);  //添加
 79        boolean tag=true;  //控制循环用
 80        while(tag)
 81        {
 82            if(bl.getBulletInPanelX()>=0&&bl.getBulletInPanelX()<=this.getWidth()&&
 83                bl.getBulletInPanelY()>=0&&bl.getBulletInPanelY()<=this.getHeight()&&    
 84                !(bl.getBulletInPanelX()>=x&&bl.getBulletInPanelX()<=x+ovalWidth&&
 85                bl.getBulletInPanelY()>=y&&bl.getBulletInPanelY()<=y+ovalWidth)  )            
 86            {
 87                bl.upMove();  //上移
 88                bl.setLocation();  //设置方位
 89            }

 90            else
 91            {
 92                this.remove(bl); //出界或撞上什么了,移除
 93                tag=false;  //结束
 94            }

 95            try
 96            {
 97                Thread.sleep(10);
 98            }

 99            catch(InterruptedException e)
100            {
101                
102            }

103        }

104    }

105
106}

107
108class Bullet extends Canvas //子弹类,每创建一个此类对象,即是一个子弹
109{
110    private final int bulletHeight=10//确定子弹大小的
111    
112    private int speed=1;  //子弹的速度
113    
114    private int beginX,beginY; //子弹头的坐标,在自己画布里的
115        
116    private int bulletX[]; // 绘制子弹形状的数组 横纵坐标
117    private int bulletY[]; //这些都是相对自己画布里的
118    
119    private int bulletInPanelX,bulletInPanelY; //子弹在Panel里的坐标,
120                                                //唯一的2个外部属性
121    Bullet(int x, int y)
122    {
123        this.setSize(bulletHeight,    2*bulletHeight);
124        bulletInPanelX=x;
125        bulletInPanelY=y;
126        bulletX=new int[5];
127        bulletY=new int[5];
128        initBeginXY();
129        setBulletXY();
130    }

131    
132    public void setBulletXY() //设置子弹5个点的坐标 
133    {
134        bulletX[0]=beginX;
135        bulletX[1]=beginX+bulletHeight/2;
136        bulletX[2]=beginX+bulletHeight/2;
137        bulletX[3]=beginX-bulletHeight/2;
138        bulletX[4]=beginX-bulletHeight/2;
139        bulletY[0]=beginY;
140        bulletY[1]=beginY+bulletHeight;
141        bulletY[2]=beginY+2*bulletHeight;
142        bulletY[3]=beginY+2*bulletHeight;
143        bulletY[4]=beginY+bulletHeight;
144    }

145    
146    public void initBeginXY() //初始化子弹头坐标
147    {
148        beginX=(int)(bulletHeight*Math.tan(Math.PI/6));
149        beginY=0;
150    }

151    
152    public void upMove()  //子弹上移
153    {
154        bulletInPanelY-=speed;
155    }

156    
157    public void downMove() //子弹下移
158    {
159        bulletInPanelY+=speed;
160    }

161    
162    public void leftMove() //子弹左移
163    {
164        bulletInPanelX-=speed;
165    }

166    
167    public void rightMove() //子弹右移
168    {
169        bulletInPanelX+=speed;
170    }

171        
172    public void setLocation() //确定子弹坐标
173    {
174        this.setLocation(bulletInPanelX, bulletInPanelY);
175    }

176    
177    public void paint(Graphics g) //画出子弹
178    {
179        g.fillPolygon(bulletX, bulletY, 5);
180    }

181    
182    public int getBulletSpeed() //获得子弹速度
183    {
184        return speed;
185    }

186    
187    public int getBulletInPanelX() //获得子弹的横坐标
188    {
189        return bulletInPanelX;
190    }

191    
192    public int getBulletInPanelY() //获得子弹的纵坐标
193    {
194        return bulletInPanelY;
195    }

196}

197
198class MyKeyAdapter extends KeyAdapter
199{
200    TestPanel tp;
201    MyKeyAdapter(TestPanel tp)
202    {
203        this.tp=tp;
204    }

205    public void keyPressed(KeyEvent e)
206    {
207        if(e.getKeyCode()==KeyEvent.VK_LEFT)
208            tp.leftMove();
209        else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
210            tp.rightMove();
211        else if(e.getKeyCode()==KeyEvent.VK_UP)
212            tp.upMove();
213        else if(e.getKeyCode()==KeyEvent.VK_DOWN)
214            tp.downMove();
215        if(e.getKeyCode()==KeyEvent.VK_SPACE)
216        {
217            Thread th=new Thread(tp);  //没按一次空格,创建一个线程
218            th.start();    //线程开始,里面有创建子弹,即为每一个子弹创建一个线程
219        }

220    }

221}

222
223public class BulletContinus extends JFrame
224{
225    BulletContinus()
226    {
227        super("学习如何每按一个空格,出现一个自动移动的子弹");
228        setBounds(300150400400);
229        setVisible(true);
230        this.addWindowListener(new WindowAdapter()
231        {
232            public void windowClosing(WindowEvent e)
233            {
234                System.exit(0);
235            }

236        }
);
237    }

238    public static void main(String[] args)
239    {
240        BulletContinus jf=new BulletContinus();
241        TestPanel tp=new TestPanel();
242        tp.addKeyListener(new MyKeyAdapter(tp));
243        jf.add(tp);
244        tp.requestFocusInWindow();    //获得焦点
245    }

246}
posted on 2009-08-12 23:20 Adol 阅读(263) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © Adol Powered by: 博客园 模板提供:沪江博客