I'm starting out with xna, I'm pretty newbie with this, but I'm making my efforts to go on with this framework, anymay, my problem is:
that I have many .png images and dont want to make an object for any of those images so I want to put them up in a Texture2D array, I thought that this is way to do it, but looks like it's not the correct way:
Texture2D[] _rCards, _bCards, _sCards;
_bCards = new Texture2D[9];
_rCards = new Texture2D[9];
_sCards = new Texture2D[6];
for (int i = 1; i < 10; i++)
{
_bCards[i] = Content.Load<Texture2D>("Images/Common/Black/"+i);
_rCards[i] = Content.Load<Texture2D>("Images/Common/Red/"+i);
if(i<6)
_sCards[i] = Content.Load<Texture2D>("Images/Special/Card" + (i-1));
}
The file names for the texture are 1.png, 2.png, 3.png, and so on.
For the special cards are card1.png, card2.png,card3.png and so on.
I'm trying to make a blackjack game.
Can you give me an advice to load all this textures in one single texture2D array.
The IDE gives an NULLREFERENCEEXCEPTION issue or something.
Maybe the language doesnt understands the entire adress to find the textures as a string.
Indexes are 0 based...
for (int i = 1; i < 10; i++)
{
_bCards[i-1] = Content.Load("Images/Common/Black/"+i);
_rCards[i-1] = Content.Load("Images/Common/Red/"+i);
if(i<6) _sCards[i-1] = Content.Load("Images/Special/Card" + (i-1));
}
if you want to load all textures at same time you can use the sprite sheet sample:
http://create.msdn.com/en-US/education/catalog/sample/sprite_sheet
You will have an unique asset and a dictionary of source rectangles to draw the sprites...
Related
I am stuck with this two dimension array with Unity framework coding with c#. I am trying to create a Checker game with an Intelligent agent. So I trying to get all the pieces and passing to the Alpha Beta algorithm and get the maximum amount of Opponent side. But in order to do that I have to check each piece on my board. I am using 8by8 board on my game. So I used
Piece[,] pieces = new Pieces[8,8];
code in order to store the pieces. Now I want to change one piece and retrieve the pieces.
I tried in several ways and experimented, But I could not found any to take this array and change one generic object and retrieve the list with the changes.
Please help me to solve this. Thanks in advance.
I written down here about what I experimented. Any suggestions about this matter or did I do something wrong?....
internal static void TryMoveInImaginaryWay(Piece[,] piece)
{
int x = 0;
int y =2;
int x1 = 1;
int y1 = 3;
Moves move = new Moves();
move.X = x;
move.Y = y;
move.X1 = x1;
move.Y1 = y1;
// Copping the pieces state
Piece p = piece[x, y];
//I am creating a list with those pieces
List<Piece> temppiecelist = new List<Piece>();
foreach (Piece pi in piece)
{
if (pi == p)
{
piece[x1, y1] = p;
temppiecelist.Add(null);
}
else
{
temppiecelist.Add(pi);
}
}
Piece[,] piek = temppiecelist; // can't convert it says Cannot Implicitly convert type "System.Collection.Generic.List<Piece>' to Piece[*,*]'"
// I know i can't convert like this, but any ideas.
//Piece[,] piek = new Piece[8, 8];
I am asking about , Is there any way to change above Piece[,] array list values. And I want the same format as output.
It looks like what you are trying to do is make a copy of the board and then make a move on that board.
Why on you would get a List involved I cannot figure out; what led you to believe that a List was the right solution? I am interested to know; by learning why people come to bad conclusions about program writing, I can help them write better programs.
To make a mutated copy of the board, just copy the board and mutate the copy:
internal static void TryMoveInImaginaryWay(Piece[,] original)
{
int x0 = 0;
int y0 = 2;
int x1 = 1;
int y1 = 3;
Piece[,] copy = (Piece[,])original.Clone();
copy[x1,y1] = copy[x0,y0];
copy[x0,y0] = null;
And you're done.
I want to detect the finger bending using a windows forms application in c#.
I have difficulty to add (I read it from this):
HandList hands = frame.Hands;
FingerList fingers = hands.Fingers;
int extendedFingers = 0;
for (int f = 0; f < hands.Fingers.Count; f++)
{
Finger digit = hands.Fingers[f];
if(fingers.IsExtended) extendedFingers++;
label1.Text = f.ToString();
}
because error appear one of them in second line of the program, and it said that
"Leap.Handlist does not contain definition of a "Fingers""
Have I missed something?
And where do I have to add those program, if I try from this one?
is in the void newFrameHandler(Frame frame)?
"Leap.Handlist does not contain definition of a "Fingers""
Presumably a Handlist is similar to an array. I would expect a single Hand to define Fingers, not an array of Hands.
You need a
HAND
not
HAND_S_
I've been thinking about trying to create a RPG game, a simple game with movement, pick up items, and open doors.
I've been thinking for a long time about the tile map engine, but i can't find anything that works :/
Basically what i'm trying to accomplish is i have an enum, like:
public enum tileSort { Dirt, Grass, Stone, Empty }
And when the engine runs through the array, which will have 0's, 1's, etc, I'm thinking about a switch statement, something like:
switch(tileSort)
{
case '0': tileList.Add(Content.Load<Texture2D>("Tiles/grass"))
}
The problem is that I have no idea on how to make this possible, all that I've been able to create is an engine that runs through and generates depending on which content you load first into the game.
I know this is confusing, as I'm not good at explaining myself.
Thanks in advance.
You can use some tools to help you:
http://jdevh.com/2012/06/01/griddy2d/
http://github.com/zachmu/tiled-xna
http://xnafantasy.wordpress.com/2008/11/22/xna-map-editor-version-30-released/
I'm sure you can find many others.
About the snippets of code you wrote, you don't want to call
Content.Load<Texture2D>("Tiles/grass")
multiple times for a single texture. Load each texture only once and print the same resource multiple times. You could have something like this:
var tileList = new List<Texture2D>();
string[] tiles = { "dirt", "grass", "stone", "empty" };
foreach (var s in tiles) {
tileList.Add(Content.Load<Texture2D>("Tiles/" + s));
}
Now each texture is loaded only once, and you can access it using tileList[index].
The next step is to print the tiles on the screen. I'll assume you have loaded your tiles into a 2 dimensional array with the tile indexes.
int[,] tileMap = {{1, 2, 0}, {0, 1, 2}, {3, 3, 1},};
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
spriteBatch.Draw(tileList[tileMap[i, j]], new Vector2D(50*i, 50*j), Color.White);
// Assuming the tiles are 50x50 pixels
This tutorial teaches what you want with more details: http://www.xnaresources.com/?page=Tutorial:TileEngineSeries:1
I am trying to figure out how to turn the following, one-line CSV file into a 30x30 2D array.
http://pastebin.com/8NP7s7N0
I've tried looking it up myself, but I just can't seem to wrap my brain around the concept of multidimensional arrays, and I don't know how to turn a one-line file like this into an array of a specified size.
I want to be able to make an array that would look like this when printed:
0,0 = 2
0,1 = 2
All the way to 30,30.
Most of the numbers in the CSV are indeed 2's, but some are 1s. The difference is very important though. I am trying to make collision detection for a game, and this CSV file is the map. All I need left is how to create this array - leave the rest to me. :)
Thank you very much to all, have a nice day.
This should be a complete example using a 5 x 5 grid. I've tried it and seems to work as expected:
namespace ConsoleApplication1
{
using System;
class Program
{
const int MapRows = 5;
const int MapColumns = 5;
static void Main(string[] args)
{
// Create map and the raw data (from file)
var map = new int[MapRows, MapColumns];
string rawMapData = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25";
string[] splitData = rawMapData.Split(',');
int index = 0;
// Loop through data
for (int row = 0; row < MapRows; row++)
{
for (int column = 0; column < MapColumns; column++)
{
// Store in map and show some debug
map[row, column] = int.Parse(splitData[index++]);
Console.WriteLine(string.Format("{0},{1} = {2}", row, column, map[row, column]));
}
}
// Wait for user to read
Console.ReadKey();
}
}
}
Assuming your file is 900 elements first you need to read it in..
something along the lines of
line = myStreamReader.readLine().Split(',').. then in John U's example, value would be the next index in this array called line
I'll let you work out whats missing from my example :P
well, first you need to get the numbers...
var numbers = Read_File_As_String().Split(new char[',']).Select(n => int.Parse(n)).ToList();
then, you need to build your array
const int ROWS = 30;
const int COLS = 30;
var result = new int[ROWS, COLS];
for (int row = 0; row < ROWS; row++)
for (int col = 0; col < COLS; col++)
result[row, col] = numbers[(row * COLS) + col];
for(row=0;row<30;row++)
{
for(col=0;col<30;col++)
{
array[row][col] = value;
}
}
Value would need to be moved along to point to the next thing each time, but I'm sure you can figure that out.
Edited to add: If it's a map it might be easier to store it as an array in the first place.
Since you asked about the concept of multi-dimensional arrays, here are some useful ways of thinking about arrays. Please note these are analogies, meant to help you visualize them.
Think of a 1D array as a list of items (not in the programming sense of list!).
Think of a 2D array as a table (again, not in the programming sense!). In a table (like a spreadsheet) you have rows and columns, and each dimension in your array accesses one of these.
For higher dimensional arrays, it may help to think geometrically. For instance, you can think of 3D arrays as 3-dimensional points in space, and 4D arrays as 4-dimensional points in space-time.
So if you have a single CSV file, start off by conceptualizing how this would be re-structured as a table. Once you have that, you have a pretty straight-forward mapping to the array.
Basically I have x amount of matrices I need to establish of y by y size. I was hoping to name the matrices: matrixnumber1 matrixnumber2..matrixnumbern
I cannot use an array as its matrices I have to form.
Is it possible to use a string to name a string (or a matrix in this case)?
Thank you in advance for any help on this!
for (int i = 1; i <= numberofmatricesrequired; i++)
{
string number = Convert.ToString(i);
Matrix (matrixnumber+number) = new Matrix(matrixsize, matrixsize);
}
You can achieve a similar effect by creating an array of Matrices and storing each Matrix in there.
For example:
Matrix[] matrices = new Matrix[numberofmatricesrequired];
for (int i = 0; i < numberofmatricesrequired; i++)
{
matrices[i] = new Matrix(matrixsize, matrixsize);
}
This will store a bunch of uniques matrices in the array.
If you really want to you could create a Dictionary<String, Matrix> to hold the matrices you create. The string is the name - created however you like.
You can then retrieve the matrix by using dict["matrix1"] (or whatever you've called it).
However an array if you have a predetermined number would be far simpler and you can refer to which ever you want via it's index:
Matrix theOne = matrix[index];
If you have a variable number a List<Matrix> would be simpler and if you always added to the end you could still refer to individual ones by its index.
I'm curious why you cannot use an array or a List, as it seems like either of those are exactly what you need.
Matrix[] matrices = new Matrix[numberofmatricesrequired];
for (int i = 0; i < matrices.Length; i++)
{
matrices[i] = new Matrix(matrixsize, matrixsize);
}
If you really want to use a string, then you could use a Dictionary<string, Matrix>, but given your naming scheme it seems like an index-based mechanism (ie. array or List) is better suited. Nonetheless, in the spirit of being comprehensive...
Dictionary<string, Matrix> matrices = new Dictionary<string, Matrix>();
for (int i = 1; i <= numberofmatricesrequired; i++)
{
matrices.Add("Matrix" + i.ToString(), new Matrix(matrixsize, matrixsize));
}