0

有一点困难。我正在写一个小游戏(试图写))有任何困难。这是游戏代码:

package Game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Board extends JPanel {

    private final String IMAGE_PATH_DOWN1 = "C:\\Users\\дНМ\\workspace\\Game\\image\\Down_Pic1.png";
    private final String IMAGE_PATH_DOWN2 = "C:\\Users\\дНМ\\workspace\\Game\\image\\Down_Pic2.png";

    private final String IMAGE_PATH_RIGHT1 = "C:\\Users\\дНМ\\workspace\\Game\\image\\Right_Pic1.png";
    private final String IMAGE_PATH_RIGHT2 = "C:\\Users\\дНМ\\workspace\\Game\\image\\Right_Pic2.png";

    private final String IMAGE_PATH_TOP1 = "C:\\Users\\дНМ\\workspace\\Game\\image\\Top_Pic1.png";
    private final String IMAGE_PATH_TOP2 = "C:\\Users\\дНМ\\workspace\\Game\\image\\Top_Pic2.png";

    private final String IMAGE_PATH_LEFT = "C:\\Users\\дНМ\\workspace\\Game\\image\\Left_Pic1.png";
    private final String IMAGE_PATH_LEFT2 = "C:\\Users\\дНМ\\workspace\\Game\\image\\Left_Pic2.png";

    private final String IMAGE_PATH_MAP_Z = "C:\\Users\\дНМ\\workspace\\Game\\image\\Map_Z.png";

    private static final long serialVersionUID = 1L;
    private Thing thing;
    private Timer repaintTimer;

    private Image imgCs;
    private Image imgMap;
    private static int speed = 3;

    public Board() {
        init();

    }

    private void init() {

        setFocusable(true);
        setDoubleBuffered(true);
        addKeyListener(new KeyHandler());
        setBackground(Color.BLACK);
        thing = new Thing();

        imgCs = Toolkit.getDefaultToolkit().getImage(IMAGE_PATH_DOWN1);
        imgMap = Toolkit.getDefaultToolkit().getImage(IMAGE_PATH_MAP_Z);

        repaintTimer = new Timer(50, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                repaint();
            }
        });

        repaintTimer.start();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        Graphics2D g2d = (Graphics2D) g;

        int width = imgCs.getWidth(this);
        int height = imgCs.getHeight(this);

        int scale = 4;
        speed = 3;

        int w = scale * width;
        int h = scale * height;

        //MAP START!
        int widthMap = imgMap.getWidth(this);
        int heightMap = imgMap.getHeight(this);
        int scalem = 4;
        int wm = scalem * widthMap;
        int hm = scalem * heightMap;

           for (int s = 0; s < wm * 10; s += wm) {
                for (int i = 0; i < hm * 10; i += hm) {
                    g2d.drawImage(imgMap, thing.getX()+s, thing.getY()+i,(int) wm, (int) hm, this);

                }
            }
        //MAP END

        g2d.drawImage(imgCs, thing.getX() * speed, thing.getY() * speed,
                (int) w, (int) h, this);

        Toolkit.getDefaultToolkit().sync();

    }

    private class KeyHandler extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            thing.pressed(e.getKeyCode());
            // LEFT!!!
            if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                if ((thing.getX() * speed % 2) == 0) {
                    imgCs = Toolkit.getDefaultToolkit().getImage(
                            IMAGE_PATH_LEFT);

                } else {
                    imgCs = Toolkit.getDefaultToolkit().getImage(
                            IMAGE_PATH_LEFT2);
                }
            }
            // RIGHT!
            if (e.getKeyCode() == KeyEvent.VK_RIGHT) {

                imgCs = Toolkit.getDefaultToolkit().getImage(IMAGE_PATH_RIGHT1);
                if ((thing.getX() * speed % 2) == 0) {
                    imgCs = Toolkit.getDefaultToolkit().getImage(
                            IMAGE_PATH_RIGHT1);
                } else {

                    imgCs = Toolkit.getDefaultToolkit().getImage(
                            IMAGE_PATH_RIGHT2);
                }
            }
            // DOWN!
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                if ((thing.getY() * speed % 2) == 0) {
                    imgCs = Toolkit.getDefaultToolkit().getImage(
                            IMAGE_PATH_DOWN1);

                } else {
                    imgCs = Toolkit.getDefaultToolkit().getImage(
                            IMAGE_PATH_DOWN2);

                }

            }
            // UP!
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                if ((thing.getY() * speed % 2) == 0) {

                    imgCs = Toolkit.getDefaultToolkit().getImage(
                            IMAGE_PATH_TOP1);

                } else {
                    imgCs = Toolkit.getDefaultToolkit().getImage(
                            IMAGE_PATH_TOP2);

                }

            }

        }

        @Override
        public void keyReleased(KeyEvent e) {
            //
        }

    }
}

另一个班级

package Game;

import java.awt.event.KeyEvent;

class Thing {

    private final int STEP = 3;
    private int x_pos;
    private int y_pos;

    public void pressed(int keyCode) {

        switch (keyCode) {
        case KeyEvent.VK_LEFT: {
            x_pos -= STEP;
            break;
        }
        case KeyEvent.VK_UP: {
            y_pos -= STEP;
            break;
        }
        case KeyEvent.VK_RIGHT: {
            x_pos += STEP;
            break;
        }
        case KeyEvent.VK_DOWN: {
            y_pos += STEP;
            break;
        }
        }
    }

    public int getX() {
        return x_pos;
    }

    public int getY() {
        return y_pos;
    }

}

还有另一堂课)

package Game;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class ThingMove {

    private String NAME = "Title";
    private int WIDTH = 500;
    private int HEIGHT = 470;

    public ThingMove() {

        JFrame frame = new JFrame(NAME);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH, HEIGHT);
        frame.setLocationRelativeTo(null);

        frame.add(new Board());

        frame.setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ThingMove();
            }
        });
    }
}

后退,使代码没有发生。

代码有效,图片显示了英雄,但是当他移动到盒子的尽头时,它会从盒子下面消失并消失,我希望你在移动图像和背景时与她一起移动。背景不是静止不动并与 hero.help 的图片一起移动来制作它

http://pastebin.com/w4kR7FuQ

http://pastebin.com/jKTCbUpY

http://pastebin.com/bPxSrrrR

4

1 回答 1

2

不会回答你的问题,但有一些想法:

  1. 不要覆盖paint()。自定义绘画是通过覆盖paintComponent() 来完成的。阅读 Swing 教程中关于自定义绘画的部分以获取更多信息。

  2. 不要使用 KeyListener。Swing 被设计为与键绑定一起使用。有关详细信息,请参阅使用键盘进行运动。

  3. 不要阅读每个事件的图像。应在程序开始时读取图像以获得更好的性能。

于 2013-10-10T19:26:15.440 回答