0

我正在尝试使用 slick2d 和 lwjgl 制作游戏。我不明白为什么这段代码不起作用

第一阶段.java

package net.CharlesDickenson;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class firstStage extends BasicGameState {

    public bossVar bossChecker() {
        if(isBeforeMiddleBoss) return bossVar.beforeBoss;

        if(isMiddleBoss) return bossVar.Middle;

        if(isBeforeBoss) return bossVar.beforeBoss;

        if(isBoss) return bossVar.Boss;

        return null;
    }

    @SuppressWarnings("static-access")
    public firstStage(int state) {
        this.state = state;
    }

    @Override
    public void init(GameContainer _arg0, StateBasedGame _arg1)
            throws SlickException {
        scoreBoard = new Image("res/scoreBoard.png");
        backs = new Image("res/1stageBack.gif");
        isBeforeMiddleBoss = true;
        isMiddleBoss = false;
        isBeforeBoss = false;
        isBoss = false;
        _arg0.setShowFPS(false);
    }

    @Override
    public void render(GameContainer arg0, StateBasedGame _arg1, Graphics _g)
            throws SlickException {
        this._g = _g;

        new Mob().getGraphics(_g);//i passed graphics
        new Char().getGraphics(_g);//i passed graphics
        new Bullet().getGraphics(_g);//i passed graphics

        _g.drawImage(scoreBoard, 550, 5);
        _g.drawImage(backs, 10, 10);
        _g.drawString(fps, 580, 570);
        _g.drawString("High Score-> Not avaiable", 560, 60);
        _g.drawString("Score-> " + currScore, 595, 80);
    }

    @Override
    public void update(GameContainer _arg0, StateBasedGame _arg1, int arg2)
            throws SlickException {
        fps = "Frame Per Second-> " + _arg0.getFPS();

        bossVar b = bossChecker();

        switch(b) {
        case beforeMiddle :
            break;
        case Boss :
            break;
        default:
            break;
        }
    }

    @SuppressWarnings("static-access")
    @Override
    public int getID() {
        return this.state;
    }

    private static int state;
    private static int currScore = 0;
    private static final int originX = 270;
    private static final int originY = 490;
    public static int X = originX;
    public static int Y = originY;
    private static String fps;
    private Image scoreBoard;
    private Image backs;
    private Graphics _g;
    public boolean isBeforeMiddleBoss;
    public boolean isMiddleBoss;
    public boolean isBeforeBoss;
    public boolean isBoss;

}

字符.java

package net.CharlesDickenson;

import org.lwjgl.input.Keyboard;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Char extends Bullet implements Entity {

    @Override
    public void getGraphics(Graphics _g) {
        this._g = _g;//so i got graphics, but 
        if(!isInit) return;
        _g.drawImage(Char, getCharX(), getCharY());//this codes doesn't works.
    }

    @Override
    public int getCharX() {
        switch(VarTracker.stage) {
        case 1:
            return firstStage.X;
        }
        return 0;
    }

    @Override
    public int getCharY() {
        switch(VarTracker.stage) {
        case 1:
            return firstStage.Y;
        }
        return 0;
    }

    public void setCharX(int i) {
        System.out.println("asdgagsd");
        switch(VarTracker.stage) {
        case 1:
            firstStage.X += i;
        }
    }

    public void setCharY(int i) {
        System.out.println("asdgagsd");
        switch(VarTracker.stage) {
        case 1:
            firstStage.Y += i;
        }
    }

    @Override
    public void update() {
        if(!isInit) return;

        _g.drawImage(Char, getCharX(), getCharY());//this code doesn't work, too.

        up = Keyboard.isKeyDown(Keyboard.KEY_UP);
        down = Keyboard.isKeyDown(Keyboard.KEY_DOWN);
        left = Keyboard.isKeyDown(Keyboard.KEY_LEFT);
        right = Keyboard.isKeyDown(Keyboard.KEY_RIGHT);
        shift = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT);
        z = Keyboard.isKeyDown(Keyboard.KEY_Z);

        if(up && !shift) {
            setCharY(6);
        }

        if(down && !shift) {
            setCharY(-6);
        }

        if(left && !shift) {
            setCharX(-6);
        }

        if(right && !shift) {
            setCharX(6);
        }

        if(up && shift) {
            setCharY(2);
        }

        if(down && shift) {
            setCharY(-2);
        }

        if(left && shift) {
            setCharX(-2);
        }

        if(right && shift) {
            setCharX(2);
        }

        if(z) {
            new Bullet().isFiring = true;
        }

        if(!z) {
            new Bullet().isFiring = false;
        }
    }

    @Override
    public void init() {
        System.out.println("<Char> Initializing...");

        isInit = false;

        try {
            Char = new Image("res/char.png");
        } catch (SlickException e) {
            e.printStackTrace();
        }

        isInit = true;

        System.out.println("<Char> Done with init()");
    }

    private boolean up;
    private boolean down;
    private boolean left;
    private boolean right;
    private boolean shift;
    private boolean z;
    private boolean isInit;
    private Image Char;
    private Graphics _g;
}

我使用 getGraphics 方法将图形传递给其他类,以放置图像,但它不起作用。在渲染方法中,它有效,但我不能将图像放在其他类中。

4

1 回答 1

0

它不起作用的原因是您使用Graphics不正确。当Slick2d绘制一些东西时,它使用render方法。这个方法传递了一个 的实例Graphics,你可以在其中绘制东西。当调用结束时,该Graphics对象不再对任何东西有用。因此,没有理由将它传递给任何不立即使用它的东西。

你要做的是render在你的 Mob、Char 和 Bullet 类中创建一个方法。在方法之外创建所述类的实例,例如在某个数据结构中并将它们存储在某个数据结构中,例如 a . 在该方法中,您只需遍历每个元素并调用or即可。一个简单的例子:renderinitListrenderlistrenderdraw

// Give the Entity interface two methods if they don't exist already:
public interface Entity {
    void render(Graphics g);
    void update(int delta);
}

// In firststage.java
List<Entity> list;

// In the init() method
public void init(GameContainer container, StateBasedGame game)
        throws SlickException {
    ...
    list = new ArrayList<Entity>();
    list.add(new Mob());
    list.add(new Char());
    list.add(new Bullet());
}

// In the render method
public void render(GameContainer container, StateBasedGame game, Graphics g)
        throws SlickException {
    ...
    for (Entity e : list) {
        e.draw(g);
    }
}

// In the update method
public void update(GameContainer container, StateBasedGame game, int delta)
        throws SlickException {
    ...
    for (Entity e : list) {
        e.update(delta);
    }
}

TL;DR 版本:Graphics 对象的存在仅用于在单个渲染调用中绘制。渲染每秒调用多次,因此不建议在该方法中创建对象。面向对象编程擅长对对象建模。游戏倾向于对很多对象进行建模。好好利用它。

于 2013-10-07T13:36:47.560 回答