Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/env python3
- from tkinter import *
- from tkinter import messagebox
- import random
- tk = Tk()
- tk.title("Пятнашки")
- tk.resizable(False, False)
- tk.bind("<Escape>", quit)
- main_frame = Frame(tk, bg="#3f3f3f", cursor="hand1", width=48*8, height=48*8, bd=10)
- main_frame.pack()
- class Bone():
- def __init__(self, main_frame, bone_row, bone_col, bone_number):
- self.btn_id = Button(main_frame, width=4, height=3, text=str(bone_number))
- self.btn_id.grid(row=bone_row, column=bone_col)
- def game(event):
- global vacant_position
- global original_seq
- global mixed_seq
- vacant_row = vacant_position // 4
- vacant_col = vacant_position % 4
- current_pos = event.widget.grid_info()
- bone_row = current_pos['row']
- bone_col = current_pos['column']
- bone_position = 4 * bone_row + bone_col
- number = mixed_seq[bone_position]
- delta_raw = abs(vacant_row - bone_row)
- delta_col = abs(vacant_col - bone_col)
- if (delta_col + delta_raw) == 1:
- event.widget.grid_forget()
- event.widget.grid(row=vacant_row, column=vacant_col)
- vacant_position, bone_position = bone_position,vacant_position
- mixed_seq[bone_position] = number
- mixed_seq[vacant_position] = "*"
- if vacant_position == 15:
- if original_seq == mixed_seq:
- if not messagebox.askyesno("Победа!", "Good for you!\nСыграешь ещё раз?"):
- quit()
- else:
- for btn in main_frame.grid_slaves():
- btn.grid_remove()
- original_seq, mixed_seq, vacant_position = init()
- def init():
- original_seq = list(range(1, 16))
- original_seq.append("*")
- vacant_position = 15
- mixed_seq = original_seq[:]
- roulette = 1, -1, 4, -4
- i = 0
- while i != 100:
- step = random.choice(roulette)
- destination = vacant_position + step
- if not (destination > 15 or destination < 0):
- native = mixed_seq[destination]
- mixed_seq[destination] = "*"
- mixed_seq[vacant_position] = native
- vacant_position = destination
- i += 1
- for i in mixed_seq:
- if i != "*":
- bone_row = mixed_seq.index(i)//4
- bone_col = mixed_seq.index(i)%4
- Bone(main_frame, bone_row, bone_col, i)
- else:
- vacant_position = int(mixed_seq.index(i))
- return original_seq, mixed_seq, vacant_position
- original_seq, mixed_seq, vacant_position = init()
- main_frame.bind_class('Button', '<Button-1>', game)
- tk.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement