吃豆子:不能创造不止一个鬼

时间:2016-03-24 22:46:13

标签: java pacman

编辑:抱歉,伙计们,但另一个话题没有帮助。看:它只是一个副本+粘贴。只有那个。

原始代码是:

p.update();   
Blinky.update();

maze.update();
dc.eatDot(p);
pc.eatPill(p);
gc.collision(p);
gc.collision(Blinky);

现在是:

        p.update(); 
        Blinky.update();
        Pinky.update();
        Inky.update();
        Clyde.update();


        maze.update();
        dc.eatDot(p);
        pc.eatPill(p);
        gc.collision(p);

        gc.collision(Blinky);
        gc.collision(Pinky);
        gc.collision(Inky);
        gc.collision(Clyde);

在构造函数上:

Blinky = new Ghost(240, 250, 0);
Pinky = new Ghost(105, 69, 1);
Inky = new Ghost(465, 573, 2);
Clyde = new Ghost(321, 357, 3);

幽灵班:

package objects;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.io.IOException;
import java.util.Random;

public class Ghost extends Entity {

private int lastDir = 0;
public int rndDir;

public int tipo;

private int chase = 0;

//private int state; //0 - scatter, 1 - chase, 2 - frightened

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

    init();
}

@Override
public void init(){

    //state = 0;
    this.startFrame = 4;
    this.endFrame = startFrame + 1;
    this.frameNumber = startFrame;
    this.frameSpeed = 4; //quanto maior, mais lento

    try{

        if(this.tipo == 0){
            this.ss = new SpriteSheet(i.load("/img/gBlinky.png"));
            System.out.println("Criado Blinky!"); 
        } else if(this.tipo == 1){
            this.ss = new SpriteSheet(i.load("/img/gPinky.png"));
            System.out.println("Criado Pinky!");
        }else if(this.tipo == 2){
            this.ss = new SpriteSheet(i.load("/img/gInky.png"));
            System.out.println("Criado Inky!");
        }else if(this.tipo == 3){
            this.ss = new SpriteSheet(i.load("/img/gClyde.png"));
            System.out.println("Criado Clyde!");
        }

    }catch (Exception e){System.out.println("Erro na inicialização dos fantasmas: "+e);}



    this.velX = -speed;
    //preVelX = -speed;
}

@Override
public void update(){

    try{
        this.x += velX;
        this.y += velY;


        //passagem pelo túnel
        if(this.x < -35 && y > 290 && this.y < 310)
            this.x = 494;        
        if(this.x > 510 && y > 290 && this.y < 310)
            this.x = -19;

        this.Animation();

    } catch(NullPointerException s) {
        //s.printStackTrace();
        throw new IllegalStateException("Some Ghost has a null property", s);
    }

}

public void chase(Entity e){

    Random rndGen = new Random();
    chase = rndGen.nextInt(1000);

    if(chase % 2 == 0){
        if(e.x > this.x){
            velX = speed;
        } else {
            velX = -speed;
        }
        preVelY = 0;

    } else {
       if(e.y > this.y){
            velY = speed;
        } else {
            velY = -speed;
        }
        velX = 0;        
    }

}

@Override
public void collisionGate(Gate gate){

    boolean[] g = new boolean[4];
    g[0] = gate.up;
    g[1] = gate.right;
    g[2] = gate.down;
    g[3] = gate.left;

    if(velX != 0){
            if(velX > 0)
                g[3] = false;
            else
                g[1] = false;
        }

        if(velY != 0){
           if(velY > 0)
                g[0] = false;
            else
                g[2] = false; 
    }

    int saidas = 0;
    for(int i = 0; i < g.length; i++){
        if(g[i] == true){
            saidas++;
        }
    }


    if((velX > 0 && gate.right == false) ||
        (velX < 0 && gate.left == false) ||
        (velY > 0 && gate.down == false) ||
        (velY < 0 && gate.up == false) ||
            saidas > 1){

        Random rndGen = new Random();
        do{
            rndDir = rndGen.nextInt(4);            
        } while(g[rndDir] == false);

        setVel(rndDir);
    }

}

public void setVel(int k){
    switch(k){
        default: System.out.println("Erro em setVel()");
            break;
        case 0:
            velY = -speed;
            velX = 0;
            break;
        case 1:
            velX = speed;
            velY = 0;
            break;
        case 2:
            velY = speed;
            velX = 0;
            break;
        case 3:
            velX = -speed;
            velY = 0;
            break;
    }
    changeDirection();
}


@Override
public void draw(Graphics g){
    g.drawImage(getEntityImage(), x, y, null);        
}

public void changeDirection(){
    if(velX > 0){
        startFrame = 0;
        endFrame = startFrame + 1;
        frameNumber = startFrame;    
    }
    if(velX < 0){
        startFrame = 4;
        endFrame = startFrame + 1;
        frameNumber = startFrame;    
    }
    if(velY > 0){
        startFrame = 2;
        endFrame = startFrame + 1;
        frameNumber = startFrame;    
    }
    if(velY < 0){
        startFrame = 6;
        endFrame = startFrame + 1;
        frameNumber = startFrame;    
    }
}

}

实体抽象类:

package objects;

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


public abstract class Entity {

    public ImageLoader i = new ImageLoader();
    public SpriteSheet ss;
    public int frame;
    public int frameSpeed, frameNumber, startFrame, endFrame;
    public int counterSS = 0;
    public Image frameSS;

    public int centerX, centerY;

    public int x,y;
    public int velX, velY;
    public int preVelX, preVelY;

    public int speed = 3;

    public void init(){}
    public void update(){}
    public void draw(Graphics g){}
    public void collisionGate(Gate gate){}

    public Image getEntityImage(){
        return frameSS;        
    }

    public void Animation(){

        frameSS = ss.crop2(25*frameNumber, 0, 25, 25);
        //System.out.println(counterSS);

        if(counterSS % frameSpeed == 0){
            if(frameNumber < endFrame){
                frameNumber++;
            } else {
                frameNumber = startFrame;
            }   
        }

        if(counterSS > 20*frameSpeed){
            counterSS = 0;
        }else {
            counterSS++;
        }

        if(velX == 0 && velY == 0)
            counterSS--;
    }

    public Rectangle getBounds(){
        Rectangle r = new Rectangle(this.x, this.y, 25, 25);
        return r;
    }

    public Rectangle getCenterBounds(){
        Rectangle r = new Rectangle(this.x+11, this.y+11, 3, 3);
        return r;
    }
}

我正在做PacMan学习。它与一个鬼完美配合,但当我尝试创建多个鬼时,我收到以下错误:

Criado Blinky!
Criado Pinky!
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Criado Inky!
Criado Clyde!
    at pacman.GamePanel.actionPerformed(GamePanel.java:113)
    at javax.swing.Timer.fireActionPerformed(Timer.java:312)
    at javax.swing.Timer$DoPostEvent.run(Timer.java:244)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

您可以在以下位置查看完整代码: https://github.com/dimarcinho/pacman

当我运行Debug时,它也会给我一个非常奇怪的消息:

  

Ouvindo em javadebug Executando programadousuárioNãofoipostívelenviaro pontodeinterruptçãoShapeBreakpoint[objects.Ghost] .collisionGate2'(Lobjects / Gate;)Lvoid;',razão:Ométodo'trackzeGate2'comassinatura'(Lobjects /栅极;)Lvoid;” nãoreistena classe objects.Ghost。

英文:

听javadebug; 运行用户程序

  

无法发送中断点MethodBreakpoint   [objects.Ghost] .collisionGate2'(Lobjects / Gate;)Lvoid;',原因:   方法'collisionGate2'与签名'(Lobjects / Gate;)Lvoid;'   类对象上不存在.Ghost。

我正在尝试使用某些代码来运行collisionGate()方法,这是其中一项试验,但我已将其删除,并且不会在任何地方使用它!对我来说很奇怪!方法collisionGate2不再存在!

同样奇怪的是,如果我试着和两个鬼一起跑,有时它会起作用,有时候不行! :$

1 个答案:

答案 0 :(得分:0)

好的,伙计们!我发现了问题。

在构造函数中,我有:

public GamePanel(){

    setFocusable(true);

    t = new Timer(20, this);
    t.start();

    maze = new Maze();
    dc = new DotController();        
    gc = new GateController();
    pc = new PillController();
    maze.createDots(dc);
    maze.createPills(pc);        
    maze.createGates(gc);   


    p = new Player(240, 465);

    Blinky = new Ghost(240, 250, 0);//240,250
    Pinky = new Ghost(105, 69, 1);
    Inky = new Ghost(465, 573, 2);
    Clyde = new Ghost(321, 357, 3);

    addKeyListener(new KeyInput(p));

    init();

}

我刚刚将Timer的实例更改为结束,现在它正在运行。我认为毫秒的差异导致了错误。它也解释了为什么有时它会与两个幽灵合作,有时候没有。

感谢您的帮助!

最终代码:

public GamePanel(){

        setFocusable(true);

        maze = new Maze();
        dc = new DotController();        
        gc = new GateController();
        pc = new PillController();
        maze.createDots(dc);
        maze.createPills(pc);        
        maze.createGates(gc);   


        p = new Player(240, 465);

        Blinky = new Ghost(240, 250, 0);//240,250
        Pinky = new Ghost(105, 69, 1);
        Inky = new Ghost(465, 573, 2);
        Clyde = new Ghost(321, 357, 3);

        addKeyListener(new KeyInput(p));

        init();

        t = new Timer(20, this);
        t.start();
    }