麻烦高手帮忙看一下,为什么按空格后炮弹不发射了,再按空格炮弹还不发射。(我想的是按空格炮弹发射,再按不发射;按一下发射一次也行);小弟菜鸟,初学Java。import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.lang.Math;
public class Pete extends JFrame
{
public enemytank enemy;
public enemytank1 enemy1;
public ammo ammo1;
public ammoo ammoo1;
public myammoo myammoo1;
public int i=0;
PetePanel pete = new PetePanel();
public Pete()
{
super("Tank Battle");
setSize(512, 430);
setResizable(false);
enemy=new enemytank(pete);
enemy1=new enemytank1(pete);
ammo1=new ammo(pete);
ammoo1=new ammoo(pete);
myammoo1=new myammoo(pete);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//添加窗口关闭
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(1, 1, 15, 15));
pane.add(pete);
this.addKeyListener(new KeyAdapter(){//得到焦点,放frame容易获取焦点。
@Override
public void keyPressed(KeyEvent e)
{
System.out.println("ppppp");
if (e.getKeyCode() == KeyEvent.VK_SPACE) //fire
{
i=i+1;
if(i%2==0)
pete.live=1;
else pete.live=0;
System.out.println(pete.live);
}
} public void keyTyped(KeyEvent e)
{
System.out.println("kkkkk");
} public void keyReleased(KeyEvent e)
{
System.out.println("rrrrrr");
}
});
setContentPane(pane);//建立联系与pane
enemy.start();
enemy1.start();
ammo1.start();
ammoo1.start();
myammoo1.start();
this.setVisible(true);//窗口可见
this.requestFocus();//具体办法可以手设焦点。如果焦点有问题,就优先想到它
}
public static void main(String[] arguments)
{
Pete Penguin = new Pete();
PetePanel a = new PetePanel();
}
}class PetePanel extends JPanel{
Image petePics[] = new Image[4];
Image petePics1[] = new Image[4];
Image petePics2[] = new Image[4];
Image petePics3[] = new Image[4];
Image petePics4[] = new Image[4];
Image petePics5[] = new Image[4];
Image back;
int current = 0;
int x = 100;
int y = 100;
int current1 = 0;
int x1 = 300;
int y1 = 300;
int current2 = 0;
int x2 = 400;
int y2 = 50;
int current3 = 0;
int x3 = -20;
int y3 = -20;
int current4 = 0;
int x4 = -20;
int y4 = -20;
int current5 = 0;
int x5 = -20;
int y5 = -20;
int live=1;
PetePanel()
{
super();
String peteSrc[] = { "right.jpg", "left.jpg", "up.jpg", "down.jpg" };
String peteSrc1[] = { "right.jpg", "left.jpg", "up.jpg", "down.jpg" };
String peteSrc2[] = { "right.jpg", "left.jpg", "up.jpg", "down.jpg" };
String peteSrc3[] = { "ar.jpg", "al.jpg", "au.jpg", "ad.jpg" };
String peteSrc4[] = { "ar.jpg", "al.jpg", "au.jpg", "ad.jpg" };
String peteSrc5[] = { "ar.jpg", "al.jpg", "au.jpg", "ad.jpg" };
Toolkit kit = Toolkit.getDefaultToolkit();
for (int i = 0; i < petePics.length; i++)
{
petePics[i] = kit.getImage(peteSrc[i]);
petePics1[i] = kit.getImage(peteSrc1[i]);
petePics2[i] = kit.getImage(peteSrc2[i]);
petePics3[i] = kit.getImage(peteSrc3[i]);
petePics4[i] = kit.getImage(peteSrc4[i]);
petePics5[i] = kit.getImage(peteSrc5[i]);
}
back = kit.getImage("30.jpg");
} public void paint(Graphics comp) //创建绘图函数
{
Graphics2D comp2D = (Graphics2D) comp;
if (petePics[current] != null)
comp2D.drawImage(back,0,0,this);
comp2D.drawImage(petePics[current], x, y, this);
comp2D.drawImage(petePics1[current1], x1, y1, this);
comp2D.drawImage(petePics2[current2], x2, y2, this);
comp2D.drawImage(petePics3[current3], x3, y3, this);
comp2D.drawImage(petePics4[current4], x4, y4, this);
comp2D.drawImage(petePics5[current5], x5, y5, this);
}
}
class myammoo extends Thread
{
PetePanel pete ;
public myammoo(PetePanel pete)
{
this.pete=pete;
}
public void run()
{
for(;true;){
while(pete.live==1)
{
if(pete.current==0)
{
System.out.println("Thread is live");
pete.current5=0;
pete.x5=pete.x+10;
pete.y5=pete.y+10;
for(int i=0;i<500;i++)
{
pete.x5=pete.x5+1;
pete.y4=pete.y4+1;
try
{
sleep(1); //线程休眠
}
catch(InterruptedException e){}
pete.repaint();
}
}
else if(pete.current==1)
{
pete.current5=1;
pete.x5=pete.x+10;
pete.y5=pete.y+10;
for(int i=0;i<500;i++)
{
pete.x5=pete.x5-1;
pete.y4=pete.y4+1;
try
{
sleep(1); //线程休眠
}
catch(InterruptedException e){}
pete.repaint();
}
}
else if(pete.current==2)
{
pete.current5=2;
pete.x5=pete.x+10;
pete.y5=pete.y+10;
for(int i=0;i<500;i++)
{
pete.y5=pete.y5-1;
pete.y4=pete.y4+1;
try
{
sleep(1); //线程休眠
}
catch(InterruptedException e){}
pete.repaint();
}
}
else if(pete.current==3)
{
pete.current5=3;
pete.x5=pete.x+10;
pete.y5=pete.y+10;
for(int i=0;i<500;i++)
{
pete.y5=pete.y5+1;
pete.y4=pete.y4+1;
try
{
sleep(1); //线程休眠
}
catch(InterruptedException e){}
pete.repaint();
}
}
pete.x5=0-20;
pete.y5=0-20;
try
{
sleep(1000); //线程休眠
}
catch(InterruptedException e){}
}
}}
}
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.lang.Math;
public class Pete extends JFrame
{
public enemytank enemy;
public enemytank1 enemy1;
public ammo ammo1;
public ammoo ammoo1;
public myammoo myammoo1;
public int i=0;
PetePanel pete = new PetePanel();
public Pete()
{
super("Tank Battle");
setSize(512, 430);
setResizable(false);
enemy=new enemytank(pete);
enemy1=new enemytank1(pete);
ammo1=new ammo(pete);
ammoo1=new ammoo(pete);
myammoo1=new myammoo(pete);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//添加窗口关闭
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(1, 1, 15, 15));
pane.add(pete);
this.addKeyListener(new KeyAdapter(){//得到焦点,放frame容易获取焦点。
@Override
public void keyPressed(KeyEvent e)
{
System.out.println("ppppp");
if (e.getKeyCode() == KeyEvent.VK_SPACE) //fire
{
i=i+1;
if(i%2==0)
pete.live=1;
else pete.live=0;
System.out.println(pete.live);
}
} public void keyTyped(KeyEvent e)
{
System.out.println("kkkkk");
} public void keyReleased(KeyEvent e)
{
System.out.println("rrrrrr");
}
});
setContentPane(pane);//建立联系与pane
enemy.start();
enemy1.start();
ammo1.start();
ammoo1.start();
myammoo1.start();
this.setVisible(true);//窗口可见
this.requestFocus();//具体办法可以手设焦点。如果焦点有问题,就优先想到它
}
public static void main(String[] arguments)
{
Pete Penguin = new Pete();
PetePanel a = new PetePanel();
}
}class PetePanel extends JPanel{
Image petePics[] = new Image[4];
Image petePics1[] = new Image[4];
Image petePics2[] = new Image[4];
Image petePics3[] = new Image[4];
Image petePics4[] = new Image[4];
Image petePics5[] = new Image[4];
Image back;
int current = 0;
int x = 100;
int y = 100;
int current1 = 0;
int x1 = 300;
int y1 = 300;
int current2 = 0;
int x2 = 400;
int y2 = 50;
int current3 = 0;
int x3 = -20;
int y3 = -20;
int current4 = 0;
int x4 = -20;
int y4 = -20;
int current5 = 0;
int x5 = -20;
int y5 = -20;
int live=1;
PetePanel()
{
super();
String peteSrc[] = { "right.jpg", "left.jpg", "up.jpg", "down.jpg" };
String peteSrc1[] = { "right.jpg", "left.jpg", "up.jpg", "down.jpg" };
String peteSrc2[] = { "right.jpg", "left.jpg", "up.jpg", "down.jpg" };
String peteSrc3[] = { "ar.jpg", "al.jpg", "au.jpg", "ad.jpg" };
String peteSrc4[] = { "ar.jpg", "al.jpg", "au.jpg", "ad.jpg" };
String peteSrc5[] = { "ar.jpg", "al.jpg", "au.jpg", "ad.jpg" };
Toolkit kit = Toolkit.getDefaultToolkit();
for (int i = 0; i < petePics.length; i++)
{
petePics[i] = kit.getImage(peteSrc[i]);
petePics1[i] = kit.getImage(peteSrc1[i]);
petePics2[i] = kit.getImage(peteSrc2[i]);
petePics3[i] = kit.getImage(peteSrc3[i]);
petePics4[i] = kit.getImage(peteSrc4[i]);
petePics5[i] = kit.getImage(peteSrc5[i]);
}
back = kit.getImage("30.jpg");
} public void paint(Graphics comp) //创建绘图函数
{
Graphics2D comp2D = (Graphics2D) comp;
if (petePics[current] != null)
comp2D.drawImage(back,0,0,this);
comp2D.drawImage(petePics[current], x, y, this);
comp2D.drawImage(petePics1[current1], x1, y1, this);
comp2D.drawImage(petePics2[current2], x2, y2, this);
comp2D.drawImage(petePics3[current3], x3, y3, this);
comp2D.drawImage(petePics4[current4], x4, y4, this);
comp2D.drawImage(petePics5[current5], x5, y5, this);
}
}
class myammoo extends Thread
{
PetePanel pete ;
public myammoo(PetePanel pete)
{
this.pete=pete;
}
public void run()
{
for(;true;){
while(pete.live==1)
{
if(pete.current==0)
{
System.out.println("Thread is live");
pete.current5=0;
pete.x5=pete.x+10;
pete.y5=pete.y+10;
for(int i=0;i<500;i++)
{
pete.x5=pete.x5+1;
pete.y4=pete.y4+1;
try
{
sleep(1); //线程休眠
}
catch(InterruptedException e){}
pete.repaint();
}
}
else if(pete.current==1)
{
pete.current5=1;
pete.x5=pete.x+10;
pete.y5=pete.y+10;
for(int i=0;i<500;i++)
{
pete.x5=pete.x5-1;
pete.y4=pete.y4+1;
try
{
sleep(1); //线程休眠
}
catch(InterruptedException e){}
pete.repaint();
}
}
else if(pete.current==2)
{
pete.current5=2;
pete.x5=pete.x+10;
pete.y5=pete.y+10;
for(int i=0;i<500;i++)
{
pete.y5=pete.y5-1;
pete.y4=pete.y4+1;
try
{
sleep(1); //线程休眠
}
catch(InterruptedException e){}
pete.repaint();
}
}
else if(pete.current==3)
{
pete.current5=3;
pete.x5=pete.x+10;
pete.y5=pete.y+10;
for(int i=0;i<500;i++)
{
pete.y5=pete.y5+1;
pete.y4=pete.y4+1;
try
{
sleep(1); //线程休眠
}
catch(InterruptedException e){}
pete.repaint();
}
}
pete.x5=0-20;
pete.y5=0-20;
try
{
sleep(1000); //线程休眠
}
catch(InterruptedException e){}
}
}}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货