- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想创建一个贪吃蛇游戏。我的第一个目标是创建一个星号,它可以在不按任何键的情况下从上到下移动,模拟蛇的“移动”。我应该在我的代码中添加什么,以便 *
在控制台中移动而不打印循环运行的次数。
到目前为止我的代码是:
import time
i=0
while (i<5):
print "*"
time.sleep(0.5)
i+=1
最佳答案
我绝对建议使用 curses
模块对终端进行细粒度控制。您可以找到许多使用此模块编程的 Snake 示例,例如 this one .
为了在没有诅咒的情况下处理它,我在下面编写了一些示例代码。请注意,您不能按原样使用 print
,因为它会在每次调用后输出一个换行符 \n
。 (在 Python 3 中,您可以编写 print("hello",end="",flush=True)
来解决这个问题)。所以,从字面上看,我正在使用 sys.stdout.write()
。
请记住使用 Ctrl+C
结束正在运行的终端程序。
import time
from sys import stdout as s
# Let's break this function down:
# After writing a line to stdout, any additional
# writing will happen at the end of this line.
# Use \r (the carriage return) to reset to the beginning of
# the line. Add 40 spaces to clear any characters on that line.
# Then, go back to the beginning of the line again.
def clear():
s.write("\r" + " "*40 + "\r")
x = 0
x_max = 20
while True:
clear()
time.sleep(0.2)
# Move the snake (*****) by increasing spaces
s.write(" "*x+"*****")
# Calling write puts characters into a 'buffer'
# Calling flush() will write that buffer to the screen
s.flush()
x += 1 # Increase the x position
# If the snake moves too far right,
# reset him
if (x > x_max):
x = 0
关于python - 如何在 python 2 程序创建蛇游戏的前一点清除屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47142536/
我是编程新手,并且卡在涉及蛇方向改变的特定部分上。该游戏与旧诺基亚手机上的游戏相同。很经典。 目前,每次我按 W、A、S 或 D 键,蛇都会移动 1 平方/20 像素。问题是我希望这个 Action
如果您熟悉任何梦幻体育选秀,选秀顺序网格如下所示: EXAMPLE 1 (3-teams): Round Team 1 Team 2 Team 3 1 1 (1.1) 2 (
我从这里得到了蛇算法的代码(在MatLab中实现) http://www.mathworks.com/matlabcentral/fileexchange/28109-snakes-active-co
我遵循了这个 link 中的示例.然而,轮廓从初始点开始收缩。是否可以做展开的轮廓?我想要像显示的图像那样的东西。左边的图像是它的样子,右边的图像是我想要的样子——向外扩展而不是收缩。红色圆圈为起点,
我正在编写一款类似于贪吃蛇的游戏。目前,我正在努力编写 move() 和 Growth() 方法。这个游戏的运作方式是,蠕虫从 1 block 开始,每 move 一步就增加 1 block ,直到达
说明 我目前正在使用 Pygame 开发蛇游戏,但我遇到了一个问题,因为我的蛇目前仅由方 block 组成,但如果蛇包含蛇头、 body 、尾部的绘制 25x25 图片,我会发现它会更好对于弯曲的 b
我是一名优秀的程序员,十分优秀!