我用 Java 编程的游戏有问题。这是代码。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Game extends JFrame {
private static final long serialVersionUID = 1L;
Graphics dbg;
Image dbImage;
static Image block;
static Block block1 = new Block();
static Image player1;
static Player player = new Player(193, 143);
public Game() {
Image playerIcon = new ImageIcon("res/play.png").getImage();
setSize(500, 400);
setTitle("Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setIconImage(playerIcon);
setLocationRelativeTo(null);
setVisible(true);
addKeyListener(new InputHandler());
setBackground(Color.BLACK);
setResizable(false);
}
public static void main(String[] args) {
new Game();
Thread p = new Thread(player);
p.start();
}
@SuppressWarnings("static-access")
public void paint(Graphics g) {
try {
dbImage = ImageIO.read(new File("res/background.png"));
} catch (Exception e) {
e.printStackTrace();
}
try {
player1 = ImageIO.read(new File("res/play.png"));
} catch (Exception e) {
e.printStackTrace();
}
try {
block = ImageIO.read(new File("res/grass.png"));
} catch (Exception e) {
e.printStackTrace();
}
dbg = dbImage.getGraphics();
draw(dbg);
g.drawImage(dbImage, 0, 0, this);
g.drawImage(player1, player.x, player.y, this);
g.drawImage(block, block1.x, block1.y, this);
}
public void draw(Graphics g) {
repaint();
}
}
我只添加了这个类,因为这是我添加图像的地方。如果您想查看其他课程,请发表评论。问题是图像 player1 和块正在闪烁。