【问题标题】:python keypress simple gamepython按键简单游戏
【发布时间】:2017-05-16 13:11:16
【问题描述】:

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

!更新:只是为了给你一个线索,我创建了“图标”,现在当你按右或左时,图标会向所需的方向移动。

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()

【问题讨论】:

  • from msvcrt import getch :在我看来,您正在使用自己的库。请提供。
  • @user7185318 msvcrt 是标准库的一部分。
  • @skrx :谢谢,我使用的是 Linux,所以我没有。

标签: python keypress


【解决方案1】:

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

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()

【讨论】:

  • 您仍然需要添加代码以防止玩家离开地图时游戏崩溃。
  • 您还可以查看curses,如果您想创建更复杂的roguelikes libtcod(仅限Python 2)。在 Windows 上,您必须从 lfd.uci.edu/~gohlke/pythonlibs/#curses 下载 curses(作为一个轮子文件)。
猜你喜欢
  • 1970-01-01
  • 2017-12-17
  • 2022-06-23
  • 1970-01-01
  • 2015-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-09
相关资源
最近更新 更多