Advertisement
jimkilled

Rock, Scissors, Paper

Dec 1st, 2020 (edited)
1,461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 KB | None | 0 0
  1. from tkinter import *
  2. from random import randint
  3.  
  4. # Функция установки параметров окна приложения
  5. def initialize_window(window, title, size=(400, 300), sizable=(False, False)):
  6.     '''
  7.    Устанавливает заголовок окна, размеры по осям X, Y и устанавливает возможность изменения по осям X, Y
  8.    args - window (Tk), title (str), size (tuple(int, int)), sizable (tuple(bool, bool))
  9.    return - None
  10.    '''
  11.     window.title(title)
  12.     window.geometry(f'{size[0]}x{size[1]}')
  13.     window.resizable(sizable[0], sizable[1])
  14.  
  15.  
  16. # Функция инициализации и установки параметров кнопок
  17. def initialize_widgets(window):
  18.     global score_label, result
  19.    
  20.     score_and_result = Frame(window)
  21.     score_label = Label(score_and_result, text=f'Всего игр: {sum(scores)}\n\nПобед: {scores[0]}\nПоражений: {scores[1]}\n Ничейный игр: {scores[2]}')
  22.     result = Label(score_and_result, text='Сделайте ход!', width=30, font=('Arial', 15, 'bold'))
  23.  
  24.     score_and_result.pack(pady=20)
  25.     score_label.pack(padx=20, side='left', expand=1)
  26.     result.pack(padx=20, side='left', expand=1,)
  27.  
  28.  
  29.     buttons = Frame()
  30.     b_stone = Button(buttons, text='Камень', command=stone, width=10, height=5)
  31.     b_scissors = Button(buttons, text='Ножницы', command=scissors, width=10, height=5)
  32.     b_paper = Button(buttons, text='Бумага', command=paper, width=10, height=5)
  33.  
  34.     buttons.pack(pady=20)
  35.     b_stone.pack(padx=10, side='left')
  36.     b_scissors.pack(padx=10, side='left')
  37.     b_paper.pack(padx=10, side='left')
  38.  
  39.  
  40. def stone():
  41.     game(0)
  42.  
  43. def scissors():
  44.     game(1)
  45.  
  46. def paper():
  47.     game(2)
  48.  
  49. def update_statistic(text, color):
  50.     result.config(text=text, fg=color)
  51.     score_label.config(text=f'Всего игр: {sum(scores)}\n\nПобед: {scores[0]}\nПоражений: {scores[1]}\n Ничейный игр: {scores[2]}')
  52.  
  53.  
  54. # Запуск игры по средству выбора знака по кнопке
  55. def game(choose):
  56.     player_choose = choose
  57.     comp_choose = randint(0,2)
  58.     text, color = find_winner(player_choose, comp_choose)
  59.     update_statistic(text, color)
  60.  
  61.  
  62. def find_winner(player, comp):
  63.     wins_player = [(0,1), (1,2), (2, 0)]    # Варианты игр с победой игрока
  64.     '''
  65.    0 - камень
  66.    1 - ножницы
  67.    2 - бумага
  68.    '''
  69.     game = (player, comp)
  70.     if player == comp:
  71.         scores[2] += 1
  72.         text = 'Ничья'
  73.         color = 'blue'
  74.     elif game in wins_player:
  75.         scores[0] += 1
  76.         text = 'Победа!'
  77.         color = 'green'
  78.     else:
  79.         scores[1] += 1
  80.         text='Поражение!'
  81.         color='red'
  82.     return text, color
  83.  
  84.  
  85. def main():
  86.     global scores
  87.     scores = [0,0,0]
  88.     root = Tk()
  89.     initialize_window(root, 'Камень, ножницы, бумага')
  90.     initialize_widgets(root)
  91.     root.mainloop()
  92.  
  93. if __name__ == '__main__':
  94.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement