import java.awt.Dimension;
import javax.swing.JFrame;
public class ShootApp extends JFrame {
public ShootApp() {
setTitle("Shooting Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setPreferredSize(new Dimension(500, 500));
pack();
GamePanel p = new GamePanel();
setContentPane(p);
setResizable(false);
setVisible(true);
p.startGame();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new ShootApp();
}
}
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
class TargetThread extends Thread {
JLabel target = null;
GamePanel panel = null;
public TargetThread(GamePanel panel, JLabel target) {
this.panel = panel;
this.target = target;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
int x = target.getX() + 5;
int y = target.getY();
if (x > panel.getWidth())
target.setLocation(0, 0);
else
target.setLocation(x, y);
target.getParent().repaint();
try {
sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class BulletThread extends Thread {
GamePanel panel = null;
JLabel bullet = null;
JLabel target = null;
TargetThread tThread = null;
public BulletThread(GamePanel panel, TargetThread tThread, JLabel target, JLabel bullet) {
this.panel = panel;
this.bullet = bullet;
this.target = target;
this.tThread = tThread;
}
public boolean hit() {
return false;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
if (hit() == true) {
bullet.setLocation(panel.getWidth() / 2 - 10, panel.getHeight() - 70);
} else {
int x = bullet.getX();
int y = bullet.getY() - 10;
if (y + bullet.getHeight() < 0) {
bullet.setLocation(panel.getWidth() / 2 - 10, panel.getHeight() - 70);
} else
bullet.setLocation(x, y);
}
target.getParent().repaint();
try {
sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class GamePanel extends JPanel {
JLabel baseLabel = new JLabel();
JLabel bulletLabel = new JLabel();
JLabel targetLabel;
TargetThread tThread = null;
public GamePanel() {
setLayout(null);
setSize(500, 500);
baseLabel.setSize(50, 50);
baseLabel.setOpaque(true);
baseLabel.setBackground(Color.BLACK);
bulletLabel.setSize(20, 20);
bulletLabel.setOpaque(true);
bulletLabel.setBackground(Color.RED);
ImageIcon img = new ImageIcon("targetImage.jpg");
targetLabel = new JLabel(img);
targetLabel.setSize(img.getIconWidth(), img.getIconHeight());
add(baseLabel);
add(bulletLabel);
add(targetLabel);
}
public void startGame() {
baseLabel.setLocation(this.getWidth() / 2 - 25, this.getHeight() - 50);
bulletLabel.setLocation(this.getWidth() / 2 - 10, this.getHeight() - 70);
targetLabel.setLocation(0, 0);
tThread = new TargetThread(this, targetLabel);
tThread.start();
baseLabel.setFocusable(true);
baseLabel.requestFocus();
baseLabel.addKeyListener(new KeyAdapter() {
BulletThread bThread = null;
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyChar() == '\n') {
if (bThread == null || !bThread.isAlive()) {
bThread = new BulletThread(GamePanel.this, tThread, targetLabel, bulletLabel);
bThread.start();
}
}
}
});
}
现在目标呗子弹击中 目标和子弹没有任何变化