黑马程序员技术交流社区
标题:
用Applet做了一个子弹打飞机的小程序,看看大虾们有什么更好的方法
[打印本页]
作者:
xiawei
时间:
2014-11-18 16:11
标题:
用Applet做了一个子弹打飞机的小程序,看看大虾们有什么更好的方法
子弹打飞机问题,获取子弹和飞机坐标和速度,那位同仁还有什么好办法。
要赚取黑马币技术分的快来
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Animation1 extends JFrame implements Runnable,ActionListener{
private Container cont;
private JButton btnUp;
private JButton btnDown;
private ImageIcon imgPlane;
private ImageIcon imgBullet;
private int planeX;
private int planeY;
private int bulletX;
private int bulletY;
private int planeSpeed;
private int bulletSpeed;
private int planeChangeSpeed;
private Thread thread;
public Animation(){
this.setBounds(30, 30, 400, 300);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
cont = this.getContentPane();
cont.setLayout(null);
imgPlane = new ImageIcon("plane.jpg");
imgBullet = new ImageIcon("bullet.jpg");
planeX = 0;
planeY = 150;
bulletX = 400;
bulletY = 150;
planeSpeed = 8;
bulletSpeed = 8;
planeChangeSpeed = 6;
btnUp = new JButton("up");
btnDown = new JButton("down");
btnUp.setBounds(0, 240, 80, 30);
btnDown.setBounds(90, 240, 80, 30);
btnUp.addActionListener(this);
btnDown.addActionListener(this);
cont.add(btnUp);
cont.add(btnDown);
thread = new Thread(this);
thread.start();
this.setVisible(true);
}
public void paint(Graphics grp){
super.paint(grp);
imgPlane.paintIcon(this, grp, planeX, planeY);
imgBullet.paintIcon(this, grp, bulletX, bulletY);
}
public void run(){
while((bulletX > 0) &&(planeX < 400)){
repaint();
planeX = planeX + planeSpeed;
bulletX = bulletX - bulletSpeed;
try{
Thread.sleep(200);
}catch(Exception e){}
}
thread = null;
}
public void actionPerformed(ActionEvent aEvt){
JButton ob=(JButton)aEvt.getSource();
if(ob==btnUp){
planeY-=planeChangeSpeed;
}
if(ob==btnDown){
planeY+=planeChangeSpeed;
}
}
public static void main(String args[]){
new Animation();
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2