如何让敌人走向Java的玩家?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (165)

以下是我使用的代码:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.awt.Font;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class GameManager extends JFrame implements KeyListener {
    private int canvasWidth;
    private int canvasHeight;
    private int borderLeft;
    private int borderTop;
    private BufferedImage canvas;
    private Stage stage;
    private Enemy[] enemies;
    private Player player;
    private Goal goal;
    private Graphics gameGraphics;
    private Graphics canvasGraphics;
    private int numEnemies;
    private boolean continueGame;

    public static void main(String[] args) {
        // During development, you can adjust the values provided in the brackets below
        // as needed. However, your code must work with different/valid combinations
        // of values.
        int choice;
        do{
            GameManager managerObj = new GameManager(1920, 1280);
            choice=JOptionPane.showConfirmDialog(null,"Play again?", "", JOptionPane.OK_CANCEL_OPTION);
        }while(choice==JOptionPane.OK_OPTION);
        System.exit(0);
    }

    public GameManager(int preferredWidth, int preferredHeight) {
        int maxEnemies; 
        try{
            maxEnemies=Integer.parseInt(JOptionPane.showInputDialog("How many enemies? (Default is 5)"));
            if (maxEnemies<0)
                maxEnemies=5;
        }
        catch (Exception e){
            maxEnemies=5;
        }

        this.borderLeft = getInsets().left;
        this.borderTop = getInsets().top;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        if (screenSize.width < preferredWidth)
            this.canvasWidth = screenSize.width - getInsets().left - getInsets().right;
        else
            this.canvasWidth = preferredWidth - getInsets().left - getInsets().right;
        if (screenSize.height < preferredHeight)
            this.canvasHeight = screenSize.height - getInsets().top - getInsets().bottom;
        else
            this.canvasHeight = preferredHeight - getInsets().top - getInsets().bottom;
        setSize(this.canvasWidth, this.canvasHeight);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        addKeyListener(this);
        Random rng = new Random();
        this.canvas = new BufferedImage(this.canvasWidth, this.canvasHeight, BufferedImage.TYPE_INT_RGB);
        // Create a Stage object to hold the background images
        this.stage = new Stage();
        // Create a Goal object with its initial x and y coordinates
        this.goal = new Goal((Math.abs(rng.nextInt()) % (this.canvasWidth)),
                (Math.abs(rng.nextInt()) % this.canvasHeight));
        // Create a Player object with its initial x and y coordinates
        this.player = new Player((Math.abs(rng.nextInt()) % (this.canvasWidth)),
                (Math.abs(rng.nextInt()) % this.canvasHeight));
        // Create the Enemy objects, each with a reference to this (GameManager) object
        // and their initial x and y coordinates.
        this.numEnemies = maxEnemies;
        this.enemies = new Enemy[this.numEnemies];
        for (int i = 0; i < this.numEnemies; i++) {
            this.enemies[i] = new Enemy(this, Math.abs(rng.nextInt()) % (this.canvasWidth),
                    Math.abs(rng.nextInt()) % this.canvasHeight);
        }
        this.gameGraphics = getGraphics();
        this.canvasGraphics = this.canvas.getGraphics();
        this.continueGame = true;
        long gameStartTime=System.nanoTime();

        while (this.continueGame) {
            updateCanvas();
        }
        this.stage.setGameOverBackground();
        double gameTime=(System.nanoTime()-gameStartTime)/1000000000.0;
        updateCanvas();
        this.gameGraphics.setFont(new Font(this.gameGraphics.getFont().getFontName(), Font.PLAIN, 50)); 
        if (gameTime<1)
            this.gameGraphics.drawString("Oops! Better luck next time...",  this.canvasWidth/3, this.canvasHeight/2 - 50);
        else
            this.gameGraphics.drawString("You survived " + String.format("%.1f", gameTime)+ " seconds with "+this.numEnemies+" enemies!",
                this.canvasWidth/4, this.canvasHeight/2 - 50);
    }

    public void updateCanvas() {
        long start = System.nanoTime();
        // If the player is alive, this should move the player in the direction of the
        // key that has been pressed
        // Note: See keyPressed and keyReleased methods in the GameManager class.
        this.player.performAction();
        // If the enemy is alive, the enemy must move towards the Player. The Player object
        // is obtained via the GameManager object that is given at the time of creating an Enemy
        // object.
        // Note: The amount that the enemy moves by must be much smaller than that of
        // the player above or else the game becomes too hard to play.
        for (int i = 0; i < this.numEnemies; i++) {
            this.enemies[i].performAction();
        }
        if ((Math.abs(this.goal.getX() - this.player.getX()) < (this.goal.getCurrentImage().getWidth() / 2))
                && (Math.abs(this.goal.getY() - this.player.getY()) < (this.goal.getCurrentImage().getWidth() / 2))) {
            for (int i = 0; i < this.numEnemies; i++) {
                // Sets the image of the enemy to the "dead" image and sets its status to
                // indicate dead
                this.enemies[i].die();
            }
            // Sets the background of the stage to the finished game background.
            this.stage.setGameOverBackground();
            this.continueGame = false;
        }
        // If an enemy is close to the player or the goal, the player and goal die
        int j = 0;
        while (j < this.numEnemies) {
            if ((Math.abs(this.player.getX() - this.enemies[j].getX()) < (this.player.getCurrentImage().getWidth() / 2))
                    && (Math.abs(this.player.getY() - this.enemies[j].getY()) < (this.player.getCurrentImage().getWidth()
                            / 2))) {
                this.player.die();
                this.goal.die();
                this.stage.setGameOverBackground();
                j = this.numEnemies;
                this.continueGame = false;
            }
            else if ((Math.abs(this.goal.getX() - this.enemies[j].getX()) < (this.goal.getCurrentImage().getWidth() / 2))
                    && (Math.abs(this.goal.getY() - this.enemies[j].getY()) < (this.goal.getCurrentImage().getWidth()
                            / 2))) {
                this.player.die();
                this.goal.die();
                this.stage.setGameOverBackground();
                j = this.numEnemies;
                this.continueGame = false;
            }
            j++;
        }
        try {
            // Draw stage
            this.canvasGraphics.drawImage(stage.getCurrentImage(), 0, 0, null);
            // Draw goal
            this.canvasGraphics.drawImage(this.goal.getCurrentImage(),
                    this.goal.getX() - (this.goal.getCurrentImage().getWidth() / 2),
                    this.goal.getY() - (this.goal.getCurrentImage().getHeight() / 2), null);
            // Draw player
            this.canvasGraphics.drawImage(player.getCurrentImage(),
                    this.player.getX() - (this.player.getCurrentImage().getWidth() / 2),
                    this.player.getY() - (this.player.getCurrentImage().getHeight() / 2), null);
            // Draw enemies
            for (int i = 0; i < this.numEnemies; i++) {
                this.canvasGraphics.drawImage(this.enemies[i].getCurrentImage(),
                        this.enemies[i].getX() - (this.enemies[i].getCurrentImage().getWidth() / 2),
                        this.enemies[i].getY() - (this.enemies[i].getCurrentImage().getHeight() / 2), null);
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        // Draw everything.
        this.gameGraphics.drawImage(this.canvas, this.borderLeft, this.borderTop, this);
        long end = System.nanoTime();
        this.gameGraphics.setFont(new Font(this.gameGraphics.getFont().getFontName(), Font.PLAIN, 15)); 
        this.gameGraphics.drawString("FPS: " + String.format("%2d", (int) (1000000000.0 / (end - start))),
                this.borderLeft + 50, this.borderTop + 75);
    }

    public Player getPlayer() {
        return this.player;
    }

    public void keyPressed(KeyEvent ke) {
        // Below, the setKey method is used to tell the Player object which key is
        // currently pressed. 

        // The Player object must keep track of the pressed key and use it for
        // determining the direction
        // to move.

        // Important: The setKey method in Player must not move the Player.
        if (ke.getKeyCode() == KeyEvent.VK_LEFT)
            this.player.setKey('L', true);
        if (ke.getKeyCode() == KeyEvent.VK_RIGHT)
            this.player.setKey('R', true);
        if (ke.getKeyCode() == KeyEvent.VK_UP)
            this.player.setKey('U', true);
        if (ke.getKeyCode() == KeyEvent.VK_DOWN)
            this.player.setKey('D', true);
        if (ke.getKeyCode() == KeyEvent.VK_ESCAPE)
            this.continueGame = false;
    }

    @Override
    public void keyReleased(KeyEvent ke) {
        // Below, the setKey method is used to tell the Player object which key is
        // currently released.

        // The Player object must keep track of the pressed key and use it for
        // determining the direction
        // to move.

        // Important: The setKey method in Player must not move the Player.
        if (ke.getKeyCode() == KeyEvent.VK_LEFT)
            this.player.setKey('L', false);
        if (ke.getKeyCode() == KeyEvent.VK_RIGHT)
            this.player.setKey('R', false);
        if (ke.getKeyCode() == KeyEvent.VK_UP)
            this.player.setKey('U', false);
        if (ke.getKeyCode() == KeyEvent.VK_DOWN)
            this.player.setKey('D', false);
    }

    @Override
    public void keyTyped(KeyEvent ke) {
    }
}

球员类

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.Point;
import javax.imageio.ImageIO;

public class Player {
    private int x;
    private int y;
    private int xPos;
    private int yPos;
    private int q;
    private GameManager gameManager;
    private BufferedImage imageCurrent;
    private BufferedImage imageRunning;
    private BufferedImage imageOver;



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

        try {
            this.imageRunning = ImageIO.read(new File("player-alive.png"));
            this.imageOver    = ImageIO.read(new File("player-dead.png"));
        } catch (IOException e) {
            e.printStackTrace();

        }
        this.imageCurrent = this.imageRunning;
    }

    public void performAction() {




    }
    public int getXpos() {
        x = xPos;
        return xPos;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public BufferedImage getCurrentImage() {
        //System.out.println(x+","+y);
        //System.out.println();
        return this.imageCurrent;
    }

    public void die() {
        this.imageCurrent = this.imageOver;

    }

    public void setKey(char c, boolean b) {
        if (c == 'L' && b == true) {x-=10;}
        else if (c == 'R' && b == true) {x+=10;}
        else if (c == 'U' && b == true) {y-=10;}
        else if (c == 'D' && b == true) {y+=10;}


    }
public void translate(int x, int y) {

        ;

    }


}

和敌人类

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.Point;

import javax.imageio.ImageIO;

public class Enemy {
    private int i;
    private int j;
    private Player player;
    private GameManager gameManager;
    private BufferedImage imageCurrent;
    private BufferedImage imageRunning;
    private BufferedImage imageOver;
    private double speed = 2;

    public Enemy(GameManager gameManager, int i, int j) {
        this.i=i;
        this.j=j;

        //i = gameManager.getPlayer().getX(); 
        //j = gameManager.getPlayer().getY();

        try {
            this.imageRunning = ImageIO.read(new File("enemy-alive.png"));
            this.imageOver    = ImageIO.read(new File("enemy-dead.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.imageCurrent = this.imageRunning;
    }

    public void performAction() {



        //i = gameManager.getPlayer().getX();

        //i = i - gameManager.getPlayer().getX();
        //j = j- gameManager.getPlayer().getX();

        //i += player.getX()- i ;
        //j= Player.getY();

        //i = (Player.x - i);
        //j = (Player.y - j);

        //i = gameManager.getPlayer().getX(); 
        //j = gameManager.getPlayer().getY();

         //i += Math.signum(gameManager.getPlayer().getX() - i);
         //j += Math.signum(gameManager.getPlayer().getX() - j);

    }

    public BufferedImage getCurrentImage() {
        //System.out.println(player.getXpos() );
        return this.imageCurrent;

    }


    public void die() {
        this.imageCurrent = this.imageOver;

    }

    public int getX() {
        return this.i;
    }

    public int getY() {
        return this.j;
    }


}

在评论中我试过的一些方法,其中没有一个工作。

这是我得到的NullPointerException,启用第52行;

Exception in thread "main" java.lang.NullPointerException
at Enemy.performAction(Enemy.java:52)
at GameManager.updateCanvas(GameManager.java:115)
at GameManager.<init>(GameManager.java:90)
at GameManager.main(GameManager.java:33)
提问于
用户回答回答于
private Player player;

定义了播放器但它是null

想在你的构造函数中你需要:

player = gameManager.getPlayer();

然后你的Enemy类的方法将有权访问你在GameManager类中创建的“player”对象。

扫码关注云+社区

领取腾讯云代金券