【发布时间】:2018-03-09 21:10:18
【问题描述】:
我是 Java 新手,我正试图让一艘船发射子弹。我想要的实际上是只要按住空格键按钮,就让飞船发射子弹。 我已经成功地让船在这里和那里移动并发射了子弹。然而,子弹就是不会上去。这是我的代码 -
package learningPackage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class Draw extends JFrame implements Runnable {
//Variables for the x and y coordinates, xDirection for modifying the values of x only.
int x, y, xDirection;
int bx, by;
Image dbImage;
Graphics dbGraphics;
boolean shot;
Rectangle bullet;
//Thread run
public void run() {
try {
while (true) {
move();
shoot();
//Setting sleep to 0 will make it light-speed!
Thread.sleep(5);
}
}
catch (Exception e) {
System.out.println("Error!");
}
}
//Ship move
//Ship moves only in one direction, x - axis
public void move() {
x += xDirection;
//Collision detection
if (x <= 10) {
x = 10;
}
if (x >= 415) {
x = 415;
}
}
//KeyListeners
public class AL extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == e.VK_LEFT) {
xDirection = -2;
}
if (keyCode == e.VK_RIGHT) {
xDirection = 2;
}
if (keyCode == e.VK_SPACE) {
shot = true;
}
}
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == e.VK_LEFT) {
xDirection = 0;
}
if (keyCode == e.VK_RIGHT) {
xDirection = 0;
}
if (keyCode == e.VK_SPACE) {
shot = false;
}
}
}
//Constructor for the game frame
public Draw() {
super("Game");
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
addKeyListener(new AL());
x = 200;
y = 465;
setVisible(true);
}
//Double - buffering
public void paint(Graphics g) {
dbImage = createImage(getWidth(), getHeight());
dbGraphics = dbImage.getGraphics();
paintComponent(dbGraphics);
g.drawImage(dbImage, 0, 0, this);
}
//All the graphics
public void paintComponent(Graphics g) {
bullet = new Rectangle(bx, by, 10, 10);
g.setColor(Color.RED);
//Ship rectangle
g.fillRect(x, y, 75, 25);
//Gun rectangle
g.fillRect(x + 32, y - 15, 10, 15);
//Setting the same values for bx and by as x and y so that the bullet will start from the Gun rectangle
bx = x + 32;
by = y - 15;
if (shot == true) {
g.setColor(Color.BLACK);
g.fillRect(bx, by, bullet.width, bullet.height);
}
repaint();
}
public void shoot() {
if (shot == true) {
by = by - 2;
}
if (by <= -5) {
//Resetting values
bx = x + 32;
by = y - 15;
bullet = new Rectangle(bx, by, 10, 10);
shot = false;
}
}
//Main method
public static void main(String[] args) {
Draw gameTry = new Draw();
Thread t1 = new Thread(gameTry);
t1.start();
}
}
Here's what happens when I just move the ship, working perfectly fine -
Here's what happens when I hold down space -
(抱歉无法在帖子中嵌入图片,我也是 Stack Overflow 的新手!)
我实际上是从教程中处理这段代码,但由于教程代码没有成功,我决定自己做,但我自己也做不到! 帮助一定会感激不尽!
【问题讨论】:
-
你永远不会改变你用作子弹的
Rectangle。如果您不告诉它移动,它就不会移动。 -
我很难弄清楚这段代码应该如何做任何事情。有一个用于船舶位置的变量,这些变量在按键上更新,但也为每个
Draw调用设置为预定义的值。目前这行得通吗? -
@TimothyGroote 很抱歉,我没有得到你。你能再详细给我解释一下吗?我不是通过更新 shoot() 方法中 y 轴的值来告诉它移动吗?
-
是的,船的运动很好,即使是碰撞。按下空格键时,只有子弹不会向上移动。它只是出现在船上(图片在帖子中),但它不会上升!
-
shoot()中的代码看起来不错,但是在您的paintcomponent方法中,您设置了by = y - 15;,有效地撤消了shoot()所做的更改。尝试设置by只在游戏开始,当你需要重置子弹的位置。