Ich bin ziemlich neu bei Python und habe mein zweites Spiel geschrieben, weil ich denke, dass dies der beste Weg ist, eine neue Sprache zu lernen. Mein Code ist wie folgt:Wie drucke ich mit Python mit verschiedenen Farben in einem Array?
Der Code:
#!/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:"))
Ich möchte, wenn der Benutzer Vermutungen Ich will nur der Buchstabe X rot sein, wie der Schlüssel vermuten lässt.
Stromausgang:
Beachten Sie, wie nur „X“ ist in rot und die eckigen Klammern sind Cyan, das ist im Grunde, was ich im Spiel erreichen will.
Ideal Ausgang:
Frage:
Wie kann ich es wie oben drucken lassen?
Kannst du die Frage auf die esse reduzieren? nce? Sehen Sie, wie man eine [mcve] fragt –