如何解决Java上的吃豆人图片代码不可见
我正在eclipse上为Java开发Pac Man游戏。所有代码全部工作...除了一部分...图像。您会看到,我可以四处走走并收集颗粒,除了鬼魂和吃豆人以外的所有东西都是看不见的。这是我的游戏逻辑和《吃豆人》抽象类的代码。
package pacman;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Model extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
private Dimension d;
private final Font smallFont = new Font("Arial",Font.BOLD,14);
private boolean inGame = false;
private boolean dying = false;
private final int BLOCK_SIZE = 24;
private final int N_BLOCKS = 15;
private final int SCREEN_SIZE = N_BLOCKS * BLOCK_SIZE;
private final int MAX_GHOSTS = 12;
private final int PACMAN_SPEED = 6;
private int N_GHOSTS = 6;
private int lives,score;
private int[] dx,dy;
private int[] ghost_x,ghost_y,ghost_dx,ghost_dy,ghostSpeed;
private Image heart,ghost;
private Image up,down,left,right;
private int pacman_x,pacman_y,pacmand_x,pacmand_y;
private int req_dx,req_dy;
//binary for the stage
private final short levelData[] = {
19,18,22,17,16,24,20,25,28,19,21,26,28
};
private final int validSpeeds[] = {1,2,3,4,6,8};
private final int maxSpeed = 6;
private int currentSpeed = 3;
private short[] screenData;
private Timer timer;
public Model() {
loadImages();
initvariables();
addKeyListener(new TAdapter());
setFocusable(true);
initGame();
}
private void loadImages() {
down = new ImageIcon("/src/images/down.gif").getimage();
up = new ImageIcon("/src/images/up.gif").getimage();
left = new ImageIcon("/src/images/left.gif").getimage();
right = new ImageIcon("/src/images/right.gif").getimage();
ghost = new ImageIcon("/src/images/ghost.gif").getimage();
heart = new ImageIcon("/src/images/heart.png").getimage();
}
private void initvariables() {
screenData = new short[N_BLOCKS * N_BLOCKS];
d = new Dimension(400,400);
ghost_x = new int[MAX_GHOSTS];
ghost_dx = new int[MAX_GHOSTS];
ghost_y = new int[MAX_GHOSTS];
ghost_dy = new int[MAX_GHOSTS];
ghostSpeed = new int[MAX_GHOSTS];
dx = new int[4];
dy = new int[4];
timer = new Timer(40,this);
timer.start();
}
private void playGame(Graphics2D g2d) {
if (dying) {
death();
} else {
movePacman();
drawPacman(g2d);
moveGhosts(g2d);
checkMaze();
}
}
private void showIntroScreen(Graphics2D g2d) {
String start = "Press SPACE to start";
g2d.setColor(Color.yellow);
g2d.drawString(start,(int) ((SCREEN_SIZE)/3.25),150);
}
private void drawscore(Graphics2D g) {
g.setFont(smallFont);
g.setColor(new Color(5,181,79));
String s = "score: " + score;
g.drawString(s,SCREEN_SIZE / 2 + 96,SCREEN_SIZE + 16);
for (int i = 0; i < lives; i++) {
g.drawImage(heart,i * 28 + 8,SCREEN_SIZE + 1,this);
}
}
private void checkMaze() {
int i = 0;
boolean finished = true;
while (i < N_BLOCKS * N_BLOCKS && finished) {
if ((screenData[i] & 48) != 0) {
finished = false;
}
i++;
}
if (finished) {
score += 50;
if (N_GHOSTS < MAX_GHOSTS) {
N_GHOSTS++;
}
if (currentSpeed < maxSpeed) {
currentSpeed++;
}
initLevel();
}
}
private void death() {
lives--;
if (lives == 0) {
inGame = false;
}
continueLevel();
}
private void moveGhosts(Graphics2D g2d) {
int pos;
int count;
for (int i = 0; i < N_GHOSTS; i++) {
if (ghost_x[i] % BLOCK_SIZE == 0 && ghost_y[i] % BLOCK_SIZE == 0) {
pos = ghost_x[i] / BLOCK_SIZE + N_BLOCKS * (int) (ghost_y[i] / BLOCK_SIZE);
count = 0;
if ((screenData[pos] & 1) == 0 && ghost_dx[i] != 1) {
dx[count] = -1;
dy[count] = 0;
count++;
}
if ((screenData[pos] & 2) == 0 && ghost_dy[i] != 1) {
dx[count] = 0;
dy[count] = -1;
count++;
}
if ((screenData[pos] & 4) == 0 && ghost_dx[i] != -1) {
dx[count] = 1;
dy[count] = 0;
count++;
}
if ((screenData[pos] & 8) == 0 && ghost_dy[i] != -1) {
dx[count] = 0;
dy[count] = 1;
count++;
}
if (count == 0) {
if ((screenData[pos] & 15) == 15) {
ghost_dx[i] = 0;
ghost_dy[i] = 0;
} else {
ghost_dx[i] = -ghost_dx[i];
ghost_dy[i] = -ghost_dy[i];
}
} else {
count = (int) (Math.random() * count);
if (count > 3) {
count = 3;
}
ghost_dx[i] = dx[count];
ghost_dy[i] = dy[count];
}
}
ghost_x[i] = ghost_x[i] + (ghost_dx[i] * ghostSpeed[i]);
ghost_y[i] = ghost_y[i] + (ghost_dy[i] * ghostSpeed[i]);
drawGhost(g2d,ghost_x[i] + 1,ghost_y[i] + 1);
if (pacman_x > (ghost_x[i] - 12) && pacman_x < (ghost_x[i] + 12)
&& pacman_y > (ghost_y[i] - 12) && pacman_y < (ghost_y[i] + 12)
&& inGame) {
dying = true;
}
}
}
private void drawGhost(Graphics2D g2d,int x,int y) {
g2d.drawImage(ghost,x,y,this);
}
private void movePacman() {
int pos;
short ch;
if (pacman_x % BLOCK_SIZE == 0 && pacman_y % BLOCK_SIZE == 0) {
pos = pacman_x / BLOCK_SIZE + N_BLOCKS * (int) (pacman_y / BLOCK_SIZE);
ch = screenData[pos];
if ((ch & 16) != 0) {
screenData[pos] = (short) (ch & 15);
score++;
}
if (req_dx != 0 || req_dy != 0) {
if (!((req_dx == -1 && req_dy == 0 && (ch & 1) != 0)
|| (req_dx == 1 && req_dy == 0 && (ch & 4) != 0)
|| (req_dx == 0 && req_dy == -1 && (ch & 2) != 0)
|| (req_dx == 0 && req_dy == 1 && (ch & 8) != 0))) {
pacmand_x = req_dx;
pacmand_y = req_dy;
}
}
// Check for standstill
if ((pacmand_x == -1 && pacmand_y == 0 && (ch & 1) != 0)
|| (pacmand_x == 1 && pacmand_y == 0 && (ch & 4) != 0)
|| (pacmand_x == 0 && pacmand_y == -1 && (ch & 2) != 0)
|| (pacmand_x == 0 && pacmand_y == 1 && (ch & 8) != 0)) {
pacmand_x = 0;
pacmand_y = 0;
}
}
pacman_x = pacman_x + PACMAN_SPEED * pacmand_x;
pacman_y = pacman_y + PACMAN_SPEED * pacmand_y;
}
private void drawPacman(Graphics2D g2d) {
if (req_dx == -1) {
g2d.drawImage(left,pacman_x + 1,pacman_y + 1,this);
} else if (req_dx == 1) {
g2d.drawImage(right,this);
} else if (req_dy == -1) {
g2d.drawImage(up,this);
} else {
g2d.drawImage(down,this);
}
}
private void drawMaze(Graphics2D g2d) {
short i = 0;
int x,y;
for (y = 0; y < SCREEN_SIZE; y += BLOCK_SIZE) {
for (x = 0; x < SCREEN_SIZE; x += BLOCK_SIZE) {
g2d.setColor(new Color(0,72,251));
g2d.setstroke(new Basicstroke(5));
if ((levelData[i] == 0)) {
g2d.fillRect(x,BLOCK_SIZE,BLOCK_SIZE);
}
if ((screenData[i] & 1) != 0) {
g2d.drawLine(x,y + BLOCK_SIZE - 1);
}
if ((screenData[i] & 2) != 0) {
g2d.drawLine(x,x + BLOCK_SIZE - 1,y);
}
if ((screenData[i] & 4) != 0) {
g2d.drawLine(x + BLOCK_SIZE - 1,y + BLOCK_SIZE - 1);
}
if ((screenData[i] & 8) != 0) {
g2d.drawLine(x,y + BLOCK_SIZE - 1,y + BLOCK_SIZE - 1);
}
if ((screenData[i] & 16) != 0) {
g2d.setColor(new Color(255,255,255));
g2d.filloval(x + 10,y + 10,6);
}
i++;
}
}
}
private void initGame() {
lives = 3;
score = 0;
initLevel();
N_GHOSTS = 6;
currentSpeed = 3;
}
private void initLevel() {
int i;
for (i = 0; i < N_BLOCKS * N_BLOCKS; i++) {
screenData[i] = levelData[i];
}
continueLevel();
}
private void continueLevel() {
int dx = 1;
int random;
for (int i = 0; i < N_GHOSTS; i++) {
ghost_y[i] = 4 * BLOCK_SIZE; //start position
ghost_x[i] = 4 * BLOCK_SIZE;
ghost_dy[i] = 0;
ghost_dx[i] = dx;
dx = -dx;
random = (int) (Math.random() * (currentSpeed + 1));
if (random > currentSpeed) {
random = currentSpeed;
}
ghostSpeed[i] = validSpeeds[random];
}
pacman_x = 7 * BLOCK_SIZE; //start position
pacman_y = 11 * BLOCK_SIZE;
pacmand_x = 0; //reset direction move
pacmand_y = 0;
req_dx = 0; // reset direction controls
req_dy = 0;
dying = false;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.black);
g2d.fillRect(0,d.width,d.height);
drawMaze(g2d);
drawscore(g2d);
if (inGame) {
playGame(g2d);
} else {
showIntroScreen(g2d);
}
Toolkit.getDefaultToolkit().sync();
g2d.dispose();
}
//controls
class TAdapter extends KeyAdapter {
@Override
public void keypressed(KeyEvent e) {
int key = e.getKeyCode();
if (inGame) {
if (key == KeyEvent.VK_LEFT) {
req_dx = -1;
req_dy = 0;
} else if (key == KeyEvent.VK_RIGHT) {
req_dx = 1;
req_dy = 0;
} else if (key == KeyEvent.VK_UP) {
req_dx = 0;
req_dy = -1;
} else if (key == KeyEvent.VK_DOWN) {
req_dx = 0;
req_dy = 1;
} else if (key == KeyEvent.VK_ESCAPE && timer.isRunning()) {
inGame = false;
}
} else {
if (key == KeyEvent.VK_SPACE) {
inGame = true;
initGame();
}
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}
这是吃豆人的主要抽象类。
package pacman;
import javax.swing.JFrame;
public class Pacman extends JFrame{
private static final long serialVersionUID = 1L;
public Pacman() {
add(new Model());
}
public static void main(String[] args) {
Pacman pac = new Pacman();
pac.setVisible(true);
pac.setTitle("Pacman");
pac.setSize(380,420);
pac.setDefaultCloSEOperation(EXIT_ON_CLOSE);
pac.setLocationRelativeto(null);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。