- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一些 XNA 塔防代码。我将其设置为让敌人( bug )从网格的某一侧沿随机路径向下移动,直到它击中房屋(目的地)或房屋一侧的行。
我调试了这个项目,错误被绘制出来并开始沿对角线(某种)路径移动。每次都一样(不是随机的)。我不知道我做错了什么。有时它还会在我指定的代码中出现 OutOfMemory
异常。我不知道我是否解释得很好,所以请随时提问。网格被不属于网格一部分的森林包围,这就是森林的意思。
public class Bug : Sprite
{
public float startHealth;
protected float currentHealth;
protected bool alive = true;
protected float speed = 0.5f;
protected int bountyGiven;
public int startplace;
public bool at_house;
public Queue<Vector2> path = new Queue<Vector2>();
Random random = new Random();
int x;
int y;
public int end_row;
public int end_column;
//Defines the space where the house is
Vector2 house1 = new Vector2(10, 13) * 91;
Vector2 house2 = new Vector2(10, 12) * 91;
Vector2 house3 = new Vector2(10, 11) * 91;
Vector2 house4 = new Vector2(10, 10) * 91;
Vector2 house5 = new Vector2(11, 13) * 91;
Vector2 house6 = new Vector2(11, 12) * 91;
Vector2 house7 = new Vector2(11, 11) * 91;
Vector2 house8 = new Vector2(11, 10) * 91;
Vector2 house9 = new Vector2(12, 13) * 91;
Vector2 house10 = new Vector2(12, 12) * 91;
Vector2 house11 = new Vector2(12, 11) * 91;
Vector2 house12 = new Vector2(12, 10) * 91;
public float CurrentHealth
{
get { return currentHealth; }
set { currentHealth = value; }
}
public bool IsDead
{
get { return currentHealth <= 0; }
}
public int BountyGiven
{
get { return bountyGiven; }
}
public float DistanceToDestination
{
get { return Vector2.Distance(position, path.Peek()); }
}
public Bug(Texture2D texture, Vector2 position, float health,
int bountyGiven, float speed)
: base(texture, position)
{
this.startHealth = health;
this.currentHealth = startHealth;
this.bountyGiven = bountyGiven;
this.speed = speed;
int startq = random.Next(1, 4);
set_start(ref startq);
//end_row and end_column detremine the row or column at which the bug turns toward the house.
end_row = random.Next(10, 13);
end_column = random.Next(10, 12);
set_path(ref startq);
}
public void set_start(ref int startq)
{
//here i am initializing the "0,0" point for the bug so it's 0,0 is't in the forest
//startx and starty should equal the number of tiles between the forest edge and the grass edge
//startx is the x co-ord in the start place and starty is the y co-ord in the start
int startx = 4;
int starty = 4;
//This generates a random number which determines the start for the bug
//Between 0 and 22 because that is the number of edge tiles on one side
int start = random.Next(0, 22);
//start determines what place on a side the buggie spawns at
//startq is a random int (1-4)(defined in constructor) which determnes which side the bug spawns at
if (startq == 1)
{
starty += 22;
startx += start;
}
if (startq == 2)
{
startx += 22;
starty += start;
}
if (startq == 3)
{
startx += start;
}
if (startq == 4)
{
starty += start;
}
x = startx;
y = starty;
path.Enqueue(new Vector2(startx, starty) * 91);
}
public bool check_for_path_end(ref int startq, ref bool at_house)
{
bool path_ends;
//checks if the bug has reached the house and if so signals using at_house
if (path.Peek() == house1 || path.Peek() == house2 || path.Peek() == house3 || path.Peek() == house4
|| path.Peek() == house5 || path.Peek() == house6 || path.Peek() == house7 || path.Peek() == house8
|| path.Peek() == house9 || path.Peek() == house10 || path.Peek() == house11 || path.Peek() == house12)
{
at_house = true;
return true;
}
//Should i add at_house = true to the else ifs?
else if (startq == 1 || startq == 3 && path.Peek().Y == end_row)
{
path.Enqueue(new Vector2(11, end_row) * 91);
return true;
}
else if (startq == 2 || startq == 4 && path.Peek().X == end_column)
{
path.Enqueue(new Vector2(end_column, 11) * 91);
path_ends = true;
}
else
{
path_ends = false;
}
return path_ends;
}
public void set_path(ref int startq)
{
bool path_ends;
bool legal = true;
int X = x;
int Y = y;
do
{
//determines which way the bug turns at it's different waypoints 1 = left, 2 = right, 3 = forward
int turn = random.Next(1, 3);
do
{
if (startq == 1)
{
switch (turn)
{
case 1:
x += 1;
break;
case 2:
x -= 1;
break;
case 3:
y += 1;
break;
}
}
else if (startq == 2)
switch (turn)
{
case 1:
y -= 1;
break;
case 2:
y += 1;
break;
case 3:
x -= 1;
break;
}
else if (startq == 3)
{
switch (turn)
{
case 1:
x -= 1;
break;
case 2:
x += 1;
break;
case 3:
y += 1;
break;
}
}
else if (startq == 4)
{
switch (turn)
{
case 1:
y += 1;
break;
case 2:
y -= 1;
break;
case 3:
x += 1;
break;
}
}
if (y > 3 && y < 28 && x > 3 && x < 28)
{
//sets up a backup in case the bug goes off track
X = x;
Y = y;
这里是异常的地方:
//Right here is where it gives the out of memory exception
path.Enqueue(new Vector2(x, y) * 91);
legal = true;
}
else
{
//restores x and y to backups X and Y
x = X;
y = Y;
//adds to turn and repeats without randomizing turn or adding waypoints
turn += 1;
legal = false;
}
} while (legal == false);
path_ends = check_for_path_end(ref startq, ref at_house);
} while (path_ends == false);
}
public bool check_corners()
{
bool start_is_corner;
if (x == 2 && y == 24 || x == 24 && y == 24 || x == 24 && y == 2 || x == 2 && y == 2)
{
start_is_corner = true;
int X = x;
int Y = y;
if (x == 4 && y == 27)
{
bool z = true;
for (int i = 0; i < 13; ++i)
{
if (z == true)
{
Y -= 1;
path.Enqueue(new Vector2(X, Y) * 91);
z = false;
}
if (z == false)
{
X += 1;
path.Enqueue(new Vector2(X, Y) * 91);
z = true;
}
}
}
if (x == 27 && y == 27)
{
bool z = true;
for (int i = 0; i < 13; ++i)
{
if (z == true)
{
Y -= 1;
path.Enqueue(new Vector2(X, Y) * 91);
z = false;
}
if (z == false)
{
X -= 1;
path.Enqueue(new Vector2(X, Y) * 91);
z = true;
}
}
}
if (x == 27 && y == 4)
{
bool z = true;
for (int i = 0; i < 13; ++i)
{
if (z == true)
{
Y += 1;
path.Enqueue(new Vector2(X, Y) * 91);
z = false;
}
if (z == false)
{
X -= 1;
path.Enqueue(new Vector2(X, Y) * 91);
z = true;
}
}
}
if (x == 4 && y == 4)
{
bool z = true;
for (int i = 0; i < 13; ++i)
{
if (z == true)
{
Y += 1;
path.Enqueue(new Vector2(X, Y) * 91);
z = false;
}
if (z == false)
{
X += 1;
path.Enqueue(new Vector2(X, Y) * 91);
z = true;
}
}
}
}
else
{
start_is_corner = false;
}
return start_is_corner;
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
if (path.Count > 0)
{
if (DistanceToDestination < speed)
{
position = path.Dequeue();
}
else
{
Vector2 direction = path.Peek() - position;
direction.Normalize();
velocity = Vector2.Multiply(direction, speed);
position += velocity;
}
}
else
alive = false;
if (currentHealth <= 0)
alive = false;
}
public override void Draw(SpriteBatch spriteBatch)
{
if (alive)
{
float healthPercentage = (float)currentHealth / (float)startHealth;
base.Draw(spriteBatch);
}
}
}
}
最佳答案
此方法是您的构造函数。这就是问题的来源:
public Bug(Texture2D texture, Vector2 position, float health, int bountyGiven, float speed)
: base(texture, position)
{
this.startHealth = health;
this.currentHealth = startHealth;
this.bountyGiven = bountyGiven;
this.speed = speed;
int startq = random.Next(1, 4);
set_start(ref startq);
//end_row and end_column detremine the row or column at which the bug turns toward the house.
end_row = random.Next(10, 13);
end_column = random.Next(10, 12);
set_path(ref startq); // <<<<<<<<<<< HERE!
}
您在这里所做的是预先确定错误的路径在初始化。您将整个路径存储在一些 Queue
中。最终队列变得如此之大以至于内存不足。
解决方法很简单。与其初始化一个 Bug
知道他在出生时 回家的每一步,不如创建一个从某处开始的 Bug
并且,在每次更新时,确定回家的下一步是什么。不要排队。
您的错误是您在构造函数中执行了属于您的 Update
方法的操作。 摆脱您的set_path
方法。一个 bug 还不够聪明,从一开始就知道回家的路。这就是拖延你比赛的原因。使用 Update
方法计算下一步移动,而不是从队列中提取它:您也不需要该队列。
这就像您在下棋,甚至在您知道对手的第一步将是什么之前,您就已经在脑海中计算了整个游戏。比赛开始前你的 Nose 就会流血。
关于c# - 为什么我的随机路径代码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19341318/
我让随机数低于之前的随机数。 if Airplane==1: while icounter0: print "You have enoph fuel to get to New
是否可以生成 BigFloat 的随机数?类型均匀分布在区间 [0,1)? 我的意思是,因为 rand(BigFloat)不可用,看来我们必须使用 BigFloat(rand())为了那个结局。然而,
我正在尝试学习 Kotlin,所以我正在学习互联网上的教程,其中讲师编写了一个与他们配合良好的代码,但它给我带来了错误。 这是错误 Error:(26, 17) Kotlin: Cannot crea
是否有任何方法可以模拟 Collections.shuffle 的行为,而不使比较器容易受到排序算法实现的影响,从而保证结果的安全? 我的意思是不违反类似的契约(Contract)等.. 最佳答案 在
我正在创建一个游戏,目前必须处理一些math.random问题。 我的Lua能力不是那么强,你觉得怎么样 您能制定一个使用 math.random 和给定百分比的算法吗? 我的意思是这样的函数: fu
我想以某种方式让按钮在按下按钮时随机改变位置。我有一个想法如何解决这个问题,其中一个我在下面突出显示,但我已经认为这不是我需要的。 import javafx.application.Applicat
对于我的 Java 类(class),我应该制作一个随机猜数字游戏。我一直陷入过去几天创建的循环中。程序的输出总是无限循环,我不明白为什么。非常感谢任何帮助。 /* This program wi
我已经查看了涉及该主题的一些其他问题,但我没有在任何地方看到这个特定问题。我有一个点击 Web 元素的测试。我尝试通过 ID 和 XPath 引用它,并使用 wait.until() 等待它变得可见。
我在具有自定义类的字典和列表中遇到了该异常。示例: List dsa = (List)Session["Display"]; 当我使用 Session 时,转换工作了 10-20 次..然后它开始抛
需要帮助以了解如何执行以下操作: 每隔 2 秒,这两个数字将生成包含从 1 到 3 的整数值的随机数。 按下“匹配”按钮后,如果两个数字相同,则绿色标签上的数字增加 1。 按下“匹配”按钮后,如果两个
void getS(char *fileName){ FILE *src; if((src = fopen(fileName, "r")) == NULL){ prin
如果我有 2 个具有以下字段的 MySQL 数据库... RequestDB: - Username - Category DisplayDB: - Username - Category
我有以下语句 select random() * 999 + 111 from generate_series(1,10) 结果是: 690,046183290426 983,732229881454
我有一个使用 3x4 CSS 网格构建的简单网站。但出于某种原因,当我在 chrome“检查”中检查页面时,有一个奇怪的空白 显然不在我的代码中的标签。 它会导致网站上出现额外的一行,从而导致出现
我有两个动画,一个是“过渡”,它在悬停时缩小图像,另一个是 animation2,其中图像的不透明度以周期性间隔重复变化。 我有 animation2 在图像上进行,当我将鼠标悬停在它上面时,anim
如图所示post在 C++ 中有几种生成随机 float 的方法。但是我不完全理解答案的第三个选项: float r3 = LO + static_cast (rand()) /( static_c
我正在尝试将类添加到具有相同类的三个 div,但我不希望任何被添加的类重复。 我有一个脚本可以将一个类添加到同时显示的 1、2 或 3 个 div。期望的效果是将图像显示为背景图像,并且在我的样式表中
我有一个基本上可以工作的程序,它创建由用户设置的大小的嵌套列表,并根据用户输入重复。 但是,我希望各个集合仅包含唯一值,目前这是我的输出。 > python3 testv.py Size of you
我正在尝试基于 C# 中的种子生成一个数字。唯一的问题是种子太大而不能成为 int32。有什么方法可以像种子一样使用 long 吗? 是的,种子必须很长。 最佳答案 这是我移植的 Java.Util.
我写这个函数是为了得到一个介于 0 .. 1 之间的伪随机 float : float randomFloat() { float r = (float)rand()/(float)RAN
我是一名优秀的程序员,十分优秀!