def add_piece(self): if DEBUG: print("Creating random piece") if self.Pool == []: self.Pool = ['s', 'z', 'r', 'L', 'o', 'I', 'T'] Chossen = random.choice(self.Pool) self.Pool.remove(Chossen) shape = self.shapes[Chossen] shape = ra(shape, random.choice((0, 90, 180, 270))) w = len(shape[0]) startUp = (10 - w) // 2 self.active_piece = Shape(shape, [], 0, startUp, []) if 3 in (len(shape), len(shape[0])): self.active_piece.rotation = [(0, 0), (1, 0), (-1, 1), (0, -1)] else: self.active_piece.rotation = [(1, -1), (0, 1), (0, 0), (-1, 0)] if len(shape) < len(shape[0]): # wide shape self.active_piece.rotation_index += 1 for y, row in enumerate(shape): self.grid[y][startUp:startUp + w] = shape[y] for x, grid_cell in enumerate(row, start=startUp): #=Takes Coords of each block and appends to list=# if grid_cell: self.active_piece.coords.append( (self.square_w * x, self.square_w * y, self.square_w * (x + 1), self.square_w * (y + 1))) #piece is created self.active_piece.piece.append( self.canvas.create_rectangle( self.active_piece.coords[-1], fill=self.color[Chossen], width=2))
def add_piece(self): if DEBUG: print("Creating random piece") shape = self.shapes[random.choice('szrLoIT')] shape = ra(shape, random.choice((0, 90, 180, 270))) w = len(shape[0]) startUp = (10 - w) // 2 self.active_piece = { 'shape': shape, 'piece': [], 'row': 0, 'col': startUp, 'coords': [] } for y, row in enumerate(shape): self.grid[y][startUp:startUp + w] = shape[y] for x, grid_cell in enumerate(row, start=startUp): #=Takes Coords of each block and appends to list=# if grid_cell: self.active_piece['coords'].append( (self.square_w * x, self.square_w * y, self.square_w * (x + 1), self.square_w * (y + 1))) self.active_piece['piece'].append( self.canvas.create_rectangle( self.active_piece['coords'][-1]))
def rotate(self, event=None): if not self.piece_is_active: return if len(self.active_piece.shape) == len(self.active_piece.shape[0]): self.active_piece.rotation_index = self.active_piece.rotation_index return r = self.active_piece.row c = self.active_piece.column l = len(self.active_piece.shape) w = len(self.active_piece.shape[0]) x = c + w // 2 # center column for old shape y = r + l // 2 # center row for old shape direction = event.keysym if direction in {'q', 'Q'}: # 4 is a magic number, number of sides of a rectangle shape = ra(self.active_piece.shape, -90) rotation_index = (self.active_piece.rotation_index - 1) % 4 rx, ry = self.active_piece.rotation[rotation_index] rotation_offsets = -rx, -ry elif direction in {'e', 'E', '0', 'Up', 'w', 'W'}: shape = ra(self.active_piece.shape, 90) rotation_index = self.active_piece.rotation_index rotation_offsets = self.active_piece.rotation[rotation_index] rotation_index = (rotation_index + 1) % 4 l = len(shape) # length of new shape w = len(shape[0]) # width of new shape rt = y - l // 2 # row of new shape ct = x - w // 2 # column of new shape x_correction, y_correction = rotation_offsets rt += y_correction ct += x_correction if self.check_and_move(shape, rt, ct, l, w): self.active_piece.rotation_index = rotation_index return if self.kick: for a, b in zip((0, 0, -1, 0, 0, -2, -1, -1), (-1, 1, 0, -2, 2, 0, -1, 1)): if self.check_and_move(shape, rt + a, ct + b, l, w): self.active_piece.rotation_index = rotation_index return
def rotate(self, event=None): if not self.piece_is_active: return if len(self.active_piece.shape) == len(self.active_piece.shape[0]): return r = self.active_piece.row c = self.active_piece.column l = len(self.active_piece.shape) w = len(self.active_piece.shape[0]) x = c + w // 2 # center column for old shape y = r + l // 2 # center row for old shape direction = event.keysym if direction in {'q', 'Q'}: shape = ra(self.active_piece.shape, -90) # 4 is a magic number, number of sides of a rectangle rotation_index = (self.active_piece.rotation_index - 1) % 4 rx, ry = self.active_piece.rotation[rotation_index] rotation_offsets = -rx, -ry elif direction in {'e', 'E', '0', 'Up', 'w', 'W'}: shape = ra(self.active_piece.shape, 90) rotation_index = self.active_piece.rotation_index rotation_offsets = self.active_piece.rotation[rotation_index] rotation_index = (rotation_index + 1) % 4 l = len(shape) # length of new shape w = len(shape[0]) # width of new shape rt = y - l // 2 # row of new shape ct = x - w // 2 # column of new shape x_correction, y_correction = rotation_offsets rt += y_correction ct += x_correction # rotation prefers upper left corner - # possibly hard-code a specific "center" square # for each piece/shape if not self.check_and_move(shape, rt, ct, l, w): return self.active_piece.rotation_index = rotation_index
def rotate(self, event=None): if not self.piece_is_active: return if len(self.active_piece.shape) == len(self.active_piece.shape[0]): return r = self.active_piece.row c = self.active_piece.column l = len(self.active_piece.shape) w = len(self.active_piece.shape[0]) x = c + w//2 # center column for old shape y = r + l//2 # center row for old shape direction = event.keysym if direction in {'q', 'Q'}: shape = ra(self.active_piece.shape, -90) # 4 is a magic number, number of sides of a rectangle rotation_index = (self.active_piece.rotation_index - 1) % 4 rx,ry = self.active_piece.rotation[rotation_index] rotation_offsets = -rx,-ry elif direction in {'e', 'E', '0', 'Up', 'w', 'W'}: shape = ra(self.active_piece.shape, 90) rotation_index = self.active_piece.rotation_index rotation_offsets = self.active_piece.rotation[rotation_index] rotation_index = (rotation_index + 1) % 4 l = len(shape) # length of new shape w = len(shape[0]) # width of new shape rt = y - l//2 # row of new shape ct = x - w//2 # column of new shape x_correction,y_correction = rotation_offsets rt += y_correction ct += x_correction # rotation prefers upper left corner - # possibly hard-code a specific "center" square # for each piece/shape if not self.check_and_move(shape, rt, ct, l, w): return self.active_piece.rotation_index = rotation_index
def rotate(self, event=None): if not self.piece_is_active: return if len(self.active_piece.shape) == len(self.active_piece.shape[0]): return r = self.active_piece.row c = self.active_piece.column l = len(self.active_piece.shape) w = len(self.active_piece.shape[0]) x = c + w // 2 y = r + l // 2 direction = event.keysym if direction in {'q', 'Q'}: shape = ra(self.active_piece.shape, -90) rotation_index = (self.active_piece.rotation_index - 1) % 4 rx, ry = self.active_piece.rotation[rotation_index] rotation_offsets = -rx, -ry elif direction in {'e', 'E', 'Up', 'w', 'W'}: shape = ra(self.active_piece.shape, 90) rotation_index = self.active_piece.rotation_index rotation_offsets = self.active_piece.rotation[rotation_index] rotation_index = (rotation_index + 1) % 4 l = len(shape) w = len(shape[0]) rt = y - l // 2 ct = x - w // 2 x_correction, y_correction = rotation_offsets rt += y_correction ct += x_correction success = self.check_and_move(shape, rt, ct, l, w) if not success: return self.active_piece.rotation_index = rotation_index
def spawn(self): shape = self.shapes[random.choice('szrLoIT')] shape = ra(shape, random.choice(0, 90, 180, 270)) width = len(shape[0]) start = (10 - width) // 2 self.active_piece = [shape, []] for y, row in enumerate(shape, start=start): for x, column in enumerate(row, start=start): if column: self.active_piece[1].append( self.canvas.create_rectangle( self.square_width * x, self.square_width * y, (self.square_width + 1) * x, (self.square_width + 1) * y, ))
def preview(self): self.preview_canvas.delete(tk.ALL) if not self.bag: if self.random: self.bag.append(random.choice('szrLoIT')) else: self.bag = random.sample('szrLoIT', 7) key = self.bag.pop() shape = ra(self.shapes[key], random.choice((0, 90, 180, 270))) self.preview_piece = Shape(shape, key, [], 0, 0, []) width = len(shape[0]) half = self.square_width//2 for y,row in enumerate(shape): for x,cell in enumerate(row): if cell: self.preview_piece.coords.append((self.square_width*x+half, self.square_width*y+half, self.square_width*(x+1)+half, self.square_width*(y+1)+half)) self.preview_piece.piece.append( self.preview_canvas.create_rectangle(self.preview_piece.coords[-1], fill=self.colors[key], width=3)) self.preview_piece.rotation_index = 0 self.preview_piece.i_nudge = (len(shape) < len(shape[0]) ) and 4 in (len(shape), len(shape[0])) self.preview_piece.row = self.preview_piece.i_nudge if 3 in (len(shape), len(shape[0])): self.preview_piece.rotation = [(0,0), (1,0), (-1,1), (0,-1)] else: self.preview_piece.rotation = [(1,-1), (0,1), (0,0), (-1,0)] if len(shape) < len(shape[0]): # wide shape self.preview_piece.rotation_index += 1
def preview(self): self.preview_canvas.delete(tk.ALL) if not self.bag: if self.random: self.bag.append(random.choice('szrLoIT')) else: self.bag = random.sample('szrLoIT', 7) key = self.bag.pop() shape = ra(self.shapes[key], random.choice((0, 90, 180, 270))) self.preview_piece = Shape(shape, key, [], 0, 0, []) width = len(shape[0]) half = self.square_width // 2 for y, row in enumerate(shape): for x, cell in enumerate(row): if cell: self.preview_piece.coords.append( (self.square_width * x + half, self.square_width * y + half, self.square_width * (x + 1) + half, self.square_width * (y + 1) + half)) self.preview_piece.piece.append( self.preview_canvas.create_rectangle( self.preview_piece.coords[-1], fill=self.colors[key], width=3)) self.preview_piece.rotation_index = 0 self.preview_piece.i_nudge = (len(shape) < len( shape[0])) and 4 in (len(shape), len(shape[0])) self.preview_piece.row = self.preview_piece.i_nudge if 3 in (len(shape), len(shape[0])): self.preview_piece.rotation = [(0, 0), (1, 0), (-1, 1), (0, -1)] else: self.preview_piece.rotation = [(1, -1), (0, 1), (0, 0), (-1, 0)] if len(shape) < len(shape[0]): # wide shape self.preview_piece.rotation_index += 1
def rotation(self, event=None): if not self.active_piece: return if len(self.active_piece.shape) == len(self.active_piece.shape[0]): self.active_piece.rotation_index = self.active_piece.rotation_index return r = self.active_piece.row c = self.active_piece.col l = len(self.active_piece.shape) w = len(self.active_piece.shape[0]) x = c + w // 2 #center for old shape y = r + l // 2 #center for old shape direction = event.keysym if direction == 'q': direction = 'left' shape = ra(self.active_piece.shape, -90) rotation_index = (self.active_piece.rotation_index - 1) % 4 rx, ry = self.active_piece.rotation[rotation_index] rotation_offsets = -rx, -ry elif direction == 'e': direction = 'right' shape = ra(self.active_piece.shape, 90) rotation_index = self.active_piece.rotation_index rotation_offsets = self.active_piece.rotation[rotation_index] rotation_index = (rotation_index + 1) % 4 l = len(shape) #rotated length w = len(shape[0]) #rotated width rt = y - l // 2 #rotated row ct = x - w // 2 #rotated column x_adjust, y_adjust = rotation_offsets rt += y_adjust ct += x_adjust if DEBUG: print(shape) for row, blocks in zip(range(rt, rt + l), shape): for column, block in zip(range(ct, ct + w), blocks): if (column not in range(self.grid_width) or (row not in range(self.grid_height)) or (blocks and self.grid[row][column] == 'x')): # also, make sure it's on the board return self.active_piece.shape = shape x_start = min(coord for tup in self.active_piece.coords for coord in (tup[0], tup[2])) y_start = min(coord for tup in self.active_piece.coords for coord in (tup[1], tup[3])) squares = iter(range(4)) # iterator of 4 indices for row_coord, row in zip( range(y_start, y_start + l * self.square_w + 1, self.square_w), shape): for col_coord, cell in zip( range(x_start, x_start + w * self.square_w + 1, self.square_w), row): if cell: square_idx = next(squares) coord = (col_coord, row_coord, col_coord + self.square_w, row_coord + self.square_w) self.active_piece.coords[square_idx] = coord self.canvas.coords(self.active_piece.piece[square_idx], coord) if DEBUG: for row in shape: print(*(cell or ' ' for cell in row)) print(direction)