如果我创建一个const值的全局数组,例如
const int SOME_LIST[SOME_LIST_SIZE] = {2, 3, 5, 7, 11};
Run Code Online (Sandbox Code Playgroud)
是否有可能以任何方式修改SOME_LIST?
我怎么写这个SOME_LIST指向一个const内存位置,并且是一个const指针本身(即不能指向其他地方)?
如何在PyGame中找到角色和图像之间的冲突?我从一个图像中抽取了一个玩家,并从瓷砖上绘制了墙壁,那么我如何才能发现这些碰撞呢?
是否可以向模块添加属性和特殊方法?我想定义一个模块,以便导入它就像一个类实例,并且正文充当类定义.从本质上讲,这是为了避免像这样丑陋的语法:
import game
if game.Game().paused:
print("The game is paused")
Run Code Online (Sandbox Code Playgroud)
例如,游戏模块看起来像这样:
_Speed = 1
@property
def paused():
return _Speed == 0
Run Code Online (Sandbox Code Playgroud)
并使用它的文件:
import game
if game.paused:
print("The game is paused")
Run Code Online (Sandbox Code Playgroud)
另外,是否可以定义特殊方法(例如__call__)?
要清楚,我不区分类/实例方法,因为我使用的game.Game是singleton/borg类.
我已经使用@property和定义进行了测试__bool__,但都没有像我希望的那样行事.
编辑(有关我为什么要使用属性的信息):
我有一个属性game.speed,一个函数game.paused()和一个函数game.pause(bool).从本质上讲,我有一个临时变量,用于在游戏暂停时存储游戏速度.有一个私人速度变量在游戏暂停时设置为零.我从不希望用户将速度视为零,并且能够在游戏暂停时修改速度,以便在游戏恢复时使用新的速度.
我正在开发一个游戏(编写我自己的物理引擎),并尝试在编写时遵循优秀的设计.最近,我遇到了许多难以发现的错误,为了更容易地捕获这些错误,我一直在尝试编写单元测试.这很难做到,因为我的很多组件都紧密耦合,尤其是game模块.
在我的game模块中,我导出一个单例类实例,它保存当前游戏状态,时间,游戏事件等.但是,在阅读了这篇文章并通过研究如何减少这种耦合后,我决定尝试重写这样的类,它不再是单身人士了.
我们的想法是使用一个GameState类,并在任何地方传递这些对象,以便单元测试可以为测试创建最小的状态.大多数功能只是成为游戏状态的一个功能,并返回一个新的游戏状态.但是,我遇到了一些设计问题:
我的实体对象的位置和速度是根据当前时间计算的python属性.这意味着我不能简单地传入一个GameState对象而不将其重写为函数(导致icky语法).代码示例:
class Entity:
@property
def position(self):
"""Interpolate the position from the last known position from that time."""
# Here, the game class instance is referenced directly, creating coupling.
dt = game.game_time - self._last_valid_time
# x_f = x_i + v_i*t + 1/2 at^2
return self._position + self._velocity * dt + .5 * self._acceleration * dt**2
Run Code Online (Sandbox Code Playgroud)
我已经做了一些关于如何解决这个问题的研究,并且遇到了依赖注入问题.基本上,我可以将GameState的'getter'对象传递给每个实体的初始化器.所有GameState'getter'对象都会返回当前状态.示例GameStateGetter类:
class GameStateGetter:
_CurrentState = None
def …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个脚本来解析由编译器/链接器生成的映射文件,如下所示:
%SEGMENT_SECTION
Start Address End Address
--------------------------------------------------------------------------------
Segment Name: S1_1, Segment Type: .bss 0A000000 0A050F23
--------------------------------------------------------------------------------
area1_start.o (.bss) 0A000000 0A000003
...
Start Address End Address
--------------------------------------------------------------------------------
Segment Name: S2_1, Segment Type: .bss 0A050F24 0A060000
--------------------------------------------------------------------------------
area2_start.o (.bss) 0A000000 0A000003
...
%NEXT_SECTION
Run Code Online (Sandbox Code Playgroud)
我目前正在编写几个正则表达式(python 的 re 模块)来解析它,但我想以一种非常易于阅读的方式编写它们,这样解析起来就非常简单。本质上:
with open('blah.map') as f:
text = f.read()
# ... Parse the file to update text to be after the %SEGMENT_SECTION
match = segment_header_re.match(text)
seg_name, seg_type, start_addr, end_addr = match.groups()
# ... (Do more with matched …Run Code Online (Sandbox Code Playgroud) 我正在尝试启动一个C++ Qt应用程序并让它在Visual Studio 2010中运行和编译.
做一些谷歌搜索,我发现有一个Visual Studio Qt加载项,所以我安装了这个.我已经安装了MinGW Qt二进制文件,当这不起作用时,我发现你必须编译Visual Studio 2010的源代码(VS 2008二进制文件将导致部署问题).
以此为指导:如何为Visual Studio 2010构建Qt,我编译了开源版本,并将其与QTDIR env变量一起添加到PATH中.希望我最终能够使用它,我使用Visual Studio 2010中的"新建项目向导"创建了"Qt应用程序".完成后,我尝试构建程序,但只看到以下错误:
1>LINK : fatal error LNK1104: cannot open file 'qtmaind.lib'
Run Code Online (Sandbox Code Playgroud)
我查看了C:\ QT\lib文件夹,发现我只有一个qtmaind.prl,但没有任何关于为什么没有qtmaind.lib的线索.
我能够回显QMAKESPEC环境变量以获得'win32-msvc2010'输出.
我已经为配置步骤尝试了几种不同的标志组合,包括链接中的标志,甚至尝试手动设置-platform标志.
如果有人可以提供任何帮助,我们将不胜感激!:d
我有一个CPU密集型功能:
def entity_intersections(ent, collidable):
intersections = []
for line1, line2 in product(ent.shape, collidable.shape):
pair_intersections = find_intersections(line1 + ent.position, ent.velocity, ent.acceleration, line2 + collidable.position, collidable.velocity, collidable.acceleration, ent, collidable)
intersections.extend(pair_intersections)
return intersections
Run Code Online (Sandbox Code Playgroud)
我希望让所有调用find_intersections并行运行,以便它们执行得更快,同时仍然将所有结果收集在一起(一旦所有执行完成).什么库允许我这样做,因为这find_intersections是一个纯函数?
将非常感谢如何生成这些并行执行以及将结果收集在一起的示例.
我在接受采访时遇到了这个问题.你有一个包含1和0的nxn矩阵.您必须以最有效的方式找到包含最大数量1的行.我知道它可以在n ^ 2中完成但是有没有最佳解决方案呢?
我正在定义一个Debug类,如下所示:
_debug = False
class Debug:
DrawOutlines = True
InvinciblePlayer = True
Run Code Online (Sandbox Code Playgroud)
我想覆盖Debug该类,以便如果_debug为False,则Debug的任何类属性(存在)都将为False.为了更改类属性的访问方式,我要覆盖哪些__函数__?
编辑:
我知道简单的覆盖__getattribute__不适用于类属性:
>>> _debug = False
False
>>> class Debug:
... DrawOutlines = True
...
... def __getattribute__(self, name):
... return _debug and object.__getattribute__(self, name)
...
>>> Debug.DrawOutlines
True
>>> Debug.cake
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'Debug' has no attribute 'cake'
Run Code Online (Sandbox Code Playgroud)
这是否需要一个元类?
我正在尝试使用 python 的unittest库来编写一些单元测试。我有一个返回对象的无序列表的函数。我想验证对象是否相同,并且我尝试使用assertCountEqual来执行此操作。
==然而,尽管各个对象彼此相等 ( ),但这似乎失败了。这是断言失败的“diff”输出:
First has 1, Second has 0: Intersection(time=8.033252939677466e-08, del_time=8.033252939677466e-08, pos=Vector(10.00, 0.00), line=Line(Vector(500.00, 0.00), Vector(0.00, 0.00)), ent=None, oth=None, invalid=False)
First has 1, Second has 0: Intersection(time=-9.918729244820295e-16, del_time=-9.918729244820295e-16, pos=Vector(10.00, 0.00), line=Line(Vector(500.00, 0.00), Vector(0.00, 0.00)), ent=None, oth=None, invalid=False)
First has 0, Second has 1: Intersection(time=8.033252939677466e-08, del_time=8.033252939677466e-08, pos=Vector(10.00, 0.00), line=Line(Vector(500.00, 0.00), Vector(0.00, 0.00)), ent=None, oth=None, invalid=False)
First has 0, Second has 1: Intersection(time=-9.918729244820295e-16, del_time=-9.918729244820295e-16, pos=Vector(10.00, 0.00), line=Line(Vector(500.00, 0.00), Vector(0.00, 0.00)), ent=None, oth=None, invalid=False)
Run Code Online (Sandbox Code Playgroud)
验证它们是否相等: …
python ×7
python-3.x ×4
pygame ×2
unit-testing ×2
algorithm ×1
arrays ×1
c++ ×1
collision ×1
const ×1
module ×1
parsing ×1
properties ×1
qt ×1
regex ×1
singleton ×1