Java游戏:射击子弹

Java游戏:射击子弹,java,Java,我(有点)不熟悉用Java创建游戏。我以前做过一些简单的游戏,比如收集袋子的游戏,但现在我想做一个自上而下的僵尸射击游戏。我已经有了一个可以移动的球员,但现在我想实现射击问题是,我不知道如何根据玩家面对的屏幕部分,制作一颗从玩家右/上/下/左射向屏幕末端的新子弹。 我已将所有代码粘贴到下面(4个类): 这是我的游戏面板课程。 这是我的主要课程: package me.mateo226.main; import java.awt.Container; import java.awt.event.

我(有点)不熟悉用Java创建游戏。我以前做过一些简单的游戏,比如收集袋子的游戏,但现在我想做一个自上而下的僵尸射击游戏。我已经有了一个可以移动的球员,但现在我想实现射击问题是,我不知道如何根据玩家面对的屏幕部分,制作一颗从玩家右/上/下/左射向屏幕末端的新子弹。 我已将所有代码粘贴到下面(4个类):

这是我的游戏面板课程。 这是我的主要课程:

package me.mateo226.main;

import java.awt.Container;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Main extends JFrame implements WindowListener {
    private static final long serialVersionUID = 1L;
    private static GamePanel panel;
    public static boolean DEBUGGING = false;

    public Main(){
        super("The Gun Reactor");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addWindowListener(this);
        Container c = getContentPane();
        panel = new GamePanel();
        c.add(panel, "Center");
        setResizable(false);
        pack();
        setLocationRelativeTo(null);
        if(JOptionPane.showConfirmDialog(null, "Enable debugging?") == 1){
            DEBUGGING = true;
        } else {
            DEBUGGING = false;
        }
        setVisible(true);

    }

    @Override
    public void windowActivated(WindowEvent arg0) {


    }

    @Override
    public void windowClosed(WindowEvent arg0) {

    }

    @Override
    public void windowClosing(WindowEvent arg0) {

    }

    @Override
    public void windowDeactivated(WindowEvent arg0) {

    }

    @Override
    public void windowDeiconified(WindowEvent arg0) {

    }

    @Override
    public void windowIconified(WindowEvent arg0) {

    }

    @Override
    public void windowOpened(WindowEvent arg0) {

    }

    public static void main(String args[]){
        new Main();
    }


}
这是我的玩家课程:

package me.mateo226.entities;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Player {

    public int x, y;
    public float moveSpeed = 0.1f;
    private BufferedImage playerTexture;;

    public Player(int x, int y, String texturePath){
        this.x = x;
        this.y = y;
        try {
        playerTexture = ImageIO.read(new File(texturePath));
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    public void drawPlayer(Graphics g){

        g.setColor(Color.white);
        g.drawImage(playerTexture, x, y, null);

    }



}
最后,这是我的bullet类,我真的不知道如何使用,甚至不知道如何正确制作:

package me.mateo226.guns;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import me.mateo226.main.GamePanel;

public class Bullet {

    private int x, y;
    private BufferedImage bulletTexture;
    private float bulletSpeed = 0.1f;

    public Bullet(String bulletTexturePath, String dir, int x, int y) {
        this.x = x;
        this.y = y;
        try {
            bulletTexture = ImageIO.read(new File(bulletTexturePath));
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public void drawBullet(Graphics g) {
        g.setColor(Color.white);
        g.drawImage(bulletTexture, x, y, null);
    }

    public void shootBullet(Graphics g, String dir) {
        switch (dir) {
        case "left":
            while (x > -32) {
                x -= bulletSpeed * GamePanel.delta;
                drawBullet(g);
            }
            break;
        case "right":
            while (x < 700) {
                x += bulletSpeed * GamePanel.delta;
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            }
            break;
        case "up":
            while (y > -32) {
                y -= bulletSpeed * GamePanel.delta;
                drawBullet(g);
            }
            break;
        case "down":
            while (y < 480) {
                y += bulletSpeed * GamePanel.delta;
                drawBullet(g);
            }
            break;
        }
    }

}
包me.mateo226.guns;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入javax.imageio.imageio;
导入me.mateo226.main.GamePanel;
公营子弹{
私有整数x,y;
私有缓冲图像;
专用浮球速度=0.1f;
公共项目符号(字符串bulletTexturePath、字符串目录、int x、int y){
这个.x=x;
这个。y=y;
试一试{
bulletTexture=ImageIO.read(新文件(bulletTexturePath));
}捕获(IOE异常){
e、 printStackTrace();
}
}
公共空标(图g){
g、 setColor(Color.white);
g、 drawImage(bulletTexture,x,y,null);
}
公共空射弹(图形g,字符串方向){
交换机(dir){
案例“左”:
而(x>-32){
x-=bulletSpeed*GamePanel.delta;
拔弹(g);
}
打破
案例“权利”:
而(x<700){
x+=bulletSpeed*GamePanel.delta;
试一试{
睡眠(500);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
打破
案例“up”:
而(y>-32){
y-=bulletSpeed*GamePanel.delta;
拔弹(g);
}
打破
案例“向下”:
而(y<480){
y+=bulletSpeed*GamePanel.delta;
拔弹(g);
}
打破
}
}
}
任何帮助都会很好!多谢各位

编辑 我刚读到你只有四个方向。在这种情况下,不需要方向向量。只需设定一次方向。 好的,下面是代码示例

首先是游戏循环。在gameloop中添加FiredBullet以进行更新。每个发射的子弹都会沿着其方向向量移动

private void gameUpdate() {
    if (!paused && !gameOver) {
        movePlayer();
        foreach(Bullet bullet : player.getFiredBullets(){
             bullet.moveInDirection();
        }
    }
}
还有你的子弹课:

public class Bullet {
    private Direction direction;
    private float speed = 1.2f;
    private int x;
    private int y;

    public Bullet(int x, int y){
         this.x =x;
         this.y=y;        
    }

    public void launchBullet(Direction direction){
        this.direction=direction;
    }

    public void moveInDirection() {
        //move the bullet with speed in the set direction. Same as you already have but without the while loop.
    }
}
所以玩家应该有一个开火的方法。 这将根据玩家的位置创建子弹。子弹的方向与玩家面对的方向相同。子弹会被添加到列表中,所以它会在每次游戏循环中更新

public class Player {
   private List<Bullet> firedBullets = new ArrayList<Bullet>();

   public void fire(){
       Bullet bullet = new Bullet(playerX, playerY);
       firedbullets.add(bullet);
       bullet.launch(direction); //this should be calculated by which direction the player is facing.
   }
}
公共类播放器{
private List firedBullets=new ArrayList();
公共场所火灾(){
子弹=新子弹(playerX,playerY);
firedbullets.add(bullet);
bullet.launch(direction);//这应该根据玩家面对的方向来计算。
}
}

所以当子弹发射时,方向设定一次。每一次游戏更新,子弹都会以子弹的速度朝这个方向移动。这种逻辑可以用于游戏中必须移动的所有东西。例如,如果你想要一颗子弹在半空中改变方向,你只需要在半空中改变它的方向。

我看到大量使用了
静态
,我强烈建议你减少它。更好的选择可能是更多地依赖状态模型
KeyListener
也有问题。密钥绑定API可能会为您提供更可靠的解决方案以及更可配置的解决方案。
Bullet
需要知道它的移动方向,这是由状态变量或其增量值的方向决定的。您可以将每个项目符号添加到某种
列表中。每次游戏循环时,您都会更新每个
子弹的位置
,并确定它是否与某物碰撞或离开游戏可视边界。在这一点上,您将从
列表中删除
项目符号
…我想这将发生在您的
游戏更新
方法中…@madcommer嘿!谢谢你的回复,但这不是我面临的问题。我已经在考虑创建一个列表了,但问题是我创建的bulle不会在屏幕上移动。。。对不起,如果我没有把这个放在原来的问题中。。。谢谢你的建议顺便说一句:)你的子弹射击方法不正确。不应该有while循环。当子弹被射出时,它只存储它应该继续移动的方向向量。每次执行gameloop时,子弹都应该以一定的“速度”沿其方向向量移动。如果有必要,我可以给出一个代码示例。@DavidMaes嘿!对不起,我不在线。。。是的,如果你能给我提供一个示例代码,那就太好了。
public class Player {
   private List<Bullet> firedBullets = new ArrayList<Bullet>();

   public void fire(){
       Bullet bullet = new Bullet(playerX, playerY);
       firedbullets.add(bullet);
       bullet.launch(direction); //this should be calculated by which direction the player is facing.
   }
}