Apologies, as I know this is rather vague!
I have a simple ‘maze’ (black squares on a grid) with a player sprite (green square) as shown below:
The black squares / walls are supposed to block the player sprite, but frustratingly, the player sprite moves just anywhere it wants. Have tried lots of things (as you’ll see in the code). Can anyone help me get the maze walls working? (Thanks for any help)
Game1.cw
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
namespace cs_project
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
Texture2D player;
Texture2D enemy;
Texture2D tile;
Texture2D wall;
SpriteFont gamefont;
player players = new player();
maze mazes = new maze();
public KeyboardState keystate = Keyboard.GetState();
KeyboardState previousState;
public Vector2 EnemyPos = new Vector2(375, 225);
bool Killstate = false;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
_graphics.PreferredBackBufferHeight = 500;
_graphics.PreferredBackBufferWidth = 500;
_graphics.ApplyChanges();
base.Initialize();
previousState = Keyboard.GetState();
mazes.gridInitialize();
mazes.addwalls();
//mazes.BreakWalls();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
gamefont = Content.Load<SpriteFont>("galleryFont");
player = Content.Load<Texture2D>("square");
enemy = Content.Load<Texture2D>("triangle");
tile = Content.Load<Texture2D>("tile");
wall = Content.Load<Texture2D>("wall tile");
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
players.playercheck();
//kills player
if (players.playerPos == EnemyPos)
{
Killstate = true;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
if (Killstate == false)
{
GraphicsDevice.Clear(Color.White);
_spriteBatch.Begin();
//draws grid
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (mazes.grid[i, j] == '.')
{
_spriteBatch.Draw(tile, new Vector2(i * 50, j * 50), Color.White);
}
else if (mazes.grid[i, j] == 'x')
{
_spriteBatch.Draw(wall, new Vector2(i * 50, j * 50), Color.White);
}
}
}
//player and enemy drawing
_spriteBatch.Draw(player, new Vector2(players.playerPos.X - 25, players.playerPos.Y - 25), Color.White);
_spriteBatch.Draw(enemy, new Vector2(EnemyPos.X - 25, EnemyPos.Y - 25), Color.White);
_spriteBatch.End();
}
//game over screen
if (Killstate == true)
{
GraphicsDevice.Clear(Color.Black);
_spriteBatch.Begin();
_spriteBatch.DrawString(gamefont, "you died", new Vector2(225, 225), Color.White);
_spriteBatch.End();
}
base.Draw(gameTime);
}
}
}
Maze.cs
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace cs_project
{
internal class maze
{
player players = new player();
public bool upvalid = true;
public bool downvalid = true;
public bool leftvalid = true;
public bool rightvalid = true;
int numofwalls = 0;
Stack<int> wallListX = new Stack<int>();
Stack<int> wallListY = new Stack<int>();
public Stack<Vector2> wallList = new Stack<Vector2>();
public char[,] grid = new char[10, 10];
public void gridInitialize()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
grid[i, j] = '.';
}
}
}
public void addwalls()
{
Random randx = new Random();
Random randy = new Random();
for (int i = 0; i < 20; i++)
{
int x = randx.Next(0, 10);
int y = randy.Next(0, 10);
if (players.modplayerPos != new Vector2(x * 50, y * 50))
{
grid[x, y] = 'x';
}
}
}
/*public void BreakWalls()
{
//walls added here
int currentX = 0; //x postion
int currentY = 0;// y postion
wallListX.Push(currentX); //adds x pos to stack
wallListY.Push(currentY); // add y pos to stack
Random rand = new Random();
//wall addition
while (wallListX.Count > 0 && wallListY.Count > 0 || numofwalls <= 100)
{
//1 = north
//2 = east
//3 = south
//4 = west
Random randDict = new Random();
int randomdirectionn = randDict.Next(1, 4);
//wall to north
if (randomdirectionn == 1 && wallListY.Peek() > 0 )
{
currentX = currentX - 1;
wallListX.Push(currentX);
wallListY.Push(currentY);
numofwalls++;
}
//wall to east
else if (randomdirectionn == 2 && wallListY.Peek() < 10)
{
currentY = currentY + 1;
wallListY.Push(currentY);
wallListX.Push(currentX);
numofwalls++;
}
//wall to south
else if (randomdirectionn == 3 && wallListY.Peek() < 10)
{
currentX = currentX + 1;
wallListX.Push(currentX);
wallListY.Push(currentY);
numofwalls++;
}
//wall to west
else if (randomdirectionn == 4 && wallListY.Peek() > 0)
{
currentY = currentY - 1;
wallListY.Push(currentY);
wallListX.Push(currentX);
numofwalls++;
}
else
{
//no avaible postion to add wall so backtracks 1 step
if (wallListX.Peek() - 1 > -1 && wallListY.Peek() + 1 < 10)
{
wallList.Push(new Vector2(wallListX.Peek() * 50, wallListY.Peek() * 50));
wallListX.Pop();
wallListY.Pop();
}
}
}
}
*/
public void collisioncheck()
{
foreach (Vector2 item in wallList)
{
if (players.modplayerPos.X == wallList.Peek().X && players.modplayerPos.Y - 50 == wallList.Peek().Y)
{
upvalid = false;
}
else
{
upvalid = true;
}
if (players.modplayerPos.X == wallList.Peek().X && players.modplayerPos.Y + 50 == wallList.Peek().Y)
{
downvalid = false;
}
else
{
downvalid = true;
}
if (players.modplayerPos.X - 50 == wallList.Peek().X && players.modplayerPos.Y == wallList.Peek().Y)
{
leftvalid = false;
}
else
{
leftvalid = true;
}
if (players.modplayerPos.X + 50 == wallList.Peek().X && players.modplayerPos.Y == wallList.Peek().Y)
{
rightvalid = false;
}
else
{
rightvalid = true;
}
}
}
}
}
Player.cs
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace cs_project
{
internal class player
{
KeyboardState previousState;
public Vector2 playerPos = new Vector2(75, 75);
public Vector2 modplayerPos = new Vector2(50, 50);
public void playercheck()
{
maze mazes = new maze();
mazes.collisioncheck();
KeyboardState keystate = Keyboard.GetState();
if ((playerPos.X <= 1000 && playerPos.X >= 0) && (playerPos.Y <= 1000 && playerPos.Y >= 0))
{
if (mazes.upvalid == true)
{
if (keystate.IsKeyDown(Keys.Up) & !previousState.IsKeyDown(Keys.Up))
{
playerPos.Y -= 50;
modplayerPos.Y -= 50;
}
}
if (mazes.downvalid == true)
{
if (keystate.IsKeyDown(Keys.Right) & !previousState.IsKeyDown(Keys.Right))
{
playerPos.X += 50;
modplayerPos.X += 50;
}
}
if (mazes.leftvalid == true)
{
if (keystate.IsKeyDown(Keys.Down) & !previousState.IsKeyDown(Keys.Down))
{
playerPos.Y += 50;
modplayerPos.Y += 50;
}
}
if (mazes.rightvalid == true)
{
if (keystate.IsKeyDown(Keys.Left) & !previousState.IsKeyDown(Keys.Left))
{
playerPos.X -= 50;
modplayerPos.X -= 50;
}
}
if (playerPos.X >= 1000)
{
playerPos.X -= 50;
modplayerPos.X -= 50;
}
if (playerPos.Y >= 1000)
{
playerPos.Y -= 50;
modplayerPos.Y -= 50;
}
if (playerPos.X <= 0)
{
playerPos.X += 50;
modplayerPos.X += 50;
}
if (playerPos.Y <= 0)
{
playerPos.Y += 50;
modplayerPos.Y += 50;
}
previousState = keystate;
}
}
}
}