python 按键简单游戏

2024-01-28

我想在屏幕上看到一个符号,例如可能是(散列)“#”。符号会有一些起始位置,比如说 (0, 0)。如果我按向右箭头,我希望看到标志向右移动,如果按向左箭头,我希望看到标志向左移动,等等。 到目前为止,我的代码看起来像这样,它适用于读取 pos,但我想添加某种“动画”,这样我就可以看到标志在屏幕上移动:

!Update:只是为了给您提供线索,我创建了“图标”,现在当您按向右或向左键时,图标会向所需的方向移动。

from msvcrt import getch

icon = chr(254)
pos = [0, 0]
t = []
def fright():
    global pos
    pos[0] += 1
    print ' ' * pos[0], 
    print(icon) 

def fleft():
    global pos 
    pos[0] -= 1
    print ' ' * pos[0], 
    print(icon) 

def fup():
    global pos
    pos[1] += 1

def fdown():
    global pos
    pos[1] -= 1

def appendTab():
    global pos, t
    t.append(pos)

while True:
    print'Distance from zero: ', pos    
    key = ord(getch())

    if key == 27: #ESC
        break
    elif key == 13: #Enter
        print('selected')
        appendTab()
    elif key == 32: #Space, just a small test - skip this line
        print('jump')
        print(t)
    elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
        key = ord(getch())
        if key == 80: #Down arrow
            print('down')
            fdown()
        elif key == 72: #Up arrow
            print('up')
            fup()
        elif key == 75: #Left arrow
            print('left')
            fleft()
        elif key == 77: #Right arrow
            print('right')
            fright()

您可以创建一个列表列表作为地图,并将玩家的单元格设置为'#'。然后只需打印地图,如果玩家移动,请使用以下命令清除命令行/终端os.system('cls' if os.name == 'nt' else 'clear') https://stackoverflow.com/a/2084628/6220679并打印更新后的地图。

import os
from msvcrt import getch

pos = [0, 0]
# The map is a 2D list filled with '-'.
gamemap = [['-'] * 5 for _ in range(7)]
# Insert the player.
gamemap[pos[1]][pos[0]] = '#'

while True:
    print('Distance from zero: ', pos    )
    key = ord(getch())

    if key == 27: #ESC
        break
    elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
        key = ord(getch())
        if key in (80, 72, 75, 77):
            # Clear previous tile if player moves.
            gamemap[pos[1]][pos[0]] = '-'
        if key == 80: #Down arrow
            pos[1] += 1
        elif key == 72: #Up arrow
            pos[1] -= 1
        elif key == 75: #Left arrow
            pos[0] -= 1
        elif key == 77: #Right arrow
            pos[0] += 1

    print('clear')
    # Clear the command-line/terminal.
    os.system('cls' if os.name == 'nt' else 'clear')
    # Set the player to the new pos.
    gamemap[pos[1]][pos[0]] = '#'
    # Print the map.
    for row in gamemap:
        for tile in row:
            print(tile, end='')
        print()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python 按键简单游戏 的相关文章

  • 熊猫按 n 最大总和分组

    我正在尝试使用groupby nlargest and sum在 Pandas 中一起运行 但在运行时遇到困难 State County Population Alabama a 100 Alabama b 50 Alabama c 40
  • Vimeo API:获取下载所有视频文件的链接列表

    再会 我正在尝试从 Vimeo 帐户获取所有视频文件的列表 直接下载的链接 有没有办法在 1 GET 请求中做到这一点 好的 如果是API限制的话 就100倍 我有硬编码脚本 我在其中发出 12 个 GET 请求 1100 多个视频 根据文
  • docker 容器中的“(pygame parachute)分段错误”

    尝试在 docker 容器中使用 pygame 时出现以下错误 我想从容器中获取显示 Fatal Python error pygame parachute Segmentation Fault 重现 Docker已安装 docker ru
  • Python 内置对象的 __enter__() 和 __exit__() 在哪里定义?

    我读到每次使用 with 时都会调用该对象的 enter 和 exit 方法 我知道对于用户定义的对象 您可以自己定义这些方法 但我不明白这对于 打开 等内置对象 函数甚至测试用例是如何工作的 这段代码按预期工作 我假设它使用 exit 关
  • 使用 Tkinter 打开网页

    因此 我的应用程序需要能够打开其中的单个网页 并且它必须来自互联网并且未保存 特别是我想使用 Tkinter GUI 工具包 因为它是我最熟悉的工具包 最重要的是 我希望能够在窗口中生成事件 例如单击鼠标 但无需实际使用鼠标 有什么好的方法
  • 如何找到多个 pandas 数据框中一对列与任意顺序对的交集?

    我有多个 pandas 数据框 为了简单起见 假设我有三个 gt gt df1 col1 col2 id1 A B id2 C D id3 B A id4 E F gt gt df2 col1 col2 id1 B A id2 D C id
  • 如何知道python运行脚本的路径?

    sys arg 0 给我 python 脚本 例如 python hello py 返回 sys arg 0 的 hello py 但我需要知道 hello py 位于完整路径中的位置 我怎样才能用Python做到这一点 os path a
  • 了解 Python 2.7 中的缩进错误

    在编写 python 代码时 我往往会遇到很多缩进错误 有时 当我删除并重写该行时 错误就会消失 有人可以为菜鸟提供 python 中 IndentationErrors 的高级解释吗 以下是我在玩 CheckIO 时收到的最近 inden
  • 使用会话在 Django 中将文件从一个视图传递到另一个视图

    我当前的工作项目要求我允许用户上传各种格式的文件 目前仅处理 CSV 格式 然后使用包含的数据来绘制图表Pandas http pandas pydata org 图书馆 我决定将图形渲染到模板的最简单方法是为图形创建特定视图 然后将图像从
  • 使用 python 脚本更改 shell 中的工作目录

    我想实现一个用户态命令 它将采用其参数之一 路径 并将目录更改为该目录 程序完成后 我希望 shell 位于该目录中 所以我想实施cd命令 但需要外部程序 可以在 python 脚本中完成还是我必须编写 bash 包装器 Example t
  • Eclipse/PyDev 中未使用导入警告,尽管已使用

    我正在我的文件中导入一个绘图包 如下所示 import matplotlib pyplot as plt 稍后我会在我的代码中成功使用此导入 fig plt figure figsize 16 10 然而 Eclipse 告诉我 未使用的导
  • 如何将 URL 添加到 Telegram Bot 的 InlineKeyboardButton

    我想制作一个按钮 可以从 Telegram 聊天中在浏览器中打开 URL 外部超链接 目前 我只开发了可点击的操作按钮 update message reply text Subscribe to us on Facebook and Te
  • 如何检测一个二维数组是否在另一个二维数组内?

    因此 在堆栈溢出成员的帮助下 我得到了以下代码 data needle s which is a png image base64 code goes here decoded data decode base64 f cStringIO
  • 如何在引发异常时将变量传递给异常并在异常时检索它?

    现在我只有一个空白的异常类 我想知道如何在引发变量时给它一个变量 然后在 try except 中处理它时检索该变量 class ExampleException Exception pass 为其构造函数提供一个参数 将其存储为属性 然后
  • tf.print() vs Python print vs tensor.eval()

    看来在Tensorflow中 至少有三种方法可以打印出张量的值 我一直在读here https www freecodecamp org news debugging tensorflow a starter e6668ce72617 an
  • PyQt5按钮lambda变量变成布尔值[重复]

    这个问题在这里已经有答案了 当我运行下面的代码时 它显示如下 为什么 x 不是 x 而是变成布尔值 这种情况仅发生在传递到用 lambda 调用的函数中的第一个参数上 错误的 y home me model some file from P
  • py2exe ImportError:没有名为 的模块

    我已经实现了一个名为 myUtils 的包 它由文件夹 myUtils 文件 组成 init py 和许多名称为 myUtils 的 py 文件 该包包含在 myOtherProject py 中 当我从 Eclipse 运行它们时可以找到
  • Chrome 驱动程序和 Chromium 二进制文件无法在 aws lambda 上运行

    我陷入了一个问题 我需要在 AWS lambda 上做一些抓取工作 所以我按照下面提到的博客及其代码库作为起点 这非常有帮助 并且在运行时环境 Python 3 6 的 AWS lambda 上对我来说工作得很好 https manivan
  • 从 Django 运行 shell 命令

    我正在 Django 中开发一个网页 使用 apache 服务器 需要调用 shell 命令来启用 禁用一些守护进程 我尝试这样做 os system service httpd restart 1 gt HOME out 2 gt HOM
  • 超过两个点的Python相对导入

    是否可以使用路径中包含两个以上点的模块引用 就像这个例子一样 Project structure sound init py codecs init py echo init py nix init py way1 py way2 py w

随机推荐