我在C#和XNA 4.0中製作遊戲。它已經完成了,但現在我想添加設置,以便玩家可以根據需要更改窗口大小。目前的設置是這樣的:如何縮放遊戲的圖形?
void Initialize()
{
//The window size is initally 800x480
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 480;
graphics.ApplyChanges();
}
void Update()
{
//If the player completes an action, the window size is changed
if (windowSizeChanged)
{
graphics.PreferredBackBufferWidth = 1024;
graphics.PreferredBackBufferHeight = 720;
graphics.ApplyChanges();
}
}
使用此代碼,這是本場比賽的樣子在具體決議:
800X480
1024x720
,你可以希望看到,當窗口大小發生變化時,它不會影響遊戲的實際圖形。所有對象的精靈和hitbox保持相同的大小,所以他們在屏幕的角落裏填滿了一個小盒子,而不是整個窗口。任何人都可以告訴我如何縮放精靈以填滿窗口?我認爲我需要使用某種矩陣,但任何人都可以指出我的方向?
編輯:
這裏是繪製代碼。
void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
spriteBatch.Begin();
//Background texture drawn at default window size
spriteBatch.Draw(background, new Rectangle(0, 0, 800, 480), Color.White);
//Each object in the level (player, block, etc.) is drawn with a specific texture and a rectangle variable specifying the size and coordinates
//E.g. Each block is a size of 64x64 pixels and in this level they are placed at X-coordinates 0, 64, 128 and so on
//Each Y-coordinate for the blocks in this level is '480 - 64' (Window height minus block height)
foreach (/*Object in level*/)
{
spriteBatch.Draw(object.Texture, object.TextureSize, Color.White);
}
spriteBatch.End();
}
發佈您的視口代碼並繪製調用。 – RadioSpace
您可能會喜歡這篇文章:http://www.david-amador.com/2010/03/xna-2d-independent-resolution-rendering/ –