我正在创建一个穴居人扔石头的游戏,当你点击时,会产生一块石头。前五个左右工作正常,然后等到岩石离开屏幕,然后它们可以再次产卵。我希望它们在我单击时生成。
提前致谢
代码:
package com.russell.raphael.birds;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Start extends JFrame {
ImageIcon landImage, manImage, skyImage, RockPileImage, RockImage;
JLabel skylbl, manlbl, landlbl, rockPilelbl;
Bird[] birds = new Bird[10];
Rock[] rocks = new Rock[10000];
public static MouseListener throwrock;
public static void main(String[] args){
new Start();
}
public Start() {
setVisible(true);
initComp();
setSize(1000, 1100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("Not the Birds!!!");
setIconImage(Toolkit.getDefaultToolkit().getImage(Start.class.getResource("/com/russell/raphael/images/Icon.png")));
}
private void initComp() {
throwrock = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int eventX = 0, eventY = 0, sourceX = 420, sourceY = 840;
int rise = 0, run = 0;
try {
JLabel source = (JLabel) e.getSource();
eventX = (source.getLocation().x) + (e.getX());
eventY = (source.getLocation().y) + (e.getY());
rise = Math.abs(eventY - sourceY);
run = eventX - sourceX;
nextRock().start(rise, run);
} catch (Exception ex) {
ex.printStackTrace();
}
}
};
setResizable(false);
for(int counter =0; counter < rocks.length; counter++){
rocks[counter] = new Rock();
getContentPane().add(rocks[counter]);
}
landImage = new ImageIcon(
Start.class.getResource("/com/russell/raphael/images/land.png"));
manImage = new ImageIcon(
Start.class.getResource("/com/russell/raphael/images/man.png"));
skyImage = new ImageIcon(
Start.class.getResource("/com/russell/raphael/images/sky.jpg"));
RockPileImage = new ImageIcon(
Start.class
.getResource("/com/russell/raphael/images/rockpile.png"));
getContentPane().setLayout(null);
skylbl = new JLabel(skyImage);
skylbl.addMouseListener(throwrock);
skylbl.setLocation(0, 0);
skylbl.setSize(1010, 983);
skylbl.setVisible(true);
manlbl = new JLabel(manImage);
manlbl.setSize(200, 300);
manlbl.addMouseListener(throwrock);
manlbl.setLocation(400, 700);
landlbl = new JLabel(landImage);
landlbl.setBounds(0, 725, 1000, 400);
manlbl.addMouseListener(throwrock);
rockPilelbl = new JLabel();
rockPilelbl.setIcon(RockPileImage);
rockPilelbl.setBounds(236, 782, 220, 174);
getContentPane().add(rockPilelbl);
manlbl.addMouseListener(throwrock);
getContentPane().add(manlbl);
getContentPane().add(landlbl);
getContentPane().add(skylbl);
}
public Rock nextRock(){
for(int counter = 0; counter < rocks.length; counter++){
if(!rocks[counter].hasBeenUsed){
return rocks[counter];
}
}
return null;
}
}
下一节课:
package com.russell.raphael.birds;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class Rock extends JLabel {
Timer timer;
Thread thread;
boolean hasBeenUsed = false;
public Rock() {
super();
setBounds(415, 840, 37, 35);
setIcon(new ImageIcon(
Rock.class.getResource("/com/russell/raphael/images/Rock.png")));
setVisible(true);
}
public void start(final int rise, final int run) {
hasBeenUsed = true;
thread = new Thread() {
public void run() {
timer = new Timer(30, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
setBounds(getBounds().x + run / 20, getBounds().y
+ -rise / 20, getBounds().width,
getBounds().height);
if (getBounds().x < 0 || getBounds().y < 0
|| getBounds().y > 1000) {
timer.stop();
hasBeenUsed = false;
setBounds(415, 840, 37, 35);
thread.stop();
}
}
});
timer.start();
}
};
thread.start();
}
}
</code>