Java飞机大战0_2

来源:互联网 发布:淘宝的淘金币在哪里 编辑:程序博客网 时间:2024/04/29 18:32

一.实验代码

//Plane.Javapackage sxau_rjxy;import java.awt.Color;import java.awt.Graphics;//敌机public class Plane {public Plane(int x, int y, int r) {super();this.x = x;this.y = y;this.r = r;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getR() {return r;}public void setR(int r) {this.r = r;}private int x;private int y;private int r;//自定义的paint()方法,名字随意public void paint(Graphics g) {// TODO Auto-generated method stubColor c = g.getColor();g.setColor(Color.black);//敌机黑色g.fillOval(x-r, y-r, r^2,r^2);g.setColor(c);}}

//Hero.javapackage sxau_rjxy;import java.awt.Color;import java.awt.Graphics;//我方飞机public class Hero {public Hero(int x, int y, int r) {super();this.x = x;this.y = y;this.r = r;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getR() {return r;}public void setR(int r) {this.r = r;}private int x;private int y;private int r;//自定义的paint()方法,名字随意public void paint(Graphics g) {// TODO Auto-generated method stubColor c = g.getColor();g.setColor(Color.blue);//我机蓝色g.fillOval(x-r, y-r, r^2,r^2);g.setColor(c);}}


//Bullet.javapackage sxau_rjxy;import java.awt.Color;import java.awt.Graphics;public class Bullet {public Bullet(int x, int y, int r) {super();this.x = x;this.y = y;this.r = r;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getR() {return r;}public void setR(int r) {this.r = r;}private int x;private int y;private int r;//自定义的paint()方法,名字随意public void paint(Graphics g) {// TODO Auto-generated method stubColor c = g.getColor();g.setColor(Color.white);//子弹白色g.fillOval(x-r, y-r, r^2,r^2);g.setColor(c);}}

//MyJPanelpackage sxau_rjxy;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionAdapter;import java.awt.event.MouseMotionListener;import java.util.ArrayList;import java.util.Iterator;import javax.swing.JPanel;public class MyJPanel extends JPanel implements Runnable{@Overridepublic void run() {// TODO Auto-generated method stub/*while(true){int y = p.getY();p.setY(y++);    repaint();   try {   Thread.sleep(100);   } catch (InterruptedException e){   // TODO Auto-generated catch block   e.printStackTrace();   }      }*/int y;int count =0;while(true){//使子弹慢下来产生if(count%10==0){Bullet bullet = new Bullet(hero.getX(),hero.getY()-hero.getR(),5);bullets.add(bullet);}count++;//让飞机动起来for(int i= 0;i<planes.size();i++){Plane p=planes.get(i);y=p.getY()+1;p.setY(y);if(y>bgy){p.setY(0);//开始循环了,但运行出来是由于横坐标没变,显示一样p.setX((int)(Math.random()*(bgx-60)+30));//两个一起则图案显示不同,随机}}//让子弹动起来for(int i= 0;i<bullets.size();i++){Bullet b=bullets.get(i);y=b.getY()-2;b.setY(y);}//bullet.setY(bullet.getY()-1);//让子弹动起来,即坐标变化try {Thread.sleep(20);//如果没sleep,那么它屏幕上没圈了,因为太快} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}repaint();//没它之前,运行出的结果是静态的,因为没有重新画它}//这是一个死循环,while(true)}//Plane p = new Plane(200,200,50);//Plane[] planes = new Plane[10];ArrayList<Plane> planes = new ArrayList<Plane>();//保存飞机的引用,才能画它;定义了容器ArrayList<Bullet> bullets = new ArrayList<Bullet>();//Plane hero= new Plane(bgx, bgy, 30);//不对,不能共用敌机的类Hero hero;Bullet bullet;@Override//重写的paint()方法,系统调用public void paint(Graphics g) {// TODO Auto-generated method stub//super.paint(g);//Color c = g.getColor();//g.setColor(Color.red);//g.fillOval(p.getX()-p.getR(), p.getY()-p.getR(), p.getR()^2,p.getR());//g.setColor(c);super.paint(g);//p.paint(g);/*画敌机 * for(int i =0;i<planes.size();i++){Plane p= planes.get(i);p.paint(g);}*///画敌机Iterator<Plane> it = planes.iterator();while(it.hasNext()){Plane p = it.next();p.paint(g);}//画子弹for(int i =0;i<bullets.size();i++){Bullet b=bullets.get(i);b.paint(g);}//画herohero.paint(g);//bullet.paint(g);}private int bgx;private int bgy;public MyJPanel (Dimension dim){bgx =dim.width;bgy =dim.height;this.setBackground(Color.lightGray);for(int i = 0;i<=10;i++){Plane p= new Plane((int)(Math.random()*(bgx-60)+30),(int)(Math.random()*(bgy-60)+30),30);planes.add(p);}hero= new Hero(bgx/2, bgy-60, 30);/* * Hero hero = new Hero(bgx/2, bgy-60, 30); * 因为前面已经定义过Hero hero ; * 所以运行出错,空白一片;*/bullet = new Bullet(hero.getX(),hero.getY()-hero.getR(),5);this.addMouseMotionListener(new MyMonitor());this.addKeyListener(new MyMonitor2());this.setFocusable(true);//必须要这,监听键盘时必须加,不然没反应new Thread(this).start();//线程启动第二种方法}//内部类private class MyMonitor extends MouseMotionAdapter{@Overridepublic void mouseDragged(MouseEvent e) {// TODO Auto-generated method stub//super.mouseDragged(e);hero.setX(e.getX());hero.setY(e.getY());}}private class MyMonitor2 extends KeyAdapter{@Overridepublic void keyPressed(KeyEvent e) {// TODO Auto-generated method stubsuper.keyPressed(e);//System.out.println("ss");//用来测试键盘事件是否响应了int code = e.getKeyCode();if(code==KeyEvent.VK_LEFT){hero.setX(hero.getX()-10);}if(code==KeyEvent.VK_RIGHT){hero.setX(hero.getX()+10);}if(code==KeyEvent.VK_UP){hero.setY(hero.getY()-10);}if(code==KeyEvent.VK_DOWN){hero.setY(hero.getY()+10);}}}}


//Mygame.javapackage sxau_rjxy;import java.awt.Color;import java.awt.Dimension;import java.awt.Toolkit;import javax.swing.JFrame;public class Mygame {public static void main(String[] args) {// TODO Auto-generated method stubJFrame jf = new JFrame("飞机大战");//创建窗体    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();jf.setBounds(0, 0, dim.width, dim.height);//jf.setBackground(Color.red);jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);MyJPanel panel= new MyJPanel(dim);//相当于管家jf.add(panel);/* * Thread t = new Thread(panel); * t.start();*/jf.setResizable(false);jf.setVisible(true);}}

二.实验结果




三.实验心得

06.28

 

1.在定义Hero类的时候,出现了错误.后经检查,发现了原因

     Hero hero;

     hero=new Hero(bgx/2, bgy-60, 30);

     /* 原因解释

     * Hero hero = new Hero(bgx/2,bgy-60, 30);

     * 因为前面已经定义过Hero hero ;

     * 所以运行出错,空白一片;

     */

 

2.int y;

  while(true){

    for(int i= 0;i<planes.size();i++){

   Plane p=planes.get(i);

y=p.getY()+1;

p.setY(y);

if(y>bgy){

p.setY(0);//开始循环了,但运行出来是由于横坐标没变,显示一样

p.setX((int)(Math.random()*(bgx-60)+30));

           //两个一起则图案显示不同,随机

}

}

try {

Thread.sleep(20);//如果没sleep,那么它屏幕上没圈了,因为太快

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

   }

repaint();//没它之前,运行出的结果是静态的,因为没有重新画它

}//这是一个死循环,while(true)

3.启动线程的两种方式

  3.1.Thread t = new Thread(panel);

      t.start();

      写在主方法中

  3.2.new Thread(this).start();写在MyPlane的构造方法中.

 

4.创建监听事件,需要创建内部类.

  建一个监听类MyMonitorNAdaper继承过来

5.键盘事件,必须有setFocusable(true);

  eg:

  this.addKeyListener(new MyMonitor2());

  this.setFocusable(true);//必须要这,监听键盘时必须加,不然没反应

 

6.Bullet,目前设置的是hero可以发射子弹,因为Bullet的对象bullet用的 是bullet =new Bullet(hero.getX(),hero.getY()-hero.getR(),5);可以通过改变bulletx,y,r来控制发射对象.

 

7.//使子弹慢下来产生,如果没有这部分,运行的效果会出现连续的子弹,看上去是一条连续的线

  if(count%10==0){

Bullet bullet = new  Bullet(hero.getX(),hero.getY()-hero.getR(),5);

bullets.add(bullet);

    }

count++;


原创粉丝点击