Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from random import randint
- # Функция установки параметров окна приложения
- def initialize_window(window, title, size=(400, 300), sizable=(False, False)):
- '''
- Устанавливает заголовок окна, размеры по осям X, Y и устанавливает возможность изменения по осям X, Y
- args - window (Tk), title (str), size (tuple(int, int)), sizable (tuple(bool, bool))
- return - None
- '''
- window.title(title)
- window.geometry(f'{size[0]}x{size[1]}')
- window.resizable(sizable[0], sizable[1])
- # Функция инициализации и установки параметров кнопок
- def initialize_widgets(window):
- global score_label, result
- score_and_result = Frame(window)
- score_label = Label(score_and_result, text=f'Всего игр: {sum(scores)}\n\nПобед: {scores[0]}\nПоражений: {scores[1]}\n Ничейный игр: {scores[2]}')
- result = Label(score_and_result, text='Сделайте ход!', width=30, font=('Arial', 15, 'bold'))
- score_and_result.pack(pady=20)
- score_label.pack(padx=20, side='left', expand=1)
- result.pack(padx=20, side='left', expand=1,)
- buttons = Frame()
- b_stone = Button(buttons, text='Камень', command=stone, width=10, height=5)
- b_scissors = Button(buttons, text='Ножницы', command=scissors, width=10, height=5)
- b_paper = Button(buttons, text='Бумага', command=paper, width=10, height=5)
- buttons.pack(pady=20)
- b_stone.pack(padx=10, side='left')
- b_scissors.pack(padx=10, side='left')
- b_paper.pack(padx=10, side='left')
- def stone():
- game(0)
- def scissors():
- game(1)
- def paper():
- game(2)
- def update_statistic(text, color):
- result.config(text=text, fg=color)
- score_label.config(text=f'Всего игр: {sum(scores)}\n\nПобед: {scores[0]}\nПоражений: {scores[1]}\n Ничейный игр: {scores[2]}')
- # Запуск игры по средству выбора знака по кнопке
- def game(choose):
- player_choose = choose
- comp_choose = randint(0,2)
- text, color = find_winner(player_choose, comp_choose)
- update_statistic(text, color)
- def find_winner(player, comp):
- wins_player = [(0,1), (1,2), (2, 0)] # Варианты игр с победой игрока
- '''
- 0 - камень
- 1 - ножницы
- 2 - бумага
- '''
- game = (player, comp)
- if player == comp:
- scores[2] += 1
- text = 'Ничья'
- color = 'blue'
- elif game in wins_player:
- scores[0] += 1
- text = 'Победа!'
- color = 'green'
- else:
- scores[1] += 1
- text='Поражение!'
- color='red'
- return text, color
- def main():
- global scores
- scores = [0,0,0]
- root = Tk()
- initialize_window(root, 'Камень, ножницы, бумага')
- initialize_widgets(root)
- root.mainloop()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement