2017-05-16 15 views
3

私は画面上に記号を表示したいと思います。e.xは(ハッシュ) '#'かもしれません。サインにはいくつかの開始位置がありますが、(0、0)としましょう。右矢印を押すと記号が右に移動し、左矢印を押すと左に移動します。 これまでのコードはこのように見えますが、posを読み込むために機能しますが、何らかの "アニメーション"だから私は看板が画面上を移動しているのを見ることができます:python keypress単純なゲーム

更新:ただuaの手がかりを与えるために、私は "アイコン"を作成し、右または左を押すと、アイコンが所望の方向に動く。

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

msvcrt import getch:あなた自身のライブラリを使用しているようです。それを提供してください。 – user7185318

+0

@ user7185318 [msvcrt](https://docs.python.org/3/library/msvcrt.html#msvcrt.getch)は、標準ライブラリの一部です。 – skrx

+0

@skrx:ありがとう、私はLinuxを使用しているので、私はそれを持っていません。 – user7185318

答えて

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

プレーヤーがマップを離れる場合、ゲームがクラッシュするのを防ぐコードを追加する必要があります。 – skrx

+0

また、[curses](https://docs.python.org/3/howto/curses.html)を見て、もっと洗練されたroguelikes [libtcod](http://www.roguebasin .com/index.php?title = Complete_Roguelike_Tutorial、_using_python%2Blibtcod)(Python 2のみ)。 Windowsでは、http://www.lfd.uci.edu/~gohlke/pythonlibs/#cursesからcurses(ホイールファイルとして)をダウンロードする必要があります。 – skrx

関連する問題