dimanche 22 février 2015

KeyCode and Random.Next not working

I'm making a game that has got an array, which represents an island, and this array should be randomly generated each time you start the game (I'll make the procedurally generated part when it will be finished). Also, if you click the arrows or wasd, it should make you see parts of the island that you couldn't. But I have two problems: 1- when I click the key, nothing happens; 2- Random is not giving random values for every value in the arrays, but it gives the same value for some time, then it changes. Here's the code for the random:



public void CreateWorld(int dim)
{
for (int i = 0; i < dim; i++)
{
for (int j = 0; j < dim; j++)
{
if (i == 0 || i == dim - 1 || j == 0 || j == dim - 1)
{
world[i, j] = 0;
}
else
{
Random r = new Random();
world[i, j] = r.Next(0, 3);
}
}
}
}


And here's the code for the movement (it's on three different classes):



//class Form
private void GameWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up || e.KeyCode == Keys.W)
{
y = -1;
}
else if (e.KeyCode == Keys.Down || e.KeyCode == Keys.S)
{
y = 1;
}
else if (e.KeyCode == Keys.Right || e.KeyCode == Keys.D)
{
x = 1;
}
else if (e.KeyCode == Keys.Left || e.KeyCode == Keys.A)
{
x = -1;
}
game.Move(x, y);
}

// class Game
public void Move(int x, int y)
{
if (x == -1 && gEngine.smallestXTileOnScreen == 0) { }
else if (x == 1 && gEngine.smallestXTileOnScreen == size - 25) { }
else if (y == -1 && gEngine.smallestYTileOnScreen == 0) { }
else if (y == 1 && gEngine.smallestYTileOnScreen == size - 15) { }
else
{
gEngine.smallestXTileOnScreen += x;
gEngine.smallestYTileOnScreen += y;
gEngine.render();
}
}
//class GraphEngine
public void render()
{
drawHandle.Clear(Color.Blue);
for (int i = 0; i < 25; i++)
{
for (int j = 0; j < 15; j++)
{
prop = world[i + smallestXTileOnScreen , j + smallestYTileOnScreen];
switch (prop)
{
case 0:
drawHandle.DrawImage(sea, i * 50, j * 50, 50, 50);
break;
case 1:
drawHandle.DrawImage(plain, i * 50, j * 50, 50, 50);
break;
case 2:
drawHandle.DrawImage(mountain, i * 50, j * 50, 50, 50);
break;
case 3:
drawHandle.DrawImage(valley, i * 50, j * 50, 50, 50);
break;
default:
drawHandle.DrawImage(sea, i * 50, j * 50, 50, 50);
break;
}
}
}
Form Debug = new Form(); //Notice that I put here a form, if it's shown,
Debug.Show();//then render() has been called. It doesn't show.
}




Aucun commentaire:

Enregistrer un commentaire