使用MVC时如何缩放游戏以达到不同的分辨率

时间:2019-04-20 15:20:18

标签: java resolution scaling

我希望能够针对不同的分辨率扩展游戏,但是只允许我在控制器中工作(我正在使用MVC)。我不确定如何提高游戏的比例。

由于我不熟悉针对不同分辨率的缩放游戏,因此我还没有真正尝试过任何操作。是否有特殊功能?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.image.BufferStrategy;
import java.util.Collection;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

import se.egy.game.resourses.Drawable;
import se.egy.game.resourses.GameCloseException;


public class GameScreen{

    private int height, width;
    private String title;
    private boolean fullScreenMode = false;

    private JFrame jf;

    private GraphicsDevice device;
    private BufferStrategy backBuffer;
    private Graphics2D g;

    private Color bgColor = Color.BLACK; // Svart, default som bakgrund
    private Image bgImg = null;

    private DisplayMode oldDM = null;

    public GameScreen(String title, int width, int height, boolean fulScreenMode) {
        this.width = width;
        this.height = height;
        this.title = title;
        this.fullScreenMode = fulScreenMode;

        try {
            EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    createWindow();
                }
            });
        }catch(Exception e) {}

        clearScreen();

        if(fullScreenMode) { // Ful lösning ;-)
            jf.setBounds(0, 0, width, height);
            try{ Thread.sleep(50);}catch(Exception e){}
        }

    }

    public GameScreen(String title, int width, int height) {
        this(title, width, height, false);
    }


    private void createWindow() {
        jf = new JFrame(title);

        if(fullScreenMode) {
            DisplayMode dm = new DisplayMode(width, height, 32, 0);
            GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
            device = env.getDefaultScreenDevice();

            jf.setUndecorated(true);
            jf.setBounds(0, 0, width, height);

            oldDM = device.getDisplayMode();

            device.setFullScreenWindow(jf);
            device.setDisplayMode(dm);
        }else {
            jf.setSize(new Dimension(width, height));
            jf.setResizable(false);
            jf.setLocationRelativeTo(null);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

        jf.setIgnoreRepaint(true);
        jf.setVisible(true);
        jf.requestFocus();

        jf.createBufferStrategy(2);
        backBuffer = jf.getBufferStrategy();
    }
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.ImageIcon;

import se.egy.game.model.Entity;
import se.egy.game.model.Sprite;
import se.egy.game.view.GameScreen;
import se.egy.game.view.TxtEntity;

public class GameController implements KeyListener{
    private GameScreen gs;

    private boolean gameRunning = true;
    private int fps;

    private Entity player;

    private ArrayList<Entity> entityList = new ArrayList<>();

    private HashMap<String, Boolean> keyDown = new HashMap<>();

    public GameController(GameScreen gs, int fps) {
        this.gs = gs;
        this.gs.setKeyListener(this);

        this.fps = fps;

        resetkeyDown();

        loadObjects();
    }
        public void runGame() {
        long renderDelay = 1000000000/fps;

        long lastUpdateTime = System.nanoTime();

        while(gameRunning) {
            long deltaTime = System.nanoTime() - lastUpdateTime;

            if(deltaTime >= renderDelay) {
                lastUpdateTime = System.nanoTime();
                gs.render(entityList);
                update(deltaTime);
            }
        }
    }

我希望游戏能以不同的分辨率工作。因此,我的“敌人”以及播放器在1920 x 1080分辨率和800 x 600分辨率下都处于相同的位置。 GameScreen(view)和GameController(controller)都有更多代码,但是我认为没有必要展示更多代码。我的模型只是我用于游戏的实体,例如玩家和敌人。

0 个答案:

没有答案