有一点困难。我正在写一个小游戏(试图写))有任何困难。这是游戏代码:
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 的图片一起移动来制作它