在工作中有一个列出已完成任务的脚本.这是由其他人编写的,并通过网络托管.我的.bashrc中有一个别名,它调用了这个脚本,有很多标志等等,我想编写一个python脚本,每隔几分钟调用一次这样的别名,这样我就可以打开一个带有更新统计数据的shell.但是,subprocess.call("myAlias")失败了.我对python仍然相当新,我正在努力解决这个问题.
from subprocess import call
def callAlias():
call("myAlias")
callAlias()
Run Code Online (Sandbox Code Playgroud)
我计划增加更多,但我在第一步遇到障碍.:P
我会发布更多,但有很多敏感的机密内容我必须要小心.对不起,代码模糊,缺少错误输出.
我有一个为游戏服务器中的所有玩家和对象运行tick()的函数.我这样做是通过每隔0.1秒循环一次.我需要它是一个坚实的.1.大量的时间和数学取决于这个暂停尽可能精确到.1秒.为实现这一点,我将其添加到tick线程:
start_time = time.time()
# loops and code and stuff for tick thread in here...
time_lapsed = time.time() - start_time # get the time it took to run the above code
if 0.1 - time_lapsed > 0:
time.sleep(0.1 - time_lapsed)
else:
print "Server is overloaded!"
# server lag is greater that .1, so don't sleep, and just eat it on this run.
# the goal is to never see this.
Run Code Online (Sandbox Code Playgroud)
我的问题是,这是最好的方法吗?如果我的循环持续时间是0.01,那么time_lapsed == 0.01 ...然后睡眠应该只有0.09.我问,因为它似乎没有起作用.我前几天开始收到重载的服务器消息,服务器肯定没有超载.关于"动态"控制睡眠的好方法的任何想法?也许有一种不同的方式来运行代码每十分之一秒不睡觉?
我需要在我正在使用python构建的MUD(或者更多,如果可能的话)中从玩家的当前房间中抽取2个半径图.房间设置为容器,self.exits = {'west':1, 'north':2}其中键是值(相邻房间的UID)所在的方向.客房仅以这种方式相连.self.location为0的玩家可以键入'n',并且基于上面的变量,他们的位置将是2,并且该房间的内容将玩家的UID附加到其内容.
所以,我希望根据上面的变量显示一个如下所示的地图,其中'u'是玩家的当前位置.
[ ]
|
[ ]-[u]
Run Code Online (Sandbox Code Playgroud)
我已经实现了这一部分,因为这只是一个半径1.这里有一个小的(经过大量修改,在这里发布)我如何做到这一点的片段,你会明白为什么我发帖,因为它的代码很差.
mloc = '[u]'
mn = ' '
mw = ' '
spn= ' '
spw= ' '
for Exit in room.exits.keys():
if Exit == 'north':
mn = '[ ]'
spn = '|'
if Exit == 'west':
mw = '[ ]-'
# player.hear() is our function for printing a line to the player's screen
player.hear(' '+mn)
player.hear(' '+sp)
player.hear(mw+mloc)
Run Code Online (Sandbox Code Playgroud)
在我疯狂的情况下,我成功地完成了所有8个不同方向的工作(对角线,不包括向上或向下).但是我必须循环使用我的第一个for循环解析的房间,然后绘制它们,然后将它全部分开,然后考虑(sp)aces的重叠,例如'\'或' |" 如果有相互交叉的路径.这个小小的任务立刻变成了噩梦,在我完成之前已经进入了200行.
另一个障碍是我只能逐行打印.因此,如果地图高50个字符,我必须有player.hear()50行,我不反对.在发布答案之前,请记住这一点.
我对格式化也不挑剔.我只想简单地想要一张"地图一览"来帮助玩家环游世界. …
因此,我不是数学知识最渊博的人,希望获得一些急需的反馈。对于初学者来说,我的目标只是使图像追逐鼠标。发生了一些我没想到会收到反馈的事情。
1)追赶非常僵化,并且以非常“ ...成角度的”方式跟随鼠标,仅在屏幕上似乎设置的点(中间x轴,中间y轴和对角线)上改变方向。
2)我希望速度保持恒定,但是图像越靠近鼠标,移动速度就越快。
3)当图像到达鼠标时,它“弹弓”通过了鼠标,从而产生了模糊的狂潮,而不是到达鼠标位置并停止。
这些是我主要关心的问题,但是如果您发现我可能会误解的任何内容,请告诉我。我渴望了解整个矢量/三角学(以便很快进入物理学)。
def follow (self):
mouse_pos = pygame.mouse.get_pos()
diff = (self.xPos-mouse_pos[0], self.yPos-mouse_pos[1])
vector = math.sqrt(diff[0]**2 + diff[1]**2)
distance = (self.xPos/vector, self.yPos/vector)
if (self.xPos, self.yPos) == mouse_pos:
return
if mouse_pos[0] >= self.xPos:
self.xPos += distance[0]
else:
self.xPos -= distance[0]
if mouse_pos[1] >= self.yPos:
self.yPos += distance[1]
else:
self.yPos -= distance[1]
Run Code Online (Sandbox Code Playgroud)
(可以在此处查看整个程序http://ideone.com/6OxWLi)
在此先感谢您的帮助!
我正在创建一个命令行脚本,我希望有一个盒子...
+--------+
| |
| |
| |
+--------+
Run Code Online (Sandbox Code Playgroud)
...将永远适合其内容。我知道怎么做顶部和底部,但是它使杠杆和杠杆正确工作。每行可能有一个字符串替换项,或5,并且这些字符串的len可以是0到80之间的任何值。
我一直在做类似的事情:
print "|%s|" % (my_string.ljust(80-len(my_string)))
但是,当当真是一团糟……而这仅仅是一种硬编码替代。我不知道如何通过第一行的2个subs和第二行的3个subs和第三行的1个subs来使其动态化(所有这些都以列格式)。
因此,对于一个基本示例,我需要:
+--------+
| 1 |
| 1 2 3 |
| 1 2 |
+--------+
Run Code Online (Sandbox Code Playgroud) 所以我只是重新格式化了一堆代码以合并textwrap.wrap,但却发现我的所有内容都已消失.这是一个例子.
from textwrap import wrap
def wrapAndPrint (msg, width=25):
""" wrap msg to width, then print """
message = wrap(msg, width)
for line in message:
print line
msg = ("Ok, this is a test. Let's write to a \nnewline\nand then "
"continue on with this message")
Run Code Online (Sandbox Code Playgroud)
如果我打印msg,我会得到以下内容
>>> print msg
Ok, this is a test. Let's write to a
newline
and then continue on with this message
>>>
Run Code Online (Sandbox Code Playgroud)
但是,如果我发送它包装它看起来像这样:
>>> wrapAndPrint(msg)
Ok, this is a test. Let's
write to a …Run Code Online (Sandbox Code Playgroud) Pygame的混音器模块pygame.mixer.get_busy返回一个简单的布尔值.
我的问题是我有声音不断播放,如爆炸和枪声,并且需要知道特定声音何时播放以防止游戏对话重叠.
我考虑制作一个当前播放对话的列表,创建一个计时器,在每个声音被触发时倒计时,但这需要我在主游戏循环中添加声音效果(处理声音的模块)更新.
这似乎很混乱,就像一个巨大的减速.
有更清洁的方法吗?
my_list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven']
Run Code Online (Sandbox Code Playgroud)
使用python,我需要将列表显示为:
one, two, three
four, five, six
seven
Run Code Online (Sandbox Code Playgroud)
我需要它灵活,因为列表会经常更改.
我正在练习在pygame中制作一个Android应用程序,并决定做你的经典泡泡射击游戏.虽然玩家发射的射弹不够精确,但我遇到了一个问题.
def move (self, time_passed):
x = (math.sin(self.angle) * self.speed) * time_passed
y = (math.cos(self.angle) * self.speed) * time_passed
c = self.rect.center
self.rect.center = (c[0]+x, c[1]+y)
def update(self, screen, time_passed):
if self.launched:
if not self.mpos:
self.mpos = pygame.mouse.get_pos()
diffx, diffy = (self.mpos[0]-self.rect.center[0],
self.mpos[1]-self.rect.center[1])
self.angle = math.atan2(diffx, diffy)
self.move(time_passed)
Run Code Online (Sandbox Code Playgroud)
在screen和time_passed参数从主回路通过,是显示和时钟类的的返回值tick(60)/1000.0,为清楚起见.
如果我打印x和y的值move()非常准确:x: 1.0017224084 y: -21.9771825359或类似的东西取决于鼠标的位置.但是,似乎pygame或move_ip只适用于整数.我可以在1.00000001和1.9999999之间点击,并且球将在精确的位置射击,为1(参考x坐标).这对我的比赛来说很糟糕.有没有解决方案,所以我可以让我的物体非常精确地移动?
编辑:我意识到我粘贴了错误的代码.应该在self.rect.center = (c[0]+x, c[1]+y)哪里self.rect.move_ip(x, y).然而,两种方法都产生相同的结果.
List<MyClass> options = new List<MyClass>();
foreach (MyClass entity in ExistingList) {
if (entity.IsCoolEnough) {
options.Add(entity);
}
}
Run Code Online (Sandbox Code Playgroud)
我只是好奇最快,最有效的方法是什么.这个列表不是很大,但它经常运行,所以我想让它保持活泼.我也不是在寻找冗长的变化.我只想尽可能快地运行.
python ×9
pygame ×3
mud ×2
python-2.7 ×2
audio ×1
bash ×1
border ×1
c# ×1
command-line ×1
list ×1
newline ×1
online-game ×1
subprocess ×1
time ×1
word-wrap ×1