Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import math
- import random
- class Game:
- def __init__(self):
- self.width = 10
- self.height = 10
- self.scores = 0
- self.map = generate_map(self.width, self.height)
- self.w = self.create_board(Game)
- #self.scores_display = Game.w.create_text(750, 400, text=str(Game.scores))
- def create_board(self):
- window = Tk()
- window.title('Coubs game')
- w = Canvas(window, width=900, height=700)
- w.pack()
- return w
- def draw(self):
- for row in range(len(self.map)):
- # Перевернутый массив (y нумеруются с 9)
- for number in range(len(Game.map[row])):
- left_x = 600 - 60*(len(Game.map[row]) - (number + 1))
- left_y = 640 - 60*(row + 1)
- if Game.map[row][number] == 0:
- obj_id = Game.set_color(self,left_x, left_y, 'white')
- Game.w.unbind_all(obj_id)
- if Game.map[row][number] == 1:
- obj_id = Game.set_color(self, left_x, left_y, 'orange')
- Game.w.tag_bind(obj_id, '<Button-1>', pressed)
- if Game.map[row][number] == 2:
- obj_id = Game.set_color(self, left_x, left_y, 'green')
- Game.w.tag_bind(obj_id, '<Button-1>', pressed)
- if Game.map[row][number] == 3:
- obj_id = Game.set_color(self, left_x, left_y, 'red')
- Game.w.tag_bind(obj_id, '<Button-1>', pressed)
- def set_color(self, left_x, left_y, color):
- return self.w.create_rectangle(left_x, left_y, left_x + 60, left_y + 60, fill=color)
- def update_map(self):
- for row in range(len(Game.map)):
- for color in range(len(Game.map[row])):
- if Game.map[row][color] == 0 and row < Game.height:
- x = 0
- while Game.map[row + x][color] == 0 and row + x < Game.height - 1:
- x += 1
- Game.map[row + x - 1][color] = Game.map[row + x][color]
- Game.map[row + x][color] = 0
- return Game.map
- def redraw(self):
- self.update_map(self)
- Canvas.update(self.w)
- Game.draw(self)
- def get_restart(self):
- restart = Button(self.w, text="restart", command=restart_game).place(x=750, y=280)
- def pressed(event):
- x = math.floor(event.x/60 - 1)
- y = math.floor((event.y-40)/60)
- color = Game.map[9-y][x]
- destroy(color, y, x, 0)
- Game.scores += 1
- print(Game.scores)
- Game.redraw(Game)
- Game.redraw(Game)
- Game.redraw(Game)
- def generate_map(width, height):
- map = [[random.choice([1, 2, 3]) for x in range(width)] for y in range(height)]
- return map
- def destroy(color, y, x, isfirst):
- if 9 - (y + 1) >= 0 and Game.map[9 - (y + 1)][x] == color:
- Game.map[9 - y][x] = 0
- Game.scores += 1
- destroy(color, y + 1, x, 1)
- if 9 - (y - 1) <= Game.width - 1 and Game.map[9 - (y - 1)][x] == color:
- Game.map[9 - y][x] = 0
- Game.scores += 1
- destroy(color, y - 1, x, 1)
- if x + 1 < Game.height and Game.map[9 - y][x + 1] == color:
- Game.map[9 - y][x] = 0
- Game.scores += 1
- destroy(color, y, x + 1, 1)
- if x - 1 >= 0 and Game.map[9 - y][x - 1] == color:
- Game.map[9 - y][x] = 0
- Game.scores += 1
- destroy(color, y, x - 1, 1)
- if isfirst == 1:
- Game.map[9 - y][x] = 0
- return isfirst
- def restart_game():
- Game.map = generate_map(10, 10)
- Game.draw(Game)
- Game.scores = 0
- if __name__ == '__main__':
- Game.__init__(Game)
- Game.draw(Game)
- Game.get_restart(Game)
- print('hello')
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement