Java 爪哇精灵坠落?

Java 爪哇精灵坠落?,java,swing,graphics,applet,Java,Swing,Graphics,Applet,对不起,我不知道怎么把标题写好。但我正在制作一个Java游戏,在屏幕底部有一个桨,你可以左右移动它以避免小行星坠落。我有桨在工作,但我不知道如何制造一颗坠落的小行星。小行星目前只是一个长方形。我也希望能够调整速度和多少小行星落下。这是我的密码: public class Main extends Applet implements KeyListener, MouseListener { private Rectangle rect; private Rectangle asteroidrect

对不起,我不知道怎么把标题写好。但我正在制作一个Java游戏,在屏幕底部有一个桨,你可以左右移动它以避免小行星坠落。我有桨在工作,但我不知道如何制造一颗坠落的小行星。小行星目前只是一个长方形。我也希望能够调整速度和多少小行星落下。这是我的密码:

public class Main extends Applet implements KeyListener, MouseListener {
private Rectangle rect;
private Rectangle asteroidrect;
private ArrayList<Integer> keysDown;
private Image dbImage;
private Graphics dbg;
Random randomGenerator = new Random();
int speed = 8;
int level = 1;     // change to 0 once start menu works
int xpos, ypos; 
int blockx = 0;
int blocky = 0;
int width = 1024;
int height = 768;
String version = "0.0.1";
public static final int START_X_POS = 160;
public static final int START_Y_POS = 160;
public static final int START_WIDTH = 256;
public static final int START_HEIGHT = 64;
boolean startClicked; 
boolean asteroid = false;


public void init() {
    setSize(width, height); 
    addKeyListener(this);
    addMouseListener(this);
    setBackground(Color.black); 
    Frame c = (Frame)this.getParent().getParent();
    c.setTitle("Asteroid Attack - Version " + version);
    keysDown = new ArrayList<Integer>();
    rect = new Rectangle(460, 700, 64, 12);
    asteroidrect = new Rectangle(0, 0, 48, 48);
    addAsteroid();
}

public void update(Graphics g) {
    dbImage = createImage(this.getSize().width, this.getSize().height);
    dbg = dbImage.getGraphics ();
    if (dbImage == null) {}
    dbg.setColor(getBackground ());
    dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
    dbg.setColor(getForeground());
    paint(dbg);
    g.drawImage(dbImage, 0, 0, this);
}


public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    if (level != 0) {
        g2.setPaint(Color.gray);
        g2.fill(rect);
        //if (asteroid == true) {
            g2.setPaint(Color.red);
            g2.fill(asteroidrect);
        //}
    }
}

@Override
public void keyPressed(KeyEvent e) {
    if (!keysDown.contains(e.getKeyCode()))
        keysDown.add(new Integer(e.getKeyCode()));
    moveChar();
}

@Override
public void keyReleased(KeyEvent e) {
    keysDown.remove(new Integer(e.getKeyCode()));
}

public void moveChar() {
    if (level != 0) {
        int x = rect.x;
        int y = rect.y;
        if (keysDown.contains(KeyEvent.VK_LEFT)) {
            x -= speed;
        }
        if (keysDown.contains(KeyEvent.VK_RIGHT)) {
            x += speed;
        }
        rect.setLocation(x, y);
        repaint(); 
    }
}

public void addAsteroid() {
    asteroid = true;    
}

@Override
public void keyTyped(KeyEvent e) {}

@Override
public void mouseClicked(MouseEvent me) {
    if (level == 0) {
        xpos = me.getX(); 
        ypos = me.getY();
        if (xpos >= START_X_POS && ypos >= START_Y_POS && xpos <= START_X_POS + START_WIDTH && ypos <= START_X_POS + START_HEIGHT ) {
            level = 1;
        }
    }
}

@Override
public void mouseEntered(MouseEvent me) {}

@Override
public void mouseExited(MouseEvent me) {}

@Override
public void mouseReleased(MouseEvent me) {}

@Override
public void mousePressed(MouseEvent me) {}
}
public类主扩展Applet实现KeyListener、MouseStener{
私有矩形矩形;
私有矩形和矩形;
私有arraylistkeysdown;
私有镜像dbImage;
专用图形数据库;
Random randomGenerator=新的Random();
内速度=8;
int level=1;//开始菜单工作后更改为0
int XPO,YPO;
int blockx=0;
int blocky=0;
整数宽度=1024;
内部高度=768;
字符串版本=“0.0.1”;
公共静态最终int START_X_POS=160;
公共静态最终int START_Y_POS=160;
公共静态最终整型起始宽度=256;
公共静态最终整数起始高度=64;
布尔startClicked;
布尔小行星=假;
公共void init(){
设置尺寸(宽度、高度);
addKeyListener(此);
addMouseListener(这个);
挫折背景(颜色:黑色);
Frame c=(Frame)this.getParent().getParent();
c、 setTitle(“小行星攻击-版本”+版本);
keysDown=newarraylist();
rect=新矩形(460700,64,12);
asteroidrect=新矩形(0,0,48,48);
添加小行星();
}
公共空间更新(图g){
dbImage=createImage(this.getSize().width,this.getSize().height);
dbg=dbImage.getGraphics();
如果(dbImage==null){}
setColor(getBackground());
dbg.fillRect(0,0,this.getSize().width,this.getSize().height);
setColor(get前台());
涂料(dbg);
g、 drawImage(dbImage,0,0,this);
}
公共空间涂料(图g){
图形2d g2=(图形2d)g;
如果(级别!=0){
g2.setPaint(颜色为灰色);
g2.填充(rect);
//如果(小行星==真){
g2.setPaint(颜色为红色);
g2.填充(小行星直线);
//}
}
}
@凌驾
按下公共无效键(按键事件e){
如果(!keysDown.contains(例如getKeyCode()))
add(新的整数(例如getKeyCode());
moveChar();
}
@凌驾
公共无效密钥已释放(密钥事件e){
remove(新整数(例如getKeyCode());
}
公共void moveChar(){
如果(级别!=0){
int x=rect.x;
int y=矩形y;
if(keysDown.contains(KeyEvent.VK_左)){
x-=速度;
}
if(keysDown.contains(KeyEvent.VK_RIGHT)){
x+=速度;
}
直线定位(x,y);
重新油漆();
}
}
小行星(){
小行星=真;
}
@凌驾
public void keyTyped(KeyEvent e){}
@凌驾
公共无效mouseClicked(MouseEvent me){
如果(级别==0){
xpos=me.getX();
ypos=me.getY();

如果(xpos>=START\u X\u POS&&ypos>=START\u Y\u POS&&xpos查看此示例。我使用了三个不同的小行星(矩形)使用不同的x点和y点。我用不同的偏移量绘制它们。我使用Sing计时器设置重新绘制的延迟。当每隔这么多毫秒,y位置将更改并重新绘制。我还添加了向上和向下键绑定以减慢或加快延迟。如果有任何问题,请告诉我

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Asteroids extends JPanel {
    int astr1X = 50;
    int astr1Y = 30;
    int astr2X = 150;        // x and y for 3 different asteriods
    int astr2Y = 60;
    int astr3X = 250;
    int astr3Y = 45;
    private final static int OFFSET = 5;
    private final static int WIDTH = 20;
    private final static int HEIGHT = 20;
    private Timer timer = null;

    public Asteroids() {
        timer = new Timer(300, new ActionListener() {     // timer with 150 millisecond delay
            public void actionPerformed(ActionEvent e) {
                astr1Y += OFFSET;                         // add 5 t the y poistion
                astr2Y += OFFSET;
                astr3Y += OFFSET;

                if (astr1Y > 590)        // if  y is size of screen
                    astr1Y = 10;         // make it 0. This brings 
                if (astr2Y > 590)        // asteroid back to top
                    astr2Y = 10;
                if (astr3Y > 590) 
                    astr3Y = 10;

                repaint();
            }
        });
        timer.start();

        Action downAction = new AbstractAction() {        // slows down the timer
            public void actionPerformed(ActionEvent e) {
                int delay = timer.getDelay();
                if (delay < 1000) {
                    delay += 100;
                    timer.setDelay(delay);
                }
            }
        };

        Action upAction = new AbstractAction() {         // speeds up the timer
            public void actionPerformed(ActionEvent e) {
                int delay = timer.getDelay();
                if (delay > 100) {
                    delay -= 100;
                    timer.setDelay(delay);
                }
            }
        };

        getInputMap().put(KeyStroke.getKeyStroke("UP"), "upAction");  // up key binding
        getActionMap().put("upAction", upAction);
        getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "downAction");  // down key binding
        getActionMap().put("downAction", downAction);
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.add(new Asteroids());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }

    public Dimension getPreferredSize() {
        return new Dimension(350, 600);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.fillRect(astr1X, astr1Y, WIDTH, HEIGHT);
        g.fillRect(astr2X, astr2Y, WIDTH, HEIGHT);
        g.fillRect(astr3X, astr3Y, WIDTH, HEIGHT);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.AbstractAction;
导入javax.swing.Action;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.KeyStroke;
导入javax.swing.SwingUtilities;
导入javax.swing.Timer;
公共级小行星扩展到JPanel{
1X=50;
int astr1Y=30;
int astr2X=150;//3个不同星号的x和y
int astr2Y=60;
int astr3X=250;
int astr3Y=45;
专用最终静态整数偏移=5;
专用最终静态整数宽度=20;
专用最终静态内部高度=20;
专用定时器=空;
公共小行星(){
计时器=新计时器(300,新ActionListener(){//具有150毫秒延迟的计时器
已执行的公共无效操作(操作事件e){
astr1Y+=OFFSET;//在y位置加上5t
ast2y+=偏移量;
astr3Y+=偏移量;
if(ast1y>590)//如果y是屏幕的大小
astr1Y=10;//设为0。这将
if(ast2y>590)//小行星返回顶部
ast2y=10;
如果(astr3Y>590)
astr3Y=10;
重新油漆();
}
});
timer.start();
Action downAction=new AbstractAction(){//减慢计时器的运行速度
已执行的公共无效操作(操作事件e){
int delay=timer.getDelay();
如果(延迟<1000){
延迟+=100;
定时器。设置延迟(延迟);
}
}
};
Action upAction=new AbstractAction(){//加速计时器
已执行的公共无效操作(操作事件e){
int delay=timer.getDelay();
如果(延迟>100){
延迟-=100;
定时器。设置延迟(延迟);
}
}
};
getInputMap().put(KeyStroke.getKeyStroke(“UP”),“upAction”);//向上键绑定
getActionMap().put(“upAction”,upAction);
getInputMap().put(KeyStroke.getKeyStroke(“向下”),“向下操作”);//向下键绑定
getActionMap().put(“downAction”,downAction);
}
私有静态void createAndShowGui(){
JFrame=新JFrame();
frame.add(新小行星());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);