-2

我有一个横向卷轴游戏,游戏中有 4 个敌人。无论我向它发射多少子弹,其中一个敌人都不会死。前两个敌人的代码完全相同,我不知道为什么它不起作用。

板级:

package knowlesm.main;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Board extends JPanel implements ActionListener, Runnable {
    Character p;
    Image img;
    Timer time;
    Thread animator;
    Enemy en, en2, en3, en4;
    Ammo a1;
    int v = 72;
    boolean lost = false;

    static Font font = new Font("SansSerif", Font.BOLD, 24);

    public Board() {

        p = new Character();
        addKeyListener(new AL());
        setFocusable(true);
        ImageIcon i = new ImageIcon("res/background.png");
        img = i.getImage();
        time = new Timer(5, this);
        time.start();
        en = new Enemy(700, 200, "res/enemy_1.png");
        en2 = new Enemy(700, 200, "res/enemy_1.png");
        en3 = new Enemy(700, 200, "res/enemy_1.png");
        en4 = new Enemy(700, 200, "res/enemy_1.png");
        a1 = new Ammo(700, 200, "res/ammo.png");

    }

    public void actionPerformed(ActionEvent e) {
        System.out.println(p.x);
        try{
        checkCollisions();  
        } catch (Exception error){
            System.out.println("Can't make rectangle probably");
        }

        ArrayList bullets = Character.getBullets();
        for (int w = 0; w < bullets.size(); w++) {
            Bullet m = (Bullet) bullets.get(w);
            if (m.getVisible() == true)
                m.move();
            else
                bullets.remove(w);
        }
        p.Move();
        if (p.x > 400)
            en.move(p.getdx(), p.getLeft());
        if (p.x > 600) {
            en2.move(p.getdx(), p.getLeft());
        }
        if (p.x > 1800)
            en3.move(p.getdx(), p.getLeft());
        if (p.x > 2000) {
            en4.move(p.getdx(), p.getLeft());
        }
        if (p.x > 1200) {
            a1.move(p.getdx(), p.getLeft());
        }

        repaint();
    }

    private void checkCollisions() {
        Rectangle r1 = en.getBounds();
        Rectangle r2 = en2.getBounds();
        Rectangle r3 = a1.getBounds();
        Rectangle r4 = en3.getBounds();
        Rectangle r5 = en4.getBounds();

        ArrayList bullets = Character.getBullets();
        for (int w = 0; w < bullets.size(); w++) {
            Bullet m = (Bullet) bullets.get(w);
            Rectangle m1 = m.getBounds();
            if (r1.intersects(m1) && en.isAlive) {
                en.isAlive = false;
                m.visible = false;
            }  if (r2.intersects(m1) && en2.isAlive) {
                en2.isAlive = false;
                m.visible = false;
            }  if (r4.intersects(m1) && en2.isAlive) {
                en3.isAlive = false;
                m.visible = false;
            }  if (r5.intersects(m1) && en2.isAlive) {
                en4.isAlive = false;
                m.visible = false;
            } 
        }

        Rectangle d = p.getBounds();

        if ((d.intersects(r1) && en.isAlive() == true)
                || (d.intersects(r2) && en2.isAlive() == true)
                    || (d.intersects(r4) && en3.isAlive() == true)
                        || (d.intersects(r5) && en4.isAlive() == true)){
            lost = true;
        }   
        if (d.intersects(r3)){
            a1.isThere = false;
            p.ammo = 10;
        }
    }

    boolean k = false;

    public void paint(Graphics g) {
        if (lost)
            System.exit(0);
        if (p.dy == 1 && k == false) {
            k = true;
            animator = new Thread(this);
            animator.start();
        }
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;

        if ((p.GetX() - 590) % 2400 == 0)
            p.nx = 0;
        if ((p.GetX() - 1790) % 2400 == 0)
            p.nx2 = 0;
        g2d.drawImage(img, 685 - p.nx2, 0, null);
        if (p.GetX() >= 590)
            g2d.drawImage(img, 685 - p.nx, 0, null);
        g2d.drawImage(p.getImage(), p.left, v, null);

        ArrayList bullets = Character.getBullets();
        for (int w = 0; w < bullets.size(); w++) {
            Bullet m = (Bullet) bullets.get(w);
            g2d.drawImage(m.getImage(), m.getX(), m.getY(), null);
        }
        g2d.setFont(font);
        g2d.setColor(Color.BLUE);
        g2d.drawString("Ammo left " + p.ammo, 500, 20);
        if (p.x > 400) {
            if (en.isAlive == true)
                g2d.drawImage(en.getImage(), en.getX(), en.getY(), null);
        }
        if (p.x > 600) {
            if (en2.isAlive == true)
                g2d.drawImage(en2.getImage(), en2.getX(), en2.getY(), null);
        }
        if (p.x > 1800){
            if(en3.isAlive == true){
                g2d.drawImage(en3.getImage(), en3.getX(), en3.getY(), null);
            }
        }
        if (p.x > 1800){
            if(en4.isAlive == true){
                g2d.drawImage(en3.getImage(), en3.getX(), en3.getY(), null);
            }
        }
        if (p.x > 1200) {
            if (a1.isThere == true) {
                g2d.drawImage(a1.getImage(), a1.getX(), a1.getY(), null);
            }
        }

    }

    private class AL extends KeyAdapter {
        public void keyReleased(KeyEvent e) {
            p.KeyReleased(e);
        }

        public void keyPressed(KeyEvent e) {
            p.KeyPressed(e);
        }
    }

    public void run() {
        long beforeTime, timeDiff, sleep;

        beforeTime = System.currentTimeMillis();
        while (done == false) {
            cycle();
            timeDiff = System.currentTimeMillis() - beforeTime;
            sleep = 10 - timeDiff;
            if (sleep < 0)
                sleep = 2;
            try {
                Thread.sleep(sleep);
            } catch (Exception e) {
            }
            beforeTime = System.currentTimeMillis();
        }
        done = false;
        h = false;
        k = false;

    }

    boolean done = false;
    boolean h = false;

    public void cycle() {
        if (h == false)
            v--;
        if (v == -10)
            h = true;
        if (h == true && v < 72)
            v++;
        if (v == 72)
            done = true;
    }

}

弹药等级:

package knowlesm.main;

import java.awt.Image;
import java.awt.Rectangle;

import javax.swing.ImageIcon;
import java.awt.*;
public class Ammo {

    int x, y;
    boolean isThere = true;
    Image img;

    public Ammo(int startX, int startY, String Location) {
        x = startX;
        y = startY;
        ImageIcon ammo = new ImageIcon(Location);
        img = ammo.getImage();
    }

    public void move(int dx, int left)
    {
            if (dx == 1 && !((left + dx )< 150))//Added this to only move enemy when character moves forward (not back)
            x = x - dx;
    }

    public Rectangle getBounds() {
        return new Rectangle(x, y, 100, 100);
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public boolean isThere() {
        return isThere;
    }

    public Image getImage(){
        return img;
    }
}

子弹类:

package knowlesm.main;

import java.awt.Image;
import java.awt.Rectangle;

import javax.swing.ImageIcon;

public class Bullet {
    int x, y;
    Image img;
    boolean visible;

    public Bullet(int startX, int startY) {
        x = startX;
        y = startY;
        ImageIcon newBullet = new ImageIcon("res/bullet.png");
        img = newBullet.getImage();
        visible = true;
    }

    public Rectangle getBounds(){
        return new Rectangle(x, y, 20, 20);
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

    public boolean getVisible(){
        return visible;
    }

    public Image getImage(){
        return img;
    }
    public void move(){
        x += 4;
        if ( x > 750) 
            visible = false;
    }
}

字符类:

package knowlesm.main;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;

public class Character {
    int x, dx, y, nx, nx2, left, dy;
    Image still;
    ImageIcon i_left = new ImageIcon("res/character_left.png");
    ImageIcon i = new ImageIcon("res/character.png");
    ImageIcon i2 = new ImageIcon("res/character_1.png");
    ImageIcon i_jump = new ImageIcon("res/character_jump.png");
    int ammo = 10;

    static ArrayList bullets;

    public Character() {
        still = i.getImage();
        x = 75;
        y = 72;
        nx2 = 685;
        nx = 0;
        left = 150;
        bullets = new ArrayList();
    }

    public static ArrayList getBullets(){
        return bullets;
    }

    public void fire(){
        if ( ammo > 0){
            ammo--;
        Bullet z = new Bullet((left + 150), (y + 365 / 2));
        bullets.add(z);
        }
    }

    public void Move() {
        if (dx != -1) {
            if (left + dx <= 150) {
                left = left + dx;
            } else {
                x += dx;
                nx2 += dx;
                nx += dx;
            }
        } else {
            if ( left + dx > 0)
                left = left + dx;
        }
    }

    public Rectangle getBounds(){
        return new Rectangle(left, y, 150, 365);
    }

    public int getLeft(){
        return left;
    }

    public int GetX() {
        return x;
    }

    public int GetY() {
        return y;
    }

    public Image getImage() {
        return still;
    }

    public int getdx(){
        return dx;
    }

    public int getnx(){
        return nx;
    }

    public int getnx2(){
        return nx2;
    }

    public int ammo(){
        return ammo;
    }

    int w = 0;
    boolean walking = false;

    public void KeyPressed(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT || key == KeyEvent.VK_A) {
            dx = -1;
            still = i_left.getImage();
        }
        if (key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_D) {
            boolean walking = true;
            if(walking){
                w++;
                if(w % 2 == 0){
                    still = i2.getImage();
                } else {
                    still = i.getImage();
                }
            }
            dx = 1;
        }
        if (key == KeyEvent.VK_UP || key == KeyEvent.VK_W || key == KeyEvent.VK_SPACE){
            dy = 1;
            still = i_jump.getImage();
        }
        if (key == KeyEvent.VK_F){
            fire();
        }
    }

    public void KeyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT || key == KeyEvent.VK_A)
            dx = 0;
        if (key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_D)
            dx = 0;
            walking = false;
            w = 0;
        if (key == KeyEvent.VK_UP || key == KeyEvent.VK_W || key == KeyEvent.VK_SPACE)
            dy = 0;
            still = i.getImage(); 
    }
}

敌人等级:

package knowlesm.main;

import java.awt.Image;
import java.awt.*;
import java.awt.Rectangle;

import javax.swing.ImageIcon;

public class Enemy {

    Image img;
    int x, y;
    boolean isAlive = true;

    public Enemy(int startX, int startY, String location){
        x = startX;
        y = startY;
        ImageIcon e = new ImageIcon(location);
        img = e.getImage();
    }

    public Rectangle getBounds(){
        return new Rectangle(x, y, 107, 100);
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

    public boolean isAlive(){
        return isAlive;
    }

    public Image getImage(){
        return img;
    }

    public void move(int dx, int left)
    {
            if (dx == 1 && !((left + dx )< 150))//Added this to only move enemy when character moves forward (not back)
            x = x - dx;
    }
}

框架类:

package knowlesm.main;

import java.io.File;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JFrame;

public class Frame {

    public static void main (String args[]){
        JFrame frame = new JFrame("2D Game");
        frame.add(new Board());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700, 365);
        frame.setVisible(true);
        playSound();
    }

    public static void playSound(){
        try{
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("res/02-buckboard-chase.wav").getAbsoluteFile());
            Clip clip = AudioSystem.getClip();
            clip.open(audioInputStream);
            clip.start();
        }catch(Exception ex){
            System.out.println("Error with playing sound.");
            ex.printStackTrace();
        }
    }
}

碰撞检测属于板级。为什么它不适用于所有敌人类型?

4

1 回答 1

3
        if (r1.intersects(m1) && en.isAlive) {
            en.isAlive = false;
            m.visible = false;
        }  if (r2.intersects(m1) && en2.isAlive) {
            en2.isAlive = false;
            m.visible = false;
        }  if (r4.intersects(m1) && en2.isAlive) {
            en3.isAlive = false;
            m.visible = false;
        }  if (r5.intersects(m1) && en2.isAlive) {
            en4.isAlive = false;
            m.visible = false;
        } 

你总是检查 en2 是否还活着,但你应该检查 en3 或 en4 是否还活着。

于 2013-11-05T16:02:32.383 回答