2016-05-27 4 views
3

私はこれが新しい言語を学ぶ最も良い方法だと思うので、私はかなり新しいPythonを使い、2番目のゲームを書いています。次のように私のコードは次のとおりです。配列内でtermcolorを使ってPythonでさまざまな色で印刷する方法は?

コード:

#!/usr/bin/env python 

from random import randint 
from termcolor import colored 
import os 
import sys 
import time 
clear = lambda : os.system('tput reset') 

clear() 

board = [] 

board_size=5 

for x in range(board_size): 
    board.append(["[W]"] * board_size) 

def print_board(board): 
    for row in board: 
     print colored(" ".join(row),"cyan") 

#print "Let's play Battleship!" 
print_board(board) 

def random_row(board): 
    return randint(0, len(board) - 1) 

def random_col(board): 
    return randint(0, len(board[0]) - 1) 

ship_row = random_row(board) +1 
ship_col = random_col(board) +1 

# Prints where the ship is placed 
# Do the right and don't cheat! 
# print ship_row 
# print ship_col 


print colored("\nNot bombed:  ","yellow") + colored("[W]","cyan") 
print colored("Has been bombed: ","yellow") + colored("[","cyan") + colored("X","red") + colored("]\n","cyan") 

guess_row = int(raw_input("Guess Row:")) 
guess_col = int(raw_input("Guess Col:")) 

counter=0 

state=True 

while bool(state): 

    counter=int(counter)+1 

    if guess_row == ship_row and guess_col == ship_col: 
     clear() 
     print "\n\n Congratulations! You sunk my battleship!\n\n" 
     print "You got it right after " + str(counter) + " guesses." 
     state=False 
     time.sleep(2) 
     clear() 
     sys.exit() 

    else: 
     if (guess_row -1 < 0 or guess_row > board_size) or (guess_col -1 < 0 or guess_col > board_size): 
      print "Oops, that's not even in the ocean." 
      counter=int(counter)-1 
      time.sleep(1) 
      clear() 

     elif(board[guess_row-1][guess_col-1] == "[X]"): 
      print "You guessed that one already." 
      counter=int(counter)-1 
      time.sleep(1) 
      clear() 

     else: 
      print "You missed my battleship!" 
      clear() 
      board[guess_row-1][guess_col-1] = "[X]" 
      #counter=int(counter)+1 


    print_board(board) 

    print colored("\nNot bombed:  ","yellow") + colored("[W]","cyan") 
    print colored("Has been bombed: ","yellow") + colored("[","cyan") + colored("X","red") + colored("]\n","cyan") 

    guess_row = int(raw_input("Guess Row:")) 
    guess_col = int(raw_input("Guess Col:")) 

私がしたい、ユーザーの推測は、私はちょうどキーが示唆するように、文字Xは、赤になりたいとき。

電流出力:これは私がゲームで達成したいものを根本的にある、

enter image description here

注どのように唯一の「X」は赤であり、角括弧はシアンです。

理想的な出力:

enter image description here

質問:

どのように私はそれが上記のように印刷することができますか?

+0

>>> color_key.get('[', 'cyan') 'cyan' 

は、そうでなければ、例外を発生させますあなたは質問をesseに還元できますか?元気? –

答えて

2

をチェックアウトする問題のコードは次のとおりです。

print colored(" ".join(row),"cyan") 

あなたが必要です:より一般的に

print ' '.join(colored(element, 'cyan') if element != 'X' 
       else colored(element, 'red') 
       for element in row) 

編集、あなたは文字に基づいて色を見ることができます。一般的なツールは、キーと値の間のマッピングを提供するPythonのdictを使用することです。

>>> color_key = { 
...  'X': 'red', 
...  'H': 'magenta'} 
>>> color_key['X'] 
'red' 

あなたが不足しているキーのデフォルトを提供することができますget使用する場合:

>>> color_key['['] 
...KeyError... 

使用法:

print ' '.join(colored(element, color_key.get(element, 'cyan') 
      for element in row) 
+0

どのように条件の数を増やすのがどのように[[H]]をマゼンタにする必要がありますか?要素!= "[X]"であれば色付けされています(要素 "緑")。 、 "red")要素が!= "[H]"であれば、行内の要素に色付けされていました(しかし、動作しませんでした) – 3kstc

+0

' '' [S] ''は3文字なので、決して一致しません。 –

1

print_boardメソッドを更新します。

def print_board(board): 
    for row in board: 
     print colored(" ".join(row),"cyan") 

あなたが書く必要があります正確なコードを書くことがなければ、これを変更する必要があります:あなたが必要となりますので、それは、改行を入れます印刷するとき

通常
def print_board(board): 
    for row in board: 
     for each cell in the row: 
      if it is a W: 
       print it cyan 
      if it is an X: 
       print it in red 
     move to a new line now 

How to print without newline or space?

関連する問題