游戏只会渲染一个敌人

时间:2016-10-04 05:56:20

标签: java render paintcomponent

我正在尝试制作,以便在关卡屏幕上显示多个方块。由于某种原因,它所呈现的唯一方格将是我输入的顺序中的最后一个方格。在它处理了2个小时并且非常烦人。任何帮助都会很棒,非常感谢。

public class Level1 extends JFrame implements ActionListener, KeyListener {

    LevelRender render;
    static Square square;

    static Player player;
    static Square squares;
    public static Timer timer;
    boolean fail, Over, Reset = false, create = false;
    static int enemyCount = 5;

    static int eX = 200, eY, z;

    ArrayList <Enemys> squareList = new ArrayList();

    public static Level1 level;

    ImageIcon Level1 = new ImageIcon("C:\\Users\\Kyle\\Documents\\NetBeansProjects\\Testing52\\src\\testing52\\Level1.png");

    public Level1(Player player){        
        timer = new Timer(50,this);
        render = new LevelRender();        
        setVisible(true);
        setSize(1000,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(render);
        addKeyListener(this);        
        this.player = player; 
        timer.start();    
        player.x = 0;
        player.y = 387;
    }

    public void render(Graphics2D g){        
         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);        
        if(!fail){            
            Level1.paintIcon(MainMenu.level1, g, -4, -25);        
            player.GameCount = 1;
            player.render(g);        
            square = new Square(50,397);
            squares = new Square(200,397);        
            square.render(g);
            squares.render(g);   
            reset();   
            g.setColor(Color.BLACK);
            g.setFont(new Font("Arial", 1, 30));        
            g.drawString(String.valueOf("Health:"), 40, 80);
            //lots of other string
            g.setColor(Color.BLACK);
            g.setFont(new Font("Arial", 1, 15));        
            g.drawString(String.valueOf("Level:"), 900, 20);
            g.drawString(String.valueOf("1"), 950, 20);        
        }

        if(fail){            
            g.setColor(Color.black);
            g.fillRect(0, 0, 1200, 500);        
            g.setColor(Color.red);
            g.setFont(new Font("Arial", 1, 50));        
            g.drawString(String.valueOf("You Have Failed"), 200, 150);        
            g.setFont(new Font("Arial", 1, 50));
            g.drawString(String.valueOf("Press ENTER to Continue"), 120, 220);            
        }

        if(enemyCount == 0){        
            g.setColor(Color.black);
            g.fillRect(0, 0, 1200, 500);        
            g.setColor(Color.green);
            g.setFont(new Font("Arial", 1, 50));        
            g.drawString(String.valueOf("You Won"), 200, 150);        
            g.setFont(new Font("Arial", 1, 50));
            g.drawString(String.valueOf("Press ENTER to Continue"), 120, 220);     
        }        
    }

    public void createEnemys(){        
         for(int i = 0; i < 5; i++){
             eX += 50;
             //squares = new Enemys(player, eX, 397);
             squareList.add(squares);
             create = true;
         }
    }

    public void update(){        
        player.update();      
        if(!create){
            createEnemys();
        }
        if(Player.health <= 0){            
             fail = true;
             Over = true;
             Player.health = 10;             
        }        
        if(enemyCount == 0){            
             fail = false;
             Over = true;
        }
        System.out.println(Enemys.health + " , " + enemyCount + " , " + squareList.get(3).x  );        
     }

     public void reset(){        
         if(Reset){
             enemyCount = 5; 
             Reset = false;
            create = false;
         }        
     }



     @Override
     public void actionPerformed(ActionEvent e) {        
         update();
         render.repaint();
    }
}

...................

public class Enemys {

public static int health, damage,x,y;  
static boolean hit;
static int count;

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

public void hit(){        
    if(Player.x == x)
    {  
        Player.health -= damage;
    }
} 

public void update(){
    hit();
}

public void hello(){
    System.out.println("Hello");
}

public void render(Graphics g){}
}

.......................

public class Square extends Enemys {

static int count;
static boolean hit;

public Square(int x, int y){
    super(x,y);
    damage = 1;
    health = 30;        
}

public void render(Graphics g){        
    g.setColor(Color.red);
    g.fillRect(x, y, 50, 50);        
    g.setColor(Color.green);
    g.fillRect(x, y - 20, (50*(health / 30)), 7);      
}

public void update(){        
    x += 1;        
}  

}

........................

public class LevelRender extends JPanel {

public void paintComponent(Graphics g){
    super.paintComponent(g);

    MainMenu.level1.render((Graphics2D)g);
}
}

...................

0 个答案:

没有答案