import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
class Buttle implements Runnable{
private int bx=Game.lx;
private int by=Game.ly;
private int ox=0;
private int oy=0;
private JLabel jLabel3;
Game g=null;
int sleep=10;
public Buttle(Game g){
this.g=g;
}
public Buttle(Game g,int sleep){
this.g=g;
this.sleep=sleep;
}
boolean judge=false;
public void run(){
jLabel3 = new JLabel();
g.getContentPane().add(jLabel3);
System.out.println(bx+","+by);
jLabel3.setText("/u25cf");
int even=1;
while(true){//子弹路径
while(ox!=bx||oy!=by){
try{
Thread.sleep(sleep);
}catch(Exception e){}
if(ox
jLabel3.setBounds(ox++, oy, 14, 15);
even=2;
}else if(ox>bx&&even==1){
jLabel3.setBounds(ox--, oy, 14, 15);
even=2;
}else if(oy
jLabel3.setBounds(ox, oy++, 14, 15);
even=1;
}else if(oy>by&&even==2){
jLabel3.setBounds(ox, oy--, 14, 15);
even=1;
}else{
if(ox
jLabel3.setBounds(ox++, oy, 14, 15);
}else if(ox>bx){
jLabel3.setBounds(ox--, oy, 14, 15);
}else if(oy
jLabel3.setBounds(ox, oy++, 14, 15);
}else if(oy>by){
jLabel3.setBounds(ox, oy--, 14, 15);
}
}
if(ox==Game.lx&&oy==Game.ly){
judge=true;
JOptionPane.showMessageDialog(g,"游戏结束!","提示框",JOptionPane.WARNING_MESSAGE);
break;
}
}
if(judge){
break;
}
//子弹重置
ox=0;
oy=0;
bx=Game.lx;
by=Game.ly;
}
}
}
public class Game extends javax.swing.JFrame {
private JLabel jLabel1;
public static int lx=399;
public static int ly=299;
private JLabel jLabel2;
//private JLabel jLabel3;
private int bx=0;
private JLabel jLabel3;
private int by=0;
Buttle bu=new Buttle(this);
Thread t1=new Thread(bu);
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Game inst = new Game();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public Game() {
super("轰炸敌人碉堡!◎");
initGUI();
}
private void initGUI() {
try {
{
getContentPane().setLayout(null);
{
jLabel1 = new JLabel();
getContentPane().add(jLabel1);
jLabel1.setText("/u2605");
jLabel1.setBounds(lx, ly, 18, 15);//x,y
}
{
jLabel2 = new JLabel();
getContentPane().add(jLabel2);
jLabel2.setText("/u25ce");
jLabel2.setBounds(12, 19, 20, 22);
}
{
jLabel3 = new JLabel();
getContentPane().add(jLabel3);
jLabel3.setText("/u25a1/u25a1");
jLabel3.setBounds(4, 5, 32, 15);
}
{
t1.start();
}
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
thisKeyPressed(evt);
}
});
}
this.setSize(460, 343);
} catch (Exception e) {
e.printStackTrace();
}
}
private void thisKeyPressed(KeyEvent evt) {//对应键盘事件控制,w上,s下,a左,d右
int op=evt.getKeyCode();
System.out.println(op);
if(op==87){//up
if(ly-1<300&&ly-1>0){
jLabel1.setBounds(lx, ly--, 39, 15);//x,y
}
}else if(op==83){//down
if(ly+1<300&&ly+1>0){
jLabel1.setBounds(lx, ly++, 39, 15);//x,y
}
}else if(op==65){//left
if(lx-1<400&&lx-1>0){
jLabel1.setBounds(lx--, ly, 39, 15);//x,y
}
}else if(op==68){//right
if(lx+1<400&&lx+1>0){
jLabel1.setBounds(lx++, ly, 39, 15);//x,y
}
}else{}
if((lx>8&&lx<20)){
if(ly>15&&ly<22){
t1.stop();
JOptionPane.showMessageDialog(this,"成功炸毁碉堡!","提示框",JOptionPane.WARNING_MESSAGE);
}
}
}
}