我正在用 Java 7 为 PC 创建一个现代版本的突破,但我遇到了一个似乎无法解决的主要问题。
为什么我的语句不会if
在此执行,即使按下空格键,live is 大于 0,并且球的 y 坐标大于 600?
我已经用很多打印语句和日志文件对此进行了测试,但似乎没有任何效果。
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.*;
//import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Game extends JFrame implements Runnable , KeyListener {
private static final long serialVersionUID = 1L;
private Graphics dbg;
private Image dbi;
int level = 0;
int lives = 6;
int powerup = 0;
int blocksGone = 0;
int vy = 0;
int vx = 0;
int by = 640;
int bx = 0;
int[] powerups = new int[8];
ImageIcon i0 = new ImageIcon("paddle.gif");
ImageIcon i1 = new ImageIcon("sidebar.gif");
ImageIcon i2 = new ImageIcon("laserpaddle.gif");
ImageIcon i3 = new ImageIcon("paddleCannon.gif");
ImageIcon i4 = new ImageIcon("laser.gif");
ImageIcon i5 = new ImageIcon("bullet.gif");
ImageIcon i6 = new ImageIcon("lifeIcon.gif");
ImageIcon i7 = new ImageIcon("ball.png");
ImageIcon i8 = new ImageIcon("bomb.png");
ImageIcon i9 = new ImageIcon("atomic.png");
//ImageIcon i10 = new ImageIcon("bullet.gif");
Image paddle;
Image side;
Image bullet;
Image bomb;
Image ball;
Image atomc;
Image life;
Image dead;
int paddlex = 400;
int pdx = 0;
public Game() {
// TODO Auto-generated constructor stub
setVisible(true);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setTitle("Breakout Smash");
setSize(1024,560);
setResizable(false);
this.addKeyListener(this);
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
init();
while (true) {
move();
Thread.sleep(5);
}
} catch (Exception e) {
System.out.println ("java.lang.Execption in line 25, class Game. Stacktrace:");
e.printStackTrace();
}
}
// TODO Auto-generated method stub
private void init() {
// TODO Auto-generated method stub
paddle = i0.getImage();
side = i1.getImage();
ball = i7.getImage();
}
public void paint (Graphics g) {
dbi = createImage (getWidth(),getHeight());
dbg = dbi.getGraphics();
paintComponent(dbg);
g.drawImage(dbi , 0 , 0 , this);
}
public void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.setColor(Color.DARK_GRAY);
g.fillRect(0,0,getWidth(),getHeight());
g.drawImage(side, this.getWidth() - side.getWidth(null), 0, null);
g.setColor(Color.CYAN);
g.drawString("Score", 950, 50);
g.drawString("Lives: " + lives , 880, 50);
g.drawImage(paddle, paddlex, 520, null);
g.drawImage(ball, bx, by, null);
repaint();
}
public void move () {
//if (by <= getHeight()) {
if (paddlex > 0 && paddlex < getWidth() - side.getWidth(null) - 50) {
paddlex += pdx;
} if (paddlex >= getWidth() - side.getWidth(null) - 50) {
paddlex-= 2;
} if (paddlex <= 0) {
paddlex++;
}
if (bx > 0 && bx < getWidth() - side.getWidth(null) && by > 10 && by < getHeight()) {
bx += vx;
by -= vy;
} if (by > 560) {
by = 575;
bx = 0;
} if (bx < 5) {
bx += 3;
vx = vx * -1;
} if (bx > getWidth() - side.getWidth(null) - 5) {
bx -= 3;
vx = vx * -1;
} if (by < 15) {
by++;
vy = vy * -1;
} if (by >= 515 && by <= 515 + paddle.getHeight(null) && bx + 8 > paddlex && bx + 8 < paddlex + paddle.getWidth(null)) {
vy = vy * -1;
vy ++;
vx *= pdx;
/*if (pdx == -2) {
vx = vx * -2;
}
if (pdx == 2) {
vx = vx * 2;
}*/
}
//} else {
//vy = 0;
//vx = 0;
//}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ESCAPE) {
int quit = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?", "Quit?", JOptionPane.YES_NO_OPTION);
if (quit == 0) System.exit(0);
}
else if (keyCode == KeyEvent.VK_LEFT) {
pdx = -2;
}
else if (keyCode == KeyEvent.VK_RIGHT) {
pdx = 2;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
pdx = 0;
}
else if (keyCode == KeyEvent.VK_RIGHT) {
pdx = 0;
}
else if (keyCode == KeyEvent.VK_SPACE) {
//if (by > 600 && lives >= 0) { // this is my problem here
lives--;
vy = 1;
vx = pdx;
bx = paddlex + 10;
by = 500;
System.out.println ("Space hit!");
/*if (lives == 0) {
JOptionPane.showMessageDialog(null, "Game Over!" , "!", JOptionPane.WARNING_MESSAGE);
}
} */
}
}
}
我的代码中一定有一些我没有看到的东西。最大的问题是:为什么球不会在按空格键时重生并同时减去一个生命?