Hello everyone, i am trying to make a game similar to Battle tank.
https://external-content.duckduckgo.com/iu/?u=http%3A%2F%2F2.bp.blogspot.com%2F_1KtbcKSJSh0%2FTI16-StxSlI%2FAAAAAAAAAxA%2F-VYSZo03EFE%2Fs1600%2Fbattlecity1a_2.bmp&f=1&nofb=1
The Tanks and objects will move inside the black texture.
-
First, I want to set all objects/assets inside the game environment to become responsive(like if user changes the game window size manually, then all it contains should resize according to it). I added some lines for it, but not working for game objects(ex : tank).
-
Secondly, I want to make all object’s measurement’s perfect, so while using them together will not create any issue. Example the tank is perfectly moving between the wall and boundary(in below image)
Here is my Game1.cs upto now :
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Tanks //Test5
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Color backgroundColor = Color.DarkGray;
Texture2D gameSpace;
// this is probably not required i do something similar its fine i guess.
GameContent gameContent; // Creating game content object
private Tank tank;
private Bricks bricks;
private Eagle eagle;
private int screenWidth = 1280;
private int screenHeight = 720;
// Controls
private MouseState oldMouseState;
private KeyboardState oldKeyboardState;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = screenWidth;
graphics.PreferredBackBufferHeight = screenHeight;
this.IsMouseVisible = true;
Window.AllowUserResizing = true;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// one time creation this should also be disposed in unload now.
gameSpace = GetSetDataCreatedTexture(graphics, (screenWidth - 280), (screenHeight - 70)); // 1000, 650
// TODO: use this.Content to load your game content here
gameContent = new GameContent(Content);
// update screen width and height when you resize the window this goes with AllowUserResizing.
Window.ClientSizeChanged += CallMeWhenTheUserResizesTheWindowHimself;
// Getting Screen Width and Height from GraphicsAdapter.DefaultAdapter.CurrentDisplayMode
screenWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
screenHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
//create game objects
// Tank
int tankX = (screenWidth) / 2; //we'll center the tank on the screen to start
int tankY = screenHeight / 2; //tank will be 100 pixels from the bottom of the screen
tank = new Tank(tankX, tankY, screenWidth, screenHeight, spriteBatch, gameContent); // Create Tank
// Bricks
int bricksX = (screenWidth) / 2;
int bricksy = (screenHeight) / 2;
bricks = new Bricks(bricksX, bricksy, Color.White, spriteBatch, gameContent);
// Eagle
eagle = new Eagle(bricksX, bricksy, Color.White, spriteBatch, gameContent);
}
// this is chained to the callback Window.ClientSizeChanged
public void CallMeWhenTheUserResizesTheWindowHimself(object sender, EventArgs e)
{
screenWidth = GraphicsDevice.Viewport.Width;
screenHeight = GraphicsDevice.Viewport.Height;
gameSpace = GetSetDataCreatedTexture(graphics, (screenWidth - 280), (screenHeight - 70)); // 1000, 650
Console.WriteLine("---- Height : " + screenHeight);
Console.WriteLine("---- Width : " + screenWidth);
}
// if you must set this from a specific need once conditionally from update.
// calling this everyframe will also lock or crash your app.
public void ManuallyChangeTheBackBuffer(int width, int height)
{
graphics.PreferredBackBufferWidth = width;
graphics.PreferredBackBufferHeight = height;
graphics.ApplyChanges();
screenWidth = GraphicsDevice.Viewport.Width;
screenHeight = GraphicsDevice.Viewport.Height;
Console.WriteLine("---- viewport screenHeight : " + screenHeight);
Console.WriteLine("---- viewport screenWidth : " + screenWidth);
Console.WriteLine("---- BackBufferHeight : " + GraphicsDevice.PresentationParameters.BackBufferHeight);
Console.WriteLine("---- BackBufferWidth : " + GraphicsDevice.PresentationParameters.BackBufferWidth);
Console.WriteLine("---- CurrentDisplayMode Height : " + GraphicsDevice.Adapter.CurrentDisplayMode.Width);
Console.WriteLine("---- CurrentDisplayMode Width : " + GraphicsDevice.Adapter.CurrentDisplayMode.Height);
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
if (gameSpace != null)
if (gameSpace.IsDisposed != true)
gameSpace.Dispose();
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
KeyboardState newKeyboardState = Keyboard.GetState();
MouseState newMouseState = Mouse.GetState();
// Process keyboard events
if (newKeyboardState.IsKeyDown(Keys.Left))
{
tank.MoveLeft();
}
if (newKeyboardState.IsKeyDown(Keys.Right))
{
tank.MoveRight();
}
if (newKeyboardState.IsKeyDown(Keys.Up))
{
tank.MoveUp();
}
if (newKeyboardState.IsKeyDown(Keys.Down))
{
tank.MoveDown();
}
if (newKeyboardState.IsKeyDown(Keys.D1))
ManuallyChangeTheBackBuffer(500, 500);
oldMouseState = newMouseState; // this saves the old state
oldKeyboardState = newKeyboardState;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
GraphicsDevice.Clear(backgroundColor);
Vector2 coor = new Vector2(110, 30);
spriteBatch.Draw(gameSpace, coor, Color.Black);
//--------------------
bricks.Draw();
tank.Draw();
eagle.Draw();
spriteBatch.End();
base.Draw(gameTime);
}
public Texture2D GetSetDataCreatedTexture(GraphicsDeviceManager graphics, int w, int h)
{
Texture2D t = new Texture2D(graphics.GraphicsDevice, w, h);
Color[] data = new Color[w * h];
for (int i = 0; i < data.Length; ++i) data[i] = Color.White;
t.SetData(data);
return t;
}
}
}