Как сделать графический интерфейс для игры на линкоре Python - PullRequest
0 голосов
/ 27 октября 2019

Итак, для школьного проекта мне нужно сделать игру на линкоре на python, которая требует графического интерфейса. Мне поручили использовать Tkinter, но у меня много проблем с настройкой графического интерфейса, я настроил игру, и в ней можно играть в текстовом режиме, но это не пройдет. Я провел немало исследований, но не могу понять, если кто-нибудь сможет мне помочь с этим, я с удовольствием это оценил бы. Мой код ниже.

from random import randint

def game():
#Board Setup-------------
hit = 0
ocean = []

for o in range (0, 10):
    ocean.append(["O"] * 10)

def print_ocean(ocean):
    for a in ocean:
        print (" ".join(a))

print_ocean(ocean)

#Ship placement----------

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

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

a_row = random_row(ocean)
a_col = random_col(ocean)


b_row = random_row(ocean)
b_col = random_col(ocean)


c_row = random_row(ocean)
c_col = random_col(ocean)


d_row = random_row(ocean)
d_col = random_col(ocean)


e_row = random_row(ocean)
e_col = random_col(ocean)


#Ship Check--------------

def b_check():
      if b_row == a_row and b_col == a_col:
            b_row = random_row(ocean)
            b_col = random_col(ocean)
            return b_row
            return b_col
            b_check()

def c_check():
      if c_row == a_row and c_col == a_col or c_row == b_row and c_col == b_col:
            c_row = random_row(ocean)
            c_col = random_col(ocean)
            return c_row
            return c_col
            c_check()

def d_check():
      if d_row == a_row and d_col == a_col or d_row == b_row and d_col == b_col\
         or d_row == c_row and d_col == c_col:
            d_row = random_row(ocean)
            d_col = random_col(ocean)
            return d_row
            return d_col
            d_check()

def e_check():
      if e_row == a_row and e_col == a_col or e_row == b_row and e_col == b_col\
         or e_row == c_row and e_col == c_col or e_row == d_row and e_col == d_col:
            e_row = random_row(ocean)
            e_col = random_col(ocean)
            return e_row
            return e_col
            e_check()

#Restart-----------------

def restart():
    again = input("Captain would you like to start another attack? (Y/N)")
    if again == "y" or again == "Y":
        print ("Heading back into battle!")
        game()
    elif again == "n" or again == "N":
        print ("Retreating...")
        exit()
    else:
        print ("Captain, I don't understand that order (Y/N)")
        restart()

#Turn Setup--------------

for t in range(30):
    print ("")
    print ("Captain, ready to fire shell", t+1)
    print ("")
    guess_row = int(input("Captain, X coordiante: "))
    print("")
    guess_col = int(input("Captain, Y coordinate: "))
    print("")
    if (guess_row == a_row and guess_col == a_col) or (guess_row == b_row and guess_col == b_col)or\     
       (guess_row == c_row and guess_col == c_col) or (guess_row == d_row and guess_col == d_col)or\ 
       (guess_row == e_row and guess_col == e_col):
        ocean[guess_row][guess_col] = "S"
        print ("")
        print ("Captain, we sunk their battleship!")
        print ("")
        hit +=1
    elif guess_row not in range(10) or guess_col not in range(10):
        print ("Captain thats miles off target")
    elif ocean[guess_row][guess_col] == "X":
        print ("Captain, we've already fired there")
    else:
        print ("Captain we missed")
        ocean[guess_row][guess_col] = "X"
    print_ocean(ocean)

    if t == 14:
        print ("Captain, we're out of shells")
        restart()

    if hit == 5:
        print ("Captain, we sunk all of their ships!")
        restart()

print_ocean(ocean)
game()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...