Created
October 17, 2017 14:27
-
-
Save NattyNarwhal/c0ad4ec57cf81eddb9f0b4a11322ca2e to your computer and use it in GitHub Desktop.
smarting about XNA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// quick hack | |
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Graphics; | |
using Microsoft.Xna.Framework.Input; | |
using System; | |
namespace MonoGameTest | |
{ | |
public class Game1 : Game | |
{ | |
GraphicsDeviceManager graphics; | |
SpriteBatch spriteBatch; | |
SpriteFont sf; | |
Texture2D bulb; | |
Texture2D folder; | |
Point playerPosition; | |
//bool[,] map = { { false, true, false, false}, { false, false, false, true }, { true, true, true, false } }; | |
bool[,] map = new bool[32,32]; | |
public Game1() | |
{ | |
graphics = new GraphicsDeviceManager(this); | |
Content.RootDirectory = "Content"; | |
} | |
protected override void Initialize() | |
{ | |
var r = new Random(); | |
for (int i = 0; i < 32; i++) | |
{ | |
for (int j = 0; j < 32; j++) | |
{ | |
if (i == 0 && j == 0) continue; | |
map[j, i] = r.Next(0, 3) > 1; | |
} | |
} | |
base.Initialize(); | |
} | |
protected override void LoadContent() | |
{ | |
// Create a new SpriteBatch, which can be used to draw textures. | |
spriteBatch = new SpriteBatch(GraphicsDevice); | |
sf = Content.Load<SpriteFont>("font"); | |
bulb = Content.Load<Texture2D>("lightbulb"); | |
folder = Content.Load<Texture2D>("folder"); | |
} | |
protected override void UnloadContent() | |
{ | |
// TODO: Unload any non ContentManager content here | |
} | |
void Move(int x, int y) | |
{ | |
for (int i = 0; i < map.GetLength(1); i++) | |
{ | |
for (int j = 0; j < map.GetLength(0); j++) | |
{ | |
if (map[j, i]) | |
{ | |
var newPos = new Rectangle(new Point(x, y), new Point(32, 32)); | |
var testTile = new Rectangle(new Point(i * 32, j * 32), new Point(32, 32)); | |
if (newPos.Intersects(testTile)) | |
return; | |
} | |
} | |
} | |
playerPosition = new Point(x, y); | |
} | |
protected override void Update(GameTime gameTime) | |
{ | |
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) | |
Exit(); | |
const int by = 2; | |
if (Keyboard.GetState().IsKeyDown(Keys.Up)) | |
Move(playerPosition.X, playerPosition.Y - by); | |
if (Keyboard.GetState().IsKeyDown(Keys.Down)) | |
Move(playerPosition.X, playerPosition.Y + by); | |
if (Keyboard.GetState().IsKeyDown(Keys.Left)) | |
Move(playerPosition.X - by, playerPosition.Y); | |
if (Keyboard.GetState().IsKeyDown(Keys.Right)) | |
Move(playerPosition.X + by, playerPosition.Y); | |
base.Update(gameTime); | |
} | |
protected override void Draw(GameTime gameTime) | |
{ | |
GraphicsDevice.Clear(Color.CornflowerBlue); | |
spriteBatch.Begin(transformMatrix: Matrix.CreateTranslation(-playerPosition.X + (GraphicsDevice.Viewport.Width / 2), -playerPosition.Y + (GraphicsDevice.Viewport.Height / 2), 0)); | |
for (int i = 0; i < map.GetLength(1); i++) | |
{ | |
for (int j = 0; j < map.GetLength(0); j++) | |
{ | |
if (map[j, i]) | |
{ | |
spriteBatch.Draw(folder, new Rectangle(new Point(i * 32, j * 32), new Point(32, 32)), Color.Beige); | |
} | |
} | |
} | |
spriteBatch.Draw(bulb, new Rectangle(playerPosition, new Point(32, 32)), Color.Beige); | |
spriteBatch.End(); | |
// draw hud | |
spriteBatch.Begin(); | |
spriteBatch.DrawString(sf, playerPosition.ToString(), new Vector2(0, 0), Color.Black); | |
spriteBatch.End(); | |
base.Draw(gameTime); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment