Dobrý den, dělám hru v xna a mam tam příšery, kterým pomocí random gneruji náhoně jakým směrem se pohybují. Zde je třída příšer:
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;
using System.IO;
namespace Hungry_Man
{
class Monster
{
public bool alive = true;
public Vector2 position;
public Rectangle rec;
public float velocity = 1.5f;
private Vector2 move;
private bool isMove = true;
private Random rnd = new Random();
private ContentManager content = new ContentManager(Configuration.service, "Content");
//Update
private Vector2 nowPosition;
private Point[] mainPosition = new Point[2];
private Vector2 previousMove;
//graphic
private Texture2D live;
public Monster(Vector2 position)
{
live = content.Load<Texture2D>("Graphic\\monster");
this.position = position * Configuration.side;
this.rec = new Rectangle((int)position.X, (int)position.Y, Configuration.side, Configuration.side);
}
public void Update(GameTime time, List<Wall> wall, string[,] array)
{
int r = rnd.Next(1, 300);
if (r == 1 || (move.X == 0 && move.Y < 0))
{
move = new Vector2(0, -velocity);
nowPosition = position + move;
mainPosition[0] = new Point((int)decimal.Floor((decimal)(nowPosition.X / Configuration.side)),
(int)decimal.Floor((decimal)(nowPosition.Y / Configuration.side)));
mainPosition[1] = new Point((int)decimal.Ceiling((decimal)(nowPosition.X / Configuration.side)),
(int)decimal.Floor((decimal)(nowPosition.Y / Configuration.side)));
foreach (Point p in mainPosition)
{
string mainArrayData = array[p.X, p.Y];
string[] SplitData = new string[2];
SplitData = mainArrayData.Split(';');
if (SplitData[0] == "1")
{
move = new Vector2(0, wall[int.Parse(SplitData[1])].rec.Bottom - position.Y);
break;
}
}
previousMove = move;
position = position + move;
}
if (r == 2 || (move.X == 0 && move.Y > 0))
{
move = new Vector2(0, velocity);
nowPosition = position + move;
mainPosition[0] = new Point((int)decimal.Floor((decimal)(nowPosition.X / Configuration.side)),
(int)decimal.Ceiling((decimal)(nowPosition.Y / Configuration.side)));
mainPosition[1] = new Point((int)decimal.Ceiling((decimal)(nowPosition.X / Configuration.side)),
(int)decimal.Ceiling((decimal)(nowPosition.Y / Configuration.side)));
foreach (Point p in mainPosition)
{
string mainArrayData = array[p.X, p.Y];
string[] SplitData = new string[2];
SplitData = mainArrayData.Split(';');
if (SplitData[0] == "1")
{
move = new Vector2(0, wall[int.Parse(SplitData[1])].rec.Top - (position.Y + Configuration.side));
break;
}
}
previousMove = move;
position = position + move;
}
if (r == 3 || (move.Y == 0 && move.X < 0))
{
move = new Vector2(-velocity, 0);
nowPosition = position + move;
mainPosition[0] = new Point((int)decimal.Floor((decimal)(nowPosition.X / Configuration.side)),
(int)decimal.Ceiling((decimal)(nowPosition.Y / Configuration.side)));
mainPosition[1] = new Point((int)decimal.Floor((decimal)(nowPosition.X / Configuration.side)),
(int)decimal.Floor((decimal)(nowPosition.Y / Configuration.side)));
foreach (Point p in mainPosition)
{
string mainArrayData = array[p.X, p.Y];
string[] SplitData = new string[2];
SplitData = mainArrayData.Split(';');
if (SplitData[0] == "1")
{
move = new Vector2(wall[int.Parse(SplitData[1])].rec.Right - (position.X), 0);
break;
}
}
previousMove = move;
position = position + move;
}
if (r == 4 || (move.Y == 0 && move.X > 0))
{
move = new Vector2(velocity, 0);
nowPosition = position + move;
mainPosition[0] = new Point((int)decimal.Ceiling((decimal)(nowPosition.X / Configuration.side)),
(int)decimal.Ceiling((decimal)(nowPosition.Y / Configuration.side)));
mainPosition[1] = new Point((int)decimal.Ceiling((decimal)(nowPosition.X / Configuration.side)),
(int)decimal.Floor((decimal)(nowPosition.Y / Configuration.side)));
foreach (Point p in mainPosition)
{
string mainArrayData = array[p.X, p.Y];
string[] SplitData = new string[2];
SplitData = mainArrayData.Split(';');
if (SplitData[0] == "1")
{
move = new Vector2(wall[int.Parse(SplitData[1])].rec.Left - (position.X + Configuration.side), 0);
break;
}
}
previousMove = move;
position = position + move;
}
}
public void Draw(SpriteBatch sprite)
{
sprite.Draw(live, position + Configuration.margin, null, Color.White, 0, Configuration.origin,
Configuration.scale, SpriteEffects.None, 0);
}
}
}
Při spuštění hry se podle mapy přidají do kolekce příšery instance této třídy. Potom se volá funkce update a draw. A náhodné generování směrů funguje nějak divně, protože při spuštění hry se určité skupiny příšer pohybují úplně stejně, což by podle mě dělat nemělo. Ale nevím jestli mám chybu v kódu nebo je chyba někde jinde. Prosím kdyby jste věděli co by mohlo způsobocat tento problém a případně i jak ho vřešit, prosím odpovězte. za odpovědi předem děkuji (když dám rnd = new Random() do konstruktoru dělá to také :( )
|