- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
好的,在我的 2d 游戏中,我有几个在初始化时具有空 self.target = [] 属性的“单元格”对象实例( Sprite 对象)。
然后它们会在附近找到一个有效的“植物”目标对象,这将成为细胞的“ self 目标”。
然后“细胞”将导航到植物,最终与植物发生碰撞并吃掉它,然后植物被设置为“.kill()”,另一个在随机的新坐标处重生。有几个植物实例(spr_plant_group 的一部分)和几个单元格(属于 spr_cell_group)。
每株植物“存活”X 个游戏刻(我认为是 200 个),如果到那时还没有被吃掉,就会被 .kill()-ed 并在随机坐标处生成一个新植物。
问题是这样的:如果一个细胞的目标植物碰巧因年龄而消失,或者被什么东西吃掉,细胞的 self.target 信息仍然指向旧的“植物”对象的数据。这意味着细胞正在追逐一个幻影对象,显然仍然具有有效的 x 和 y 坐标。
问题:如何告诉单元格它的目标已死、消失、无效?如果我能做到这一点,我认为它会修复“幻影”目标对象。
相关代码片段:
class Agent(sprite.Sprite):
def __init__(self, sense_range, size, food, maxspeed):
sprite.Sprite.__init__(self)
self.x = randint(0,450)
self.y = randint(0,450)
self.ang = randint(0,359)
self.turn_rate = 2
self.dx = 0
self.dy = 0
self.speed = 1
self.maxspeed = maxspeed
self.food = int(food)
self.max_food = food*1.5
self.target = []
self.sense_range = sense_range
# Snip------This part below is supposed to find a new target, but it only
# works for the first one when the cell spawns, then I can't seem to
# get it to become 'empty' so that the "if self.target == []" can do its
# thing....
def seek_food(self):
if (self.target == []):
#find cell a target within "sense_range" distance (say 200 pixels)
dist = self.sense_range
targ = []
for t in spr_plant_group:
t_dist = abs(self.x - t.x)
if t_dist <= dist:
targ = t
dist = t_dist
self.target = targ
print ("Found target...",dist, self.target)
else:
#already have a target, so move cell toward target
dx = self.target.x - self.x
dy = self.target.y - self.y
rads = atan2(dy,dx)
rads %= 2*pi
degs = degrees(rads)
direction = degs - self.ang
if direction > 0:
self.ang = self.ang + self.turn_rate
elif direction < 0:
self.ang = self.ang - self.turn_rate
# Correct for angle being out of 0-360* range
if self.ang > 360:
self.ang -= 360
elif self.ang < 0:
self.ang += 360
#---This is just a piece of the Plant class for your reference
class Plant (sprite.Sprite):
def __init__(self):
sprite.Sprite.__init__(self)
self.x = randint(0,450)
self.y = randint(0,450)
self.age = 1 + randint(0,50)
供引用,如下。植物的年龄会增加到 200 ticks,然后它们会被杀死,然后新的植物会重生...
def update_plants():
for shrub in spr_plant_group:
shrub.age += 1
# Respawn a new plant in a different place
if shrub.age >= 200:
shrub.kill()
plant.append (Plant())
最佳答案
编辑:实际上,它要简单得多。 Pygame's Sprite class supports an alive method ,所以只需这样做:
def seek_food(self):
if (self.target == [] or not self.target.alive()):
# Find a new target that isn't dead...
else:
# Move to the (alive) target...
您可以使用 the Observer design pattern 的一些变体.使用额外的属性扩展您的 Plant
类,该属性跟踪任何以植物为目标的 Cell
实例。当一个细胞以植物为目标时,它会将自己添加到植物的列表中。在植物死亡之前,它会通知它的所有细胞。
Plant 类中的新属性:
class Plant (sprite.Sprite):
def __init__(self):
# ...
self.followers = []
# ...
细胞订阅植物的追随者列表:
def seek_food(self):
if (self.target == []):
# ...
self.target = targ
self.target.followers.append(self)
# ...
else:
# ...
Plant 类重写其父类的 kill 函数,以便它在死亡前通知其追随者:
(这里,细胞类直接修改每个植物的目标,但如果你愿意,你可以封装不同的行为)
def kill(self):
for follower in self.followers:
follower.target = []
self.followers = []
super().kill()
关于Python如何检查目标对象是否不再是同一个实例或 "valid",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46231449/
当我尝试输入时,我正在关注 Ray Wenderlich ( https://videos.raywenderlich.com/screencasts/545-server-side-swift-wi
我正在使用 javax.validation.Validation 来验证 jpa 实体。我总是针对相同的实体进行验证。 我想知道是对所有验证使用一个 validator 更好,还是每次验证时实例化一
Controller : @RequestMapping(...) public void foo(@Valid Parent p){ } class Parent { @NotNull // j
在 login.phtml 中,validator 和 validate 之间有什么区别 - 有人可以解释下面每一行的含义 function onepageLogin(button)
我有一个 java bean 用于将 JSON 消息发送到 spring @RestController 并且我有 bean 验证设置并使用 @Valid 运行得很好。但我想转移到 Protobuf/
我正在使用 vee-validate 来验证注册表单,我编写了如下代码,
使用 有什么区别属性和 标签? 我应该什么时候使用哪一种? 最佳答案 validator属性允许您引用独立的托管 bean 方法,而不仅仅是像这样的验证器
我们有这个 Alamofire 错误处理: Alamofire.request(.GET, getUrl("mystuff")).responseData { response in guar
以下代码创建了两个单选按钮。每个选项都包含一个日期值,该日期值已成功转换为格式为“yyyy-MM-dd”的标签。一旦我做出选择并单击下一步按钮,我就会收到以下错误“j_idt12:comDateCho
是否有类似的验证: req.checkBody('property', 'should be present').isPresent(); 值本身并不重要。 现在我使用这个解决方案: .isLengt
系列文章: 1、async-validator 源码学习(一):文档翻译 2、async-validator 源码学习笔记(二):目录结构 3、async-validator 源码学习笔记(三):ru
我正在使用 Backbone.Validation插入。我有一个模型可以在服务器端进行唯一性检查。我如何让 backbone.validation 识别错误并运行我设置的回调以显示返回的错误消息? t
我有一个 Knockout.Validation 场景,我认为该场景相当普遍,但尚未通过在网络上搜索和本网站上的各种答案找到解决方案。 我正在验证的属性在 ajax 调用之后才会添加到可观察到的 kn
我是 Knockout JS 的新手。我需要一个验证器来验证用户将在文本框中输入的日期。为此编写了如下代码 ko.validation.rules['date'] = { validator:
如何在 JSF 验证器中比较两个字符串是否相等? if (!settingsBean.getNewPassword().equals(settingsBean.getConfirmPassword()
我有简单的域: package app class Customers { String CUSTOMER String NOTE static mapping = {
我正在使用 ember-validations 来验证表单中的模型。 如果我使用 createRecord 创建记录,则模型的实例已经过验证,因此在用户输入值之前表单已经显示验证错误。 我只想在提交表
我有两种不同的服务:第一个是将对象保存到数据库,第二个是更新现有对象。 我正在对我的对象使用验证约束,例如(@NotBlank、@Size、@Pattern 等),在第一种情况下,我需要验证对象的所有
我有 2 个 ensure - 验证装饰器用于 2 个字段:password 和 retypePassword。我想在填写 retypePassword 字段时检查这两个字段是否相等。 问题是,当我在
I'm aware that it's a bug ,但是在域类上调用 validate() 会覆盖之前放入的任何拒绝: def save = { def assignment = new A
我是一名优秀的程序员,十分优秀!