Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. from tkinter import *
  2. import math
  3. import random
  4.  
  5.  
  6. class Game:
  7. def __init__(self):
  8. self.width = 10
  9. self.height = 10
  10. self.scores = 0
  11. self.map = generate_map(self.width, self.height)
  12. self.w = self.create_board(Game)
  13. #self.scores_display = Game.w.create_text(750, 400, text=str(Game.scores))
  14.  
  15. def create_board(self):
  16. window = Tk()
  17. window.title('Coubs game')
  18. w = Canvas(window, width=900, height=700)
  19. w.pack()
  20. return w
  21.  
  22. def draw(self):
  23. for row in range(len(self.map)):
  24. # Перевернутый массив (y нумеруются с 9)
  25. for number in range(len(Game.map[row])):
  26. left_x = 600 - 60*(len(Game.map[row]) - (number + 1))
  27. left_y = 640 - 60*(row + 1)
  28. if Game.map[row][number] == 0:
  29. obj_id = Game.set_color(self,left_x, left_y, 'white')
  30. Game.w.unbind_all(obj_id)
  31. if Game.map[row][number] == 1:
  32. obj_id = Game.set_color(self, left_x, left_y, 'orange')
  33. Game.w.tag_bind(obj_id, '<Button-1>', pressed)
  34. if Game.map[row][number] == 2:
  35. obj_id = Game.set_color(self, left_x, left_y, 'green')
  36. Game.w.tag_bind(obj_id, '<Button-1>', pressed)
  37. if Game.map[row][number] == 3:
  38. obj_id = Game.set_color(self, left_x, left_y, 'red')
  39. Game.w.tag_bind(obj_id, '<Button-1>', pressed)
  40.  
  41. def set_color(self, left_x, left_y, color):
  42. return self.w.create_rectangle(left_x, left_y, left_x + 60, left_y + 60, fill=color)
  43.  
  44. def update_map(self):
  45. for row in range(len(Game.map)):
  46. for color in range(len(Game.map[row])):
  47. if Game.map[row][color] == 0 and row < Game.height:
  48. x = 0
  49. while Game.map[row + x][color] == 0 and row + x < Game.height - 1:
  50. x += 1
  51. Game.map[row + x - 1][color] = Game.map[row + x][color]
  52. Game.map[row + x][color] = 0
  53. return Game.map
  54.  
  55. def redraw(self):
  56. self.update_map(self)
  57. Canvas.update(self.w)
  58. Game.draw(self)
  59.  
  60. def get_restart(self):
  61. restart = Button(self.w, text="restart", command=restart_game).place(x=750, y=280)
  62.  
  63.  
  64. def pressed(event):
  65. x = math.floor(event.x/60 - 1)
  66. y = math.floor((event.y-40)/60)
  67. color = Game.map[9-y][x]
  68. destroy(color, y, x, 0)
  69. Game.scores += 1
  70. print(Game.scores)
  71. Game.redraw(Game)
  72. Game.redraw(Game)
  73. Game.redraw(Game)
  74.  
  75.  
  76. def generate_map(width, height):
  77. map = [[random.choice([1, 2, 3]) for x in range(width)] for y in range(height)]
  78. return map
  79.  
  80.  
  81. def destroy(color, y, x, isfirst):
  82. if 9 - (y + 1) >= 0 and Game.map[9 - (y + 1)][x] == color:
  83. Game.map[9 - y][x] = 0
  84. Game.scores += 1
  85. destroy(color, y + 1, x, 1)
  86. if 9 - (y - 1) <= Game.width - 1 and Game.map[9 - (y - 1)][x] == color:
  87. Game.map[9 - y][x] = 0
  88. Game.scores += 1
  89. destroy(color, y - 1, x, 1)
  90. if x + 1 < Game.height and Game.map[9 - y][x + 1] == color:
  91. Game.map[9 - y][x] = 0
  92. Game.scores += 1
  93. destroy(color, y, x + 1, 1)
  94. if x - 1 >= 0 and Game.map[9 - y][x - 1] == color:
  95. Game.map[9 - y][x] = 0
  96. Game.scores += 1
  97. destroy(color, y, x - 1, 1)
  98. if isfirst == 1:
  99. Game.map[9 - y][x] = 0
  100. return isfirst
  101.  
  102.  
  103. def restart_game():
  104. Game.map = generate_map(10, 10)
  105. Game.draw(Game)
  106. Game.scores = 0
  107.  
  108. if __name__ == '__main__':
  109. Game.__init__(Game)
  110. Game.draw(Game)
  111. Game.get_restart(Game)
  112. print('hello')
  113. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement