Related
As you can see from the screenshot there are occasions when errors happen
My code should at those moments be observing the points above the white blocks, checking either side of them to see if there are any other empty sections, and if not the circled areas should be empty, not blocks.
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//initiate lists (1 list for object), (2 list for occupied nearby)
int[] list1 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list2 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list3 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list4 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list5 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list6 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list7 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list8 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list9 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list10 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list11 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list12 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list13 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list14 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list15 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list16 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[][] lists = new int[][] { list1, list2, list3, list4, list5, list6, list7, list8, list9, list10, list11, list12, list13, list14, list15, list16 };
int[] list1a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list2a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list3a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list4a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list5a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list6a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list7a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list8a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list9a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list10a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list11a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list12a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list13a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list14a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list15a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list16a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[][] listsa = new int[][] { list1a, list2a, list3a, list4a, list5a, list6a, list7a, list8a, list9a, list10a, list11a, list12a, list13a, list14a, list15a, list16a };
//assign walls
Random random = new Random();
//full
// open = 2, unnasigned = 0, block = 1
for (int i = 0; i <= 15; i++)
{
for (int j = 0; j <= 15; j++)
{
// % chance of block spawning
int r = 2;
int p = random.Next(1, 11);
if (p > 5) { r = 1; }
else if (p < 6) { r = 2; }
//do we have any surrounding open* blocks? (above,sides) *(not unassigned, OPEN)
int[] openspaces = new int[3] { 0, 0, 0 }; //for recording which spaces are open
var open = "no"; //assumed to be no
//check space to sides and above, but not below as always unassigned
try { if (lists[j][i - 1] == 2) { open = "yes"; openspaces[0] = 1; } } catch (IndexOutOfRangeException) { } //above
try { if (lists[j + 1][i] == 2) { open = "yes"; openspaces[1] = 1; } } catch (IndexOutOfRangeException) { } //right
try { if (lists[j - 1][i] == 2) { open = "yes"; openspaces[2] = 1; } } catch (IndexOutOfRangeException) { } //left
if (open == "no")
{
lists[j][i] = r; // 50/50 chance as completely singular square
}
else if (open == "yes")
{ // if the spaces are open, do they have any other open or undecided blocks near them (under, sides)
int[] otheropen = new int[3] { 0, 0, 0 }; //for recording which nearby open block have other nearby open spaces
if (openspaces[0] == 1) //above
{
//check space to sides, but not above as not relavent, and below would be checking itself, and none are invalid
try { if ((lists[j + 1][i - 1] == 0 || lists[j + 1][i - 1] == 2) && listsa[j + 1][i - 1] != 1) { otheropen[0] = 1; } } catch (IndexOutOfRangeException) { }
try { if ((lists[j - 1][i - 1] == 0 || lists[j - 1][i - 1] == 2) && listsa[j - 1][i - 1] != 1) { otheropen[0] = 1; } } catch (IndexOutOfRangeException) { }
}
else if (openspaces[0] == 0) { otheropen[0] = 2; } // if the block is not open it is ignored
if (openspaces[1] == 1) //right
{
//check space to right and below, but not above as not relavent, and left would be checking itself, and none are invalid
try { if ((lists[j + 1][i + 1] == 0 || lists[j][i - 1] == 2) && listsa[j + 1][i + 1] != 1) { otheropen[1] = 1; } } catch (IndexOutOfRangeException) { }
try { if ((lists[j + 2][i] == 0 || lists[j + 1][i] == 2) && listsa[j + 2][i] != 1) { otheropen[1] = 1; } } catch (IndexOutOfRangeException) { }
}
else if (openspaces[1] == 0) { otheropen[1] = 2; } // if the block is not open it is ignored
if (openspaces[2] == 1) //left
{
//check space to left and below, but not above as not relavent, and right would be checking itself, and none are invalid
try { if ((lists[j - 1][i + 1] == 0 || lists[j][i - 1] == 2) && listsa[j - 1][i + 1] != 1) { otheropen[2] = 1; } } catch (IndexOutOfRangeException) { }
try { if ((lists[j - 2][i] == 0 || lists[j - 1][i] == 2) && listsa[j - 2][i] != 1) { otheropen[2] = 1; } } catch (IndexOutOfRangeException) { }
}
else if (openspaces[2] == 0) { otheropen[2] = 2; } // if the block is not open it is ignored
if (otheropen[0] == 0 || otheropen[1] == 0 || otheropen[2] == 0) { lists[j][i] = 2; } //sets self as open, as a nearby open block has no other open blocks
else if (otheropen[0] != 0 && otheropen[1] != 0 && otheropen[2] != 0)
//yes all the near open block have a possible other route
{
lists[j][i] = r; // 50/50 as this block will not create a dead end
if (r == 1)//if original block closed then nearby open blocks will become invalid for next check, i.e. considered closed
{
try { if (otheropen[0] == 1) { listsa[j][i - 1] = 1; } } catch (IndexOutOfRangeException) { }//top
try { if (otheropen[1] == 1) { listsa[j + 1][i] = 1; } } catch (IndexOutOfRangeException) { }//right
try { if (otheropen[2] == 1) { listsa[j + 1][i] = 1; } } catch (IndexOutOfRangeException) { }//left
}
}
}
}
}
for (int j = 0; j <= 15; j++)
{
Console.WriteLine();
for (int i = 0; i <= 15; i++)
{
if (lists[j][i] == 1) { Console.Write("██"); }
if (lists[j][i] == 2) { Console.Write("░░"); }
}
}
}
}
I can't figure out why this error is happening, could it be due to the try checking. I know this is a large code dump, and probably pretty difficult to interpret from what I've posted, and I'm sure I could do stuff more efficiently as I'm new to C#, but any help would be incredible.
I really want for every square to be either accessible or to be a wall, but as you can see wall cross the whole area at points.
EDIT: Process explination:
So it iterates through a large array. First, it looks to see if there are any "open blocks" (of value 2) above and to the sides of the current specific value in the 2d array. If there aren't it randomly decides if the block is "open" or "filled" (value of 2 or 1 respectively).
If there is an open block, it will check each of those blocks surrounding blocks (the side and below) to see if there are any other open(2) blocks that could act as paths. If there are then it acknowledges this and randomly chooses again. If one of these blocks has no surrounding open blocks, then the current value should become an open block.
If a random choice is to be a 'filled' block after the latter process, then it will assign the near block as no longer being valid as open to the same process again, so that when another block is considering if it can use that as a path it knows it cannot.
Alright, after reformatting your code to make it much easier I found what I believe to be the error (at least I cannot get the error to reproduce after these changes. (The code below is not reformatted to make it easier for you to find in your code)
In this section
if (openspaces[1] == 1) //right
{
//check space to right and below, but not above as not relavent, and left would be checking itself, and none are invalid
try { if ((lists[j + 1][i + 1] == 0 || lists[j][i - 1] == 2) && listsa[j + 1][i + 1] != 1) { otheropen[1] = 1; } } catch (IndexOutOfRangeException) { }
try { if ((lists[j + 2][i] == 0 || lists[j + 1][i] == 2) && listsa[j + 2][i] != 1) { otheropen[1] = 1; } } catch (IndexOutOfRangeException) { }
}
else if (openspaces[1] == 0) { otheropen[1] = 2; } // if the block is not open it is ignored
if (openspaces[2] == 1) //left
{
//check space to left and below, but not above as not relavent, and right would be checking itself, and none are invalid
try { if ((lists[j - 1][i + 1] == 0 || lists[j][i - 1] == 2) && listsa[j - 1][i + 1] != 1) { otheropen[2] = 1; } } catch (IndexOutOfRangeException) { }
try { if ((lists[j - 2][i] == 0 || lists[j - 1][i] == 2) && listsa[j - 2][i] != 1) { otheropen[2] = 1; } } catch (IndexOutOfRangeException) { }
}
If you look at where it says try { if ... you will notice that the second condition in your or statement is different then the first condition. They should be the same. It looks like you fell victim to a copy paste error.
As #Tipx suggested reformatting your code would have made this much easier to find. As you are new to C#, learn from the beginning to make your code readable. You will thank yourself later.
Note, your code still allows chunks of path to be separated from the main path (set your random seed to 0 and look at the top right corner), but you shouldn't have any more individual squares like you were having.
I am trying to have a loop read a multidimensional array defined in a separate class to place objects at the start of a game.
This is the Start function code from the LevelStart class:
void Start () {
//Get Level
Levels levels = ScriptableObject.CreateInstance<Levels>();
level = levels.GetLevel (levelName);
x = level.GetLength(1);
y = level.GetLength (2);
temp = GameObject.Find ("temp").GetComponent<Temp>();
lPos = temp.GetLevelNum() - 1; //Chosen Level
//Read array and place objects
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (level[lPos,i,j] == 1) {
Debug.Log ("Spawn Point");
}
else if (level[lPos,i,j] == 2) {
Debug.Log ("Hole");
}
else if (level[lPos,i,j] == 3) {
Debug.Log ("Obstacle");
}
else
Debug.Log ("Open Space");
}
}
}
And the class holding the arrays is Levels.cs:
public int[,,] cave = new int[,,] {
{ //Level 1
{ 0, 0, 0, 3, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0},
{ 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 3, 0, 0},
{ 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 3, 0, 0, 0},
{ 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 3, 0},
{ 0, 0, 3, 0, 3, 0, 0, 3, 0, 0, 0, 0, 0, 0},
{ 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0},
{ 0, 0, 3, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 2, 0, 0, 0, 3, 0, 3, 0, 0, 0}
},
{ //Level 2
{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0 },
{ 0, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 0, 0 },
{ 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 3, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0 },
{ 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 3, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0 },
{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0 }
}
};
It reads most of the array correctly but positions such as [0,0,0] and [0,0,12] returns 3's when it should be 0
Thanks in advance
Seems to me to be pretty likely that you're simply using the wrong level number - code that is not shown. I suggest using the print statement to ensure temp.GetLevelNum() and lPos are the numbers you think they should be.
I am using C# in a console application.
I need to load data from a text file and load it into a 2d array.
This is what I tried, but when I try to print out the contents of what gets returned nothing gets printed.
public static int[,] LoadMap()
{
const string path = #"1.txt";
string[] fileLines = File.ReadAllLines(path);
int[,] map = new int[fileLines.Length, 15];
string line;
for (int i = 0; i < fileLines.Length; ++i)
{
line = fileLines[i];
for (int j = 0; j < line.Length; ++j)
{
map[i, j] = (int)(line[j] - '0');
}
}
return map;
}
But when I hardcode the data like that, then everything gets displayed perfectly.
private static int[,] Map = new int[MapX, MapY]
{
{ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 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1,1
Any help will be appreciated whether you fix what I tried or propose something completely different, thanks.
string[] fileLines = File.ReadAllLines(path);
int[,] map = new int[fileLines.Length,fileLines[0].Split(',').Length];
for (int i = 0; i < fileLines.Length; ++i)
{
string line = fileLines[i];
for (int j = 0; j < map.GetLength(1); ++j)
{
string[] split = line.Split(',');
map[i, j] = Convert.ToInt32(split[j]);
}
}
return map;
}
You could use LINQ to parse the lines:
var lines = File.ReadAllLines(path);
int[,] map = new int[fileLines.Length, 25];
for (int i = 0; i < fileLines.Length; ++i)
{
var data = lines[i].Split(',').Select(c => Convert.ToInt32(c)).ToList();
for(int j =0; j<25; ++j)
map[i,j] = data[j];
}
return map;
If you could use a jagged array instead of a 2D array, this becomes simpler:
public static int[][] LoadMap()
{
return File.ReadLines(path)
.Select(l => l.Split(',').Select(Convert.ToInt32).ToArray())
.ToArray();
}
If your text file has commas separating the values, replace this line:
for (int j = 0; j < line.Length; ++j)
With:
for (int j = 0; j < line.Length; j += 2)
That's assuming your values will always be only 1 char long.
When the data comes in from the text file. If it is separated by a comma you can separate it using string.split. Then you load what you get into an array and access it like you normally would do an array.
Like below:
string[] lines = System.IO.File.ReadAllLines(#"path");
foreach (string line in lines)
{
string[] first= line.Split(comma);
}
I made a tile level system that uses a multidimensinal array, for example:
new int[,]{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},
Every specific number represents a block(tile).
This is just in code. I want to code a system that can load a level(an multidimensinal int array)
That array needs to be converted from a string. How do I do that?
public static int[,] getLvl(string lvlName = "")
{
string readed = "";
using (StreamReader read = new StreamReader(path))
{
readed = read.ReadToEnd();
}
return null; //What to put here?!?!?!
}
EDIT: I do not have yet a format for the file to read. So you can be flexible in that.
I don't think you really need to or should bother trying to serialize it into XML or some other format, since your data storage is so simple.
One easy way is to just store your array in a text file as comma separated values. So one level might have:
0,0,0,1
0,1,1,0
0,1,1,3
3,3,4,1
The String.Split() method is really useful for parsing something simple like this. It allows you to split a string into an array of substrings based on a certain delimiting character.
Step by step:
First you can use var RowArray = MyString.Split('\n') (the newline character) to split your string into an array of rows. This leaves you with the array:
[0]: "0,0,0,1"
[1]: "0,1,1,0"
[2]: "0,1,1,3"
[3]: "3,3,4,1"
You can sort of see what Split() does here and why that's useful for your case. You can in turn run split each row on ',' leaving with you an array of arrays, which you can very easily convert to exactly the 2D array you're looking for.
The one pitfall here is somewhere, depending on your design needs, one invariant might have to be that in the file all rows will be of the same length. Or if you can't guarantee that, you'll have to write some code so that when turning the below text into an array from an array of rows, you make the width equal to the longest row and fill in blanks with 0s or some other method.
0,0,0,1,6,4
0,1,1,0
0,1,1,3,2,6,3,7,1
3,3,4,1,2,4
The shortest method is using linq, for this format:
0,0,0,0,0
1,0,1,0,1
....
You can use this sentence:
int[][] Data = File.ReadAllLines( path )
.Select( s => s.Trim())
.Where( s => !string.IsNullOrempty(s))
.Select( s => s.Split( ',' )
.Select( token => int.Parse( token ) )
.ToArray( ) )
.ToArray( );
var DataToOneDim = Data.SelectMany( a => a).ToArray();
var Result = new int[Data[0].Length, Data.Length];
Buffer.BlockCopy( DataToOneDim, 0, Result, 0, DataToOneDim.Length );
I believe there are a few other posts regarding this, but you can use either XML and parse through each possible dimension or serialize everything at once using the SoapFormatter class. Below is a link to a similar question with some examples:
Convert Multi-Dimensional Array to String and Back
I make it for you fast you can make it better do not Forget the vote :_)
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
var array =
"{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,} , {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,}";
string input = array;
string pattern = #"{|}";
var index = 0;
var results = new int[100,100];
foreach (var result in Regex.Split(input, pattern))
{
var sp = result.Split(',');
if (sp.Length <4)
continue;
for (int i = 0; i < sp.Count(); i++)
{
if (!string.IsNullOrEmpty(sp[i]))
results[index,i] = Convert.ToInt32(sp[i]);
}
index++;
}
}
}
}
I've been looking through the other questions but just can't seem to figure this out.. I'm using XNA in a custom tilemap loader. Here is the code.
http://pastebin.com/cuatQHTb
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Pressure
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D player;
Vector2 pos = Vector2.Zero;
Vector2 playersp = new Vector2(50.0f, 50.0f);
Texture2D road1;
Texture2D road2;
Texture2D brickwall;
Texture2D floor1;
Texture2D floor2;
Texture2D floor3;
Texture2D grass;
Texture2D sidewalk1;
Texture2D wood;
Texture2D road3;
private Vector2 origin;
KeyboardState currentState;
Camera camera = new Camera();
Vector2 motion;
List<Texture2D> tiles = new List<Texture2D>();
static int tileWidth = 64;
static int tileHeight = 64;
int tileMapWidth;
int tileMapHeight;
static int screenWidth;
static int screenHeight;
static int mapWidthInPixels;
static int mapHeightInPixels;
int[,] map = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 5, 6, 5, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 8, 7, 5, 6, 5, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 8, 1, 1, 1, 2, 2, 1, 8, 7, 5, 6, 5, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 8, 1, 1, 1, 2, 2, 1, 8, 7, 5, 5, 5, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 8, 1, 1, 1, 1, 1, 1, 8, 7, 5, 5, 5, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 8, 1, 3, 3, 3, 1, 1, 8, 7, 5, 6, 5, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 8, 8, 1, 1, 8, 8, 8, 8, 7, 5, 6, 5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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
};
public static int ScreenWidth
{
get { return screenWidth; }
}
public static int ScreenHeight
{
get { return screenHeight; }
}
public static int MapWidthInPixels
{
get { return mapWidthInPixels; }
}
public static int MapHeightInPixels
{
get { return mapHeightInPixels; }
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
player = Content.Load<Texture2D>("haz");
origin.X = player.Width /2;
origin.Y = player.Height /2;
grass = Content.Load<Texture2D>("grass");
floor1 = Content.Load<Texture2D>("floor1");
floor2 = Content.Load<Texture2D>("floor2");
floor3 = Content.Load<Texture2D>("floor3");
wood = Content.Load<Texture2D>("wood");
road1 = Content.Load<Texture2D>("road1");
road2 = Content.Load<Texture2D>("road2");
sidewalk1 = Content.Load<Texture2D>("sidewalk1");
brickwall = Content.Load<Texture2D>("brickwall");
road3 = Content.Load<Texture2D>("road3");
tiles.Add(grass); //0
tiles.Add(floor1);//1
tiles.Add(floor2);//2
tiles.Add(floor3);//3
tiles.Add(wood);//4
tiles.Add(road1);//5
tiles.Add(road2);//6
tiles.Add(sidewalk1);//7
tiles.Add(brickwall);//8
tiles.Add(road3);//9
tileMapWidth = map.GetLength(1);
tileMapHeight = map.GetLength(0);
mapWidthInPixels = tileMapWidth * tileWidth;
mapHeightInPixels = tileMapHeight * tileHeight;
screenWidth = GraphicsDevice.Viewport.Width;
screenHeight = GraphicsDevice.Viewport.Height;
}
protected override void UnloadContent()
{
}
private float RotationAngle;
private float oldx;
private float oldy;
protected override void Update(GameTime gameTime)
{
oldx = playersp.X;
oldy = playersp.Y;
currentState = Keyboard.GetState();
pos = playersp;
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
IsMouseVisible = true;
MouseState Mouses = Mouse.GetState();
Vector2 mouseLook = new Vector2(Mouses.X, Mouses.Y);
motion = Vector2.Zero;
Vector2 direction = (playersp ) - mouseLook;
float angle = (float)(Math.Atan2(direction.Y, direction.X));
RotationAngle = angle ;
if (currentState.IsKeyDown(Keys.W))
{
if (playersp.Y > screenHeight /4)
{
playersp.Y = playersp.Y - 1;
}
else
{
ScrollUp();
}
}
if (currentState.IsKeyDown(Keys.A))
{
if (playersp.X > screenWidth / 4)
{
playersp.X = playersp.X - 1;
}
else
{
ScrollLeft();
}
}
if (currentState.IsKeyDown(Keys.S))
{
if (playersp.Y > screenHeight / 1.5f)
{
ScrollDown();
}else{
playersp.Y = playersp.Y + 1;
}
}
if (currentState.IsKeyDown( Keys.D))
{
if (playersp.X > screenWidth / 1.5f)
{
ScrollRight();
}
else
{
playersp.X = playersp.X + 1;
}
}
if (motion != Vector2.Zero)
{
motion.Normalize();
camera.Position += motion * camera.Speed;
}
base.Update(gameTime);
}
private void ScrollUp()
{
motion.Y = -0.5f;
}
private void ScrollRight()
{
motion.X = 0.5f;
}
private void ScrollDown()
{
motion.Y = 0.5f;
}
private void ScrollLeft()
{
motion.X = -0.5f;
}
private Point VectorToCell(Vector2 vector)
{
return new Point(
(int)(vector.X / tileWidth),
(int)(vector.Y / tileHeight));
}
private Vector2 ViewPortVector()
{
return new Vector2(
screenWidth + tileWidth,
screenHeight + tileHeight);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
DrawMap();
base.Draw(gameTime);
}
private void DrawMap()
{
Point cameraPoint = VectorToCell(camera.Position);
Point viewPoint = VectorToCell(camera.Position +
ViewPortVector());
Point min = new Point();
Point max = new Point();
min.X = cameraPoint.X;
min.Y = cameraPoint.Y;
max.X = (int)Math.Min(viewPoint.X, map.GetLength(1));
max.Y = (int)Math.Min(viewPoint.Y, map.GetLength(0));
Rectangle tileRectangle = new Rectangle(
0,
0,
tileWidth,
tileHeight);
spriteBatch.Begin();
for (int y = min.Y; y < max.Y; y++)
{
for (int x = min.X; x < max.X; x++)
{
tileRectangle.X = x * tileWidth - (int)camera.Position.X;
tileRectangle.Y = y * tileHeight - (int)camera.Position.Y;
spriteBatch.Draw(tiles[map[y, x]],
tileRectangle,
Color.White);
spriteBatch.Draw(player, pos, null, Color.White, RotationAngle,
origin, 1.0f, SpriteEffects.None, 0f);
}
}
spriteBatch.End();
}
}
}
How would I detect the tiles position and make sure the player doesn't enter that tile? thanks!
You could do something like this:
private static float scalingFactor = 10;
private static float mapSizeX = 19;
private static float mapSizeY = 29;
in Update:
if (playersp.Y > screenHeight /4) {
int mapX = (int) (playersp.X / scalingFactor);
int mapY = (int) (playersp.Y / scalingFactor) - 1;
if (isMovable(mapX, mapY)) {
playersp.Y = playersp.Y - scalingFactor;
}
} else {
ScrollUp();
}
and a new method:
public bool isMovable(int mapX, int mapY)
{
if (mapX < 0 || mapX > 19 || mapY < 0 || mapY > 29) {
return false;
}
int tile = map[mapX, mapY];
if (tile == 4 || tile == 8) {
return false;
}
return true;
}
Similarly for the other directions.
The above code calls the function isMovable to decide whether the player can move to the new location based on the type of tile stored in the map at that position. The decision is false (the player cannot move there) if it is wood or brick wall, otherwise it is true.
The scaling factor is to map between the screen position (captured in playersp) and the tile map. In this case each tile is equivalent to 10 screen "pixels" (You can break it into two separate scales if you want to: one for X dimention, the other for Y).
Note, you need to make sure the values mapSizeX and mapSizeY are correct.
It would be best if you introduced named constants for the type of tile instead of using the numbers -- it will make your code more readable (for you in the future and for others reading it).
EDIT: updated code & explanation