1

Я хочу в этой игре сделать так, чтобы в диалоговом окне в начале пользователь мог бы выбрать тип игры один игрок или два. И создать диалоговое окно simlpedialog в котором пользователь мог бы ввести свое имя. И в конце игры когда победитель обьявляеться, высвечивалось это имя.

from tkinter import *
from tkinter import messagebox
from random import randint
from tkinter import simpledialog


answer = simpledialog.askstr('Input', 'Input number')





ActivePlayer = 1
p1 = []
p2 = []
window = Tk()
window.title("Game")


button1 = Button(window, text = "")
button1.grid(row = 0, column = 0, sticky = "snew", ipadx = 40, ipady = 40)
button1.config(command = lambda: ButtonClick(1))

button2 = Button(window, text = "")
button2.grid(row = 0, column = 1, sticky = "snew", ipadx = 40, ipady = 40)
button2.config(command = lambda: ButtonClick(2))

button3 = Button(window, text = "")
button3.grid(row = 0, column = 2, sticky = "snew", ipadx = 40, ipady = 40)
button3.config(command = lambda: ButtonClick(3))

button4 = Button(window, text = "")
button4.grid(row = 1, column = 0, sticky = "snew", ipadx = 40, ipady = 40)
button4.config(command = lambda: ButtonClick(4))

button5 = Button(window, text = "")
button5.grid(row = 1, column = 1, sticky = "snew", ipadx = 40, ipady = 40)
button5.config(command = lambda: ButtonClick(5))

button6 = Button(window, text = "")
button6.grid(row = 1, column = 2, sticky = "snew", ipadx = 40, ipady = 40)
button6.config(command = lambda: ButtonClick(6))

button7 = Button(window, text = "")
button7.grid(row = 2, column = 0, sticky = "snew", ipadx = 40, ipady = 40)
button7.config(command = lambda: ButtonClick(7))

button8 = Button(window, text = "")
button8.grid(row = 2, column = 1, sticky = "snew", ipadx = 40, ipady = 40)
button8.config(command = lambda: ButtonClick(8))

button9 = Button(window, text = "")
button9.grid(row = 2, column = 2, sticky = "snew", ipadx = 40, ipady = 40)
button9.config(command = lambda: ButtonClick(9))

def ButtonClick(id):
    global ActivePlayer
    global p1
    global p2
    print("ID:{}".format(id))

    if (ActivePlayer == 1):
        SetLayout(id, "X")
        p1.append(id)
        ActivePlayer = 2
        print("P1:{}".format(p1))

    elif (ActivePlayer == 2):
        SetLayout(id, "O")
        p2.append(id)
        ActivePlayer = 1
        print("P2:{}".format(p2))

    ChooseWinner()

def SetLayout(id, PlayerSymbol):
    if (id == 1):
        button1.config(text = PlayerSymbol,
                    state = DISABLED)
    elif (id == 2):
        button2.config(text = PlayerSymbol,
                    state = DISABLED)
    elif (id == 3):
        button3.config(text = PlayerSymbol,
                    state = DISABLED)
    elif (id == 4):
        button4.config(text = PlayerSymbol,
                    state = DISABLED)
    elif (id == 5):
        button5.config(text = PlayerSymbol,
                    state = DISABLED)

    elif (id == 6):
        button6.config(text = PlayerSymbol,
                       state = DISABLED)
    elif (id == 7):
        button7.config(text = PlayerSymbol,
                       state = DISABLED)
    elif (id == 8):
        button8.config(text = PlayerSymbol,
                       state = DISABLED)
    elif (id == 9):
        button9.config(text = PlayerSymbol,
                       state = DISABLED)


def ChooseWinner():
    Winner = -1
    ''' W I N N E R - 1 '''
    if ((1 in p1) and (2 in p1) and (3 in p1)):
        Winner = 1
    if ((1 in p2) and (2 in p2) and (3 in p2)):
        Winner = 2

    if ((4 in p1) and (5 in p1) and (6 in p1)):
        Winner = 1
    if ((4 in p2) and (5 in p2) and (6 in p2)):
        Winner = 2

    if ((7 in p1) and (8 in p1) and (9 in p1)):
        Winner = 1
    if ((7 in p2) and (8 in p2) and (9 in p2)):
        Winner = 2
    ''' W I N N E R - 2 '''
    if ((1 in p1) and (4 in p1) and (7 in p1)):
        Winner = 1
    if ((1 in p2) and (4 in p2) and (7 in p2)):
        Winner = 2

    if ((2 in p1) and (6 in p1) and (8 in p1)):
        Winner = 1
    if ((2 in p2) and (6 in p2) and (8 in p2)):
        Winner = 2

    if ((3 in p1) and (7 in p1) and (9 in p1)):
        Winner = 1
    if ((3 in p2) and (7 in p2) and (9 in p2)):
            Winner = 2
    ''' W I N N E R - 3 '''
    if ((1 in p1) and (5 in p1) and (9 in p1)):
        Winner = 1
    if ((1 in p2) and (5 in p2) and (9 in p2)):
        Winner = 2

    if ((3 in p1) and (5 in p1) and (7 in p1)):
        Winner = 1
    if ((3 in p2) and (5 in p2) and (7 in p2)):
        Winner = 2
    if Winner == 1:
        messagebox.showinfo("Winner", "Player 1 is        Winner")
    elif Winner == 2:
        messagebox.showinfo("Winner", "Player 2 is        Winner")

def AutoPlay():
    global p1
    global p2
    EmplyCells = []
    for i in range(9):
        if ( (i+1 in p1) or (i+1 in p2)):
            EmplyCells.append(i+1)

    RandomIndex = randint(0, len(EmplyCells)-1)
    ButtonClick(EmplyCells[RandomIndex])

window.mainloop()
2
  • 1
    и что у вас не работает? Commented 1 нояб. 2018 в 8:29
  • @michael_best answer = simpledialog.askstr('Input', 'Input number') в этой строке у меня ошибка answer = simpledialog.askstr('Input', 'Input number') Commented 1 нояб. 2018 в 9:45

1 ответ 1

1

Перетащите ваш диплог в основное окна tkinter.Tk()

from tkinter import *
from tkinter import messagebox
from random import randint
from tkinter import simpledialog

ActivePlayer = 1
p1 = []
p2 = []
window = Tk()

answer = simpledialog.askstring('Input', 'Input number')
....

Ваш ответ

Нажимая «Отправить ответ», вы соглашаетесь с условиями пользования и подтверждаете, что прочитали политику конфиденциальности.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.