diff --git a/python/ledris.py b/python/ledris.py index cc6472a..469acfe 100644 --- a/python/ledris.py +++ b/python/ledris.py @@ -9,9 +9,6 @@ from inputmodule.gui.ledmatrix import show_string from inputmodule.inputmodule import ledmatrix -# Initialize pygame -pygame.init() - # Set the screen width and height for a 34 x 9 block Ledris game block_width = 20 block_height = 20 @@ -25,12 +22,6 @@ black = (0, 0, 0) white = (255, 255, 255) -# Create the screen -screen = pygame.display.set_mode((width, height)) - -# Clock to control the speed of the game -clock = pygame.time.Clock() - # Ledrimino shapes shapes = [ [[1, 1, 1, 1]], # I shape @@ -63,25 +54,6 @@ def draw_ledmatrix(board, devices): #vals = [0 for _ in range(39)] #send_command(dev, CommandVals.Draw, vals) -# Function to draw the game based on the board state -def draw_board(board, devices): - draw_ledmatrix(board, devices) - screen.fill(white) - for y in range(rows): - for x in range(cols): - if board[y][x]: - rect = pygame.Rect(x * block_width, y * block_height, block_width, block_height) - pygame.draw.rect(screen, black, rect) - draw_grid() - pygame.display.update() - -# Function to draw a grid -def draw_grid(): - for y in range(rows): - for x in range(cols): - rect = pygame.Rect(x * block_width, y * block_height, block_width, block_height) - pygame.draw.rect(screen, black, rect, 1) - # Function to check if the position is valid def check_collision(board, shape, offset): off_x, off_y = offset @@ -139,106 +111,144 @@ def display_score(board, score): [[1, 1, 1], [1, 0, 1], [1, 1, 1], [0, 0, 1], [1, 1, 1]], # 9 ] -# Main game function -def gameLoop(devices): - board = [[0 for _ in range(cols)] for _ in range(rows)] - current_shape = random.choice(shapes) - current_pos = [cols // 2 - len(current_shape[0]) // 2, 5] # Start below the score display - game_over = False - fall_time = 0 - fall_speed = 500 # Falling speed in milliseconds - score = 0 - - while not game_over: - # Adjust falling speed based on score - fall_speed = max(100, 500 - (score * 10)) - - # Draw the current board state + +class Ledris: + # Function to draw a grid + def draw_grid(self): + for y in range(rows): + for x in range(cols): + rect = pygame.Rect(x * block_width, y * block_height, block_width, block_height) + pygame.draw.rect(self.screen, black, rect, 1) + + # Function to draw the game based on the board state + def draw_board(self, board, devices): + draw_ledmatrix(board, devices) + self.screen.fill(white) + for y in range(rows): + for x in range(cols): + if board[y][x]: + rect = pygame.Rect(x * block_width, y * block_height, block_width, block_height) + pygame.draw.rect(self.screen, black, rect) + self.draw_grid() + pygame.display.update() + + # Main game function + def gameLoop(self, devices): + board = [[0 for _ in range(cols)] for _ in range(rows)] + current_shape = random.choice(shapes) + current_pos = [cols // 2 - len(current_shape[0]) // 2, 5] # Start below the score display + game_over = False + fall_time = 0 + fall_speed = 500 # Falling speed in milliseconds + score = 0 + + while not game_over: + # Adjust falling speed based on score + fall_speed = max(100, 500 - (score * 10)) + + # Draw the current board state + board_state = get_board_state(board, current_shape, current_pos) + display_score(board_state, score) + self.draw_board(board_state, devices) + + # Event handling + for event in pygame.event.get(): + if event.type == pygame.QUIT: + game_over = True + + if event.type == pygame.KEYDOWN: + if event.key in [pygame.K_LEFT, pygame.K_h]: + new_pos = [current_pos[0] - 1, current_pos[1]] + if not check_collision(board, current_shape, new_pos): + current_pos = new_pos + elif event.key in [pygame.K_RIGHT, pygame.K_l]: + new_pos = [current_pos[0] + 1, current_pos[1]] + if not check_collision(board, current_shape, new_pos): + current_pos = new_pos + elif event.key in [pygame.K_DOWN, pygame.K_j]: + new_pos = [current_pos[0], current_pos[1] + 1] + if not check_collision(board, current_shape, new_pos): + current_pos = new_pos + elif event.key in [pygame.K_UP, pygame.K_k]: + rotated_shape = list(zip(*current_shape[::-1])) + if not check_collision(board, rotated_shape, current_pos): + current_shape = rotated_shape + elif event.key == pygame.K_SPACE: # Hard drop + while not check_collision(board, current_shape, [current_pos[0], current_pos[1] + 1]): + current_pos[1] += 1 + + # Automatic falling + fall_time += self.clock.get_time() + if fall_time >= fall_speed: + fall_time = 0 + new_pos = [current_pos[0], current_pos[1] + 1] + if not check_collision(board, current_shape, new_pos): + current_pos = new_pos + else: + merge_shape(board, current_shape, current_pos) + board, cleared_rows = clear_rows(board) + score += cleared_rows # Increase score by one for each row cleared + current_shape = random.choice(shapes) + current_pos = [cols // 2 - len(current_shape[0]) // 2, 5] # Start below the score display + if check_collision(board, current_shape, current_pos): + game_over = True + + self.clock.tick(30) + + # Flash the screen twice before waiting for restart + for _ in range(2): + for dev in devices: + ledmatrix.percentage(dev, 0) + self.screen.fill(black) + pygame.display.update() + time.sleep(0.3) + + for dev in devices: + ledmatrix.percentage(dev, 100) + self.screen.fill(white) + pygame.display.update() + time.sleep(0.3) + + # Display final score and wait for restart without clearing the screen board_state = get_board_state(board, current_shape, current_pos) display_score(board_state, score) - draw_board(board_state, devices) - - # Event handling - for event in pygame.event.get(): - if event.type == pygame.QUIT: - game_over = True - - if event.type == pygame.KEYDOWN: - if event.key in [pygame.K_LEFT, pygame.K_h]: - new_pos = [current_pos[0] - 1, current_pos[1]] - if not check_collision(board, current_shape, new_pos): - current_pos = new_pos - elif event.key in [pygame.K_RIGHT, pygame.K_l]: - new_pos = [current_pos[0] + 1, current_pos[1]] - if not check_collision(board, current_shape, new_pos): - current_pos = new_pos - elif event.key in [pygame.K_DOWN, pygame.K_j]: - new_pos = [current_pos[0], current_pos[1] + 1] - if not check_collision(board, current_shape, new_pos): - current_pos = new_pos - elif event.key in [pygame.K_UP, pygame.K_k]: - rotated_shape = list(zip(*current_shape[::-1])) - if not check_collision(board, rotated_shape, current_pos): - current_shape = rotated_shape - elif event.key == pygame.K_SPACE: # Hard drop - while not check_collision(board, current_shape, [current_pos[0], current_pos[1] + 1]): - current_pos[1] += 1 - - # Automatic falling - fall_time += clock.get_time() - if fall_time >= fall_speed: - fall_time = 0 - new_pos = [current_pos[0], current_pos[1] + 1] - if not check_collision(board, current_shape, new_pos): - current_pos = new_pos - else: - merge_shape(board, current_shape, current_pos) - board, cleared_rows = clear_rows(board) - score += cleared_rows # Increase score by one for each row cleared - current_shape = random.choice(shapes) - current_pos = [cols // 2 - len(current_shape[0]) // 2, 5] # Start below the score display - if check_collision(board, current_shape, current_pos): + self.draw_board(board_state, devices) + + waiting = True + while waiting: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + waiting = False game_over = True + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_q: + waiting = False + if event.key == pygame.K_r: + board = [[0 for _ in range(cols)] for _ in range(rows)] + gameLoop() - clock.tick(30) + pygame.quit() + quit() - # Flash the screen twice before waiting for restart - for _ in range(2): - for dev in devices: - ledmatrix.percentage(dev, 0) - screen.fill(black) - pygame.display.update() - time.sleep(0.3) + def __init__(self): + # Initialize pygame + pygame.init() - for dev in devices: - ledmatrix.percentage(dev, 100) - screen.fill(white) - pygame.display.update() - time.sleep(0.3) - - # Display final score and wait for restart without clearing the screen - board_state = get_board_state(board, current_shape, current_pos) - display_score(board_state, score) - draw_board(board_state, devices) - - waiting = True - while waiting: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - waiting = False - game_over = True - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_q: - waiting = False - if event.key == pygame.K_r: - board = [[0 for _ in range(cols)] for _ in range(rows)] - gameLoop() + # Create the screen + self.screen = pygame.display.set_mode((width, height)) - pygame.quit() - quit() + # Clock to control the speed of the game + self.clock = pygame.time.Clock() -if __name__ == "__main__": +def main_devices(devices): + ledris = Ledris() + ledris.gameLoop(devices) + +def main(): devices = cli.find_devs() - for dev in devices: - show_string(dev, 'YAY') - gameLoop(devices) + + ledris = Ledris() + ledris.gameLoop(devices) + +if __name__ == "__main__": + main()