Java 2d游戏 - 游戏中非常奇怪的“bug”

时间:2015-08-27 16:11:20

标签: java user-interface jframe jpanel 2d

我在使用Java制作的2D游戏中有一个非常奇怪的错误。 游戏描述:玩家可以用鼠标移动火箭精灵,并且必须躲避一个跟随玩家并杀死玩家的矩形(通过碰撞)。它不是跟随玩家,而是移动到玩家的起始坐标,即400,400。它应该移动到玩家前往的方向。如果你知道什么是错的,请帮帮我。

主要课程:

import javax.swing.*;

public class Main extends JFrame{
    private static final long serialVersionUID = 1L;
    final static int WW = 800;
    final static int WH = 600;
    public Main(){
        setSize(WW,WH);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setTitle("Space Game");
        add(new GameClass());
        setVisible(true);
    }
    public static void main(String[] args) {
        new Main();
    }
}

GameClass类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GameClass extends JPanel implements ActionListener, KeyListener{
    private static final long serialVersionUID = 1L;
    Enemy enemy = new Enemy();
    Player player = new Player();

    public GameClass(){
        Timer time = new Timer(15, this);
        time.start();
        this.addKeyListener(this);
        this.setFocusable(true);
        player.openImage();
    }

    public void update(){
        player.update();
        enemy.update();
    }

    public void paintComponent(Graphics g){
        g.setColor(Color.BLACK);
        g.fillRect(0,0,Main.WW,Main.WH);
        player.paint(g);
        enemy.paint(g);
        g.dispose();
    }

    public void actionPerformed(ActionEvent e){
        update();
        repaint();
    }

    public void keyPressed(KeyEvent e){
        if(e.getKeyCode() == KeyEvent.VK_LEFT){
            player.setSpeed(-5);
        }
        else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
            player.setSpeed(5);
        }
        else if(e.getKeyCode() == KeyEvent.VK_UP){
            player.setFly(-5);
        }
        else if(e.getKeyCode() == KeyEvent.VK_DOWN){
            player.setFly(5);
            Player.goDown = true;
        }
    }

    public void keyReleased(KeyEvent e){
        if(e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT){
            player.setSpeed(0);
        }
        if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN){
            player.setFly(0);
            Player.goDown = false;
        }
    }

    public void keyTyped(KeyEvent e){

    }
}

敌人类:

import java.awt.*;

public class Enemy {
    private int x = -100;
    private int y = -100;
    Player player = new Player();

    public void update(){
        System.out.println(player.getX());
        if(player.getX() < this.x){
            this.x -= 5;
        }
        if(player.getX() > this.x){
            this.x += 5;
        }
        if(player.getY() > this.y){
            this.y += 5;
        }
        if(player.getY() < this.y){
            this.y -= 5;
        }

    }

    public void paint(Graphics g){
        g.setColor(Color.ORANGE);
        g.fillRect(x, y, 50, 50);
    }
}

玩家类:

import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.*;

public class Player {
    private Image image;
    private int player_x = Main.WW/2;
    private int player_xspeed = 0;
    private int player_y = Main.WH/2;
    private int player_yspeed = 0;
    private int flamey = player_y + 100;
    private int flamex;
    private int newx;
    private int newy;
    static boolean goDown = false;

    public Player(){
        newx = player_x;
        newy = player_x;
    }

    public void openImage(){
        try {
            image = ImageIO.read(new File("spaceship.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void paint(Graphics g){
        flamex = player_x + 20;
        g.drawImage(image,player_x,player_y,null);
        g.setColor(Color.YELLOW);
        g.fillRect(flamex, flamey+5, 6, 6);
        g.fillRect(flamex+10, flamey, 6, 6);
        g.fillRect(flamex+10, flamey+15, 6, 6);
        g.fillRect(flamex+20, flamey+5, 6, 6);
    }

    public void update(){
        player_x += player_xspeed;
        player_y += player_yspeed;
        if(flamey < (player_y + 125)){
            if(goDown == false)
                flamey += 5;
            else
                flamey += 10;
        }else
            flamey = player_y + 100;
        newx = player_x;
        newy = player_y;
    }

    public void setSpeed(int speed){
        player_xspeed = speed;
    }

    public void setFly(int speed){
        player_yspeed = speed;
    }

    public int getX(){
        return newx;
    }

    public int getY(){
        return newy;
    }
}

抱歉英语不好,请帮帮我。 任何帮助appreaciated:)

1 个答案:

答案 0 :(得分:2)

这是因为它跟随的Player是通过以下行在Enemy内创建的:

public class Enemy {
  //...
  Player player = new Player();
  //...
}

因此,它与您控制的玩家实例不同,该实例是在GameClass中通过以下方式创建的:

public class GameClass extends JPanel implements ActionListener, KeyListener{
    Enemy enemy = new Enemy();
    Player player = new Player();
    //...
}

您需要将受控播放器实例作为Enemy上的构造函数参数注入,例如

public class GameClass extends JPanel implements ActionListener, KeyListener{
    Player player = new Player();
    Enemy enemy = new Enemy(player);
    //...
}

public class Enemy {
  //...
  Player player;

  public Enemy(Player player) { this.player = player; }
  //...
}

或通过制定者。